Skip to content

Commit

Permalink
fix: Include DSN in envelope header for sessions (#3680)
Browse files Browse the repository at this point in the history
* fix: Include DSN in envelope header for sessions

* Add url tests for non-tunnel requests
  • Loading branch information
kamilogorek authored Jun 15, 2021
1 parent e9c6d6a commit ab9efb2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/core/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function sessionToSentryRequest(session: Session | SessionAggregates, api
const envelopeHeaders = JSON.stringify({
sent_at: new Date().toISOString(),
...(sdkInfo && { sdk: sdkInfo }),
...(api.forceEnvelope() && { dsn: api.getDsn().toString() }),
});
// I know this is hacky but we don't want to add `session` to request type since it's never rate limited
const type: SentryRequestType = 'aggregates' in session ? ('sessions' as SentryRequestType) : 'session';
Expand Down
54 changes: 31 additions & 23 deletions packages/core/test/lib/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { DebugMeta, Event, SentryRequest, TransactionSamplingMethod } from '@sen
import { API } from '../../src/api';
import { eventToSentryRequest, sessionToSentryRequest } from '../../src/request';

const api = new API('https://[email protected]/12312012', {
const ingestDsn = 'https://[email protected]/12312012';
const ingestUrl =
'https://squirrelchasers.ingest.sentry.io/api/12312012/envelope/?sentry_key=dogsarebadatkeepingsecrets&sentry_version=7';
const tunnel = 'https://hello.com/world';

const api = new API(ingestDsn, {
sdk: {
integrations: ['AWSLambda'],
name: 'sentry.javascript.browser',
Expand All @@ -12,9 +17,6 @@ const api = new API('https://[email protected]
},
});

const ingestDsn = 'https://[email protected]/12312012';
const tunnel = 'https://hello.com/world';

function parseEnvelopeRequest(request: SentryRequest): any {
const [envelopeHeaderString, itemHeaderString, eventString] = request.body.split('\n');

Expand All @@ -26,19 +28,9 @@ function parseEnvelopeRequest(request: SentryRequest): any {
}

describe('eventToSentryRequest', () => {
let api: API;
let event: Event;

beforeEach(() => {
api = new API(ingestDsn, {
sdk: {
integrations: ['AWSLambda'],
name: 'sentry.javascript.browser',
version: `12.31.12`,
packages: [{ name: 'npm:@sentry/browser', version: `12.31.12` }],
},
});

event = {
contexts: { trace: { trace_id: '1231201211212012', span_id: '12261980', op: 'pageload' } },
environment: 'dogpark',
Expand Down Expand Up @@ -140,17 +132,15 @@ describe('eventToSentryRequest', () => {
});

it('uses tunnel as the url if it is configured', () => {
api = new API(ingestDsn, {}, tunnel);
const tunnelRequest = eventToSentryRequest(event, new API(ingestDsn, {}, tunnel));
expect(tunnelRequest.url).toEqual(tunnel);

const result = eventToSentryRequest(event, api);

expect(result.url).toEqual(tunnel);
const defaultRequest = eventToSentryRequest(event, new API(ingestDsn, {}));
expect(defaultRequest.url).toEqual(ingestUrl);
});

it('adds dsn to envelope header if tunnel is configured', () => {
api = new API(ingestDsn, {}, tunnel);

const result = eventToSentryRequest(event, api);
const result = eventToSentryRequest(event, new API(ingestDsn, {}, tunnel));
const envelope = parseEnvelopeRequest(result);

expect(envelope.envelopeHeader).toEqual(
Expand All @@ -161,10 +151,9 @@ describe('eventToSentryRequest', () => {
});

it('adds default "event" item type to item header if tunnel is configured', () => {
api = new API(ingestDsn, {}, tunnel);
delete event.type;

const result = eventToSentryRequest(event, api);
const result = eventToSentryRequest(event, new API(ingestDsn, {}, tunnel));
const envelope = parseEnvelopeRequest(result);

expect(envelope.itemHeader).toEqual(
Expand Down Expand Up @@ -202,4 +191,23 @@ describe('sessionToSentryRequest', () => {
}),
);
});

it('uses tunnel as the url if it is configured', () => {
const tunnelRequest = sessionToSentryRequest({ aggregates: [] }, new API(ingestDsn, {}, tunnel));
expect(tunnelRequest.url).toEqual(tunnel);

const defaultRequest = sessionToSentryRequest({ aggregates: [] }, new API(ingestDsn, {}));
expect(defaultRequest.url).toEqual(ingestUrl);
});

it('adds dsn to envelope header if tunnel is configured', () => {
const result = sessionToSentryRequest({ aggregates: [] }, new API(ingestDsn, {}, tunnel));
const envelope = parseEnvelopeRequest(result);

expect(envelope.envelopeHeader).toEqual(
expect.objectContaining({
dsn: ingestDsn,
}),
);
});
});

0 comments on commit ab9efb2

Please sign in to comment.