llmMonitoringDetailsPage.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 {useModuleURL} from 'sentry/views/performance/utils/useModuleURL';
  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 moduleURL = useModuleURL('ai');
  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. return (
  80. <Layout.Page>
  81. <NoProjectMessage organization={organization}>
  82. <Layout.Header>
  83. <Layout.HeaderContent>
  84. <Breadcrumbs
  85. crumbs={[
  86. {
  87. label: t('Dashboard'),
  88. },
  89. {
  90. label: t('LLM Monitoring'),
  91. to: moduleURL,
  92. },
  93. {
  94. label: spanDescription ?? t('(no name)'),
  95. },
  96. ]}
  97. />
  98. <Layout.Title>{t('LLM Monitoring')}</Layout.Title>
  99. </Layout.HeaderContent>
  100. </Layout.Header>
  101. <Layout.Body>
  102. <Layout.Main fullWidth>
  103. <ModuleLayout.Layout>
  104. <ModuleLayout.Full>
  105. <SpaceBetweenWrap>
  106. <PageFilterBar condensed>
  107. <ProjectPageFilter />
  108. <EnvironmentPageFilter />
  109. <DatePageFilter />
  110. </PageFilterBar>
  111. <MetricsRibbon>
  112. <MetricReadout
  113. title={t('Total Tokens Used')}
  114. value={tokenUsedMetric['ai_total_tokens_used()']}
  115. unit={'count'}
  116. isLoading={isTotalTokenDataLoading}
  117. />
  118. <MetricReadout
  119. title={t('Total Cost')}
  120. value={
  121. tokenUsedMetric['ai_total_tokens_used(c:spans/ai.total_cost@usd)']
  122. }
  123. unit={CurrencyUnit.USD}
  124. isLoading={isTotalTokenDataLoading}
  125. />
  126. <MetricReadout
  127. title={t('Pipeline Duration')}
  128. value={spanMetrics?.[`avg(${SpanMetricsField.SPAN_DURATION})`]}
  129. unit={DurationUnit.MILLISECOND}
  130. isLoading={areSpanMetricsLoading}
  131. />
  132. <MetricReadout
  133. title={t('Pipeline Runs Per Minute')}
  134. value={spanMetrics?.[`${SpanFunction.SPM}()`]}
  135. unit={RateUnit.PER_MINUTE}
  136. isLoading={areSpanMetricsLoading}
  137. />
  138. </MetricsRibbon>
  139. </SpaceBetweenWrap>
  140. </ModuleLayout.Full>
  141. <ModuleLayout.Third>
  142. <TotalTokensUsedChart groupId={groupId} />
  143. </ModuleLayout.Third>
  144. <ModuleLayout.Third>
  145. <NumberOfPipelinesChart groupId={groupId} />
  146. </ModuleLayout.Third>
  147. <ModuleLayout.Third>
  148. <PipelineDurationChart groupId={groupId} />
  149. </ModuleLayout.Third>
  150. <ModuleLayout.Full>
  151. <PipelineSpansTable groupId={groupId} />
  152. </ModuleLayout.Full>
  153. </ModuleLayout.Layout>
  154. </Layout.Main>
  155. </Layout.Body>
  156. </NoProjectMessage>
  157. </Layout.Page>
  158. );
  159. }
  160. function PageWithProviders({params}: Props) {
  161. const location = useLocation<Query>();
  162. const {'span.description': spanDescription} = location.query;
  163. return (
  164. <ModulePageProviders
  165. title={[spanDescription ?? t('(no name)'), t('Pipeline Details')].join(' — ')}
  166. features="ai-analytics"
  167. >
  168. <LLMMonitoringPage params={params} />
  169. </ModulePageProviders>
  170. );
  171. }
  172. export default PageWithProviders;
  173. const SpaceBetweenWrap = styled('div')`
  174. display: flex;
  175. justify-content: space-between;
  176. flex-wrap: wrap;
  177. gap: ${space(2)};
  178. `;
  179. const MetricsRibbon = styled('div')`
  180. display: flex;
  181. flex-wrap: wrap;
  182. gap: ${space(4)};
  183. `;