11import type { Span } from '@opentelemetry/api' ;
22import { SpanKind } from '@opentelemetry/api' ;
3- import { registerInstrumentations } from '@opentelemetry/instrumentation' ;
3+ import type { Instrumentation } from '@opentelemetry/instrumentation' ;
44import { hasTracingEnabled } from '@sentry/core' ;
5- import type { EventProcessor , Hub , Integration } from '@sentry/types' ;
5+ import type { Integration } from '@sentry/types' ;
66import { FetchInstrumentation } from 'opentelemetry-instrumentation-fetch-node' ;
77
88import { OTEL_ATTR_ORIGIN } from '../constants' ;
99import type { NodeExperimentalClient } from '../sdk/client' ;
1010import { getCurrentHub } from '../sdk/hub' ;
1111import { getRequestSpanData } from '../utils/getRequestSpanData' ;
1212import { getSpanKind } from '../utils/getSpanKind' ;
13+ import { NodePerformanceIntegration } from './NodePerformanceIntegration' ;
1314
1415interface NodeFetchOptions {
1516 /**
@@ -31,7 +32,7 @@ interface NodeFetchOptions {
3132 * * Create breadcrumbs for outgoing requests
3233 * * Create spans for outgoing requests
3334 */
34- export class NodeFetch implements Integration {
35+ export class NodeFetch extends NodePerformanceIntegration < NodeFetchOptions > implements Integration {
3536 /**
3637 * @inheritDoc
3738 */
@@ -47,7 +48,6 @@ export class NodeFetch implements Integration {
4748 */
4849 public shouldCreateSpansForRequests : boolean ;
4950
50- private _unload ?: ( ) => void ;
5151 private readonly _breadcrumbs : boolean ;
5252 // If this is undefined, use default behavior based on client settings
5353 private readonly _spans : boolean | undefined ;
@@ -56,6 +56,8 @@ export class NodeFetch implements Integration {
5656 * @inheritDoc
5757 */
5858 public constructor ( options : NodeFetchOptions = { } ) {
59+ super ( options ) ;
60+
5961 this . name = NodeFetch . id ;
6062 this . _breadcrumbs = typeof options . breadcrumbs === 'undefined' ? true : options . breadcrumbs ;
6163 this . _spans = typeof options . spans === 'undefined' ? undefined : options . spans ;
@@ -64,33 +66,30 @@ export class NodeFetch implements Integration {
6466 this . shouldCreateSpansForRequests = false ;
6567 }
6668
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+
6781 /**
6882 * @inheritDoc
6983 */
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 ( ) ;
7586
7687 const client = getCurrentHub ( ) . getClient < NodeExperimentalClient > ( ) ;
7788 const clientOptions = client ?. getOptions ( ) ;
7889
7990 // This is used in the sampler function
8091 this . shouldCreateSpansForRequests =
8192 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- } ) ;
9493 }
9594
9695 /**
0 commit comments