|
| 1 | +import * as OpenTelemetry from '@opentelemetry/api'; |
| 2 | +import { Span as OtelSpan } from '@opentelemetry/sdk-trace-base'; |
| 3 | +import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; |
| 4 | +import { Hub, makeMain } from '@sentry/core'; |
| 5 | +import { addExtensionMethods, Span as SentrySpan, Transaction } from '@sentry/tracing'; |
| 6 | + |
| 7 | +import { SentrySpanProcessor } from '../src/spanprocessor'; |
| 8 | + |
| 9 | +// Integration Test of SentrySpanProcessor |
| 10 | + |
| 11 | +beforeAll(() => { |
| 12 | + addExtensionMethods(); |
| 13 | +}); |
| 14 | + |
| 15 | +describe('SentrySpanProcessor', () => { |
| 16 | + let hub: Hub; |
| 17 | + let provider: NodeTracerProvider; |
| 18 | + let spanProcessor: SentrySpanProcessor; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + hub = new Hub(); |
| 22 | + makeMain(hub); |
| 23 | + |
| 24 | + spanProcessor = new SentrySpanProcessor(); |
| 25 | + provider = new NodeTracerProvider(); |
| 26 | + provider.addSpanProcessor(spanProcessor); |
| 27 | + provider.register(); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(async () => { |
| 31 | + await provider.forceFlush(); |
| 32 | + await provider.shutdown(); |
| 33 | + }); |
| 34 | + |
| 35 | + function getSpanForOtelSpan(otelSpan: OtelSpan | OpenTelemetry.Span) { |
| 36 | + return spanProcessor._map.get(otelSpan.spanContext().spanId); |
| 37 | + } |
| 38 | + |
| 39 | + it('creates a transaction', async () => { |
| 40 | + const startTime = otelNumberToHrtime(new Date().valueOf()); |
| 41 | + |
| 42 | + const otelSpan = provider.getTracer('default').startSpan('GET /users', { startTime }) as OtelSpan; |
| 43 | + |
| 44 | + const sentrySpanTransaction = getSpanForOtelSpan(otelSpan) as Transaction | undefined; |
| 45 | + expect(sentrySpanTransaction).toBeInstanceOf(Transaction); |
| 46 | + |
| 47 | + expect(sentrySpanTransaction?.name).toBe('GET /users'); |
| 48 | + expect(sentrySpanTransaction?.startTimestamp).toEqual(otelSpan.startTime[0]); |
| 49 | + expect(sentrySpanTransaction?.startTimestamp).toEqual(startTime[0]); |
| 50 | + expect(sentrySpanTransaction?.traceId).toEqual(otelSpan.spanContext().traceId); |
| 51 | + expect(sentrySpanTransaction?.parentSpanId).toEqual(otelSpan.parentSpanId); |
| 52 | + expect(sentrySpanTransaction?.spanId).toEqual(otelSpan.spanContext().spanId); |
| 53 | + |
| 54 | + expect(hub.getScope()?.getSpan()).toBeUndefined(); |
| 55 | + |
| 56 | + const endTime = otelNumberToHrtime(new Date().valueOf()); |
| 57 | + otelSpan.end(endTime); |
| 58 | + |
| 59 | + expect(sentrySpanTransaction?.endTimestamp).toBe(endTime[0]); |
| 60 | + expect(sentrySpanTransaction?.endTimestamp).toBe(otelSpan.endTime[0]); |
| 61 | + |
| 62 | + expect(hub.getScope()?.getSpan()).toBeUndefined(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('creates a child span if there is a running transaction', () => { |
| 66 | + const tracer = provider.getTracer('default'); |
| 67 | + |
| 68 | + tracer.startActiveSpan('GET /users', parentOtelSpan => { |
| 69 | + tracer.startActiveSpan('SELECT * FROM users;', child => { |
| 70 | + const childOtelSpan = child as OtelSpan; |
| 71 | + |
| 72 | + const sentrySpanTransaction = getSpanForOtelSpan(parentOtelSpan) as Transaction | undefined; |
| 73 | + expect(sentrySpanTransaction).toBeInstanceOf(Transaction); |
| 74 | + |
| 75 | + const sentrySpan = getSpanForOtelSpan(childOtelSpan); |
| 76 | + expect(sentrySpan).toBeInstanceOf(SentrySpan); |
| 77 | + expect(sentrySpan?.description).toBe('SELECT * FROM users;'); |
| 78 | + expect(sentrySpan?.startTimestamp).toEqual(childOtelSpan.startTime[0]); |
| 79 | + expect(sentrySpan?.spanId).toEqual(childOtelSpan.spanContext().spanId); |
| 80 | + expect(sentrySpan?.parentSpanId).toEqual(sentrySpanTransaction?.spanId); |
| 81 | + |
| 82 | + expect(hub.getScope()?.getSpan()).toBeUndefined(); |
| 83 | + |
| 84 | + const endTime = otelNumberToHrtime(new Date().valueOf()); |
| 85 | + child.end(endTime); |
| 86 | + |
| 87 | + expect(sentrySpan?.endTimestamp).toEqual(childOtelSpan.endTime[0]); |
| 88 | + expect(sentrySpan?.endTimestamp).toEqual(endTime[0]); |
| 89 | + }); |
| 90 | + |
| 91 | + parentOtelSpan.end(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('allows to create multiple child spans on same level', () => { |
| 96 | + const tracer = provider.getTracer('default'); |
| 97 | + |
| 98 | + tracer.startActiveSpan('GET /users', parentOtelSpan => { |
| 99 | + const sentrySpanTransaction = getSpanForOtelSpan(parentOtelSpan) as Transaction | undefined; |
| 100 | + |
| 101 | + expect(sentrySpanTransaction).toBeInstanceOf(SentrySpan); |
| 102 | + expect(sentrySpanTransaction?.name).toBe('GET /users'); |
| 103 | + |
| 104 | + // Create some parallel, independent spans |
| 105 | + const span1 = tracer.startSpan('SELECT * FROM users;') as OtelSpan; |
| 106 | + const span2 = tracer.startSpan('SELECT * FROM companies;') as OtelSpan; |
| 107 | + const span3 = tracer.startSpan('SELECT * FROM locations;') as OtelSpan; |
| 108 | + |
| 109 | + const sentrySpan1 = getSpanForOtelSpan(span1); |
| 110 | + const sentrySpan2 = getSpanForOtelSpan(span2); |
| 111 | + const sentrySpan3 = getSpanForOtelSpan(span3); |
| 112 | + |
| 113 | + expect(sentrySpan1?.parentSpanId).toEqual(sentrySpanTransaction?.spanId); |
| 114 | + expect(sentrySpan2?.parentSpanId).toEqual(sentrySpanTransaction?.spanId); |
| 115 | + expect(sentrySpan3?.parentSpanId).toEqual(sentrySpanTransaction?.spanId); |
| 116 | + |
| 117 | + expect(sentrySpan1?.description).toEqual('SELECT * FROM users;'); |
| 118 | + expect(sentrySpan2?.description).toEqual('SELECT * FROM companies;'); |
| 119 | + expect(sentrySpan3?.description).toEqual('SELECT * FROM locations;'); |
| 120 | + |
| 121 | + span1.end(); |
| 122 | + span2.end(); |
| 123 | + span3.end(); |
| 124 | + |
| 125 | + parentOtelSpan.end(); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
| 129 | + |
| 130 | +// OTEL expects a custom date format |
| 131 | +const NANOSECOND_DIGITS = 9; |
| 132 | +const SECOND_TO_NANOSECONDS = Math.pow(10, NANOSECOND_DIGITS); |
| 133 | + |
| 134 | +function otelNumberToHrtime(epochMillis: number): OpenTelemetry.HrTime { |
| 135 | + const epochSeconds = epochMillis / 1000; |
| 136 | + // Decimals only. |
| 137 | + const seconds = Math.trunc(epochSeconds); |
| 138 | + // Round sub-nanosecond accuracy to nanosecond. |
| 139 | + const nanos = Number((epochSeconds - seconds).toFixed(NANOSECOND_DIGITS)) * SECOND_TO_NANOSECONDS; |
| 140 | + return [seconds, nanos]; |
| 141 | +} |
0 commit comments