123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- import styled from '@emotion/styled';
- import {Breadcrumbs} from 'sentry/components/breadcrumbs';
- import * as Layout from 'sentry/components/layouts/thirds';
- import NoProjectMessage from 'sentry/components/noProjectMessage';
- import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
- import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
- import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
- import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
- import {t} from 'sentry/locale';
- import {space} from 'sentry/styles/space';
- import {CurrencyUnit, DurationUnit, RateUnit} from 'sentry/utils/discover/fields';
- import {decodeScalar} from 'sentry/utils/queryString';
- import {MutableSearch} from 'sentry/utils/tokenizeSearch';
- import {useLocation} from 'sentry/utils/useLocation';
- import useOrganization from 'sentry/utils/useOrganization';
- import {
- NumberOfPipelinesChart,
- PipelineDurationChart,
- TotalTokensUsedChart,
- } from 'sentry/views/llmMonitoring/llmMonitoringCharts';
- import {PipelineSpansTable} from 'sentry/views/llmMonitoring/pipelineSpansTable';
- import {MODULE_TITLE} from 'sentry/views/llmMonitoring/settings';
- import {MetricReadout} from 'sentry/views/performance/metricReadout';
- import * as ModuleLayout from 'sentry/views/performance/moduleLayout';
- import {ModulePageProviders} from 'sentry/views/performance/modulePageProviders';
- import {useModuleBreadcrumbs} from 'sentry/views/performance/utils/useModuleBreadcrumbs';
- import {useSpanMetrics} from 'sentry/views/starfish/queries/useDiscover';
- import {
- SpanFunction,
- SpanMetricsField,
- type SpanMetricsQueryFilters,
- } from 'sentry/views/starfish/types';
- interface Props {
- params: {
- groupId: string;
- };
- }
- type Query = {
- 'span.description'?: string;
- };
- export function LLMMonitoringPage({params}: Props) {
- const location = useLocation<Query>();
- const organization = useOrganization();
- const {groupId} = params;
- const spanDescription = decodeScalar(location.query?.['span.description']);
- const filters: SpanMetricsQueryFilters = {
- 'span.group': groupId,
- 'span.category': 'ai.pipeline',
- };
- const {data, isLoading: areSpanMetricsLoading} = useSpanMetrics(
- {
- search: MutableSearch.fromQueryObject(filters),
- fields: [
- SpanMetricsField.SPAN_OP,
- 'count()',
- `${SpanFunction.SPM}()`,
- `avg(${SpanMetricsField.SPAN_DURATION})`,
- ],
- enabled: Boolean(groupId),
- },
- 'api.ai-pipelines.view'
- );
- const spanMetrics = data[0] ?? {};
- const {data: totalTokenData, isLoading: isTotalTokenDataLoading} = useSpanMetrics(
- {
- search: MutableSearch.fromQueryObject({
- 'span.category': 'ai',
- 'span.ai.pipeline.group': groupId,
- }),
- fields: [
- 'ai_total_tokens_used()',
- 'ai_total_tokens_used(c:spans/ai.total_cost@usd)',
- ],
- enabled: Boolean(groupId),
- },
- 'api.ai-pipelines.view'
- );
- const tokenUsedMetric = totalTokenData[0] ?? {};
- const crumbs = useModuleBreadcrumbs('ai');
- return (
- <Layout.Page>
- <NoProjectMessage organization={organization}>
- <Layout.Header>
- <Layout.HeaderContent>
- <Breadcrumbs
- crumbs={[
- ...crumbs,
- {
- label: spanDescription ?? t('(no name)'),
- },
- ]}
- />
- <Layout.Title>{MODULE_TITLE}</Layout.Title>
- </Layout.HeaderContent>
- </Layout.Header>
- <Layout.Body>
- <Layout.Main fullWidth>
- <ModuleLayout.Layout>
- <ModuleLayout.Full>
- <SpaceBetweenWrap>
- <PageFilterBar condensed>
- <ProjectPageFilter />
- <EnvironmentPageFilter />
- <DatePageFilter />
- </PageFilterBar>
- <MetricsRibbon>
- <MetricReadout
- title={t('Total Tokens Used')}
- value={tokenUsedMetric['ai_total_tokens_used()']}
- unit={'count'}
- isLoading={isTotalTokenDataLoading}
- />
- <MetricReadout
- title={t('Total Cost')}
- value={
- tokenUsedMetric['ai_total_tokens_used(c:spans/ai.total_cost@usd)']
- }
- unit={CurrencyUnit.USD}
- isLoading={isTotalTokenDataLoading}
- />
- <MetricReadout
- title={t('Pipeline Duration')}
- value={spanMetrics?.[`avg(${SpanMetricsField.SPAN_DURATION})`]}
- unit={DurationUnit.MILLISECOND}
- isLoading={areSpanMetricsLoading}
- />
- <MetricReadout
- title={t('Pipeline Runs Per Minute')}
- value={spanMetrics?.[`${SpanFunction.SPM}()`]}
- unit={RateUnit.PER_MINUTE}
- isLoading={areSpanMetricsLoading}
- />
- </MetricsRibbon>
- </SpaceBetweenWrap>
- </ModuleLayout.Full>
- <ModuleLayout.Third>
- <TotalTokensUsedChart groupId={groupId} />
- </ModuleLayout.Third>
- <ModuleLayout.Third>
- <NumberOfPipelinesChart groupId={groupId} />
- </ModuleLayout.Third>
- <ModuleLayout.Third>
- <PipelineDurationChart groupId={groupId} />
- </ModuleLayout.Third>
- <ModuleLayout.Full>
- <PipelineSpansTable groupId={groupId} />
- </ModuleLayout.Full>
- </ModuleLayout.Layout>
- </Layout.Main>
- </Layout.Body>
- </NoProjectMessage>
- </Layout.Page>
- );
- }
- function PageWithProviders({params}: Props) {
- const location = useLocation<Query>();
- const {'span.description': spanDescription} = location.query;
- return (
- <ModulePageProviders
- moduleName="ai"
- pageTitle={spanDescription ?? t('(no name)')}
- features="ai-analytics"
- >
- <LLMMonitoringPage params={params} />
- </ModulePageProviders>
- );
- }
- export default PageWithProviders;
- const SpaceBetweenWrap = styled('div')`
- display: flex;
- justify-content: space-between;
- flex-wrap: wrap;
- gap: ${space(2)};
- `;
- const MetricsRibbon = styled('div')`
- display: flex;
- flex-wrap: wrap;
- gap: ${space(4)};
- `;
|