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.

1.7 KB
Raw Blame
import { IconGraph } from '@posthog/icons'
import { LemonButton, LemonButtonProps } from '@posthog/lemon-ui'

import { getAccessControlDisabledReason } from 'lib/utils/accessControlUtils'

import { AccessControlLevel, AccessControlResourceType } from '~/types'

import { traceUrl } from './traceUrl'

export interface ViewTraceButtonProps extends Pick<
    LemonButtonProps,
    'size' | 'type' | 'className' | 'data-attr' | 'onClick'
> {
    /** Hex trace id, as every product's query runner returns it. */
    traceId: string | null | undefined
    /** Anchors the waterfall on one span. */
    spanId?: string | null
    /** Bounds the cold-load lookup — OTel trace ids embed no timestamp. */
    timestamp?: string | null
    iconOnly?: boolean
}

/**
 * The shared "go to this trace" entry point, for anything that carries a trace id: a log row, an
 * error event, a metric sample. Mirrors logs' `ViewLogsButton`.
 */
export function ViewTraceButton({
    traceId,
    spanId,
    timestamp,
    iconOnly,
    ...buttonProps
}: ViewTraceButtonProps): JSX.Element | null {
    // Most rows have no trace. Rendering a link to nothing is worse than rendering nothing.
    // Hidden rather than disabled without access, matching the trace links logs already has.
    if (!traceId || getAccessControlDisabledReason(AccessControlResourceType.Tracing, AccessControlLevel.Viewer)) {
        return null
    }

    return (
        <LemonButton
            icon={<IconGraph />}
            to={traceUrl({ traceId, spanId, ts: timestamp })}
            tooltip={iconOnly ? 'View the trace this came from' : undefined}
            {...buttonProps}
        >
            {iconOnly ? undefined : 'View trace'}
        </LemonButton>
    )
}