|
| 1 | +import { trace } from '@opentelemetry/api'; |
| 2 | +import type { TransactionEvent } from '@sentry/core'; |
| 3 | +import { startSpan } from '@sentry/core'; |
| 4 | +import { beforeEach, describe, expect, test } from 'vitest'; |
| 5 | +import { init } from '../src/sdk'; |
| 6 | +import { resetSdk } from './testUtils'; |
| 7 | + |
| 8 | +describe('opentelemetry compatibility', () => { |
| 9 | + beforeEach(() => { |
| 10 | + resetSdk(); |
| 11 | + }); |
| 12 | + |
| 13 | + test('should not capture spans emitted via @opentelemetry/api when skipOpenTelemetrySetup is true', async () => { |
| 14 | + const transactionEvents: TransactionEvent[] = []; |
| 15 | + |
| 16 | + const client = init({ |
| 17 | + dsn: 'https://username@domain/123', |
| 18 | + tracesSampleRate: 1, |
| 19 | + skipOpenTelemetrySetup: true, |
| 20 | + beforeSendTransaction: event => { |
| 21 | + transactionEvents.push(event); |
| 22 | + return null; |
| 23 | + }, |
| 24 | + }); |
| 25 | + |
| 26 | + const tracer = trace.getTracer('test'); |
| 27 | + const span = tracer.startSpan('test'); |
| 28 | + span.end(); |
| 29 | + |
| 30 | + await client!.flush(); |
| 31 | + |
| 32 | + tracer.startActiveSpan('test 2', { attributes: { 'test.attribute': 'test' } }, span2 => { |
| 33 | + const span = tracer.startSpan('test 3', { attributes: { 'test.attribute': 'test2' } }); |
| 34 | + span.end(); |
| 35 | + span2.end(); |
| 36 | + }); |
| 37 | + |
| 38 | + await client!.flush(); |
| 39 | + |
| 40 | + expect(transactionEvents).toHaveLength(0); |
| 41 | + }); |
| 42 | + |
| 43 | + test('should capture spans emitted via @opentelemetry/api', async () => { |
| 44 | + const transactionEvents: TransactionEvent[] = []; |
| 45 | + |
| 46 | + const client = init({ |
| 47 | + dsn: 'https://username@domain/123', |
| 48 | + tracesSampleRate: 1, |
| 49 | + beforeSendTransaction: event => { |
| 50 | + transactionEvents.push(event); |
| 51 | + return null; |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + const tracer = trace.getTracer('test'); |
| 56 | + const span = tracer.startSpan('test'); |
| 57 | + span.end(); |
| 58 | + |
| 59 | + await client!.flush(); |
| 60 | + |
| 61 | + tracer.startActiveSpan('test 2', { attributes: { 'test.attribute': 'test' } }, span2 => { |
| 62 | + const span = tracer.startSpan('test 3', { attributes: { 'test.attribute': 'test2' } }); |
| 63 | + span.end(); |
| 64 | + span2.end(); |
| 65 | + }); |
| 66 | + |
| 67 | + await client!.flush(); |
| 68 | + |
| 69 | + expect(transactionEvents).toHaveLength(2); |
| 70 | + const [transactionEvent, transactionEvent2] = transactionEvents; |
| 71 | + |
| 72 | + expect(transactionEvent?.spans?.length).toBe(0); |
| 73 | + expect(transactionEvent?.transaction).toBe('test'); |
| 74 | + expect(transactionEvent?.contexts?.trace?.data).toEqual({ |
| 75 | + 'sentry.cloudflare_tracer': true, |
| 76 | + 'sentry.origin': 'manual', |
| 77 | + 'sentry.sample_rate': 1, |
| 78 | + 'sentry.source': 'custom', |
| 79 | + }); |
| 80 | + |
| 81 | + expect(transactionEvent2?.spans?.length).toBe(1); |
| 82 | + expect(transactionEvent2?.transaction).toBe('test 2'); |
| 83 | + expect(transactionEvent2?.contexts?.trace?.data).toEqual({ |
| 84 | + 'sentry.cloudflare_tracer': true, |
| 85 | + 'sentry.origin': 'manual', |
| 86 | + 'sentry.sample_rate': 1, |
| 87 | + 'sentry.source': 'custom', |
| 88 | + 'test.attribute': 'test', |
| 89 | + }); |
| 90 | + |
| 91 | + expect(transactionEvent2?.spans).toEqual([ |
| 92 | + expect.objectContaining({ |
| 93 | + description: 'test 3', |
| 94 | + data: { |
| 95 | + 'sentry.cloudflare_tracer': true, |
| 96 | + 'sentry.origin': 'manual', |
| 97 | + 'test.attribute': 'test2', |
| 98 | + }, |
| 99 | + }), |
| 100 | + ]); |
| 101 | + }); |
| 102 | + |
| 103 | + test('opentelemetry spans should interop with Sentry spans', async () => { |
| 104 | + const transactionEvents: TransactionEvent[] = []; |
| 105 | + |
| 106 | + const client = init({ |
| 107 | + dsn: 'https://username@domain/123', |
| 108 | + tracesSampleRate: 1, |
| 109 | + beforeSendTransaction: event => { |
| 110 | + transactionEvents.push(event); |
| 111 | + return null; |
| 112 | + }, |
| 113 | + }); |
| 114 | + |
| 115 | + const tracer = trace.getTracer('test'); |
| 116 | + |
| 117 | + startSpan({ name: 'sentry span' }, () => { |
| 118 | + const span = tracer.startSpan('otel span'); |
| 119 | + span.end(); |
| 120 | + }); |
| 121 | + |
| 122 | + await client!.flush(); |
| 123 | + |
| 124 | + expect(transactionEvents).toHaveLength(1); |
| 125 | + const [transactionEvent] = transactionEvents; |
| 126 | + |
| 127 | + expect(transactionEvent?.spans?.length).toBe(1); |
| 128 | + expect(transactionEvent?.transaction).toBe('sentry span'); |
| 129 | + expect(transactionEvent?.contexts?.trace?.data).toEqual({ |
| 130 | + 'sentry.origin': 'manual', |
| 131 | + 'sentry.sample_rate': 1, |
| 132 | + 'sentry.source': 'custom', |
| 133 | + }); |
| 134 | + |
| 135 | + expect(transactionEvent?.spans).toEqual([ |
| 136 | + expect.objectContaining({ |
| 137 | + description: 'otel span', |
| 138 | + data: { |
| 139 | + 'sentry.cloudflare_tracer': true, |
| 140 | + 'sentry.origin': 'manual', |
| 141 | + }, |
| 142 | + }), |
| 143 | + ]); |
| 144 | + }); |
| 145 | +}); |
0 commit comments