Skip to content

feat(tracing): Add dynamic sampling correlation context data to envelope header #3062

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/core/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ export function eventToSentryRequest(event: Event, api: API): SentryRequest {
const envelopeHeaders = JSON.stringify({
event_id: event.event_id,
sent_at: new Date().toISOString(),

// trace context for dynamic sampling on relay
trace: {
trace_id: event.contexts?.trace?.trace_id,
// TODO: any reason we can't change this property to be called publicKey, since that's what it is?
public_key: api.getDsn().user,
environment: event.environment || 'no environment specified',
release: event.release || 'no release specified',
},
});

const itemHeaders = JSON.stringify({
type: event.type,
// The content-type is assumed to be 'application/json' and not part of
Expand All @@ -55,6 +65,7 @@ export function eventToSentryRequest(event: Event, api: API): SentryRequest {
//
// length: new TextEncoder().encode(req.body).length,
});

// The trailing newline is optional. We intentionally don't send it to avoid
// sending unnecessary bytes.
//
Expand Down
65 changes: 65 additions & 0 deletions packages/core/test/lib/request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Event } from '@sentry/types';

import { API } from '../../src/api';
import { eventToSentryRequest } from '../../src/request';

describe('eventToSentryRequest', () => {
const api = new API('https://[email protected]/12312012');

it('correctly handles error/message events', () => {
const event = {
event_id: '1231201211212012',
exception: { values: [{ type: 'PuppyProblemsError', value: 'Charlie ate the flip-flops :-(' }] },
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
};

const result = eventToSentryRequest(event, api);
expect(result.type).toEqual('event');
expect(result.url).toEqual(
'https://squirrelchasers.ingest.sentry.io/api/12312012/store/?sentry_key=dogsarebadatkeepingsecrets&sentry_version=7',
);
expect(result.body).toEqual(JSON.stringify(event));
});

it('correctly handles transaction events', () => {
const eventId = '1231201211212012';
const traceId = '0908201304152013';
const event = {
contexts: { trace: { trace_id: traceId, span_id: '12261980', op: 'pageload' } },
environment: 'dogpark',
event_id: eventId,
release: 'off.leash.park',
spans: [],
transaction: '/dogs/are/great/',
type: 'transaction',
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
};

const result = eventToSentryRequest(event as Event, api);

const [envelopeHeaderString, itemHeaderString, eventString] = result.body.split('\n');

const envelope = {
envelopeHeader: JSON.parse(envelopeHeaderString),
itemHeader: JSON.parse(itemHeaderString),
event: JSON.parse(eventString),
};

expect(result.type).toEqual('transaction');
expect(result.url).toEqual(
'https://squirrelchasers.ingest.sentry.io/api/12312012/envelope/?sentry_key=dogsarebadatkeepingsecrets&sentry_version=7',
);
expect(envelope.envelopeHeader).toEqual({
event_id: eventId,
sent_at: expect.any(String),
trace: {
environment: 'dogpark',
public_key: 'dogsarebadatkeepingsecrets',
release: 'off.leash.park',
trace_id: traceId,
},
});
expect(envelope.itemHeader).toEqual({ type: 'transaction' });
expect(envelope.event).toEqual(event);
});
});