Skip to content

feat(logs): Add confidence footer for logs #96696

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 2 commits into from
Jul 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions static/app/views/explore/components/chart/chartFooter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import styled from '@emotion/styled';

import usePrevious from 'sentry/utils/usePrevious';

export function usePreviouslyLoaded<T>(current: T, isLoading: boolean): T {
const previous = usePrevious(current, isLoading);
return isLoading ? previous : current;
}

export const Container = styled('span')`
color: ${p => p.theme.subText};
font-size: ${p => p.theme.fontSize.sm};
`;
1 change: 1 addition & 0 deletions static/app/views/explore/components/chart/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export interface ChartInfo {
isSampled?: boolean | null;
sampleCount?: number;
samplingMode?: SamplingMode;
topEvents?: number;
}
144 changes: 144 additions & 0 deletions static/app/views/explore/logs/confidenceFooter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import {Tooltip} from 'sentry/components/core/tooltip';
import Count from 'sentry/components/count';
import {t, tct} from 'sentry/locale';
import type {Confidence} from 'sentry/types/organization';
import {defined} from 'sentry/utils';
import {
Container,
usePreviouslyLoaded,
} from 'sentry/views/explore/components/chart/chartFooter';
import type {ChartInfo} from 'sentry/views/explore/components/chart/types';

interface ConfidenceFooterProps {
chartInfo: ChartInfo;
isLoading: boolean;
}

export function ConfidenceFooter({
chartInfo: currentChartInfo,
isLoading,
}: ConfidenceFooterProps) {
const chartInfo = usePreviouslyLoaded(currentChartInfo, isLoading);
return (
<Container>
<ConfidenceMessage
confidence={chartInfo.confidence}
dataScanned={chartInfo.dataScanned}
isSampled={chartInfo.isSampled}
sampleCount={chartInfo.sampleCount}
topEvents={chartInfo.topEvents}
/>
</Container>
);
}

interface ConfidenceMessageProps {
confidence?: Confidence;
dataScanned?: 'full' | 'partial';
isSampled?: boolean | null;
sampleCount?: number;
topEvents?: number;
}

export function ConfidenceMessage({
sampleCount,
dataScanned,
confidence,
topEvents,
isSampled,
}: ConfidenceMessageProps) {
const isTopN = defined(topEvents) && topEvents > 1;

if (!defined(sampleCount)) {
return isTopN
? t('* Top %s groups extrapolated based on \u2026', topEvents)
: t('* Extrapolated based on \u2026');
}

const noSampling = defined(isSampled) && !isSampled;
const sampleCountComponent = <Count value={sampleCount} />;

if (dataScanned === 'full') {
// For logs, if the full data was scanned, we can assume that no
// extrapolation happened and we should remove mentions of extrapolation.
if (isTopN) {
return tct('Top [topEvents] groups based on [sampleCountComponent] logs', {
topEvents,
sampleCountComponent,
});
}

return tct('Based on [sampleCountComponent] logs', {
sampleCountComponent,
});
}

if (confidence === 'low') {
const lowAccuracyFullSampleCount = <LowAccuracyFullTooltip noSampling={noSampling} />;

if (isTopN) {
return tct(
'Top [topEvents] groups extrapolated based on [tooltip:[sampleCountComponent] logs]',
{
topEvents,
tooltip: lowAccuracyFullSampleCount,
sampleCountComponent,
}
);
}

return tct('Extrapolated based on [tooltip:[sampleCountComponent] logs]', {
tooltip: lowAccuracyFullSampleCount,
sampleCountComponent,
});
}

if (isTopN) {
return tct(
'Top [topEvents] groups extrapolated based on [sampleCountComponent] logs',
{
topEvents,
sampleCountComponent,
}
);
}

return tct('Extrapolated based on [sampleCountComponent] logs', {
sampleCountComponent,
});
}

function LowAccuracyFullTooltip({
noSampling,
children,
}: {
noSampling: boolean;
children?: React.ReactNode;
}) {
return (
<Tooltip
title={
<div>
{t(
'You may not have enough logs for a high accuracy extrapolation of your query.'
)}
<br />
<br />
{t(
"You can try adjusting your query by narrowing the date range, removing filters or increasing the chart's time interval."
)}
<br />
<br />
{t(
'You can also increase your sampling rates to get more samples and accurate trends.'
)}
</div>
}
disabled={noSampling}
maxWidth={270}
showUnderline
>
{children}
</Tooltip>
);
}
28 changes: 25 additions & 3 deletions static/app/views/explore/logs/logsGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@ import {CompactSelect} from 'sentry/components/core/compactSelect';
import {Tooltip} from 'sentry/components/core/tooltip';
import {IconClock, IconGraph} from 'sentry/icons';
import {t} from 'sentry/locale';
import {defined} from 'sentry/utils';
import {determineSeriesSampleCountAndIsSampled} from 'sentry/views/alerts/rules/metric/utils/determineSeriesSampleCount';
import {Widget} from 'sentry/views/dashboards/widgets/widget/widget';
import {ChartVisualization} from 'sentry/views/explore/components/chart/chartVisualization';
import {useLogsAggregate} from 'sentry/views/explore/contexts/logs/logsPageParams';
import {
useLogsAggregate,
useLogsGroupBy,
} from 'sentry/views/explore/contexts/logs/logsPageParams';
import {
ChartIntervalUnspecifiedStrategy,
useChartInterval,
} from 'sentry/views/explore/hooks/useChartInterval';
import {TOP_EVENTS_LIMIT} from 'sentry/views/explore/hooks/useTopEvents';
import {ConfidenceFooter} from 'sentry/views/explore/logs/confidenceFooter';
import {EXPLORE_CHART_TYPE_OPTIONS} from 'sentry/views/explore/spans/charts';
import {prettifyAggregation} from 'sentry/views/explore/utils';
import {
combineConfidenceForSeries,
prettifyAggregation,
} from 'sentry/views/explore/utils';
import {ChartType} from 'sentry/views/insights/common/components/chart';
import type {useSortedTimeSeries} from 'sentry/views/insights/common/queries/useSortedTimeSeries';

Expand All @@ -22,6 +32,7 @@ interface LogsGraphProps {

export function LogsGraph({timeseriesResult}: LogsGraphProps) {
const aggregate = useLogsAggregate();
const groupBy = useLogsGroupBy();

const [chartType, setChartType] = useState<ChartType>(ChartType.BAR);
const [interval, setInterval, intervalOptions] = useChartInterval({
Expand All @@ -30,13 +41,21 @@ export function LogsGraph({timeseriesResult}: LogsGraphProps) {

const chartInfo = useMemo(() => {
const series = timeseriesResult.data[aggregate] ?? [];
const isTopEvents = defined(groupBy);
const samplingMeta = determineSeriesSampleCountAndIsSampled(series, isTopEvents);
return {
chartType,
series,
timeseriesResult,
yAxis: aggregate,
confidence: combineConfidenceForSeries(series),
dataScanned: samplingMeta.dataScanned,
isSampled: samplingMeta.isSampled,
sampleCount: samplingMeta.sampleCount,
samplingMode: undefined,
topEvents: isTopEvents ? TOP_EVENTS_LIMIT : undefined,
Copy link
Contributor

Choose a reason for hiding this comment

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

Bug: Inconsistent Empty GroupBy Handling

An inconsistency exists in how an empty groupBy string is handled. logsGraph.tsx incorrectly treats an empty groupBy string as a valid grouping field (due to defined(groupBy)), enabling "Top X groups" mode and displaying misleading UI. This conflicts with logsTab.tsx's logic (groupBy ? TOP_EVENTS_LIMIT : undefined), which correctly treats an empty string as no grouping, leading to potentially incorrect chart configurations.

Locations (2)
Fix in Cursor Fix in Web

Copy link
Contributor

Choose a reason for hiding this comment

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

Bug: Top Events Logic Mismatch

Inconsistent logic for determining topEvents between logsGraph.tsx and logsTab.tsx. logsGraph.tsx uses defined(groupBy) to set isTopEvents for chart display and confidence footer logic, while logsTab.tsx uses a truthy check (groupBy ? ...) for the actual query parameter. When groupBy is an empty string, logsGraph.tsx incorrectly assumes topEvents are applied, leading to the confidence footer displaying TOP_EVENTS_LIMIT messaging, even though the query did not apply topEvents and the data was not sampled.

Locations (1)
Fix in Cursor Fix in Web

};
}, [chartType, timeseriesResult, aggregate]);
}, [chartType, timeseriesResult, aggregate, groupBy]);

const Title = (
<Widget.WidgetTitle title={prettifyAggregation(aggregate) ?? aggregate} />
Expand Down Expand Up @@ -83,6 +102,9 @@ export function LogsGraph({timeseriesResult}: LogsGraphProps) {
Title={Title}
Actions={Actions}
Visualization={<ChartVisualization chartInfo={chartInfo} />}
Footer={
<ConfidenceFooter chartInfo={chartInfo} isLoading={timeseriesResult.isLoading} />
}
revealActions="always"
/>
);
Expand Down
3 changes: 2 additions & 1 deletion static/app/views/explore/logs/logsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
ChartIntervalUnspecifiedStrategy,
useChartInterval,
} from 'sentry/views/explore/hooks/useChartInterval';
import {TOP_EVENTS_LIMIT} from 'sentry/views/explore/hooks/useTopEvents';
import {
HiddenColumnEditorLogFields,
HiddenLogSearchFields,
Expand Down Expand Up @@ -171,7 +172,7 @@ export function LogsTabContent({
yAxis: [aggregate],
interval,
fields: [...(groupBy ? [groupBy] : []), aggregate],
topEvents: groupBy?.length ? 5 : undefined,
topEvents: groupBy ? TOP_EVENTS_LIMIT : undefined,
orderby,
},
'explore.ourlogs.main-chart',
Expand Down
Loading