llmMonitoringDetailsPage.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import styled from '@emotion/styled';
  2. import FeatureBadge from 'sentry/components/badge/featureBadge';
  3. import {Breadcrumbs} from 'sentry/components/breadcrumbs';
  4. import * as Layout from 'sentry/components/layouts/thirds';
  5. import NoProjectMessage from 'sentry/components/noProjectMessage';
  6. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  7. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  8. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  9. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  10. import {t} from 'sentry/locale';
  11. import {space} from 'sentry/styles/space';
  12. import {CurrencyUnit, DurationUnit, RateUnit} from 'sentry/utils/discover/fields';
  13. import {decodeScalar} from 'sentry/utils/queryString';
  14. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  15. import {useLocation} from 'sentry/utils/useLocation';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import {
  18. NumberOfPipelinesChart,
  19. PipelineDurationChart,
  20. TotalTokensUsedChart,
  21. } from 'sentry/views/llmMonitoring/llmMonitoringCharts';
  22. import {PipelineSpansTable} from 'sentry/views/llmMonitoring/pipelineSpansTable';
  23. import {MODULE_TITLE, RELEASE_LEVEL} from 'sentry/views/llmMonitoring/settings';
  24. import {MetricReadout} from 'sentry/views/performance/metricReadout';
  25. import * as ModuleLayout from 'sentry/views/performance/moduleLayout';
  26. import {ModulePageProviders} from 'sentry/views/performance/modulePageProviders';
  27. import {useModuleBreadcrumbs} from 'sentry/views/performance/utils/useModuleBreadcrumbs';
  28. import {useSpanMetrics} from 'sentry/views/starfish/queries/useDiscover';
  29. import {
  30. SpanFunction,
  31. SpanMetricsField,
  32. type SpanMetricsQueryFilters,
  33. } from 'sentry/views/starfish/types';
  34. interface Props {
  35. params: {
  36. groupId: string;
  37. };
  38. }
  39. type Query = {
  40. 'span.description'?: string;
  41. };
  42. export function LLMMonitoringPage({params}: Props) {
  43. const location = useLocation<Query>();
  44. const organization = useOrganization();
  45. const {groupId} = params;
  46. const spanDescription = decodeScalar(location.query?.['span.description']);
  47. const filters: SpanMetricsQueryFilters = {
  48. 'span.group': groupId,
  49. 'span.category': 'ai.pipeline',
  50. };
  51. const {data, isLoading: areSpanMetricsLoading} = useSpanMetrics(
  52. {
  53. search: MutableSearch.fromQueryObject(filters),
  54. fields: [
  55. SpanMetricsField.SPAN_OP,
  56. 'count()',
  57. `${SpanFunction.SPM}()`,
  58. `avg(${SpanMetricsField.SPAN_DURATION})`,
  59. ],
  60. enabled: Boolean(groupId),
  61. },
  62. 'api.ai-pipelines.view'
  63. );
  64. const spanMetrics = data[0] ?? {};
  65. const {data: totalTokenData, isLoading: isTotalTokenDataLoading} = useSpanMetrics(
  66. {
  67. search: MutableSearch.fromQueryObject({
  68. 'span.category': 'ai',
  69. 'span.ai.pipeline.group': groupId,
  70. }),
  71. fields: ['sum(ai.total_tokens.used)', 'sum(ai.total_cost)'],
  72. enabled: Boolean(groupId),
  73. },
  74. 'api.ai-pipelines.view'
  75. );
  76. const tokenUsedMetric = totalTokenData[0] ?? {};
  77. const crumbs = useModuleBreadcrumbs('ai');
  78. return (
  79. <Layout.Page>
  80. <NoProjectMessage organization={organization}>
  81. <Layout.Header>
  82. <Layout.HeaderContent>
  83. <Breadcrumbs
  84. crumbs={[
  85. ...crumbs,
  86. {
  87. label: spanDescription ?? t('(no name)'),
  88. },
  89. ]}
  90. />
  91. <Layout.Title>
  92. {MODULE_TITLE}
  93. <FeatureBadge type={RELEASE_LEVEL} />
  94. </Layout.Title>
  95. </Layout.HeaderContent>
  96. </Layout.Header>
  97. <Layout.Body>
  98. <Layout.Main fullWidth>
  99. <ModuleLayout.Layout>
  100. <ModuleLayout.Full>
  101. <SpaceBetweenWrap>
  102. <PageFilterBar condensed>
  103. <ProjectPageFilter />
  104. <EnvironmentPageFilter />
  105. <DatePageFilter />
  106. </PageFilterBar>
  107. <MetricsRibbon>
  108. <MetricReadout
  109. title={t('Total Tokens Used')}
  110. value={tokenUsedMetric['sum(ai.total_tokens.used)']}
  111. unit={'count'}
  112. isLoading={isTotalTokenDataLoading}
  113. />
  114. <MetricReadout
  115. title={t('Total Cost')}
  116. value={tokenUsedMetric['sum(ai.total_cost)']}
  117. unit={CurrencyUnit.USD}
  118. isLoading={isTotalTokenDataLoading}
  119. />
  120. <MetricReadout
  121. title={t('Pipeline Duration')}
  122. value={spanMetrics?.[`avg(${SpanMetricsField.SPAN_DURATION})`]}
  123. unit={DurationUnit.MILLISECOND}
  124. isLoading={areSpanMetricsLoading}
  125. />
  126. <MetricReadout
  127. title={t('Pipeline Runs Per Minute')}
  128. value={spanMetrics?.[`${SpanFunction.SPM}()`]}
  129. unit={RateUnit.PER_MINUTE}
  130. isLoading={areSpanMetricsLoading}
  131. />
  132. </MetricsRibbon>
  133. </SpaceBetweenWrap>
  134. </ModuleLayout.Full>
  135. <ModuleLayout.Third>
  136. <TotalTokensUsedChart groupId={groupId} />
  137. </ModuleLayout.Third>
  138. <ModuleLayout.Third>
  139. <NumberOfPipelinesChart groupId={groupId} />
  140. </ModuleLayout.Third>
  141. <ModuleLayout.Third>
  142. <PipelineDurationChart groupId={groupId} />
  143. </ModuleLayout.Third>
  144. <ModuleLayout.Full>
  145. <PipelineSpansTable groupId={groupId} />
  146. </ModuleLayout.Full>
  147. </ModuleLayout.Layout>
  148. </Layout.Main>
  149. </Layout.Body>
  150. </NoProjectMessage>
  151. </Layout.Page>
  152. );
  153. }
  154. function PageWithProviders({params}: Props) {
  155. const location = useLocation<Query>();
  156. const {'span.description': spanDescription} = location.query;
  157. return (
  158. <ModulePageProviders
  159. moduleName="ai"
  160. pageTitle={spanDescription ?? t('(no name)')}
  161. features="insights-addon-modules"
  162. >
  163. <LLMMonitoringPage params={params} />
  164. </ModulePageProviders>
  165. );
  166. }
  167. export default PageWithProviders;
  168. const SpaceBetweenWrap = styled('div')`
  169. display: flex;
  170. justify-content: space-between;
  171. flex-wrap: wrap;
  172. gap: ${space(2)};
  173. `;
  174. const MetricsRibbon = styled('div')`
  175. display: flex;
  176. flex-wrap: wrap;
  177. gap: ${space(4)};
  178. `;