Skip to content

Commit d866b9b

Browse files
committed
add correlation context, tests
1 parent c723860 commit d866b9b

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

packages/core/src/request.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,20 @@ export function eventToSentryRequest(event: Event, api: API): SentryRequest {
3737
if (useEnvelope) {
3838
const envelopeHeaders = JSON.stringify({
3939
event_id: event.event_id,
40+
4041
// We need to add * 1000 since we divide it by 1000 by default but JS works with ms precision
4142
// The reason we use timestampWithMs here is that all clocks across the SDK use the same clock
4243
sent_at: new Date().toISOString(),
44+
45+
// trace context for dynamic sampling on relay
46+
trace: {
47+
trace_id: event.contexts?.trace?.trace_id,
48+
// TODO: any reason we can't change this property to be called publicKey, since that's what it is?
49+
public_key: api.getDsn().user,
50+
release: event.release || 'no release specified',
51+
},
4352
});
53+
4454
const itemHeaders = JSON.stringify({
4555
type: event.type,
4656
// The content-type is assumed to be 'application/json' and not part of
@@ -57,6 +67,7 @@ export function eventToSentryRequest(event: Event, api: API): SentryRequest {
5767
//
5868
// length: new TextEncoder().encode(req.body).length,
5969
});
70+
6071
// The trailing newline is optional. We intentionally don't send it to avoid
6172
// sending unnecessary bytes.
6273
//
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
event_id: eventId,
30+
release: 'off.leash.park',
31+
spans: [],
32+
transaction: '/dogs/are/great/',
33+
type: 'transaction',
34+
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
35+
};
36+
37+
const result = eventToSentryRequest(event as Event, api);
38+
39+
const [envelopeHeaderString, itemHeaderString, eventString] = result.body.split('\n');
40+
41+
const envelope = {
42+
envelopeHeader: JSON.parse(envelopeHeaderString),
43+
itemHeader: JSON.parse(itemHeaderString),
44+
event: JSON.parse(eventString),
45+
};
46+
47+
expect(result.type).toEqual('transaction');
48+
expect(result.url).toEqual(
49+
'https://squirrelchasers.ingest.sentry.io/api/12312012/envelope/?sentry_key=dogsarebadatkeepingsecrets&sentry_version=7',
50+
);
51+
expect(envelope.envelopeHeader).toEqual({
52+
event_id: eventId,
53+
sent_at: expect.any(String),
54+
trace: { public_key: 'dogsarebadatkeepingsecrets', release: 'off.leash.park', trace_id: traceId },
55+
});
56+
expect(envelope.itemHeader).toEqual({ type: 'transaction' });
57+
expect(envelope.event).toEqual(event);
58+
});
59+
});

0 commit comments

Comments
 (0)