-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ref(node): Extract propagation context in tracingHandler
#8425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8e7de36
096c095
0593684
d2c28cc
b9bcb93
0f64764
b4bda1f
f30e965
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,7 +1,7 @@ | ||||||||
import type { Hub } from '@sentry/core'; | ||||||||
import * as sentryCore from '@sentry/core'; | ||||||||
import { setAsyncContextStrategy, Transaction } from '@sentry/core'; | ||||||||
import type { Event } from '@sentry/types'; | ||||||||
import type { Event, PropagationContext } from '@sentry/types'; | ||||||||
import { SentryError } from '@sentry/utils'; | ||||||||
import * as http from 'http'; | ||||||||
|
||||||||
|
@@ -209,6 +209,11 @@ describe('tracingHandler', () => { | |||||||
jest.restoreAllMocks(); | ||||||||
}); | ||||||||
|
||||||||
function getPropagationContext(): PropagationContext { | ||||||||
// @ts-expect-error accesing private property for test | ||||||||
return hub.getScope()._propagationContext; | ||||||||
Comment on lines
+213
to
+214
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
You can access private properties without TS complaining and without There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh TIL! good call. |
||||||||
} | ||||||||
|
||||||||
it('creates a transaction when handling a request', () => { | ||||||||
const startTransaction = jest.spyOn(sentryCore, 'startTransaction'); | ||||||||
|
||||||||
|
@@ -251,6 +256,13 @@ describe('tracingHandler', () => { | |||||||
|
||||||||
const transaction = (res as any).__sentry_transaction; | ||||||||
|
||||||||
expect(getPropagationContext()).toEqual({ | ||||||||
traceId: '12312012123120121231201212312012', | ||||||||
parentSpanId: '1121201211212012', | ||||||||
spanId: expect.any(String), | ||||||||
sampled: false, | ||||||||
}); | ||||||||
|
||||||||
// since we have no tracesSampler defined, the default behavior (inherit if possible) applies | ||||||||
expect(transaction.traceId).toEqual('12312012123120121231201212312012'); | ||||||||
expect(transaction.parentSpanId).toEqual('1121201211212012'); | ||||||||
|
@@ -260,18 +272,26 @@ describe('tracingHandler', () => { | |||||||
|
||||||||
it("pulls parent's data from tracing and baggage headers on the request", () => { | ||||||||
req.headers = { | ||||||||
'sentry-trace': '12312012123120121231201212312012-1121201211212012-0', | ||||||||
'sentry-trace': '12312012123120121231201212312012-1121201211212012-1', | ||||||||
baggage: 'sentry-version=1.0,sentry-environment=production', | ||||||||
}; | ||||||||
|
||||||||
sentryTracingMiddleware(req, res, next); | ||||||||
|
||||||||
expect(getPropagationContext()).toEqual({ | ||||||||
traceId: '12312012123120121231201212312012', | ||||||||
parentSpanId: '1121201211212012', | ||||||||
spanId: expect.any(String), | ||||||||
sampled: true, | ||||||||
dsc: { version: '1.0', environment: 'production' }, | ||||||||
}); | ||||||||
|
||||||||
const transaction = (res as any).__sentry_transaction; | ||||||||
|
||||||||
// since we have no tracesSampler defined, the default behavior (inherit if possible) applies | ||||||||
expect(transaction.traceId).toEqual('12312012123120121231201212312012'); | ||||||||
expect(transaction.parentSpanId).toEqual('1121201211212012'); | ||||||||
expect(transaction.sampled).toEqual(false); | ||||||||
expect(transaction.sampled).toEqual(true); | ||||||||
expect(transaction.metadata?.dynamicSamplingContext).toStrictEqual({ version: '1.0', environment: 'production' }); | ||||||||
}); | ||||||||
|
||||||||
|
@@ -283,6 +303,8 @@ describe('tracingHandler', () => { | |||||||
|
||||||||
sentryTracingMiddleware(req, res, next); | ||||||||
|
||||||||
expect(getPropagationContext().dsc).toEqual({ version: '1.0', environment: 'production' }); | ||||||||
|
||||||||
const transaction = (res as any).__sentry_transaction; | ||||||||
expect(transaction.metadata?.dynamicSamplingContext).toStrictEqual({ version: '1.0', environment: 'production' }); | ||||||||
}); | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: could be slightly simplified to:
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have the
isString
check here because req.headers['sentry-trace'] can provide an array, and we currently don't consider that valid.I'd rather come back and re-evaluate that decision for every header usage, but for now don't want to unblock this change in particular. Adding as a TODO to the GH issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, I see, makes sense. 👍