|
1 | 1 | import type { SpanContext, TimeInput } from '@opentelemetry/api';
|
2 |
| -import { SpanKind } from '@opentelemetry/api'; |
| 2 | +import { context, trace, SpanKind } from '@opentelemetry/api'; |
3 | 3 | import type { SpanProcessor } from '@opentelemetry/sdk-trace-node';
|
4 | 4 | import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
|
5 | 5 | import { uuid4 } from '@sentry/core';
|
@@ -31,36 +31,29 @@ export function createSpan(
|
31 | 31 | });
|
32 | 32 |
|
33 | 33 | const tracer = provider.getTracer('test-instrumentation-library');
|
| 34 | + const tId = traceId || uuid4(); |
34 | 35 |
|
35 |
| - // const tracer = { |
36 |
| - // resource: 'test-resource', |
37 |
| - // instrumentationLibrary: 'test-instrumentation-library', |
38 |
| - // getSpanLimits: () => ({}), |
39 |
| - // getActiveSpanProcessor: () => spanProcessor, |
40 |
| - // } as unknown as Tracer; |
| 36 | + const parentSpan = tracer.startSpan(name || 'test', { |
| 37 | + kind: SpanKind.INTERNAL, |
| 38 | + links: [], |
| 39 | + startTime, |
| 40 | + }); |
41 | 41 |
|
42 |
| - const tId = traceId || uuid4(); |
| 42 | + parentSpan.spanContext().spanId = parentSpanId || uuid4(); |
| 43 | + parentSpan.spanContext().traceId = tId; |
| 44 | + parentSpan.spanContext().traceFlags = 0; |
43 | 45 |
|
44 |
| - const parentSpanContext: SpanContext = { |
45 |
| - spanId: parentSpanId || uuid4(), |
46 |
| - traceId: tId, |
47 |
| - traceFlags: 0, |
48 |
| - }; |
| 46 | + trace.setSpan(context.active(), parentSpan); |
49 | 47 |
|
50 |
| - const spanContext: SpanContext = { |
51 |
| - spanId: spanId || uuid4(), |
52 |
| - traceId: tId, |
53 |
| - traceFlags: 0, |
54 |
| - }; |
| 48 | + const childSpan = tracer.startSpan(name || 'test', { |
| 49 | + kind: SpanKind.INTERNAL, |
| 50 | + links: [], |
| 51 | + startTime, |
| 52 | + }); |
| 53 | + |
| 54 | + childSpan.spanContext().spanId = spanId || uuid4(); |
| 55 | + childSpan.spanContext().traceId = tId; |
| 56 | + childSpan.spanContext().traceFlags = 0; |
55 | 57 |
|
56 |
| - return tracer.startSpan( |
57 |
| - name || 'test', |
58 |
| - { |
59 |
| - kind: SpanKind.INTERNAL, |
60 |
| - links: [], |
61 |
| - spanContext, |
62 |
| - startTime, |
63 |
| - }, |
64 |
| - parentSpanContext, |
65 |
| - ); |
| 58 | + return childSpan; |
66 | 59 | }
|
0 commit comments