/
hub.test.ts
107 lines (97 loc) · 3.87 KB
/
hub.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { BrowserClient } from '@sentry/browser';
import { Hub, Scope } from '@sentry/hub';
import { addExtensionMethods } from '../src/hubextensions';
addExtensionMethods();
describe('Hub', () => {
afterEach(() => {
jest.resetAllMocks();
jest.useRealTimers();
});
describe('getTransaction', () => {
test('simple invoke', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
const transaction = hub.startTransaction({ name: 'foo' });
hub.configureScope(scope => {
scope.setSpan(transaction);
});
hub.configureScope(s => {
expect(s.getTransaction()).toBe(transaction);
});
});
test('not invoke', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
const transaction = hub.startTransaction({ name: 'foo' });
hub.configureScope(s => {
expect(s.getTransaction()).toBeUndefined();
});
transaction.finish();
});
});
describe('spans', () => {
describe('sampling', () => {
test('set tracesSampleRate 0 on span', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0 }));
const span = hub.startSpan({}) as any;
expect(span.sampled).toBeUndefined();
});
test('set tracesSampleRate 0 on transaction', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0 }));
const transaction = hub.startTransaction({ name: 'foo' });
expect(transaction.sampled).toBe(false);
});
test('set tracesSampleRate 1 on transaction', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
const transaction = hub.startTransaction({ name: 'foo' });
expect(transaction.sampled).toBeTruthy();
});
test('set tracesSampleRate should be propergated to children', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 0 }));
const transaction = hub.startTransaction({ name: 'foo' });
const child = transaction.startChild({ op: 'test' });
expect(child.sampled).toBeFalsy();
});
});
describe('startSpan', () => {
test('simple standalone Span', () => {
const hub = new Hub(new BrowserClient());
const span = hub.startSpan({}) as any;
expect(span.spanId).toBeTruthy();
});
test('simple standalone Transaction', () => {
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
const transaction = hub.startTransaction({ name: 'transaction' });
expect(transaction.spanId).toBeTruthy();
// tslint:disable-next-line: no-unbound-method
expect(transaction.setName).toBeTruthy();
});
test('Transaction inherits trace_id from span on scope', () => {
const myScope = new Scope();
const hub = new Hub(new BrowserClient(), myScope);
const parentSpan = hub.startSpan({}) as any;
hub.configureScope(scope => {
scope.setSpan(parentSpan);
});
// @ts-ignore
const span = hub.startSpan({ name: 'test' }) as any;
expect(span.trace_id).toEqual(parentSpan.trace_id);
});
test('create a child if there is a Span already on the scope', () => {
const myScope = new Scope();
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }), myScope);
const transaction = hub.startTransaction({ name: 'transaction' });
hub.configureScope(scope => {
scope.setSpan(transaction);
});
const span = hub.startSpan({});
expect(span.traceId).toEqual(transaction.traceId);
expect(span.parentSpanId).toEqual(transaction.spanId);
hub.configureScope(scope => {
scope.setSpan(span);
});
const span2 = hub.startSpan({});
expect(span2.traceId).toEqual(span.traceId);
expect(span2.parentSpanId).toEqual(span.spanId);
});
});
});
});