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): mark the opening log on its span in the trace dialog (#117997)

Jon McCallum80c281f50a5c
diff --git a/products/tracing/frontend/components/TraceDialog/TraceDialogLogMark.tsx b/products/tracing/frontend/components/TraceDialog/TraceDialogLogMark.tsx
new file mode 100644
index 000000000000..fd4e962fce31
--- /dev/null
+++ b/products/tracing/frontend/components/TraceDialog/TraceDialogLogMark.tsx
@@ -0,0 +1,45 @@
+import { IconLive } from '@posthog/icons'
+import type { LemonTagType } from '@posthog/lemon-ui'
+
+import { cn } from 'lib/utils/css-classes'
+
+import type { LogMessage } from '~/queries/schema/schema-general'
+
+// Matches LogTag, so the mark reads in the same colour as the log's level tag.
+export const LOG_MARK_TAG_TYPE: Record<LogMessage['severity_text'], LemonTagType> = {
+ trace: 'muted',
+ debug: 'muted',
+ info: 'default',
+ warn: 'warning',
+ error: 'danger',
+ fatal: 'danger',
+}
+
+const MARK_CLASS: Partial<Record<LemonTagType, string>> = {
+ danger: 'bg-danger',
+ warning: 'bg-warning',
+ default: 'bg-secondary',
+ muted: 'bg-muted',
+}
+
+// One mark across the strip, the span chip and the bar pin, so a person learns once which mark is their log.
+export function TraceDialogLogMark({
+ severity,
+ className,
+}: {
+ severity: LogMessage['severity_text']
+ className?: string
+}): JSX.Element {
+ return (
+ <span
+ className={cn(
+ 'inline-flex size-3.5 shrink-0 items-center justify-center rounded-sm text-[10px] text-white',
+ MARK_CLASS[LOG_MARK_TAG_TYPE[severity]] ?? 'bg-muted',
+ className
+ )}
+ aria-hidden
+ >
+ <IconLive />
+ </span>
+ )
+}
diff --git a/products/tracing/frontend/components/TraceDialog/TraceDialogOpenedFromLog.tsx b/products/tracing/frontend/components/TraceDialog/TraceDialogOpenedFromLog.tsx
index a8d3a6a08d46..866b87999021 100644
--- a/products/tracing/frontend/components/TraceDialog/TraceDialogOpenedFromLog.tsx
+++ b/products/tracing/frontend/components/TraceDialog/TraceDialogOpenedFromLog.tsx
@@ -6,6 +6,7 @@ import { LogTag } from 'products/logs/frontend/components/LogTag'
import { formatDuration } from 'products/tracing/frontend/TraceWaterfallView'
import { traceDialogLogic } from './traceDialogLogic'
+import { TraceDialogLogMark } from './TraceDialogLogMark'
export function TraceDialogOpenedFromLog(): JSX.Element | null {
const { openedFrom, openedSpan, openedAtUs, timeline } = useValues(traceDialogLogic)
@@ -20,7 +21,10 @@ export function TraceDialogOpenedFromLog(): JSX.Element | null {
className="flex flex-wrap items-center gap-2 rounded border border-accent bg-accent-highlight-secondary px-2 py-1 text-xs"
data-attr="trace-dialog-opened-from-log"
>
- <span className="font-semibold">Opened from log</span>
+ <span className="flex items-center gap-1.5 font-semibold">
+ <TraceDialogLogMark severity={openedFrom.severity} />
+ Opened from log
+ </span>
<LogTag level={openedFrom.severity} />
<span
className="ph-replay-block ph-no-capture font-mono truncate flex-1 basis-60 min-w-0"
diff --git a/products/tracing/frontend/components/TraceDialog/TraceDialogSpanRow.tsx b/products/tracing/frontend/components/TraceDialog/TraceDialogSpanRow.tsx
index 58f31120f1a6..916e2b49949f 100644
--- a/products/tracing/frontend/components/TraceDialog/TraceDialogSpanRow.tsx
+++ b/products/tracing/frontend/components/TraceDialog/TraceDialogSpanRow.tsx
@@ -1,4 +1,5 @@
import { IconChevronRight, IconWarning } from '@posthog/icons'
+import { LemonTag, Tooltip } from '@posthog/lemon-ui'
import { getSeriesColor } from 'lib/colors'
import { cn } from 'lib/utils/css-classes'
@@ -6,7 +7,8 @@ import { cn } from 'lib/utils/css-classes'
import { parseTimestampUs } from 'products/tracing/frontend/spanTree'
import { formatDuration } from 'products/tracing/frontend/TraceWaterfallView'
-import type { TraceDialogTimeline } from './traceDialogLogic'
+import type { TraceDialogSource, TraceDialogTimeline } from './traceDialogLogic'
+import { LOG_MARK_TAG_TYPE, TraceDialogLogMark } from './TraceDialogLogMark'
import type { TraceDialogRow } from './traceDialogRows'
import { indentPx, MAX_VISUAL_DEPTH, TraceDialogTreeLines } from './TraceDialogTreeLines'
@@ -19,6 +21,7 @@ export interface TraceDialogSpanRowProps {
isSelected: boolean
/** Set only on the span that emitted the record the dialog was opened from. */
openedAtUs: number | null
+ openedFrom: TraceDialogSource | null
/** How many spans sit under this one. Zero for a span with no children. */
descendantCount: number
isCollapsed: boolean
@@ -27,6 +30,17 @@ export interface TraceDialogSpanRowProps {
onToggleChain: (spanId: string) => void
}
+// Clock skew between a service's logs and spans can put the log outside the span it names.
+function logTimingLabel(logUs: number, startUs: number, durationUs: number): string {
+ if (logUs < startUs) {
+ return `written ${formatDuration((startUs - logUs) * 1_000)} before this span started`
+ }
+ if (logUs > startUs + durationUs) {
+ return `written ${formatDuration((logUs - startUs - durationUs) * 1_000)} after this span ended`
+ }
+ return `+${formatDuration((logUs - startUs) * 1_000)} into this span`
+}
+
function percentOfTrace(us: number, timeline: TraceDialogTimeline): number {
return timeline.durationUs > 0 ? ((us - timeline.startUs) / timeline.durationUs) * 100 : 0
}
@@ -37,6 +51,7 @@ export function TraceDialogSpanRow({
serviceColorMap,
isSelected,
openedAtUs,
+ openedFrom,
descendantCount,
isCollapsed,
onSelect,
@@ -61,7 +76,8 @@ export function TraceDialogSpanRow({
const isError = (row.kind === 'chain' ? row.nodes : [node]).some((member) => member.span.status_code === 2)
const serviceColor = getSeriesColor(serviceColorMap.get(span.service_name) ?? 0)
const barColor = isError ? ERROR_COLOR : serviceColor
- const openedAtPct = openedAtUs != null ? Math.min(Math.max(percentOfTrace(openedAtUs, timeline), 0), 100) : null
+ const openedAtPct =
+ openedAtUs != null && openedFrom ? Math.min(Math.max(percentOfTrace(openedAtUs, timeline), 0), 100) : null
return (
// The collapse toggle cannot sit inside the select button, so the button fills the row from
@@ -155,6 +171,16 @@ export function TraceDialogSpanRow({
>
{name}
</span>
+ {openedAtPct != null && openedFrom && (
+ <LemonTag
+ size="small"
+ type={LOG_MARK_TAG_TYPE[openedFrom.severity] ?? 'muted'}
+ className="relative shrink-0"
+ data-attr="trace-dialog-your-log-chip"
+ >
+ Your log
+ </LemonTag>
+ )}
{openChainLength && (
<button
type="button"
@@ -180,13 +206,33 @@ export function TraceDialogSpanRow({
}}
/>
)}
- {openedAtPct != null && (
- <span
- className="absolute -inset-y-1 w-0.5 bg-accent"
- // eslint-disable-next-line react/forbid-dom-props
- style={{ left: `${openedAtPct}%` }}
- data-attr="trace-dialog-opened-at-marker"
- />
+ {openedAtPct != null && openedFrom && (
+ <Tooltip
+ title={
+ <div className="flex flex-col gap-0.5 max-w-80">
+ <span className="text-xs opacity-75">
+ {/* A missing parent has no real start or duration to measure the log against. */}
+ Your log ·{' '}
+ {isMissingParent
+ ? ''
+ : `${logTimingLabel(openedAtUs!, startUs, span.duration_nano / 1_000)} · `}
+ {openedFrom.severity}
+ </span>
+ <span className="ph-replay-block ph-no-capture font-mono line-clamp-4" translate="no">
+ {openedFrom.body}
+ </span>
+ </div>
+ }
+ >
+ <span
+ className="pointer-events-auto absolute top-1/2 -translate-x-1/2 -translate-y-1/2 flex"
+ // eslint-disable-next-line react/forbid-dom-props
+ style={{ left: `${openedAtPct}%` }}
+ data-attr="trace-dialog-opened-at-marker"
+ >
+ <TraceDialogLogMark severity={openedFrom.severity} />
+ </span>
+ </Tooltip>
)}
</span>
<span
diff --git a/products/tracing/frontend/components/TraceDialog/TraceDialogSpanTree.tsx b/products/tracing/frontend/components/TraceDialog/TraceDialogSpanTree.tsx
index f579a518c83d..0a67969ac652 100644
--- a/products/tracing/frontend/components/TraceDialog/TraceDialogSpanTree.tsx
+++ b/products/tracing/frontend/components/TraceDialog/TraceDialogSpanTree.tsx
@@ -8,7 +8,7 @@ import { SizeProps } from 'lib/components/AutoSizer/AutoSizer'
import { getTimelineTicks } from 'products/tracing/frontend/spanTree'
import { formatDuration } from 'products/tracing/frontend/TraceWaterfallView'
-import { traceDialogLogic, type TraceDialogTimeline } from './traceDialogLogic'
+import { traceDialogLogic, type TraceDialogSource, type TraceDialogTimeline } from './traceDialogLogic'
import { TraceDialogMoreSpansRow } from './TraceDialogMoreSpansRow'
import { rowSpanNode, type TraceDialogRow } from './traceDialogRows'
import { TraceDialogSpanRow } from './TraceDialogSpanRow'
@@ -24,6 +24,7 @@ interface SpanListRowData {
selectedSpanId: string | null
openedSpanId: string | null
openedAtUs: number | null
+ openedFrom: TraceDialogSource | null
onSelect: (spanId: string) => void
onToggleCollapsed: (spanId: string) => void
onToggleChain: (spanId: string) => void
@@ -40,6 +41,7 @@ function SpanListRow({
selectedSpanId,
openedSpanId,
openedAtUs,
+ openedFrom,
onShowAll,
...rowProps
}: {
@@ -59,6 +61,7 @@ function SpanListRow({
selectedSpanId={selectedSpanId}
openedSpanId={openedSpanId}
openedAtUs={openedAtUs}
+ openedFrom={openedFrom}
descendantCounts={descendantCounts}
collapsedSpanIds={collapsedSpanIds}
rowProps={rowProps}
@@ -73,6 +76,7 @@ function SpanOrChainRow({
selectedSpanId,
openedSpanId,
openedAtUs,
+ openedFrom,
descendantCounts,
collapsedSpanIds,
rowProps,
@@ -81,6 +85,7 @@ function SpanOrChainRow({
selectedSpanId: string | null
openedSpanId: string | null
openedAtUs: number | null
+ openedFrom: TraceDialogSource | null
descendantCounts: Map<string, number>
collapsedSpanIds: Set<string>
rowProps: Pick<SpanListRowData, 'timeline' | 'serviceColorMap' | 'onSelect' | 'onToggleCollapsed' | 'onToggleChain'>
@@ -91,6 +96,7 @@ function SpanOrChainRow({
row={row}
isSelected={spanId === selectedSpanId}
openedAtUs={spanId === openedSpanId ? openedAtUs : null}
+ openedFrom={spanId === openedSpanId ? openedFrom : null}
descendantCount={descendantCounts.get(spanId) ?? 0}
isCollapsed={collapsedSpanIds.has(spanId)}
{...rowProps}
@@ -126,6 +132,7 @@ export function TraceDialogSpanTree(): JSX.Element {
missingParentSpanId,
trace,
openedAtUs,
+ openedFrom,
} = useValues(traceDialogLogic)
const { selectSpan, toggleSpanCollapsed, toggleChain, showAllChildren } = useActions(traceDialogLogic)
const [topIndex, setTopIndex] = useState(0)
@@ -245,6 +252,7 @@ export function TraceDialogSpanTree(): JSX.Element {
selectedSpanId,
openedSpanId,
openedAtUs,
+ openedFrom,
onSelect: selectSpan,
onToggleCollapsed: toggleSpanCollapsed,
onToggleChain: toggleChain,