Skip to content

Commit 10d0600

Browse files
committed
feat(tracing): Add dynamic sampling correlation context data to envelope header (#3062)
1 parent 928e96a commit 10d0600

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

packages/core/src/request.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,17 @@ export function eventToSentryRequest(event: Event, api: API): SentryRequest {
3838
const envelopeHeaders = JSON.stringify({
3939
event_id: event.event_id,
4040
sent_at: new Date().toISOString(),
41+
42+
// trace context for dynamic sampling on relay
43+
trace: {
44+
trace_id: event.contexts?.trace?.trace_id,
45+
// TODO: any reason we can't change this property to be called publicKey, since that's what it is?
46+
public_key: api.getDsn().user,
47+
environment: event.environment || 'no environment specified',
48+
release: event.release || 'no release specified',
49+
},
4150
});
51+
4252
const itemHeaders = JSON.stringify({
4353
type: event.type,
4454
// The content-type is assumed to be 'application/json' and not part of
@@ -55,6 +65,7 @@ export function eventToSentryRequest(event: Event, api: API): SentryRequest {
5565
//
5666
// length: new TextEncoder().encode(req.body).length,
5767
});
68+
5869
// The trailing newline is optional. We intentionally don't send it to avoid
5970
// sending unnecessary bytes.
6071
//
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Event } from '@sentry/types';
2+
3+
import { API } from '../../src/api';
4+
import { eventToSentryRequest } from '../../src/request';
5+
6+
describe('eventToSentryRequest', () => {
7+
const api = new API('https://[email protected]/12312012');
8+
9+
it('correctly handles error/message events', () => {
10+
const event = {
11+
event_id: '1231201211212012',
12+
exception: { values: [{ type: 'PuppyProblemsError', value: 'Charlie ate the flip-flops :-(' }] },
13+
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
14+
};
15+
16+
const result = eventToSentryRequest(event, api);
17+
expect(result.type).toEqual('event');
18+
expect(result.url).toEqual(
19+
'https://squirrelchasers.ingest.sentry.io/api/12312012/store/?sentry_key=dogsarebadatkeepingsecrets&sentry_version=7',
20+
);
21+
expect(result.body).toEqual(JSON.stringify(event));
22+
});
23+
24+
it('correctly handles transaction events', () => {
25+
const eventId = '1231201211212012';
26+
const traceId = '0908201304152013';
27+
const event = {
28+
contexts: { trace: { trace_id: traceId, span_id: '12261980', op: 'pageload' } },
29+
environment: 'dogpark',
30+
event_id: eventId,
31+
release: 'off.leash.park',
32+
spans: [],
33+
transaction: '/dogs/are/great/',
34+
type: 'transaction',
35+
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
36+
};
37+
38+
const result = eventToSentryRequest(event as Event, api);
39+
40+
const [envelopeHeaderString, itemHeaderString, eventString] = result.body.split('\n');
41+
42+
const envelope = {
43+
envelopeHeader: JSON.parse(envelopeHeaderString),
44+
itemHeader: JSON.parse(itemHeaderString),
45+
event: JSON.parse(eventString),
46+
};
47+
48+
expect(result.type).toEqual('transaction');
49+
expect(result.url).toEqual(
50+
'https://squirrelchasers.ingest.sentry.io/api/12312012/envelope/?sentry_key=dogsarebadatkeepingsecrets&sentry_version=7',
51+
);
52+
expect(envelope.envelopeHeader).toEqual({
53+
event_id: eventId,
54+
sent_at: expect.any(String),
55+
trace: {
56+
environment: 'dogpark',
57+
public_key: 'dogsarebadatkeepingsecrets',
58+
release: 'off.leash.park',
59+
trace_id: traceId,
60+
},
61+
});
62+
expect(envelope.itemHeader).toEqual({ type: 'transaction' });
63+
expect(envelope.event).toEqual(event);
64+
});
65+
});

0 commit comments

Comments
 (0)