Skip to content

Commit d2d361f

Browse files
committed
feat(tracing): Add dynamic sampling correlation context data to envelope header (#3062)
1 parent 38937c6 commit d2d361f

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

packages/core/src/request.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ export function eventToSentryRequest(event: Event, api: API): SentryRequest {
4242
const envelopeHeaders = JSON.stringify({
4343
event_id: event.event_id,
4444
sent_at: new Date().toISOString(),
45+
46+
// trace context for dynamic sampling on relay
47+
trace: {
48+
trace_id: event.contexts?.trace?.trace_id,
49+
// TODO: any reason we can't change this property to be called publicKey, since that's what it is?
50+
public_key: api.getDsn().user,
51+
environment: event.environment || 'no environment specified',
52+
release: event.release || 'no release specified',
53+
},
4554
});
55+
4656
const itemHeaders = JSON.stringify({
4757
type: event.type,
4858

@@ -64,6 +74,7 @@ export function eventToSentryRequest(event: Event, api: API): SentryRequest {
6474
//
6575
// length: new TextEncoder().encode(req.body).length,
6676
});
77+
6778
// The trailing newline is optional. We intentionally don't send it to avoid
6879
// sending unnecessary bytes.
6980
//

packages/core/test/lib/request.test.ts

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,74 @@ describe('eventToSentryRequest', () => {
1616
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
1717
};
1818

19+
it('injects correct data for error/message events', () => {
20+
const event = {
21+
event_id: '1231201211212012',
22+
exception: { values: [{ type: 'PuppyProblemsError', value: 'Charlie ate the flip-flops :-(' }] },
23+
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
24+
};
25+
26+
const result = eventToSentryRequest(event, api);
27+
expect(result.type).toEqual('event');
28+
expect(result.url).toEqual(
29+
'https://squirrelchasers.ingest.sentry.io/api/12312012/store/?sentry_key=dogsarebadatkeepingsecrets&sentry_version=7',
30+
);
31+
expect(result.body).toEqual(JSON.stringify(event));
32+
});
33+
34+
it('injects correct data for transaction events', () => {
35+
const eventId = '1231201211212012';
36+
const traceId = '0908201304152013';
37+
const event = {
38+
contexts: { trace: { trace_id: traceId, span_id: '12261980', op: 'pageload' } },
39+
environment: 'dogpark',
40+
event_id: eventId,
41+
release: 'off.leash.park',
42+
spans: [],
43+
transaction: '/dogs/are/great/',
44+
type: 'transaction',
45+
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
46+
};
47+
48+
const result = eventToSentryRequest(event as Event, api);
49+
50+
const [envelopeHeaderString, itemHeaderString, eventString] = result.body.split('\n');
51+
52+
const envelope = {
53+
envelopeHeader: JSON.parse(envelopeHeaderString),
54+
itemHeader: JSON.parse(itemHeaderString),
55+
event: JSON.parse(eventString),
56+
};
57+
58+
expect(result.type).toEqual('transaction');
59+
expect(result.url).toEqual(
60+
'https://squirrelchasers.ingest.sentry.io/api/12312012/envelope/?sentry_key=dogsarebadatkeepingsecrets&sentry_version=7',
61+
);
62+
expect(envelope.envelopeHeader).toEqual({
63+
event_id: eventId,
64+
sent_at: expect.any(String),
65+
trace: {
66+
environment: 'dogpark',
67+
public_key: 'dogsarebadatkeepingsecrets',
68+
release: 'off.leash.park',
69+
trace_id: traceId,
70+
},
71+
});
72+
expect(envelope.itemHeader).toEqual({
73+
type: 'transaction',
74+
sample_rates: expect.any(Array),
75+
});
76+
expect(envelope.event).toEqual(event);
77+
});
78+
1979
[
2080
{ method: TransactionSamplingMethod.Rate, rate: '0.1121', dog: 'Charlie' },
2181
{ method: TransactionSamplingMethod.Sampler, rate: '0.1231', dog: 'Maisey' },
2282
{ method: TransactionSamplingMethod.Inheritance, dog: 'Cory' },
2383
{ method: TransactionSamplingMethod.Explicit, dog: 'Bodhi' },
2484

25-
// this shouldn't ever happen (at least the method should always be included in tags), but good to know that things
26-
// won't blow up if it does
85+
// this shouldn't ever happen (tags should always include at least the sampling method), but good to know that
86+
// things won't blow up if it does happen
2787
{ dog: 'Lucy' },
2888
].forEach(({ method, rate, dog }) => {
2989
it(`adds transaction sampling information to item header - ${method}, ${rate}, ${dog}`, () => {

0 commit comments

Comments
 (0)