1
1
import { getCurrentHub , Hub } from '@sentry/core' ;
2
- import { EventProcessor , Integration , Span , TracePropagationTargets } from '@sentry/types' ;
2
+ import { EventProcessor , Integration , Span } from '@sentry/types' ;
3
3
import {
4
4
dynamicSamplingContextToSentryBaggageHeader ,
5
5
fill ,
@@ -10,6 +10,7 @@ import {
10
10
import * as http from 'http' ;
11
11
import * as https from 'https' ;
12
12
13
+ import { NodeClient } from '../client' ;
13
14
import { NodeClientOptions } from '../types' ;
14
15
import {
15
16
cleanSpanDescription ,
@@ -67,13 +68,8 @@ export class Http implements Integration {
67
68
return ;
68
69
}
69
70
70
- const clientOptions = setupOnceGetCurrentHub ( ) . getClient ( ) ?. getOptions ( ) as NodeClientOptions | undefined ;
71
-
72
- const wrappedHandlerMaker = _createWrappedRequestMethodFactory (
73
- this . _breadcrumbs ,
74
- this . _tracing ,
75
- clientOptions ?. tracePropagationTargets ,
76
- ) ;
71
+ const clientOptions = setupOnceGetCurrentHub ( ) . getClient < NodeClient > ( ) ?. getOptions ( ) ;
72
+ const wrappedHandlerMaker = _createWrappedRequestMethodFactory ( this . _breadcrumbs , this . _tracing , clientOptions ) ;
77
73
78
74
// eslint-disable-next-line @typescript-eslint/no-var-requires
79
75
const httpModule = require ( 'http' ) ;
@@ -109,24 +105,40 @@ type WrappedRequestMethodFactory = (original: OriginalRequestMethod) => WrappedR
109
105
function _createWrappedRequestMethodFactory (
110
106
breadcrumbsEnabled : boolean ,
111
107
tracingEnabled : boolean ,
112
- tracePropagationTargets : TracePropagationTargets | undefined ,
108
+ options : NodeClientOptions | undefined ,
113
109
) : WrappedRequestMethodFactory {
114
- // We're caching results so we dont have to recompute regexp everytime we create a request.
115
- const urlMap : Record < string , boolean > = { } ;
110
+ // We're caching results so we don't have to recompute regexp every time we create a request.
111
+ const createSpanUrlMap : Record < string , boolean > = { } ;
112
+ const headersUrlMap : Record < string , boolean > = { } ;
113
+
114
+ const shouldCreateSpan = ( url : string ) : boolean => {
115
+ if ( options ?. shouldCreateSpanForRequest === undefined ) {
116
+ return true ;
117
+ }
118
+
119
+ if ( createSpanUrlMap [ url ] ) {
120
+ return createSpanUrlMap [ url ] ;
121
+ }
122
+
123
+ createSpanUrlMap [ url ] = options . shouldCreateSpanForRequest ( url ) ;
124
+
125
+ return createSpanUrlMap [ url ] ;
126
+ } ;
127
+
116
128
const shouldAttachTraceData = ( url : string ) : boolean => {
117
- if ( tracePropagationTargets === undefined ) {
129
+ if ( options ?. tracePropagationTargets === undefined ) {
118
130
return true ;
119
131
}
120
132
121
- if ( urlMap [ url ] ) {
122
- return urlMap [ url ] ;
133
+ if ( headersUrlMap [ url ] ) {
134
+ return headersUrlMap [ url ] ;
123
135
}
124
136
125
- urlMap [ url ] = tracePropagationTargets . some ( tracePropagationTarget =>
137
+ headersUrlMap [ url ] = options . tracePropagationTargets . some ( tracePropagationTarget =>
126
138
isMatchingPattern ( url , tracePropagationTarget ) ,
127
139
) ;
128
140
129
- return urlMap [ url ] ;
141
+ return headersUrlMap [ url ] ;
130
142
} ;
131
143
132
144
return function wrappedRequestMethodFactory ( originalRequestMethod : OriginalRequestMethod ) : WrappedRequestMethod {
@@ -148,7 +160,7 @@ function _createWrappedRequestMethodFactory(
148
160
149
161
const scope = getCurrentHub ( ) . getScope ( ) ;
150
162
151
- if ( scope && tracingEnabled ) {
163
+ if ( scope && tracingEnabled && shouldCreateSpan ( requestUrl ) ) {
152
164
parentSpan = scope . getSpan ( ) ;
153
165
154
166
if ( parentSpan ) {
0 commit comments