Skip to content

ref(insights): Refactor Insights -> LLM charts #90094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely a yolo refactor, I don't have an issue that renders this component. @gggritso helped me look at some Sentry traces and it doesn't seem like this is used at all (at least in last 30 days)

Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,15 @@ import {LinkButton} from 'sentry/components/core/button';
import {ButtonBar} from 'sentry/components/core/button/buttonBar';
import {IconOpen} from 'sentry/icons';
import {t} from 'sentry/locale';
import type {Event} from 'sentry/types/event';
import {MutableSearch} from 'sentry/utils/tokenizeSearch';
import * as ModuleLayout from 'sentry/views/insights/common/components/moduleLayout';
import {useSpansIndexed} from 'sentry/views/insights/common/queries/useDiscover';
import LlmEventNumberOfPipelinesChartWidget from 'sentry/views/insights/common/components/widgets/llmEventNumberOfPipelinesChartWidget';
import LlmEventTotalTokensUsedChartWidget from 'sentry/views/insights/common/components/widgets/llmEventTotalTokensUsedChartWidget';
import {useModuleURL} from 'sentry/views/insights/common/utils/useModuleURL';
import {
NumberOfPipelinesChart,
TotalTokensUsedChart,
} from 'sentry/views/insights/llmMonitoring/components/charts/llmMonitoringCharts';
import {SpanIndexedField} from 'sentry/views/insights/types';
import {SectionKey} from 'sentry/views/issueDetails/streamline/context';
import {InterimSection} from 'sentry/views/issueDetails/streamline/interimSection';

interface Props {
event: Event;
}

export default function LLMMonitoringSection({event}: Props) {
export default function LLMMonitoringSection() {
const moduleUrl = useModuleURL('ai');
const trace = event.contexts.trace;

const {data} = useSpansIndexed(
{
limit: 1,
fields: [SpanIndexedField.SPAN_AI_PIPELINE_GROUP],
search: new MutableSearch(`trace:${trace?.trace_id} id:"${trace?.span_id}"`),
enabled: Boolean(trace?.span_id) && Boolean(trace?.trace_id),
},
'api.ai-pipelines.view'
);

const aiPipelineGroup = data[0]?.[SpanIndexedField.SPAN_AI_PIPELINE_GROUP];
Comment on lines -26 to -36
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved into a new hook that is used in the new widgets


const actions = (
<ButtonBar gap={1}>
Expand All @@ -50,18 +27,14 @@ export default function LLMMonitoringSection({event}: Props) {
help={t('Charts showing how many tokens are being used')}
actions={actions}
>
{aiPipelineGroup ? (
<ModuleLayout.Layout>
<ModuleLayout.Half>
<TotalTokensUsedChart groupId={aiPipelineGroup} />
</ModuleLayout.Half>
<ModuleLayout.Half>
<NumberOfPipelinesChart groupId={aiPipelineGroup} />
</ModuleLayout.Half>
</ModuleLayout.Layout>
) : (
'loading'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💀

)}
<ModuleLayout.Layout>
<ModuleLayout.Half>
<LlmEventTotalTokensUsedChartWidget />
</ModuleLayout.Half>
<ModuleLayout.Half>
<LlmEventNumberOfPipelinesChartWidget />
</ModuleLayout.Half>
</ModuleLayout.Layout>
</InterimSection>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {useTheme} from '@emotion/react';

import {t} from 'sentry/locale';
import {MutableSearch} from 'sentry/utils/tokenizeSearch';
import {InsightsLineChartWidget} from 'sentry/views/insights/common/components/insightsLineChartWidget';
import type {LoadableChartWidgetProps} from 'sentry/views/insights/common/components/widgets/types';
import {useSpanMetricsSeries} from 'sentry/views/insights/common/queries/useDiscoverSeries';

export default function BaseLlmNumberOfPipelinesChartWidget({
groupId,
...props
}: {
error?: Error | null;
groupId?: string;
isLoading?: boolean;
} & LoadableChartWidgetProps) {
const theme = useTheme();
const aggregate = 'count()';

let query = 'span.category:"ai.pipeline"';
if (groupId) {
query = `${query} span.group:"${groupId}"`;
}
const {data, isPending, error} = useSpanMetricsSeries(
{
yAxis: [aggregate],
search: new MutableSearch(query),
transformAliasToInputFormat: true,
},
'api.ai-pipelines.view',
props.pageFilters
);

const colors = theme.chart.getColorPalette(2);
return (
<InsightsLineChartWidget
isLoading={isPending}
error={error}
{...props}
title={t('Number of AI pipelines')}
series={[{...data[aggregate], color: colors[1]}]}
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {useTheme} from '@emotion/react';

import {t} from 'sentry/locale';
import {MutableSearch} from 'sentry/utils/tokenizeSearch';
import {InsightsLineChartWidget} from 'sentry/views/insights/common/components/insightsLineChartWidget';
import type {LoadableChartWidgetProps} from 'sentry/views/insights/common/components/widgets/types';
import {useSpanMetricsSeries} from 'sentry/views/insights/common/queries/useDiscoverSeries';

export default function BaseLlmPipelineDurationChartWidget({
groupId,
...props
}: {
error?: Error | null;
groupId?: string;
isLoading?: boolean;
} & LoadableChartWidgetProps) {
const theme = useTheme();
const aggregate = 'avg(span.duration)';

let query = 'span.category:"ai.pipeline"';
if (groupId) {
query = `${query} span.group:"${groupId}"`;
}
const {data, isPending, error} = useSpanMetricsSeries(
{
yAxis: [aggregate],
search: new MutableSearch(query),
transformAliasToInputFormat: true,
},
'api.ai-pipelines.view',
props.pageFilters
);

const colors = theme.chart.getColorPalette(2);
return (
<InsightsLineChartWidget
isLoading={isPending}
error={error}
{...props}
title={t('Pipeline Duration')}
series={[{...data[aggregate], color: colors[2]}]}
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {useTheme} from '@emotion/react';

import {t} from 'sentry/locale';
import {MutableSearch} from 'sentry/utils/tokenizeSearch';
import {InsightsLineChartWidget} from 'sentry/views/insights/common/components/insightsLineChartWidget';
import type {LoadableChartWidgetProps} from 'sentry/views/insights/common/components/widgets/types';
import {useSpanMetricsSeries} from 'sentry/views/insights/common/queries/useDiscoverSeries';

export default function BaseLlmTotalTokensUsedChartWidget({
groupId,
...props
}: {
error?: Error | null;
groupId?: string;
isLoading?: boolean;
} & LoadableChartWidgetProps) {
const theme = useTheme();
const aggregate = 'sum(ai.total_tokens.used)';

let query = 'span.category:"ai"';
if (groupId) {
query = `${query} span.ai.pipeline.group:"${groupId}"`;
}
const {data, isPending, error} = useSpanMetricsSeries(
{
yAxis: [aggregate],
search: new MutableSearch(query),
transformAliasToInputFormat: true,
},
'api.ai-pipelines.view',
props.pageFilters
);

const colors = theme.chart.getColorPalette(2);
return (
<InsightsLineChartWidget
isLoading={isPending}
error={error}
{...props}
title={t('Total tokens used')}
series={[{...data[aggregate], color: colors[0]}]}
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {MutableSearch} from 'sentry/utils/tokenizeSearch';
import {useSpansIndexed} from 'sentry/views/insights/common/queries/useDiscover';
import {SpanIndexedField} from 'sentry/views/insights/types';
import {useGroupEvent} from 'sentry/views/issueDetails/useGroupEvent';

/**
* Given an issue's groupId + eventId, fetch the AI
* Pipeline's group ID via event's trace
*/
export function useAiPipelineGroup({
groupId,
eventId,
}: {
groupId: string;
eventId?: string;
}) {
const {
data: event,
isPending: isGroupPending,
error: groupError,
} = useGroupEvent({groupId, eventId});

const trace = event?.contexts.trace;
const {
data,
isPending: isSpanPending,
error: spanError,
} = useSpansIndexed(
{
limit: 1,
fields: [SpanIndexedField.SPAN_AI_PIPELINE_GROUP],
search: new MutableSearch(`trace:${trace?.trace_id} id:"${trace?.span_id}"`),
enabled: Boolean(trace?.span_id) && Boolean(trace?.trace_id),
},
'api.ai-pipelines.view'
);

return {
groupId: data[0]?.[SpanIndexedField.SPAN_AI_PIPELINE_GROUP],
isPending: isGroupPending || isSpanPending,
error: groupError || spanError,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {useParams} from 'sentry/utils/useParams';
import BaseLlmNumberOfPipelinesChartWidget from 'sentry/views/insights/common/components/widgets/base/baseLlmNumberOfPipelinesChartWidget';
import {useAiPipelineGroup} from 'sentry/views/insights/common/components/widgets/hooks/useAiPipelineGroup';
import type {LoadableChartWidgetProps} from 'sentry/views/insights/common/components/widgets/types';

export default function LlmEventNumberOfPipelinesChartWidget(
props: LoadableChartWidgetProps
) {
const params = useParams<{groupId: string; eventId?: string}>();
const {groupId, isPending, error} = useAiPipelineGroup(params);

return (
<BaseLlmNumberOfPipelinesChartWidget
{...props}
id="llmEventNumberOfPipelinesChartWidget"
groupId={groupId}
isLoading={isPending}
error={error}
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {useParams} from 'sentry/utils/useParams';
import BaseLlmTotalTokensUsedChartWidget from 'sentry/views/insights/common/components/widgets/base/baseLlmTotalTokensUsedChartWidget';
import {useAiPipelineGroup} from 'sentry/views/insights/common/components/widgets/hooks/useAiPipelineGroup';
import type {LoadableChartWidgetProps} from 'sentry/views/insights/common/components/widgets/types';

export default function LlmEventTotalTokensUsedChartWidget(
props: LoadableChartWidgetProps
) {
const params = useParams<{groupId: string; eventId?: string}>();
const {groupId, isPending, error} = useAiPipelineGroup(params);

return (
<BaseLlmTotalTokensUsedChartWidget
{...props}
id="llmEventTotalTokensUsedChartWidget"
groupId={groupId}
isLoading={isPending}
error={error}
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {useParams} from 'sentry/utils/useParams';
import BaseLlmNumberOfPipelinesChartWidget from 'sentry/views/insights/common/components/widgets/base/baseLlmNumberOfPipelinesChartWidget';
import type {LoadableChartWidgetProps} from 'sentry/views/insights/common/components/widgets/types';

export default function LlmNumberOfPipelinesChartWidget(props: LoadableChartWidgetProps) {
const {groupId} = useParams();

return (
<BaseLlmNumberOfPipelinesChartWidget
{...props}
id="llmNumberOfPipelinesChartWidget"
groupId={groupId}
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {useParams} from 'sentry/utils/useParams';
import BaseLlmPipelineDurationChartWidget from 'sentry/views/insights/common/components/widgets/base/baseLlmPipelineDurationChartWidget';
import type {LoadableChartWidgetProps} from 'sentry/views/insights/common/components/widgets/types';

export default function LlmPipelineDurationChartWidget(props: LoadableChartWidgetProps) {
const {groupId} = useParams();

return (
<BaseLlmPipelineDurationChartWidget
{...props}
id="llmPipelineDurationChartWidget"
groupId={groupId}
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {useParams} from 'sentry/utils/useParams';
import BaseLlmTotalTokensUsedChartWidget from 'sentry/views/insights/common/components/widgets/base/baseLlmTotalTokensUsedChartWidget';
import type {LoadableChartWidgetProps} from 'sentry/views/insights/common/components/widgets/types';

export default function LlmTotalTokensUsedChartWidget(props: LoadableChartWidgetProps) {
const {groupId} = useParams();

return (
<BaseLlmTotalTokensUsedChartWidget
{...props}
id="llmTotalTokensUsedChartWidget"
groupId={groupId}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import type {PageFilters} from 'sentry/types/core';
* render an Insight Chart Widget
*/
export interface LoadableChartWidgetProps {
// TODO(billy): This should be required when all chart widgets are converted
/**
* Unique ID for the widget
*
* TODO(billy): This should be required when all chart widgets are converted
*/
id?: string;

Expand Down
Loading
Loading