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.

feat(tracing): add service-metrics pivot to the span metrics tab (#102416)

Daniel Visca8ed738a803f2
diff --git a/products/tracing/frontend/components/TraceDrawer/SpanMetricsTab.test.tsx b/products/tracing/frontend/components/TraceDrawer/SpanMetricsTab.test.tsx
index aa27b2b646c8..be52e205fee2 100644
--- a/products/tracing/frontend/components/TraceDrawer/SpanMetricsTab.test.tsx
+++ b/products/tracing/frontend/components/TraceDrawer/SpanMetricsTab.test.tsx
@@ -1,4 +1,13 @@
+import '@testing-library/jest-dom'
+
import { fireEvent, render, screen } from '@testing-library/react'
+import { Provider } from 'kea'
+
+import { FEATURE_FLAGS } from 'lib/constants'
+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
+
+import { initKeaTests } from '~/test/init'
+import { AccessControlLevel, AccessControlResourceType, AppContext } from '~/types'
import type { Span } from '../../types'
import { SpanMetricsTab } from './SpanMetricsTab'
@@ -16,16 +25,32 @@ jest.mock('products/metrics/frontend/components/TraceMetricSamples', () => ({
const span = {
trace_id: 'trace-abc',
span_id: 'span-xyz',
+ service_name: 'billing-worker',
timestamp: '2026-06-11T08:00:00.000Z',
} as Span
describe('SpanMetricsTab', () => {
beforeEach(() => {
+ initKeaTests()
+ featureFlagLogic.mount()
+ // Enable both gates on ViewServiceMetricsButton so the real component renders its link.
+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.METRICS]: true })
+ window.POSTHOG_APP_CONTEXT = {
+ ...window.POSTHOG_APP_CONTEXT,
+ resource_access_control: {
+ ...window.POSTHOG_APP_CONTEXT?.resource_access_control,
+ [AccessControlResourceType.Metrics]: AccessControlLevel.Viewer,
+ },
+ } as AppContext
capturedProps.length = 0
})
it('defaults to whole-trace scope with a window around the span timestamp', () => {
- render(<SpanMetricsTab span={span} />)
+ render(
+ <Provider>
+ <SpanMetricsTab span={span} />
+ </Provider>
+ )
const props = capturedProps[capturedProps.length - 1]
expect(props.traceId).toBe('trace-abc')
@@ -36,7 +61,11 @@ describe('SpanMetricsTab', () => {
})
it('narrows to the inspected span when the scope toggle flips', () => {
- render(<SpanMetricsTab span={span} />)
+ render(
+ <Provider>
+ <SpanMetricsTab span={span} />
+ </Provider>
+ )
// LemonSegmentedButton renders each option label more than once; any instance works.
fireEvent.click(screen.getAllByText('This span')[0])
@@ -44,4 +73,44 @@ describe('SpanMetricsTab', () => {
const props = capturedProps[capturedProps.length - 1]
expect(props.spanId).toBe('span-xyz')
})
+
+ it('links to the span service metrics over the trace window', () => {
+ render(
+ <Provider>
+ <SpanMetricsTab span={span} />
+ </Provider>
+ )
+
+ const href = screen.getAllByText('View metrics').at(-1)?.closest('a')?.getAttribute('href')
+ expect(href).toContain('billing-worker')
+ // Same ±1h window the samples query uses, so the chart and the samples agree.
+ expect(href).toContain('dateFrom=2026-06-11T07%3A00%3A00.000Z')
+ expect(href).toContain('dateTo=2026-06-11T09%3A00%3A00.000Z')
+ })
+
+ it('moves the metrics link window when a different span is selected', () => {
+ const otherSpan = {
+ ...span,
+ span_id: 'span-later',
+ service_name: 'billing-api',
+ timestamp: '2026-06-11T20:00:00.000Z',
+ } as Span
+
+ const { rerender } = render(
+ <Provider>
+ <SpanMetricsTab span={span} />
+ </Provider>
+ )
+ rerender(
+ <Provider>
+ <SpanMetricsTab span={otherSpan} />
+ </Provider>
+ )
+
+ const href = screen.getAllByText('View metrics').at(-1)?.closest('a')?.getAttribute('href')
+ expect(href).toContain('billing-api')
+ // The window follows the newly selected span, not the first span's.
+ expect(href).toContain('dateFrom=2026-06-11T19%3A00%3A00.000Z')
+ expect(href).toContain('dateTo=2026-06-11T21%3A00%3A00.000Z')
+ })
})
diff --git a/products/tracing/frontend/components/TraceDrawer/SpanMetricsTab.tsx b/products/tracing/frontend/components/TraceDrawer/SpanMetricsTab.tsx
index 405ed4c77583..b200baffde79 100644
--- a/products/tracing/frontend/components/TraceDrawer/SpanMetricsTab.tsx
+++ b/products/tracing/frontend/components/TraceDrawer/SpanMetricsTab.tsx
@@ -1,10 +1,11 @@
-import { useState } from 'react'
+import { useMemo, useState } from 'react'
import { LemonSegmentedButton } from '@posthog/lemon-ui'
import { dayjs } from 'lib/dayjs'
import { TraceMetricSamples } from 'products/metrics/frontend/components/TraceMetricSamples'
+import { ViewServiceMetricsButton } from 'products/metrics/frontend/components/ViewServiceMetricsButton'
import { traceLookupDateRange } from '../../traceLinks'
import type { Span } from '../../types'
@@ -17,12 +18,13 @@ type TraceMetricScope = 'trace' | 'span'
export function SpanMetricsTab({ span }: { span: Span }): JSX.Element {
const [scope, setScope] = useState<TraceMetricScope>('trace')
- // The samples endpoint needs a concrete ISO window; ±1h around the trace covers any of
- // its emissions (same window cold trace loads use). Computed once per mount: the drawer
- // remounts this tab per trace, and pinning the window here keeps span selection from
- // shifting it a few milliseconds and refetching for nothing.
- const [dateRange] = useState(() =>
- traceLookupDateRange(dayjs(span.timestamp).isValid() ? span.timestamp : dayjs().toISOString())
+ // The samples endpoint needs a concrete ISO window; ±1h around the span covers any of
+ // its emissions (same window cold trace loads use). Keyed by span timestamp so selecting
+ // another span in the mounted drawer moves the samples query and the metrics pivot to the
+ // new span's window.
+ const dateRange = useMemo(
+ () => traceLookupDateRange(dayjs(span.timestamp).isValid() ? span.timestamp : dayjs().toISOString()),
+ [span.timestamp]
)
return (
@@ -38,6 +40,17 @@ export function SpanMetricsTab({ span }: { span: Span }): JSX.Element {
]}
data-attr="tracing-metrics-scope"
/>
+ {/* Exemplar samples below answer "which emissions carried this trace"; the pivot
+ answers "what was the host doing" by opening the service's own metric charts
+ over the same window. */}
+ <ViewServiceMetricsButton
+ serviceName={span.service_name}
+ dateFrom={dateRange.date_from}
+ dateTo={dateRange.date_to}
+ size="xsmall"
+ type="secondary"
+ data-attr="tracing-metrics-open-service-metrics"
+ />
</div>
<TraceMetricSamples
traceId={span.trace_id}