Skip to content

Commit c097348

Browse files
committed
add tests for creating and retrieving tracestate header
1 parent 5aef526 commit c097348

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

packages/tracing/test/hub.test.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { BrowserClient } from '@sentry/browser';
33
import { getMainCarrier, Hub } from '@sentry/hub';
44
import * as hubModule from '@sentry/hub';
55
import * as utilsModule from '@sentry/utils'; // for mocking
6-
import { getGlobalObject, isNodeEnv, logger } from '@sentry/utils';
6+
import { base64ToUnicode, getGlobalObject, isNodeEnv, logger } from '@sentry/utils';
77
import * as nodeHttpModule from 'http';
88

9+
import { Transaction } from '../src';
910
import { BrowserTracing } from '../src/browser/browsertracing';
1011
import { addExtensionMethods } from '../src/hubextensions';
1112
import { extractTraceparentData, TRACEPARENT_REGEXP } from '../src/utils';
@@ -31,6 +32,43 @@ describe('Hub', () => {
3132
jest.useRealTimers();
3233
});
3334

35+
describe('transaction creation', () => {
36+
it('uses inherited values when given in transaction context', () => {
37+
const transactionContext = {
38+
name: 'dogpark',
39+
traceId: '12312012123120121231201212312012',
40+
parentSpanId: '1121201211212012',
41+
tracestate: 'doGsaREgReaT==',
42+
};
43+
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));
44+
const transaction = hub.startTransaction(transactionContext);
45+
46+
expect(transaction).toEqual(expect.objectContaining(transactionContext));
47+
});
48+
49+
it('creates a new tracestate value (with the right data) if not given one in transaction context', () => {
50+
const hub = new Hub(
51+
new BrowserClient({
52+
dsn: 'https://[email protected]/12312012',
53+
release: 'off.leash.park',
54+
environment: 'dogpark',
55+
}),
56+
);
57+
const transaction = hub.startTransaction({ name: 'FETCH /ball' });
58+
59+
const b64Value =
60+
'eyJwdWJsaWNfa2V5IjoiZG9nc2FyZWJhZGF0a2VlcGluZ3NlY3JldHMiLCJlbnZpcm9ubWVudCI6ImRvZ3BhcmsiLCJyZWxlYXNlI' +
61+
'joib2ZmLmxlYXNoLnBhcmsifQ.';
62+
63+
expect(transaction.tracestate).toEqual(b64Value);
64+
expect(JSON.parse(base64ToUnicode(b64Value.replace('.', '=')))).toEqual({
65+
environment: 'dogpark',
66+
public_key: 'dogsarebadatkeepingsecrets',
67+
release: 'off.leash.park',
68+
});
69+
});
70+
});
71+
3472
describe('getTransaction()', () => {
3573
it('should find a transaction which has been set on the scope if sampled = true', () => {
3674
const hub = new Hub(new BrowserClient({ tracesSampleRate: 1 }));

packages/tracing/test/span.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BrowserClient } from '@sentry/browser';
22
import { Hub, Scope } from '@sentry/hub';
3+
import { TraceHeadersArr, TraceHeadersObj } from '@sentry/types';
34

45
import { Span, SpanStatus, Transaction } from '../src';
56
import { TRACEPARENT_REGEXP } from '../src/utils';
@@ -101,6 +102,47 @@ describe('Span', () => {
101102
});
102103
});
103104

105+
describe('getTraceHeaders', () => {
106+
it('returns correct headers', () => {
107+
const hub = new Hub(
108+
new BrowserClient({
109+
dsn: 'https://[email protected]/12312012',
110+
tracesSampleRate: 1,
111+
release: 'off.leash.park',
112+
environment: 'dogpark',
113+
}),
114+
);
115+
const transaction = hub.startTransaction({ name: 'FETCH /ball' });
116+
const span = transaction.startChild({ op: 'dig.hole' });
117+
118+
const headers = span.getTraceHeaders('object') as TraceHeadersObj;
119+
120+
expect(headers['sentry-trace']).toEqual(span.toTraceparent());
121+
expect(headers.tracestate).toEqual(transaction.tracestate);
122+
});
123+
124+
it('returns the same data either as an object or an array', () => {
125+
const hub = new Hub(
126+
new BrowserClient({
127+
dsn: 'https://[email protected]/12312012',
128+
tracesSampleRate: 1,
129+
release: 'off.leash.park',
130+
environment: 'dogpark',
131+
}),
132+
);
133+
const transaction = hub.startTransaction({ name: 'FETCH /ball' });
134+
const span = transaction.startChild({ op: 'dig.hole' });
135+
136+
const headersObj = span.getTraceHeaders('object') as TraceHeadersObj;
137+
const headersArr = span.getTraceHeaders('array') as TraceHeadersArr;
138+
139+
headersArr.forEach(headerTuple => {
140+
const [headerName, headerValue] = headerTuple;
141+
expect((headersObj as any)[headerName]).toEqual(headerValue);
142+
});
143+
});
144+
});
145+
104146
describe('toJSON', () => {
105147
test('simple', () => {
106148
const span = JSON.parse(

0 commit comments

Comments
 (0)