1
1
import { getCurrentHub , Hub } from '@sentry/core' ;
2
- import { EventProcessor , Integration , Span } from '@sentry/types' ;
2
+ import { EventProcessor , Integration , Span , TracePropagationTargets } from '@sentry/types' ;
3
3
import {
4
4
dynamicSamplingContextToSentryBaggageHeader ,
5
5
fill ,
@@ -11,7 +11,6 @@ import * as http from 'http';
11
11
import * as https from 'https' ;
12
12
13
13
import { NodeClient } from '../client' ;
14
- import { NodeClientOptions } from '../types' ;
15
14
import {
16
15
cleanSpanDescription ,
17
16
extractUrl ,
@@ -23,6 +22,39 @@ import {
23
22
24
23
const NODE_VERSION = parseSemver ( process . versions . node ) ;
25
24
25
+ interface TracingOptions {
26
+ /**
27
+ * List of strings/regex controlling to which outgoing requests
28
+ * the SDK will attach tracing headers.
29
+ *
30
+ * By default the SDK will attach those headers to all outgoing
31
+ * requests. If this option is provided, the SDK will match the
32
+ * request URL of outgoing requests against the items in this
33
+ * array, and only attach tracing headers if a match was found.
34
+ */
35
+ tracePropagationTargets ?: TracePropagationTargets ;
36
+
37
+ /**
38
+ * Function determining whether or not to create spans to track outgoing requests to the given URL.
39
+ * By default, spans will be created for all outgoing requests.
40
+ */
41
+ shouldCreateSpanForRequest ?: ( url : string ) => boolean ;
42
+ }
43
+
44
+ interface HttpOptions {
45
+ /**
46
+ * Whether breadcrumbs should be recorded for requests
47
+ * Defaults to true
48
+ */
49
+ breadcrumbs ?: boolean ;
50
+
51
+ /**
52
+ * Whether tracing spans should be created for requests
53
+ * Defaults to false
54
+ */
55
+ tracing ?: TracingOptions | boolean ;
56
+ }
57
+
26
58
/**
27
59
* The http module integration instruments Node's internal http module. It creates breadcrumbs, transactions for outgoing
28
60
* http requests and attaches trace data when tracing is enabled via its `tracing` option.
@@ -38,22 +70,15 @@ export class Http implements Integration {
38
70
*/
39
71
public name : string = Http . id ;
40
72
41
- /**
42
- * @inheritDoc
43
- */
44
73
private readonly _breadcrumbs : boolean ;
74
+ private readonly _tracing : TracingOptions | undefined ;
45
75
46
76
/**
47
77
* @inheritDoc
48
78
*/
49
- private readonly _tracing : boolean ;
50
-
51
- /**
52
- * @inheritDoc
53
- */
54
- public constructor ( options : { breadcrumbs ?: boolean ; tracing ?: boolean } = { } ) {
79
+ public constructor ( options : HttpOptions = { } ) {
55
80
this . _breadcrumbs = typeof options . breadcrumbs === 'undefined' ? true : options . breadcrumbs ;
56
- this . _tracing = typeof options . tracing === 'undefined' ? false : options . tracing ;
81
+ this . _tracing = ! options . tracing ? undefined : options . tracing === true ? { } : options . tracing ;
57
82
}
58
83
59
84
/**
@@ -76,7 +101,11 @@ export class Http implements Integration {
76
101
return ;
77
102
}
78
103
79
- const wrappedHandlerMaker = _createWrappedRequestMethodFactory ( this . _breadcrumbs , this . _tracing , clientOptions ) ;
104
+ // TODO (v8): `tracePropagationTargets` and `shouldCreateSpanForRequest` will be removed from clientOptions
105
+ // and we will no longer have to do this optional merge, we can just pass `this._tracing` directly.
106
+ const tracingOptions = this . _tracing ? { ...clientOptions , ...this . _tracing } : undefined ;
107
+
108
+ const wrappedHandlerMaker = _createWrappedRequestMethodFactory ( this . _breadcrumbs , tracingOptions ) ;
80
109
81
110
// eslint-disable-next-line @typescript-eslint/no-var-requires
82
111
const httpModule = require ( 'http' ) ;
@@ -111,37 +140,36 @@ type WrappedRequestMethodFactory = (original: OriginalRequestMethod) => WrappedR
111
140
*/
112
141
function _createWrappedRequestMethodFactory (
113
142
breadcrumbsEnabled : boolean ,
114
- tracingEnabled : boolean ,
115
- options : NodeClientOptions | undefined ,
143
+ tracingOptions : TracingOptions | undefined ,
116
144
) : WrappedRequestMethodFactory {
117
145
// We're caching results so we don't have to recompute regexp every time we create a request.
118
146
const createSpanUrlMap : Record < string , boolean > = { } ;
119
147
const headersUrlMap : Record < string , boolean > = { } ;
120
148
121
149
const shouldCreateSpan = ( url : string ) : boolean => {
122
- if ( options ?. shouldCreateSpanForRequest === undefined ) {
150
+ if ( tracingOptions ?. shouldCreateSpanForRequest === undefined ) {
123
151
return true ;
124
152
}
125
153
126
154
if ( createSpanUrlMap [ url ] ) {
127
155
return createSpanUrlMap [ url ] ;
128
156
}
129
157
130
- createSpanUrlMap [ url ] = options . shouldCreateSpanForRequest ( url ) ;
158
+ createSpanUrlMap [ url ] = tracingOptions . shouldCreateSpanForRequest ( url ) ;
131
159
132
160
return createSpanUrlMap [ url ] ;
133
161
} ;
134
162
135
163
const shouldAttachTraceData = ( url : string ) : boolean => {
136
- if ( options ?. tracePropagationTargets === undefined ) {
164
+ if ( tracingOptions ?. tracePropagationTargets === undefined ) {
137
165
return true ;
138
166
}
139
167
140
168
if ( headersUrlMap [ url ] ) {
141
169
return headersUrlMap [ url ] ;
142
170
}
143
171
144
- headersUrlMap [ url ] = options . tracePropagationTargets . some ( tracePropagationTarget =>
172
+ headersUrlMap [ url ] = tracingOptions . tracePropagationTargets . some ( tracePropagationTarget =>
145
173
isMatchingPattern ( url , tracePropagationTarget ) ,
146
174
) ;
147
175
@@ -167,7 +195,7 @@ function _createWrappedRequestMethodFactory(
167
195
168
196
const scope = getCurrentHub ( ) . getScope ( ) ;
169
197
170
- if ( scope && tracingEnabled && shouldCreateSpan ( requestUrl ) ) {
198
+ if ( scope && tracingOptions && shouldCreateSpan ( requestUrl ) ) {
171
199
parentSpan = scope . getSpan ( ) ;
172
200
173
201
if ( parentSpan ) {
@@ -235,7 +263,7 @@ function _createWrappedRequestMethodFactory(
235
263
if ( breadcrumbsEnabled ) {
236
264
addRequestBreadcrumb ( 'response' , requestUrl , req , res ) ;
237
265
}
238
- if ( tracingEnabled && requestSpan ) {
266
+ if ( requestSpan ) {
239
267
if ( res . statusCode ) {
240
268
requestSpan . setHttpStatus ( res . statusCode ) ;
241
269
}
@@ -250,7 +278,7 @@ function _createWrappedRequestMethodFactory(
250
278
if ( breadcrumbsEnabled ) {
251
279
addRequestBreadcrumb ( 'error' , requestUrl , req ) ;
252
280
}
253
- if ( tracingEnabled && requestSpan ) {
281
+ if ( requestSpan ) {
254
282
requestSpan . setHttpStatus ( 500 ) ;
255
283
requestSpan . description = cleanSpanDescription ( requestSpan . description , requestOptions , req ) ;
256
284
requestSpan . finish ( ) ;
0 commit comments