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): show the trace's person and session in the trace dialog (#117043)

Jon McCallum2bda06199a15
diff --git a/products/tracing/frontend/components/TraceDialog/TraceDialog.tsx b/products/tracing/frontend/components/TraceDialog/TraceDialog.tsx
index b5a6607bbed0..639f4053fc37 100644
--- a/products/tracing/frontend/components/TraceDialog/TraceDialog.tsx
+++ b/products/tracing/frontend/components/TraceDialog/TraceDialog.tsx
@@ -6,12 +6,14 @@ import { LemonBanner, LemonButton, LemonModal, LemonTag, Spinner } from '@postho
import { Resizer } from 'lib/components/Resizer/Resizer'
import { type ResizerLogicProps, resizerLogic } from 'lib/components/Resizer/resizerLogic'
+import { RecordingPlayerType } from 'lib/components/ViewRecordingButton/ViewRecordingButton'
import { traceUrl } from 'lib/components/ViewTraceButton/traceUrl'
import { FloatingContainerContext } from 'lib/hooks/useFloatingContainerContext'
import { pluralize } from 'lib/utils/strings'
import { SpanInspector } from 'products/tracing/frontend/components/SpanInspector/SpanInspector'
import { MissingParentSpanNotice } from 'products/tracing/frontend/components/TraceDrawer/MissingParentSpanNotice'
+import { TraceIdentityChips } from 'products/tracing/frontend/components/TraceDrawer/TraceIdentityChips'
import { formatDuration } from 'products/tracing/frontend/TraceWaterfallView'
import { TRACE_DIALOG_DATA_ATTR, traceDialogLogic } from './traceDialogLogic'
@@ -46,9 +48,12 @@ export function TraceDialog(): JSX.Element | null {
serviceColorMap,
activeInspectorTab,
sessionErrorBadgesEnabled,
+ traceIdentity,
+ traceSessionId,
} = useValues(traceDialogLogic)
const { closeTraceDialog, loadTraceSpans, setInspectorTab } = useActions(traceDialogLogic)
- // The dialog sits above popovers, so the inspector's menus and drawers portal inside it instead.
+ // The dialog sits above popovers, so the inspector's menus and drawers, and the person popover,
+ // portal inside it instead.
const [floatingContainer, setFloatingContainer] = useState<HTMLDivElement | null>(null)
const floatingContainerRef = useCallback((el: HTMLDivElement | null) => setFloatingContainer(el), [])
@@ -102,6 +107,12 @@ export function TraceDialog(): JSX.Element | null {
{/* The span list virtualizes, so it needs a definite height to measure against. */}
<div className="flex flex-col gap-2 h-[65vh]">
<TraceDialogOpenedFromLog />
+ <TraceIdentityChips
+ identity={traceIdentity}
+ timestamp={summary.rootSpan?.timestamp ?? trace.ts ?? null}
+ // The player modal stacks below this dialog, so the recording opens in a new tab.
+ openRecordingIn={RecordingPlayerType.NewTab}
+ />
{traceSpans?.truncated && (
<LemonBanner type="info">
Showing the first {summary.spanCount.toLocaleString()} spans. Open this trace in Tracing to
@@ -164,7 +175,7 @@ export function TraceDialog(): JSX.Element | null {
traceTimestamp={
summary.rootSpan?.timestamp ?? selectedSpan.timestamp
}
- sessionId={null}
+ sessionId={traceSessionId}
showErrorsTab={sessionErrorBadgesEnabled}
errorsScope="span"
resolving={false}
diff --git a/products/tracing/frontend/components/TraceDialog/traceDialogLogic.test.ts b/products/tracing/frontend/components/TraceDialog/traceDialogLogic.test.ts
index 93c7e691c36b..2c0235d05be3 100644
--- a/products/tracing/frontend/components/TraceDialog/traceDialogLogic.test.ts
+++ b/products/tracing/frontend/components/TraceDialog/traceDialogLogic.test.ts
@@ -54,7 +54,10 @@ describe('traceDialogLogic', () => {
} as AppContext
initKeaTests()
featureFlagLogic.mount()
- featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.TRACING_SPAN_ERROR_BADGES]: true })
+ featureFlagLogic.actions.setFeatureFlags([], {
+ [FEATURE_FLAGS.TRACING_SPAN_ERROR_BADGES]: true,
+ [FEATURE_FLAGS.TRACING_SESSION_PERSON_LINKS]: true,
+ })
getTraceSpy = jest.spyOn(api.tracing, 'getTrace').mockResolvedValue(page(['a'], null))
router.actions.push('/logs')
logic = traceDialogLogic()
@@ -324,4 +327,28 @@ describe('traceDialogLogic', () => {
expect(pageListener).toHaveBeenCalledTimes(1)
})
})
+
+ it.each([
+ ['names the person and session of a complete trace', 1, { distinctId: 'person-1', sessionId: 'session-1' }],
+ // A later page could carry a second person, so a truncated trace names no one.
+ ['names no one when the trace is truncated', 6, { distinctId: null, sessionId: null }],
+ ])('%s', async (_name, pageCount, expectedIdentity) => {
+ for (let i = 0; i < pageCount; i++) {
+ const identified = {
+ ...span(`s${i}`),
+ attributes: { posthogDistinctId: 'person-1', sessionId: 'session-1' },
+ }
+ getTraceSpy.mockResolvedValueOnce({
+ results: [identified],
+ hasMore: i < pageCount - 1,
+ nextOffset: i < pageCount - 1 ? i + 1 : null,
+ })
+ }
+
+ await expectLogic(logic, () => {
+ logic.actions.openTraceDialog({ traceId: 'abc123' })
+ })
+ .toDispatchActions(['loadTraceSpansSuccess'])
+ .toMatchValues({ traceIdentity: expectedIdentity })
+ })
})
diff --git a/products/tracing/frontend/components/TraceDialog/traceDialogLogic.ts b/products/tracing/frontend/components/TraceDialog/traceDialogLogic.ts
index 9b30816d1854..a74394a74887 100644
--- a/products/tracing/frontend/components/TraceDialog/traceDialogLogic.ts
+++ b/products/tracing/frontend/components/TraceDialog/traceDialogLogic.ts
@@ -23,6 +23,12 @@ import {
logsViewerModalLogic,
} from 'products/logs/frontend/components/LogsViewer/LogsViewerModal/logsViewerModalLogic'
import { buildSpanTree, parseTimestampUs, type SpanNode } from 'products/tracing/frontend/spanTree'
+import {
+ EMPTY_TRACE_IDENTITY,
+ resolveTraceIdentity,
+ resolveTraceSessionId,
+ type TraceIdentity,
+} from 'products/tracing/frontend/traceIdentity'
import { resolveTraceLookupRange } from 'products/tracing/frontend/traceLinks'
import { buildServiceColorMap } from 'products/tracing/frontend/TraceWaterfallView'
import { tracingCorrelationConfigLogic } from 'products/tracing/frontend/tracingCorrelationConfigLogic'
@@ -79,6 +85,9 @@ export interface TraceDialogSummary {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface traceDialogLogicValues {
featureFlags: FeatureFlagsSet // featureFlagLogic
+ configuredDistinctIdKeys: string[] | undefined // tracingCorrelationConfigLogic
+ configuredSessionIdKeys: string[] | undefined // tracingCorrelationConfigLogic
+ correlationLinksEnabled: boolean // tracingCorrelationConfigLogic
sessionErrorBadgesEnabled: boolean // tracingCorrelationConfigLogic
activeInspectorTab: SpanInspectorTab
chosenInspectorTab: SpanInspectorTab | null
@@ -99,6 +108,8 @@ export interface traceDialogLogicValues {
summary: TraceDialogSummary
timeline: TraceDialogTimeline
trace: TraceLinkParams | null
+ traceIdentity: TraceIdentity
+ traceSessionId: string | null
traceSpans: TraceDialogSpans | null
traceSpansFailed: boolean
traceSpansLoading: boolean
@@ -211,6 +222,17 @@ export interface traceDialogLogicMeta {
summary: TraceDialogSummary
) => Span | null
missingParentSpanId: (traceSpans: TraceDialogSpans | null, selectedSpanId: string | null) => string | null
+ traceIdentity: (
+ traceSpans: TraceDialogSpans | null,
+ configuredDistinctIdKeys: string[] | undefined,
+ configuredSessionIdKeys: string[] | undefined,
+ correlationLinksEnabled: boolean
+ ) => TraceIdentity
+ traceSessionId: (
+ traceSpans: TraceDialogSpans | null,
+ configuredSessionIdKeys: string[] | undefined,
+ sessionErrorBadgesEnabled: boolean
+ ) => string | null
openedSpan: (traceSpans: TraceDialogSpans | null, trace: TraceLinkParams | null) => Span | null
activeInspectorTab: (
chosenInspectorTab: SpanInspectorTab | null,
@@ -242,7 +264,17 @@ export const traceDialogLogic = kea<traceDialogLogicType>([
logsViewerModalLogic,
['openLogsViewerModal'],
],
- values: [tracingCorrelationConfigLogic, ['sessionErrorBadgesEnabled'], featureFlagLogic, ['featureFlags']],
+ values: [
+ tracingCorrelationConfigLogic,
+ [
+ 'configuredDistinctIdKeys',
+ 'configuredSessionIdKeys',
+ 'correlationLinksEnabled',
+ 'sessionErrorBadgesEnabled',
+ ],
+ featureFlagLogic,
+ ['featureFlags'],
+ ],
})),
actions({
openTraceDialog: (trace: TraceLinkParams, openedFrom?: TraceDialogSource | null) => ({
@@ -426,6 +458,33 @@ export const traceDialogLogic = kea<traceDialogLogicType>([
? selectedSpanId
: null,
],
+ // A truncated trace may hold a second, conflicting person on a page the dialog never
+ // loaded, so it names no one rather than risk naming the wrong one.
+ traceIdentity: [
+ (s) => [s.traceSpans, s.configuredDistinctIdKeys, s.configuredSessionIdKeys, s.correlationLinksEnabled],
+ (
+ traceSpans: TraceDialogSpans | null,
+ distinctIdKeys: string[] | undefined,
+ sessionIdKeys: string[] | undefined,
+ linksEnabled: boolean
+ ): TraceIdentity =>
+ linksEnabled && traceSpans && !traceSpans.truncated
+ ? resolveTraceIdentity(traceSpans.spans, distinctIdKeys, sessionIdKeys)
+ : EMPTY_TRACE_IDENTITY,
+ ],
+ // The Errors tab's session scope follows the badge flag rather than the links flag, the
+ // same split the drawer makes.
+ traceSessionId: [
+ (s) => [s.traceSpans, s.configuredSessionIdKeys, s.sessionErrorBadgesEnabled],
+ (
+ traceSpans: TraceDialogSpans | null,
+ sessionIdKeys: string[] | undefined,
+ badgesEnabled: boolean
+ ): string | null =>
+ badgesEnabled && traceSpans && !traceSpans.truncated
+ ? resolveTraceSessionId(traceSpans.spans, sessionIdKeys)
+ : null,
+ ],
openedSpan: [
(s) => [s.traceSpans, s.trace],
(traceSpans: TraceDialogSpans | null, trace: TraceLinkParams | null): Span | null =>
diff --git a/products/tracing/frontend/components/TraceDrawer/TraceIdentityChips.tsx b/products/tracing/frontend/components/TraceDrawer/TraceIdentityChips.tsx
index 15c049f1ecdd..9eec191d5a04 100644
--- a/products/tracing/frontend/components/TraceDrawer/TraceIdentityChips.tsx
+++ b/products/tracing/frontend/components/TraceDrawer/TraceIdentityChips.tsx
@@ -13,13 +13,19 @@ export interface TraceIdentityChipsProps {
identity: TraceIdentity
/** Trace start, used to seek the recording to just before the trace ran. */
timestamp: string | null
+ /** Where the recording opens. A host that stacks above the player modal should open it in a new tab. */
+ openRecordingIn?: RecordingPlayerType
}
// Who the open trace belongs to. The person and the recording are properties of the trace, not of
// the inspected span, so this sits above the waterfall and does not change as the user walks spans.
// Server-only traffic carries no identity, and then this renders nothing.
// Modeled on AI Observability's trace-scene PersonChip.
-export function TraceIdentityChips({ identity, timestamp }: TraceIdentityChipsProps): JSX.Element | null {
+export function TraceIdentityChips({
+ identity,
+ timestamp,
+ openRecordingIn = RecordingPlayerType.Modal,
+}: TraceIdentityChipsProps): JSX.Element | null {
if (!identity.distinctId && !identity.sessionId) {
return null
}
@@ -36,7 +42,7 @@ export function TraceIdentityChips({ identity, timestamp }: TraceIdentityChipsPr
<ViewRecordingButton
sessionId={identity.sessionId}
timestamp={timestamp ?? undefined}
- openPlayerIn={RecordingPlayerType.Modal}
+ openPlayerIn={openRecordingIn}
variant={ViewRecordingButtonVariant.Link}
checkRecordingExists
data-attr="tracing-trace-view-recording"