1
1
import type { Span } from '@opentelemetry/api' ;
2
2
import { SpanKind } from '@opentelemetry/api' ;
3
- import { registerInstrumentations } from '@opentelemetry/instrumentation' ;
3
+ import type { Instrumentation } from '@opentelemetry/instrumentation' ;
4
4
import { hasTracingEnabled } from '@sentry/core' ;
5
- import type { EventProcessor , Hub , Integration } from '@sentry/types' ;
5
+ import type { Integration } from '@sentry/types' ;
6
6
import { FetchInstrumentation } from 'opentelemetry-instrumentation-fetch-node' ;
7
7
8
8
import { OTEL_ATTR_ORIGIN } from '../constants' ;
9
9
import type { NodeExperimentalClient } from '../sdk/client' ;
10
10
import { getCurrentHub } from '../sdk/hub' ;
11
11
import { getRequestSpanData } from '../utils/getRequestSpanData' ;
12
12
import { getSpanKind } from '../utils/getSpanKind' ;
13
+ import { NodePerformanceIntegration } from './NodePerformanceIntegration' ;
13
14
14
15
interface NodeFetchOptions {
15
16
/**
@@ -31,7 +32,7 @@ interface NodeFetchOptions {
31
32
* * Create breadcrumbs for outgoing requests
32
33
* * Create spans for outgoing requests
33
34
*/
34
- export class NodeFetch implements Integration {
35
+ export class NodeFetch extends NodePerformanceIntegration < NodeFetchOptions > implements Integration {
35
36
/**
36
37
* @inheritDoc
37
38
*/
@@ -47,7 +48,6 @@ export class NodeFetch implements Integration {
47
48
*/
48
49
public shouldCreateSpansForRequests : boolean ;
49
50
50
- private _unload ?: ( ) => void ;
51
51
private readonly _breadcrumbs : boolean ;
52
52
// If this is undefined, use default behavior based on client settings
53
53
private readonly _spans : boolean | undefined ;
@@ -56,6 +56,8 @@ export class NodeFetch implements Integration {
56
56
* @inheritDoc
57
57
*/
58
58
public constructor ( options : NodeFetchOptions = { } ) {
59
+ super ( options ) ;
60
+
59
61
this . name = NodeFetch . id ;
60
62
this . _breadcrumbs = typeof options . breadcrumbs === 'undefined' ? true : options . breadcrumbs ;
61
63
this . _spans = typeof options . spans === 'undefined' ? undefined : options . spans ;
@@ -64,33 +66,30 @@ export class NodeFetch implements Integration {
64
66
this . shouldCreateSpansForRequests = false ;
65
67
}
66
68
69
+ /** @inheritDoc */
70
+ public setupInstrumentation ( ) : void | Instrumentation [ ] {
71
+ return [
72
+ new FetchInstrumentation ( {
73
+ onRequest : ( { span } : { span : Span } ) => {
74
+ this . _updateSpan ( span ) ;
75
+ this . _addRequestBreadcrumb ( span ) ;
76
+ } ,
77
+ } ) ,
78
+ ] ;
79
+ }
80
+
67
81
/**
68
82
* @inheritDoc
69
83
*/
70
- public setupOnce ( _addGlobalEventProcessor : ( callback : EventProcessor ) => void , _getCurrentHub : ( ) => Hub ) : void {
71
- // No need to instrument if we don't want to track anything
72
- if ( ! this . _breadcrumbs && this . _spans === false ) {
73
- return ;
74
- }
84
+ public setupOnce ( ) : void {
85
+ super . setupOnce ( ) ;
75
86
76
87
const client = getCurrentHub ( ) . getClient < NodeExperimentalClient > ( ) ;
77
88
const clientOptions = client ?. getOptions ( ) ;
78
89
79
90
// This is used in the sampler function
80
91
this . shouldCreateSpansForRequests =
81
92
typeof this . _spans === 'boolean' ? this . _spans : hasTracingEnabled ( clientOptions ) ;
82
-
83
- // Register instrumentations we care about
84
- this . _unload = registerInstrumentations ( {
85
- instrumentations : [
86
- new FetchInstrumentation ( {
87
- onRequest : ( { span } : { span : Span } ) => {
88
- this . _updateSpan ( span ) ;
89
- this . _addRequestBreadcrumb ( span ) ;
90
- } ,
91
- } ) ,
92
- ] ,
93
- } ) ;
94
93
}
95
94
96
95
/**
0 commit comments