1
- import type { Context , SpanContext , TimeInput } from '@opentelemetry/api' ;
1
+ import type { SpanContext , TimeInput } from '@opentelemetry/api' ;
2
2
import { SpanKind } from '@opentelemetry/api' ;
3
- import type { Tracer } from '@opentelemetry/sdk-trace-base ' ;
4
- import { Span } from '@opentelemetry/sdk-trace-base ' ;
3
+ import type { SpanProcessor } from '@opentelemetry/sdk-trace-node ' ;
4
+ import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node ' ;
5
5
import { uuid4 } from '@sentry/core' ;
6
6
7
7
export function createSpan (
@@ -17,24 +17,50 @@ export function createSpan(
17
17
traceId ?: string ;
18
18
startTime ?: TimeInput ;
19
19
} = { } ,
20
- ) : Span {
21
- const spanProcessor = {
20
+ ) {
21
+ const spanProcessor : SpanProcessor = {
22
22
onStart : ( ) => { } ,
23
23
onEnd : ( ) => { } ,
24
+ forceFlush : ( ( ) => { } ) as ( ) => Promise < void > ,
25
+ shutdown : ( ( ) => { } ) as ( ) => Promise < void > ,
26
+ } ;
27
+
28
+ const provider = new NodeTracerProvider ( {
29
+ spanLimits : { } ,
30
+ spanProcessors : [ spanProcessor ] ,
31
+ } ) ;
32
+
33
+ const tracer = provider . getTracer ( 'test-instrumentation-library' ) ;
34
+
35
+ // const tracer = {
36
+ // resource: 'test-resource',
37
+ // instrumentationLibrary: 'test-instrumentation-library',
38
+ // getSpanLimits: () => ({}),
39
+ // getActiveSpanProcessor: () => spanProcessor,
40
+ // } as unknown as Tracer;
41
+
42
+ const tId = traceId || uuid4 ( ) ;
43
+
44
+ const parentSpanContext : SpanContext = {
45
+ spanId : parentSpanId || uuid4 ( ) ,
46
+ traceId : tId ,
47
+ traceFlags : 0 ,
24
48
} ;
25
- const tracer = {
26
- resource : 'test-resource' ,
27
- instrumentationLibrary : 'test-instrumentation-library' ,
28
- getSpanLimits : ( ) => ( { } ) ,
29
- getActiveSpanProcessor : ( ) => spanProcessor ,
30
- } as unknown as Tracer ;
31
49
32
50
const spanContext : SpanContext = {
33
51
spanId : spanId || uuid4 ( ) ,
34
- traceId : traceId || uuid4 ( ) ,
52
+ traceId : tId ,
35
53
traceFlags : 0 ,
36
54
} ;
37
55
38
- // eslint-disable-next-line deprecation/deprecation
39
- return new Span ( tracer , { } as Context , name || 'test' , spanContext , SpanKind . INTERNAL , parentSpanId , [ ] , startTime ) ;
56
+ return tracer . startSpan (
57
+ name || 'test' ,
58
+ {
59
+ kind : SpanKind . INTERNAL ,
60
+ links : [ ] ,
61
+ spanContext,
62
+ startTime,
63
+ } ,
64
+ parentSpanContext ,
65
+ ) ;
40
66
}
0 commit comments