llmMonitoringDetailsPage.tsx 6.5 KB

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