llmMonitoringDetailsPage.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 {MetricReadout} from 'sentry/views/performance/metricReadout';
  23. import * as ModuleLayout from 'sentry/views/performance/moduleLayout';
  24. import {ModulePageProviders} from 'sentry/views/performance/modulePageProviders';
  25. import {useModuleBreadcrumbs} from 'sentry/views/performance/utils/useModuleBreadcrumbs';
  26. import {useSpanMetrics} from 'sentry/views/starfish/queries/useDiscover';
  27. import {
  28. SpanFunction,
  29. SpanMetricsField,
  30. type SpanMetricsQueryFilters,
  31. } from 'sentry/views/starfish/types';
  32. interface Props {
  33. params: {
  34. groupId: string;
  35. };
  36. }
  37. type Query = {
  38. 'span.description'?: string;
  39. };
  40. export function LLMMonitoringPage({params}: Props) {
  41. const location = useLocation<Query>();
  42. const organization = useOrganization();
  43. const {groupId} = params;
  44. const spanDescription = decodeScalar(location.query?.['span.description']);
  45. const filters: SpanMetricsQueryFilters = {
  46. 'span.group': groupId,
  47. 'span.category': 'ai.pipeline',
  48. };
  49. const {data, isLoading: areSpanMetricsLoading} = useSpanMetrics(
  50. {
  51. search: MutableSearch.fromQueryObject(filters),
  52. fields: [
  53. SpanMetricsField.SPAN_OP,
  54. 'count()',
  55. `${SpanFunction.SPM}()`,
  56. `avg(${SpanMetricsField.SPAN_DURATION})`,
  57. ],
  58. enabled: Boolean(groupId),
  59. },
  60. 'api.ai-pipelines.view'
  61. );
  62. const spanMetrics = data[0] ?? {};
  63. const {data: totalTokenData, isLoading: isTotalTokenDataLoading} = useSpanMetrics(
  64. {
  65. search: MutableSearch.fromQueryObject({
  66. 'span.category': 'ai',
  67. 'span.ai.pipeline.group': groupId,
  68. }),
  69. fields: [
  70. 'ai_total_tokens_used()',
  71. 'ai_total_tokens_used(c:spans/ai.total_cost@usd)',
  72. ],
  73. enabled: Boolean(groupId),
  74. },
  75. 'api.ai-pipelines.view'
  76. );
  77. const tokenUsedMetric = totalTokenData[0] ?? {};
  78. const crumbs = useModuleBreadcrumbs('ai');
  79. return (
  80. <Layout.Page>
  81. <NoProjectMessage organization={organization}>
  82. <Layout.Header>
  83. <Layout.HeaderContent>
  84. <Breadcrumbs
  85. crumbs={[
  86. ...crumbs,
  87. {
  88. label: spanDescription ?? t('(no name)'),
  89. },
  90. ]}
  91. />
  92. <Layout.Title>{t('LLM Monitoring')}</Layout.Title>
  93. </Layout.HeaderContent>
  94. </Layout.Header>
  95. <Layout.Body>
  96. <Layout.Main fullWidth>
  97. <ModuleLayout.Layout>
  98. <ModuleLayout.Full>
  99. <SpaceBetweenWrap>
  100. <PageFilterBar condensed>
  101. <ProjectPageFilter />
  102. <EnvironmentPageFilter />
  103. <DatePageFilter />
  104. </PageFilterBar>
  105. <MetricsRibbon>
  106. <MetricReadout
  107. title={t('Total Tokens Used')}
  108. value={tokenUsedMetric['ai_total_tokens_used()']}
  109. unit={'count'}
  110. isLoading={isTotalTokenDataLoading}
  111. />
  112. <MetricReadout
  113. title={t('Total Cost')}
  114. value={
  115. tokenUsedMetric['ai_total_tokens_used(c:spans/ai.total_cost@usd)']
  116. }
  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="ai-analytics"
  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. `;