Skip to content

Commit 1ed9f52

Browse files
committed
fix(posthog): use traceparent header as canonical request_id source
1 parent 116a3c5 commit 1ed9f52

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,10 @@ export function useChat(
13601360
const onRequestStartedRef = useRef(options?.onRequestStarted)
13611361
onRequestStartedRef.current = options?.onRequestStarted
13621362

1363-
const getCurrentRequestId = useCallback(() => streamRequestIdRef.current, [])
1363+
const getCurrentRequestId = useCallback(() => {
1364+
const traceId = streamTraceparentRef.current?.split('-')[1] ?? ''
1365+
return /^[0-9a-f]{32}$/.test(traceId) ? traceId : undefined
1366+
}, [])
13641367

13651368
const clearQueueDispatchState = useCallback(() => {
13661369
queueDispatchEpochRef.current++

apps/sim/lib/posthog/server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ export function captureServerEvent<E extends PostHogEventName>(
7171
const client = getClient()
7272
if (!client) return
7373

74-
const requestId = getRequestContext()?.requestId
74+
const contextRequestId = getRequestContext()?.requestId
75+
const props = properties as Record<string, unknown>
7576
client.capture({
7677
distinctId,
7778
event,
7879
properties: {
79-
...(requestId ? { request_id: requestId } : {}),
8080
...properties,
81+
...(contextRequestId && !('request_id' in props) ? { request_id: contextRequestId } : {}),
8182
...(options?.groups ? { $groups: options.groups } : {}),
8283
...(options?.set ? { $set: options.set } : {}),
8384
...(options?.setOnce ? { $set_once: options.setOnce } : {}),

0 commit comments

Comments
 (0)