Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(metrics): always show group by and default its aggregation to sum (#114637)

Frank Hamandeabc429dcfb2
diff --git a/products/metrics/frontend/components/MetricsClauseRow.tsx b/products/metrics/frontend/components/MetricsClauseRow.tsx
index 72f9f8555662..acad01ef93c6 100644
--- a/products/metrics/frontend/components/MetricsClauseRow.tsx
+++ b/products/metrics/frontend/components/MetricsClauseRow.tsx
@@ -115,13 +115,11 @@ export function MetricsClauseRow({
onAggregationChange={withSelect(setAggregation)}
disabledReason={disabledReason}
/>
- {clause.aggregation && (
- <MetricsGroupByButton
- groupByKeys={clause.groupByKeys}
- onChange={withSelect(setGroupByKeys)}
- disabledReason={disabledReason}
- />
- )}
+ <MetricsGroupByButton
+ groupByKeys={clause.groupByKeys}
+ onChange={withSelect(setGroupByKeys)}
+ disabledReason={disabledReason}
+ />
<LemonMenu
items={[
{
diff --git a/products/metrics/frontend/components/metricsViewerLogic.test.ts b/products/metrics/frontend/components/metricsViewerLogic.test.ts
index 755f8aaaa6cb..f369a780aa52 100644
--- a/products/metrics/frontend/components/metricsViewerLogic.test.ts
+++ b/products/metrics/frontend/components/metricsViewerLogic.test.ts
@@ -141,6 +141,21 @@ describe('metricsViewerLogic', () => {
expect(logic.values.groupByKeys).toEqual([])
})
+ it('adds a sum aggregation when grouping a clause without one', () => {
+ logic.actions.setMetricName('queue_depth')
+ logic.actions.setGroupByKeys(['container'])
+ expect(logic.values.aggregation).toBe('sum')
+ expect(logic.values.groupByKeys).toEqual(['container'])
+ })
+
+ it('keeps the chosen aggregation when grouping', () => {
+ logic.actions.setMetricName('queue_depth')
+ logic.actions.setAggregation('max')
+ logic.actions.setGroupByKeys(['container'])
+ logic.actions.setGroupByKeys([])
+ expect(logic.values.aggregation).toBe('max')
+ })
+
// metricsQueryNode is what "Save as insight" persists: a wrong mapping here
// silently saves insights that re-run a different query than the viewer showed.
it('maps viewer state to a MetricsQuery node, translating p95 to quantile', () => {
diff --git a/products/metrics/frontend/components/metricsViewerLogic.tsx b/products/metrics/frontend/components/metricsViewerLogic.tsx
index 6a021b2fa331..5afba95dec4e 100644
--- a/products/metrics/frontend/components/metricsViewerLogic.tsx
+++ b/products/metrics/frontend/components/metricsViewerLogic.tsx
@@ -964,8 +964,13 @@ export const metricsViewerLogic = kea<metricsViewerLogicType>([
setRangeFunction: (state, { rangeFunction }) =>
withActiveClause(state, (clause) => ({ ...clause, rangeFunction })),
// Attribute keys to split the clause into one series each (e.g. ['service.name', 'env']).
+ // Grouping needs an aggregation to combine series, so default to sum when none is set.
setGroupByKeys: (state, { groupByKeys }) =>
- withActiveClause(state, (clause) => ({ ...clause, groupByKeys })),
+ withActiveClause(state, (clause) => ({
+ ...clause,
+ groupByKeys,
+ aggregation: clause.aggregation ?? (groupByKeys.length ? 'sum' : null),
+ })),
// The clause's UniversalFilters group; converted into backend matchers by `metricFiltersForGroup`.
setFilterGroup: (state, { filterGroup }) =>
withActiveClause(state, (clause) => ({ ...clause, filterGroup })),