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(logs): show person and errors in the log drawer header (#115342)

Jon McCallumf05734427904
diff --git a/products/logs/frontend/components/LogsViewer/LogContextLinks/LogContextLinks.tsx b/products/logs/frontend/components/LogsViewer/LogContextLinks/LogContextLinks.tsx
new file mode 100644
index 000000000000..863e1089570c
--- /dev/null
+++ b/products/logs/frontend/components/LogsViewer/LogContextLinks/LogContextLinks.tsx
@@ -0,0 +1,90 @@
+import { useValues } from 'kea'
+
+import { LemonTag } from '@posthog/lemon-ui'
+
+import ViewRecordingButton, { RecordingPlayerType } from 'lib/components/ViewRecordingButton/ViewRecordingButton'
+import { userHasAccess } from 'lib/utils/accessControlUtils'
+
+import { AccessControlLevel, AccessControlResourceType } from '~/types'
+
+import { buildLogErrorsLogicProps } from 'products/logs/frontend/components/LogsViewer/LogDetailsModal/Tabs/RelatedErrors/buildLogErrorsLogicProps'
+import { logsConfigLogic } from 'products/logs/frontend/logsConfigLogic'
+import { ParsedLogMessage } from 'products/logs/frontend/types'
+import { getDistinctIdWithKey, getSessionIdFromLogAttributes } from 'products/logs/frontend/utils'
+import { PersonDisplay } from 'products/persons/frontend/components/PersonDisplay'
+import { ViewTraceButton } from 'products/tracing/frontend/components/ViewTraceButton'
+import { type ErrorScope, usableId } from 'products/tracing/frontend/errorCorrelation'
+
+import { LogErrorCount } from './LogErrorCount'
+
+export type LogContextLinksSurface = 'drawer' | 'row'
+
+// Frozen data-attr values: autocapture dashboards match on them, so the two surfaces keep different trace attrs.
+const TRACE_DATA_ATTR: Record<LogContextLinksSurface, string> = {
+ drawer: 'logs-details-view-trace',
+ row: 'logs-row-view-trace',
+}
+
+export interface LogContextLinksProps {
+ log: ParsedLogMessage
+ surface: LogContextLinksSurface
+ /** Called when the error count is clicked, with the scope the count was taken on. */
+ onOpenRelatedErrors: (scope: ErrorScope) => void
+}
+
+export function LogContextLinks({ log, surface, onOpenRelatedErrors }: LogContextLinksProps): JSX.Element | null {
+ const { configuredSessionIdKeys, configuredDistinctIdKeys } = useValues(logsConfigLogic)
+
+ const traceId = usableId(log.trace_id)
+ const sessionId = getSessionIdFromLogAttributes(log.attributes, log.resource_attributes, configuredSessionIdKeys)
+ const distinctId = getDistinctIdWithKey(log.attributes, log.resource_attributes, configuredDistinctIdKeys)?.value
+
+ const canViewErrors = userHasAccess(AccessControlResourceType.ErrorTracking, AccessControlLevel.Viewer)
+
+ if (!traceId && !sessionId && !distinctId) {
+ return null
+ }
+
+ return (
+ <div className="flex items-center gap-1 flex-wrap min-w-0">
+ {traceId && (
+ <ViewTraceButton
+ traceId={traceId}
+ spanId={log.span_id}
+ timestamp={log.timestamp}
+ size="xsmall"
+ type="secondary"
+ data-attr={TRACE_DATA_ATTR[surface]}
+ />
+ )}
+ {(traceId || sessionId) && canViewErrors && (
+ <LogErrorCount
+ logicProps={buildLogErrorsLogicProps(log, sessionId)}
+ surface={surface}
+ onOpenRelatedErrors={onOpenRelatedErrors}
+ />
+ )}
+ {distinctId && (
+ <LemonTag
+ size="small"
+ className="bg-surface-primary min-w-0"
+ data-attr={`logs-${surface}-context-person`}
+ >
+ <span className="sr-only">Person</span>
+ <PersonDisplay person={{ distinct_id: distinctId }} withIcon="sm" />
+ </LemonTag>
+ )}
+ {sessionId && (
+ <ViewRecordingButton
+ sessionId={sessionId}
+ timestamp={log.timestamp}
+ size="xsmall"
+ type="secondary"
+ openPlayerIn={RecordingPlayerType.Modal}
+ checkRecordingExists
+ data-attr={`logs-${surface}-context-recording`}
+ />
+ )}
+ </div>
+ )
+}
diff --git a/products/logs/frontend/components/LogsViewer/LogContextLinks/LogErrorCount.tsx b/products/logs/frontend/components/LogsViewer/LogContextLinks/LogErrorCount.tsx
new file mode 100644
index 000000000000..69b03579cf2b
--- /dev/null
+++ b/products/logs/frontend/components/LogsViewer/LogContextLinks/LogErrorCount.tsx
@@ -0,0 +1,63 @@
+import { useValues } from 'kea'
+
+import { IconWarning } from '@posthog/icons'
+import { LemonButton } from '@posthog/lemon-ui'
+
+import { pluralize } from 'lib/utils/strings'
+
+import {
+ TraceErrorsLogicProps,
+ traceErrorsLogic,
+} from 'products/tracing/frontend/components/TraceDrawer/traceErrorsLogic'
+import type { ErrorScope } from 'products/tracing/frontend/errorCorrelation'
+
+import type { LogContextLinksSurface } from './LogContextLinks'
+
+const SCOPE_WHERE: Record<ErrorScope, string> = {
+ span: 'in this span',
+ trace: 'in this trace',
+ session: 'in this session',
+}
+
+export interface LogErrorCountProps {
+ logicProps: TraceErrorsLogicProps
+ surface: LogContextLinksSurface
+ onOpenRelatedErrors: (scope: ErrorScope) => void
+}
+
+export function LogErrorCount({ logicProps, surface, onOpenRelatedErrors }: LogErrorCountProps): JSX.Element | null {
+ const { issues, issuesLoading, issuesFailed, effectiveScope } = useValues(traceErrorsLogic(logicProps))
+
+ if (!effectiveScope) {
+ return null
+ }
+
+ const where = SCOPE_WHERE[effectiveScope]
+ const errorCount = issues.reduce((sum, issue) => sum + issue.occurrences, 0)
+
+ if (!issuesLoading && !issuesFailed && errorCount === 0) {
+ return (
+ <span className="text-xs text-secondary" data-attr={`logs-${surface}-context-no-errors`}>
+ {`No errors ${where}`}
+ </span>
+ )
+ }
+
+ return (
+ <LemonButton
+ size="xsmall"
+ type="secondary"
+ status={issuesLoading || issuesFailed ? 'default' : 'danger'}
+ icon={<IconWarning />}
+ loading={issuesLoading}
+ onClick={() => onOpenRelatedErrors(effectiveScope)}
+ data-attr={`logs-${surface}-context-errors`}
+ >
+ {issuesLoading
+ ? `Counting errors ${where}`
+ : issuesFailed
+ ? "Couldn't count errors"
+ : `${pluralize(errorCount, 'error')} ${where}`}
+ </LemonButton>
+ )
+}
diff --git a/products/logs/frontend/components/LogsViewer/LogDetailsModal/LogDetailsModal.tsx b/products/logs/frontend/components/LogsViewer/LogDetailsModal/LogDetailsModal.tsx
index 49891582dc9a..434390d5310f 100644
--- a/products/logs/frontend/components/LogsViewer/LogDetailsModal/LogDetailsModal.tsx
+++ b/products/logs/frontend/components/LogsViewer/LogDetailsModal/LogDetailsModal.tsx
@@ -5,15 +5,14 @@ import { LemonButton, LemonCheckbox, LemonDrawer, LemonTabs } from '@posthog/lem
import { JSONViewer } from 'lib/components/JSONViewer'
import { TZLabel } from 'lib/components/TZLabel'
-import ViewRecordingButton, { RecordingPlayerType } from 'lib/components/ViewRecordingButton/ViewRecordingButton'
import { IconLink } from 'lib/lemon-ui/icons'
import { PropertyFilterType, PropertyOperator } from '~/types'
import { CopyLogButton, copyLogRaw } from 'products/logs/frontend/components/LogsViewer/CopyLogButton'
+import { LogContextLinks } from 'products/logs/frontend/components/LogsViewer/LogContextLinks/LogContextLinks'
import { LogContextSelector } from 'products/logs/frontend/components/LogsViewer/LogContextSelector/LogContextSelector'
import { LogDetailsTabContent } from 'products/logs/frontend/components/LogsViewer/LogDetailsModal/Tabs/Details/LogDetailsTab'
-import { ViewTraceButton } from 'products/tracing/frontend/components/ViewTraceButton'
import { logsViewerLogic } from '../logsViewerLogic'
import { LogComments } from './LogComments'
@@ -114,7 +113,7 @@ export function LogDetailsModal({ timezone }: LogDetailsModalProps): JSX.Element
/>
</div>
</div>
- <div className="flex items-center justify-between">
+ <div className="flex items-center justify-between flex-wrap gap-2">
<div className="flex items-center gap-6">
<div className="flex items-center gap-2">
<span className="text-muted text-xs font-semibold uppercase">Timestamp</span>
@@ -136,25 +135,14 @@ export function LogDetailsModal({ timezone }: LogDetailsModalProps): JSX.Element
</div>
</div>
</div>
- <div className="flex items-center gap-1">
- <ViewTraceButton
- traceId={selectedLog.trace_id}
- spanId={selectedLog.span_id}
- timestamp={selectedLog.timestamp}
- size="xsmall"
- type="secondary"
- data-attr="logs-details-view-trace"
- />
- {sessionId && (
- <ViewRecordingButton
- sessionId={sessionId}
- timestamp={selectedLog.timestamp}
- size="xsmall"
- openPlayerIn={RecordingPlayerType.Modal}
- checkRecordingExists
- />
- )}
- </div>
+ <LogContextLinks
+ surface="drawer"
+ log={selectedLog}
+ // The count and the tab share one logic instance, so the counted scope is already
+ // selected. Passing it as the initial scope would reapply it each time the tab
+ // remounts and overwrite a scope the user picked in the tab.
+ onOpenRelatedErrors={() => setActiveTab('related-errors')}
+ />
</div>
</LemonDrawer.Header>
<LemonDrawer.Content>
diff --git a/products/logs/frontend/components/LogsViewer/LogDetailsModal/Tabs/RelatedErrors/RelatedErrorsTab.tsx b/products/logs/frontend/components/LogsViewer/LogDetailsModal/Tabs/RelatedErrors/RelatedErrorsTab.tsx
index 1dae89195678..c712300bd396 100644
--- a/products/logs/frontend/components/LogsViewer/LogDetailsModal/Tabs/RelatedErrors/RelatedErrorsTab.tsx
+++ b/products/logs/frontend/components/LogsViewer/LogDetailsModal/Tabs/RelatedErrors/RelatedErrorsTab.tsx
@@ -1,19 +1,17 @@
import { BindLogic, useActions, useValues } from 'kea'
+import { useEffect } from 'react'
import { LemonSegmentedButton } from '@posthog/lemon-ui'
import { EmptyMessage } from 'lib/components/EmptyMessage/EmptyMessage'
import { ParsedLogMessage } from 'products/logs/frontend/types'
-import { parseLogTimestamp } from 'products/logs/frontend/utils'
import { ErrorIssuesList } from 'products/tracing/frontend/components/TraceDrawer/ErrorIssuesList'
-import {
- TraceErrorsLogicProps,
- TraceErrorsScope,
- traceErrorsLogic,
-} from 'products/tracing/frontend/components/TraceDrawer/traceErrorsLogic'
+import { TraceErrorsScope, traceErrorsLogic } from 'products/tracing/frontend/components/TraceDrawer/traceErrorsLogic'
import { SESSION_ERRORS_WINDOW_HOURS } from 'products/tracing/frontend/errorCorrelation'
+import { buildLogErrorsLogicProps } from './buildLogErrorsLogicProps'
+
const SCOPE_ORDER: TraceErrorsScope[] = ['span', 'trace', 'session']
const SCOPES: Record<
@@ -57,27 +55,24 @@ export interface RelatedErrorsTabProps {
}
export function RelatedErrorsTab({ log, sessionId, initialScope }: RelatedErrorsTabProps): JSX.Element {
- // Some pipelines send epoch timestamps, which the logic's window would read as an invalid date.
- const timestamp = parseLogTimestamp(log.timestamp)
- const logicProps: TraceErrorsLogicProps = {
- traceId: log.trace_id,
- spanId: log.span_id || null,
- timestamp: timestamp.isValid() ? timestamp.toISOString() : null,
- sessionId,
- initialScope,
- }
-
return (
- <BindLogic logic={traceErrorsLogic} props={logicProps}>
- <RelatedErrorsTabContent />
+ <BindLogic logic={traceErrorsLogic} props={buildLogErrorsLogicProps(log, sessionId)}>
+ <RelatedErrorsTabContent initialScope={initialScope} />
</BindLogic>
)
}
-function RelatedErrorsTabContent(): JSX.Element {
+function RelatedErrorsTabContent({ initialScope }: { initialScope: TraceErrorsScope | null }): JSX.Element {
const { issues, issuesLoading, issuesFailed, availableScopes, effectiveScope } = useValues(traceErrorsLogic)
const { loadIssues, setScope } = useActions(traceErrorsLogic)
+ // Runs only when the requested scope changes, so a scope the user then picks in the tab stays.
+ useEffect(() => {
+ if (initialScope && initialScope !== effectiveScope) {
+ setScope(initialScope)
+ }
+ }, [initialScope]) // oxlint-disable-line react-hooks/exhaustive-deps
+
if (!effectiveScope) {
return (
<div className="flex justify-center w-full py-8">
diff --git a/products/logs/frontend/components/LogsViewer/LogDetailsModal/Tabs/RelatedErrors/buildLogErrorsLogicProps.ts b/products/logs/frontend/components/LogsViewer/LogDetailsModal/Tabs/RelatedErrors/buildLogErrorsLogicProps.ts
new file mode 100644
index 000000000000..79054bfe65f0
--- /dev/null
+++ b/products/logs/frontend/components/LogsViewer/LogDetailsModal/Tabs/RelatedErrors/buildLogErrorsLogicProps.ts
@@ -0,0 +1,18 @@
+import { ParsedLogMessage } from 'products/logs/frontend/types'
+import { parseLogTimestamp } from 'products/logs/frontend/utils'
+import { TraceErrorsLogicProps } from 'products/tracing/frontend/components/TraceDrawer/traceErrorsLogic'
+
+// Every logs surface that counts or lists a log's errors builds its props here, so they all bind
+// the same keyed logic instance and share one query. The initial scope stays null because it is
+// not part of the logic key: two callers that disagreed on it would overwrite each other's props.
+export function buildLogErrorsLogicProps(log: ParsedLogMessage, sessionId: string | null): TraceErrorsLogicProps {
+ // Some pipelines send epoch timestamps, which the logic's window would read as an invalid date.
+ const timestamp = parseLogTimestamp(log.timestamp)
+ return {
+ traceId: log.trace_id,
+ spanId: log.span_id || null,
+ timestamp: timestamp.isValid() ? timestamp.toISOString() : null,
+ sessionId,
+ initialScope: null,
+ }
+}