: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(tracing): link spans to person and session replay (#91874)
Jimmy Zhuf6ea4034ba0f
diff --git a/products/tracing/frontend/components/VirtualizedSpanList/SpanAttributes.test.tsx b/products/tracing/frontend/components/VirtualizedSpanList/SpanAttributes.test.tsxnew file mode 100644index 000000000000..4f58521f159f--- /dev/null+++ b/products/tracing/frontend/components/VirtualizedSpanList/SpanAttributes.test.tsx@@ -0,0 +1,101 @@+import '@testing-library/jest-dom'++import { render, screen, waitFor } from '@testing-library/react'++import { FEATURE_FLAGS } from 'lib/constants'+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'++import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'+import { PropertyFilterType } from '~/types'++import { SpanAttributes } from './SpanAttributes'++// The branch under test is which component a value renders as, not the link internals:+// both targets mount their own data-fetching logics, so they're stubbed at the boundary.+jest.mock('scenes/persons/PersonDisplay', () => ({+ PersonDisplay: ({ person }: { person: { distinct_id: string } }) => (+ <div data-attr="mock-person-display">{person.distinct_id}</div>+ ),+}))+jest.mock('lib/components/ViewRecordingButton/ViewRecordingButton', () => ({+ __esModule: true,+ default: ({ sessionId }: { sessionId: string }) => <div data-attr="mock-view-recording">{sessionId}</div>,+ RecordingPlayerType: { Modal: 'modal' },+ ViewRecordingButtonVariant: { Link: 'link' },+}))++describe('SpanAttributes correlation links', () => {+ beforeEach(() => {+ useMocks({+ get: {+ '/api/projects/:teamId/tracing_config/': () => [+ 200,+ {+ tracing_distinct_id_attribute_keys: ['posthogDistinctId'],+ tracing_session_id_attribute_keys: ['sessionId', 'my.custom.session'],+ },+ ],+ },+ })+ initKeaTests()+ featureFlagLogic.mount()+ featureFlagLogic.actions.setFeatureFlags([FEATURE_FLAGS.TRACING_SESSION_PERSON_LINKS], {+ [FEATURE_FLAGS.TRACING_SESSION_PERSON_LINKS]: true,+ })+ })++ it('renders person and replay links for matching keys, plain text otherwise', () => {+ render(+ <SpanAttributes+ title="Attributes"+ attributes={{ posthogDistinctId: 'user-1', sessionId: 'session-1', 'http.method': 'GET' }}+ propertyType={PropertyFilterType.SpanAttribute}+ />+ )++ expect(screen.getByText('user-1')).toHaveAttribute('data-attr', 'mock-person-display')+ expect(screen.getByText('session-1')).toHaveAttribute('data-attr', 'mock-view-recording')+ expect(screen.getByText('GET')).not.toHaveAttribute('data-attr')+ })++ it('links a session id under a team-configured custom key once the config loads', async () => {+ const { container } = render(+ <SpanAttributes+ title="Attributes"+ attributes={{ 'my.custom.session': 'session-2' }}+ propertyType={PropertyFilterType.SpanAttribute}+ />+ )++ // The custom key only matches after the mocked tracing_config resolves; until then the+ // value renders as plain text, so wait for the link element rather than the text.+ await waitFor(() =>+ expect(container.querySelector('[data-attr="mock-view-recording"]')).toHaveTextContent('session-2')+ )+ })++ it('renders plain text when the flag is off', () => {+ featureFlagLogic.actions.setFeatureFlags([], {})++ const { container } = render(+ <SpanAttributes+ title="Attributes"+ attributes={{ sessionId: 'session-1' }}+ propertyType={PropertyFilterType.SpanAttribute}+ />+ )++ expect(screen.getAllByText('session-1').length).toBeGreaterThan(0)+ expect(container.querySelector('[data-attr="mock-view-recording"]')).toBeNull()+ })++ it('never links values in the synthetic details table (no propertyType)', () => {+ const { container } = render(+ <SpanAttributes title="Span details" attributes={{ 'session.id': 'session-3' }} showFilterActions={false} />+ )++ expect(screen.getAllByText('session-3').length).toBeGreaterThan(0)+ expect(container.querySelector('[data-attr="mock-view-recording"]')).toBeNull()+ })+})diff --git a/products/tracing/frontend/components/VirtualizedSpanList/SpanAttributes.tsx b/products/tracing/frontend/components/VirtualizedSpanList/SpanAttributes.tsxindex a3c045792a8e..8e69dcaa684e 100644--- a/products/tracing/frontend/components/VirtualizedSpanList/SpanAttributes.tsx+++ b/products/tracing/frontend/components/VirtualizedSpanList/SpanAttributes.tsx@@ -1,14 +1,25 @@-import { useActions } from 'kea'+import { useActions, useValues } from 'kea'import { useEffect, useRef, useState } from 'react'import { IconCheck, IconMinusSquare, IconPlusSquare } from '@posthog/icons'import { LemonButton, LemonTable } from '@posthog/lemon-ui'import { CopyToClipboardInline } from 'lib/components/CopyToClipboard'+import ViewRecordingButton, {+ RecordingPlayerType,+ ViewRecordingButtonVariant,+} from 'lib/components/ViewRecordingButton/ViewRecordingButton'+import { FEATURE_FLAGS } from 'lib/constants'import { LemonTableColumns } from 'lib/lemon-ui/LemonTable'+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'+import { PersonDisplay } from 'scenes/persons/PersonDisplay'import { PropertyFilterType, PropertyOperator } from '~/types'+// The key-matching helpers and their convention lists are shared with Logs, because both+// products resolve the same SDK-emitted attribute keys (posthogDistinctId, sessionId, ...).+import { isDistinctIdKey, isSessionIdKey } from 'products/logs/frontend/utils'+import { tracingCorrelationConfigLogic } from 'products/tracing/frontend/tracingCorrelationConfigLogic'import { tracingFiltersLogic } from 'products/tracing/frontend/tracingFiltersLogic'const APPLIED_INDICATOR_MS = 2000@@ -41,9 +52,16 @@ export function SpanAttributes({propertyType,}: SpanAttributesProps): JSX.Element {const { addFilter } = useActions(tracingFiltersLogic)+ const { featureFlags } = useValues(featureFlagLogic)+ const { configuredDistinctIdKeys, configuredSessionIdKeys } = useValues(tracingCorrelationConfigLogic)const [appliedFilter, setAppliedFilter] = useState<{ key: string; direction: FilterDirection } | null>(null)const appliedFilterTimeoutRef = useRef<number | null>(null)+ // Person/replay links only apply to real OTel attribute tables (propertyType set), because+ // the synthetic "Span details" table repeats span metadata under conventional-looking keys.+ const correlationLinksEnabled =+ !!featureFlags[FEATURE_FLAGS.TRACING_SESSION_PERSON_LINKS] && propertyType !== undefined+useEffect(() => () => {if (appliedFilterTimeoutRef.current !== null) {@@ -127,6 +145,26 @@ export function SpanAttributes({if (record.value === '') {return <span className="font-mono text-xs text-muted italic">(empty)</span>}+ // The stopPropagation wrapper keeps a link click from also triggering any+ // ancestor row handler, matching SpanRowActions' convention.+ const correlationLink = !correlationLinksEnabled ? null : isDistinctIdKey(+ record.key,+ configuredDistinctIdKeys+ ) ? (+ <span onClick={(e) => e.stopPropagation()}>+ <PersonDisplay person={{ distinct_id: record.value }} noEllipsis inline />+ </span>+ ) : isSessionIdKey(record.key, configuredSessionIdKeys) ? (+ <span onClick={(e) => e.stopPropagation()}>+ <ViewRecordingButton+ sessionId={record.value}+ openPlayerIn={RecordingPlayerType.Modal}+ label={record.value}+ variant={ViewRecordingButtonVariant.Link}+ checkRecordingExists+ />+ </span>+ ) : nullreturn (<CopyToClipboardInlineexplicitValue={record.value}@@ -136,7 +174,7 @@ export function SpanAttributes({selectableclassName="gap-1 font-mono text-xs">- {record.value}+ {correlationLink ?? <span>{record.value}</span>}</CopyToClipboardInline>)},