Skip to content

Commit 64ca793

Browse files
committed
feat(otel): Add inject functionality to SentryPropagator
1 parent f36c268 commit 64ca793

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

packages/opentelemetry-node/test/propagator.test.ts

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,185 @@ describe('SentryPropagator', () => {
204204
});
205205
});
206206
});
207+
208+
describe('inject', () => {
209+
describe('sentry-trace', () => {
210+
it.each([
211+
[
212+
'should set sentry-trace header when sampled',
213+
{
214+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
215+
spanId: '6e0c63257de34c92',
216+
traceFlags: TraceFlags.SAMPLED,
217+
},
218+
'd4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-1',
219+
],
220+
[
221+
'should set sentry-trace header when not sampled',
222+
{
223+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
224+
spanId: '6e0c63257de34c92',
225+
traceFlags: TraceFlags.NONE,
226+
},
227+
'd4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-0',
228+
],
229+
[
230+
'should NOT set sentry-trace header when traceId is empty',
231+
{
232+
traceId: '',
233+
spanId: '6e0c63257de34c92',
234+
traceFlags: TraceFlags.SAMPLED,
235+
},
236+
undefined,
237+
],
238+
[
239+
'should NOT set sentry-trace header when spanId is empty',
240+
{
241+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
242+
spanId: '',
243+
traceFlags: TraceFlags.NONE,
244+
},
245+
undefined,
246+
],
247+
])('%s', (_name, spanContext, expected) => {
248+
const context = trace.setSpanContext(ROOT_CONTEXT, spanContext);
249+
propagator.inject(context, carrier, defaultTextMapSetter);
250+
expect(carrier[SENTRY_TRACE_HEADER]).toBe(expected);
251+
});
252+
253+
it('should NOT set sentry-trace header if instrumentation is supressed', () => {
254+
const spanContext = {
255+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
256+
spanId: '6e0c63257de34c92',
257+
traceFlags: TraceFlags.SAMPLED,
258+
};
259+
const context = suppressTracing(trace.setSpanContext(ROOT_CONTEXT, spanContext));
260+
propagator.inject(context, carrier, defaultTextMapSetter);
261+
expect(carrier[SENTRY_TRACE_HEADER]).toBe(undefined);
262+
});
263+
});
264+
265+
describe('baggage', () => {
266+
const client = {
267+
getOptions: () => ({
268+
environment: 'production',
269+
release: '1.0.0',
270+
}),
271+
getDsn: () => ({
272+
publicKey: 'abc',
273+
}),
274+
};
275+
// @ts-ignore Use mock client for unit tests
276+
const hub: Hub = new Hub(client);
277+
makeMain(hub);
278+
279+
afterEach(() => {
280+
SENTRY_SPAN_PROCESSOR_MAP.clear();
281+
});
282+
283+
enum PerfType {
284+
Transaction = 'transaction',
285+
Span = 'span',
286+
}
287+
288+
function createTransactionAndMaybeSpan(type: PerfType, transactionContext: TransactionContext) {
289+
const transaction = new Transaction(transactionContext, hub);
290+
SENTRY_SPAN_PROCESSOR_MAP.set(transaction.spanId, transaction);
291+
if (type === PerfType.Span) {
292+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
293+
const { spanId, ...ctx } = transactionContext;
294+
const span = transaction.startChild({ ...ctx, description: transaction.name });
295+
SENTRY_SPAN_PROCESSOR_MAP.set(span.spanId, span);
296+
}
297+
}
298+
299+
describe.each([PerfType.Transaction, PerfType.Span])('with active %s', type => {
300+
it.each([
301+
[
302+
'should set baggage header when sampled',
303+
{
304+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
305+
spanId: '6e0c63257de34c92',
306+
traceFlags: TraceFlags.SAMPLED,
307+
},
308+
{
309+
name: 'sampled-transaction',
310+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
311+
spanId: '6e0c63257de34c92',
312+
sampled: true,
313+
},
314+
'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=sampled-transaction,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b',
315+
],
316+
[
317+
'should NOT set baggage header when not sampled',
318+
{
319+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
320+
spanId: '6e0c63257de34c92',
321+
traceFlags: TraceFlags.NONE,
322+
},
323+
{
324+
name: 'not-sampled-transaction',
325+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
326+
spanId: '6e0c63257de34c92',
327+
sampled: false,
328+
},
329+
'sentry-environment=production,sentry-release=1.0.0,sentry-transaction=not-sampled-transaction,sentry-public_key=abc,sentry-trace_id=d4cda95b652f4a1592b449d5929fda1b',
330+
],
331+
[
332+
'should NOT set baggage header when traceId is empty',
333+
{
334+
traceId: '',
335+
spanId: '6e0c63257de34c92',
336+
traceFlags: TraceFlags.SAMPLED,
337+
},
338+
{
339+
name: 'empty-traceId-transaction',
340+
traceId: '',
341+
spanId: '6e0c63257de34c92',
342+
sampled: true,
343+
},
344+
undefined,
345+
],
346+
[
347+
'should NOT set baggage header when spanId is empty',
348+
{
349+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
350+
spanId: '',
351+
traceFlags: TraceFlags.SAMPLED,
352+
},
353+
{
354+
name: 'empty-spanId-transaction',
355+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
356+
spanId: '',
357+
sampled: true,
358+
},
359+
undefined,
360+
],
361+
])('%s', (_name, spanContext, transactionContext, expected) => {
362+
createTransactionAndMaybeSpan(type, transactionContext);
363+
const context = trace.setSpanContext(ROOT_CONTEXT, spanContext);
364+
propagator.inject(context, carrier, defaultTextMapSetter);
365+
expect(carrier[SENTRY_BAGGAGE_HEADER]).toBe(expected);
366+
});
367+
368+
it('should NOT set sentry-trace header if instrumentation is supressed', () => {
369+
const spanContext = {
370+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
371+
spanId: '6e0c63257de34c92',
372+
traceFlags: TraceFlags.SAMPLED,
373+
};
374+
const transactionContext = {
375+
name: 'sampled-transaction',
376+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
377+
spanId: '6e0c63257de34c92',
378+
sampled: true,
379+
};
380+
createTransactionAndMaybeSpan(type, transactionContext);
381+
const context = suppressTracing(trace.setSpanContext(ROOT_CONTEXT, spanContext));
382+
propagator.inject(context, carrier, defaultTextMapSetter);
383+
expect(carrier[SENTRY_TRACE_HEADER]).toBe(undefined);
384+
});
385+
});
386+
});
387+
});
207388
});

0 commit comments

Comments
 (0)