Skip to content

Commit dd215ec

Browse files
committed
fix rebase artifacts
1 parent 3111ed0 commit dd215ec

File tree

1 file changed

+73
-64
lines changed

1 file changed

+73
-64
lines changed

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

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,53 @@ import { API } from '../../src/api';
55
import { eventToSentryRequest } from '../../src/request';
66

77
describe('eventToSentryRequest', () => {
8-
const api = new API('https://[email protected]/12312012');
9-
const event: Event = {
10-
contexts: { trace: { trace_id: '1231201211212012', span_id: '12261980', op: 'pageload' } },
11-
environment: 'dogpark',
12-
event_id: '0908201304152013',
13-
release: 'off.leash.park',
8+
const public_key = 'dogsarebadatkeepingsecrets';
9+
const api = new API(`https://${public_key}@squirrelchasers.ingest.sentry.io/12312012`);
10+
11+
const event_id = '1231201211212012';
12+
const trace_id = '0908201304152013';
13+
const environment = 'dogpark';
14+
const release = 'off.leash.park';
15+
16+
const baseEvent: Event = {
17+
event_id,
18+
environment,
19+
release,
20+
contexts: { trace: { trace_id: trace_id, span_id: '12261980', op: 'ball.fetch' } },
21+
// TODO
22+
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
23+
};
24+
25+
const errorEvent: Event = {
26+
...baseEvent,
27+
exception: { values: [{ type: 'PuppyProblemsError', value: 'Charlie ate the flip-flops :-(' }] },
28+
};
29+
30+
const tracestateObject = {
31+
trace_id,
32+
environment,
33+
release,
34+
public_key,
35+
};
36+
37+
const transactionEvent: Event = {
38+
...baseEvent,
1439
spans: [],
40+
tags: {
41+
dog: 'Charlie',
42+
__sentry_samplingMethod: TransactionSamplingMethod.Rate,
43+
__sentry_sampleRate: '.1121',
44+
__sentry_tracestate: computeTracestate(tracestateObject),
45+
},
1546
transaction: '/dogs/are/great/',
1647
type: 'transaction',
17-
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
1848
};
1949

2050
it('injects correct data for error/message events', () => {
21-
const event = {
22-
event_id: '1231201211212012',
23-
exception: { values: [{ type: 'PuppyProblemsError', value: 'Charlie ate the flip-flops :-(' }] },
24-
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
25-
};
51+
const event = { ...errorEvent };
2652

2753
const result = eventToSentryRequest(event, api);
54+
2855
expect(result.type).toEqual('event');
2956
expect(result.url).toEqual(
3057
'https://squirrelchasers.ingest.sentry.io/api/12312012/store/?sentry_key=dogsarebadatkeepingsecrets&sentry_version=7',
@@ -33,29 +60,9 @@ describe('eventToSentryRequest', () => {
3360
});
3461

3562
it('injects correct data for transaction events', () => {
36-
const eventId = '1231201211212012';
37-
const traceId = '0908201304152013';
38-
const environment = 'dogpark';
39-
const release = 'off.leash.park';
40-
41-
const event = {
42-
contexts: { trace: { trace_id: traceId, span_id: '12261980', op: 'pageload' } },
43-
environment,
44-
event_id: eventId,
45-
release,
46-
spans: [],
47-
transaction: '/dogs/are/great/',
48-
tracestate: computeTracestate({
49-
trace_id: traceId,
50-
environment,
51-
public_key: 'dogsarebadatkeepingsecrets',
52-
release,
53-
}),
54-
type: 'transaction',
55-
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
56-
};
63+
const event = { ...transactionEvent };
5764

58-
const result = eventToSentryRequest(event as Event, api);
65+
const result = eventToSentryRequest(event, api);
5966

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

@@ -71,14 +78,9 @@ describe('eventToSentryRequest', () => {
7178
);
7279

7380
expect(envelope.envelopeHeader).toEqual({
74-
event_id: eventId,
81+
event_id,
7582
sent_at: expect.any(String),
76-
trace: {
77-
trace_id: traceId,
78-
environment,
79-
public_key: 'dogsarebadatkeepingsecrets',
80-
release,
81-
},
83+
trace: tracestateObject,
8284
});
8385
expect(envelope.itemHeader).toEqual({
8486
type: 'transaction',
@@ -88,40 +90,47 @@ describe('eventToSentryRequest', () => {
8890
});
8991

9092
[
91-
{ method: TransactionSamplingMethod.Rate, rate: '0.1121', dog: 'Charlie' },
92-
{ method: TransactionSamplingMethod.Sampler, rate: '0.1231', dog: 'Maisey' },
93-
{ method: TransactionSamplingMethod.Inheritance, dog: 'Cory' },
94-
{ method: TransactionSamplingMethod.Explicit, dog: 'Bodhi' },
93+
// TODO kmclb - once tag types are loosened, don't need to cast rate and undefined to strings here
94+
{ __sentry_samplingMethod: TransactionSamplingMethod.Rate, __sentry_sampleRate: '0.1121', dog: 'Charlie' },
95+
{ __sentry_samplingMethod: TransactionSamplingMethod.Sampler, __sentry_sampleRate: '0.1231', dog: 'Maisey' },
96+
{ __sentry_samplingMethod: TransactionSamplingMethod.Inheritance, __sentry_sampleRate: '', dog: 'Cory' },
97+
{ __sentry_samplingMethod: TransactionSamplingMethod.Explicit, __sentry_sampleRate: '', dog: 'Bodhi' },
9598

9699
// this shouldn't ever happen (tags should always include at least the sampling method), but good to know that
97100
// things won't blow up if it does happen
98-
{ dog: 'Lucy' },
99-
].forEach(({ method, rate, dog }) => {
100-
it(`adds transaction sampling information to item header - ${method}, ${rate}, ${dog}`, () => {
101-
// TODO kmclb - once tag types are loosened, don't need to cast to string here
102-
event.tags = { __sentry_samplingMethod: String(method), __sentry_sampleRate: String(rate), dog };
101+
{ __sentry_samplingMethod: '', __sentry_sampleRate: '', dog: 'Lucy' },
102+
].forEach(tags => {
103+
const { __sentry_samplingMethod: method, __sentry_sampleRate: rate, dog } = tags;
103104

104-
const result = eventToSentryRequest(event as Event, api);
105+
it(`adds transaction sampling information to item header - ${method}, ${rate}, ${dog}`, () => {
106+
const event = {
107+
...transactionEvent,
108+
tags,
109+
};
105110

106-
const [envelopeHeaderString, itemHeaderString, eventString] = result.body.split('\n');
111+
const result = eventToSentryRequest(event, api);
107112

108-
const envelope = {
109-
envelopeHeader: JSON.parse(envelopeHeaderString),
110-
itemHeader: JSON.parse(itemHeaderString),
111-
event: JSON.parse(eventString),
112-
};
113+
const itemHeaderJSON = result.body.split('\n')[1];
114+
const itemHeader = JSON.parse(itemHeaderJSON);
113115

114-
// the right stuff is added to the item header
115-
expect(envelope.itemHeader).toEqual({
116+
expect(itemHeader).toEqual({
116117
type: 'transaction',
117118
// TODO kmclb - once tag types are loosened, don't need to cast to string here
118119
sample_rates: [{ id: String(method), rate: String(rate) }],
119120
});
120-
121-
// show that it pops the right tags and leaves the rest alone
122-
expect('__sentry_samplingMethod' in envelope.event.tags).toBe(false);
123-
expect('__sentry_sampleRate' in envelope.event.tags).toBe(false);
124-
expect('dog' in envelope.event.tags).toBe(true);
125121
});
126122
});
123+
124+
it('filters out temporary tags before sending (but leaves other tags alone)', () => {
125+
const event = { ...transactionEvent };
126+
127+
const result = eventToSentryRequest(event, api);
128+
129+
const eventString = result.body.split('\n')[2];
130+
const envelopeEvent = JSON.parse(eventString);
131+
const internalTags = Object.keys(envelopeEvent.tags).filter(tagName => tagName.startsWith('__sentry'));
132+
133+
expect(internalTags).toEqual([]);
134+
expect('dog' in envelopeEvent.tags).toBe(true);
135+
});
127136
});

0 commit comments

Comments
 (0)