Skip to content

Commit ddb7375

Browse files
committed
add tests
1 parent 83926ad commit ddb7375

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
const event: Event = {
9+
contexts: { trace: { trace_id: '1231201211212012', span_id: '12261980', op: 'pageload' } },
10+
environment: 'dogpark',
11+
event_id: '0908201304152013',
12+
release: 'off.leash.park',
13+
spans: [],
14+
transaction: '/dogs/are/great/',
15+
type: 'transaction',
16+
user: { id: '1121', username: 'CharlieDog', ip_address: '11.21.20.12' },
17+
};
18+
19+
it('adds sampling information to transaction item header', () => {
20+
event.tags = { samplingMethod: 'client_rate', sampleRate: '0.1121', dog: 'Charlie' };
21+
22+
const result = eventToSentryRequest(event as Event, api);
23+
24+
const [envelopeHeaderString, itemHeaderString, eventString] = result.body.split('\n');
25+
26+
const envelope = {
27+
envelopeHeader: JSON.parse(envelopeHeaderString),
28+
itemHeader: JSON.parse(itemHeaderString),
29+
event: JSON.parse(eventString),
30+
};
31+
32+
// the right stuff is added to the item header
33+
expect(envelope.itemHeader).toEqual({ type: 'transaction', sample_rates: [{ id: 'client_rate', rate: '0.1121' }] });
34+
35+
// show that it pops the right tags and leaves the rest alone
36+
expect('samplingMethod' in envelope.event.tags).toBe(false);
37+
expect('sampleRate' in envelope.event.tags).toBe(false);
38+
expect('dog' in envelope.event.tags).toBe(true);
39+
});
40+
});

packages/tracing/test/hub.test.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ describe('Hub', () => {
171171
});
172172

173173
describe('sample()', () => {
174-
it('should not sample transactions when tracing is disabled', () => {
174+
it('should set sampled = false when tracing is disabled', () => {
175175
// neither tracesSampleRate nor tracesSampler is defined -> tracing disabled
176176
const hub = new Hub(new BrowserClient({}));
177177
const transaction = hub.startTransaction({ name: 'dogpark' });
@@ -219,7 +219,7 @@ describe('Hub', () => {
219219
expect(transaction.sampled).toBe(true);
220220
});
221221

222-
it('should not try to override positive sampling decision provided in transaction context', () => {
222+
it('should not try to override explicitly set positive sampling decision', () => {
223223
// so that the decision otherwise would be false
224224
const tracesSampler = jest.fn().mockReturnValue(0);
225225
const hub = new Hub(new BrowserClient({ tracesSampler }));
@@ -228,7 +228,7 @@ describe('Hub', () => {
228228
expect(transaction.sampled).toBe(true);
229229
});
230230

231-
it('should not try to override negative sampling decision provided in transaction context', () => {
231+
it('should not try to override explicitly set negative sampling decision', () => {
232232
// so that the decision otherwise would be true
233233
const tracesSampler = jest.fn().mockReturnValue(1);
234234
const hub = new Hub(new BrowserClient({ tracesSampler }));
@@ -255,6 +255,40 @@ describe('Hub', () => {
255255
expect(tracesSampler).toHaveBeenCalled();
256256
expect(transaction.sampled).toBe(true);
257257
});
258+
259+
it('should record sampling method when sampling decision is explicitly set', () => {
260+
const tracesSampler = jest.fn().mockReturnValue(0.1121);
261+
const hub = new Hub(new BrowserClient({ tracesSampler }));
262+
const transaction = hub.startTransaction({ name: 'dogpark', sampled: true });
263+
264+
expect(transaction.tags).toEqual(expect.objectContaining({ samplingMethod: 'explicitly_set' }));
265+
});
266+
267+
it('should record sampling method and rate when sampling decision comes from tracesSampler', () => {
268+
const tracesSampler = jest.fn().mockReturnValue(0.1121);
269+
const hub = new Hub(new BrowserClient({ tracesSampler }));
270+
const transaction = hub.startTransaction({ name: 'dogpark' });
271+
272+
expect(transaction.tags).toEqual(
273+
expect.objectContaining({ samplingMethod: 'client_sampler', sampleRate: '0.1121' }),
274+
);
275+
});
276+
277+
it('should record sampling method when sampling decision is inherited', () => {
278+
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0.1121 }));
279+
const transaction = hub.startTransaction({ name: 'dogpark', parentSampled: true });
280+
281+
expect(transaction.tags).toEqual(expect.objectContaining({ samplingMethod: 'inheritance' }));
282+
});
283+
284+
it('should record sampling method and rate when sampling decision comes from traceSampleRate', () => {
285+
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0.1121 }));
286+
const transaction = hub.startTransaction({ name: 'dogpark' });
287+
288+
expect(transaction.tags).toEqual(
289+
expect.objectContaining({ samplingMethod: 'client_rate', sampleRate: '0.1121' }),
290+
);
291+
});
258292
});
259293

260294
describe('isValidSampleRate()', () => {

0 commit comments

Comments
 (0)