: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
fix(nodejs): capture instrumented errors while their span is active (#117254)
Paul D'Ambra6ad608752024
diff --git a/nodejs/src/common/tracing/tracing-utils.test.ts b/nodejs/src/common/tracing/tracing-utils.test.tsindex ee8b5c16acce..665490b6a891 100644--- a/nodejs/src/common/tracing/tracing-utils.test.ts+++ b/nodejs/src/common/tracing/tracing-utils.test.ts@@ -2,6 +2,8 @@ import { context, propagation, trace } from '@opentelemetry/api'import { InMemorySpanExporter, NodeTracerProvider, SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node'import { register } from 'prom-client'+import * as posthogUtils from '~/common/utils/posthog'+import { instrumentFn, startDetachedSpan } from './tracing-utils'describe('instrumentFn', () => {@@ -48,4 +50,16 @@ describe('instrumentFn', () => {expect(Object.keys(byName).sort()).toEqual(['batch', 'step', 'unrelated'])expect(byName.step.parentSpanContext?.spanId).toBe(byName.batch.spanContext().spanId)})++ it('captures a failure while its span is active, so the error carries its trace id', async () => {+ let activeSpanIdAtCapture: string | undefined+ jest.spyOn(posthogUtils, 'captureException').mockImplementationOnce(() => {+ activeSpanIdAtCapture = trace.getActiveSpan()?.spanContext().spanId+ })++ await expect(instrumentFn('failing_step', () => Promise.reject(new Error('boom')))).rejects.toThrow('boom')++ const [span] = exporter.getFinishedSpans()+ expect(activeSpanIdAtCapture).toBe(span.spanContext().spanId)+ })})diff --git a/nodejs/src/common/tracing/tracing-utils.ts b/nodejs/src/common/tracing/tracing-utils.tsindex b36dc550c302..ac8a69ec0587 100644--- a/nodejs/src/common/tracing/tracing-utils.ts+++ b/nodejs/src/common/tracing/tracing-utils.ts@@ -188,20 +188,30 @@ export async function instrumentFn<T>(})const startTime = performance.now()const end = measureTime ? instrumentedFunctionDuration.startTimer({ function: key }) : undefined+ const funcReportingErrorsInSpan = sendException+ ? async (): Promise<T> => {+ try {+ return await func()+ } catch (error) {+ captureException(error)+ throw error+ }+ }+ : functry {// Skip expensive span creation when tracing is disabledconst result = defaultConfig.DISABLE_OPENTELEMETRY_TRACING- ? await func()+ ? await funcReportingErrorsInSpan(): span? await withSpan('instrumented_function',key,tag ? { ...attributes, tag } : attributes,- func,+ funcReportingErrorsInSpan,parentContext)- : await withSummary(key, tag, func)+ : await withSummary(key, tag, funcReportingErrorsInSpan)end?.({ success: 'true' })if (logExecutionTime) {logTime(startTime, key)@@ -213,9 +223,6 @@ export async function instrumentFn<T>(if (logExecutionTime) {logTime(startTime, key, error)}- if (sendException) {- captureException(error)- }throw error} finally {clearTimeout(t)