@@ -19,21 +19,29 @@ export class Http implements Integration {
19
19
public name : string = Http . id ;
20
20
21
21
/**
22
- * @inheritDoc
22
+ * Whether or not to record outgoing requests as breadcrumbs
23
23
*/
24
24
private readonly _breadcrumbs : boolean ;
25
25
26
26
/**
27
- * @inheritDoc
27
+ * Whether or not to record outgoing requests as tracing spans
28
28
*/
29
29
private readonly _tracing : boolean ;
30
30
31
+ /**
32
+ * Hook allowing filering of request spans
33
+ */
34
+ private readonly _shouldCreateSpan ?: ( url : string ) => boolean ;
35
+
31
36
/**
32
37
* @inheritDoc
33
38
*/
34
- public constructor ( options : { breadcrumbs ?: boolean ; tracing ?: boolean } = { } ) {
39
+ public constructor (
40
+ options : { breadcrumbs ?: boolean ; tracing ?: boolean ; shouldCreateSpanForRequest ?: ( url : string ) => boolean } = { } ,
41
+ ) {
35
42
this . _breadcrumbs = typeof options . breadcrumbs === 'undefined' ? true : options . breadcrumbs ;
36
43
this . _tracing = typeof options . tracing === 'undefined' ? false : options . tracing ;
44
+ this . _shouldCreateSpan = options . shouldCreateSpanForRequest ;
37
45
}
38
46
39
47
/**
@@ -45,7 +53,7 @@ export class Http implements Integration {
45
53
return ;
46
54
}
47
55
48
- const wrappedHandlerMaker = _createWrappedHandlerMaker ( this . _breadcrumbs , this . _tracing ) ;
56
+ const wrappedHandlerMaker = _createWrappedHandlerMaker ( this . _breadcrumbs , this . _tracing , this . _shouldCreateSpan ) ;
49
57
50
58
const httpModule = require ( 'http' ) ;
51
59
fill ( httpModule , 'get' , wrappedHandlerMaker ) ;
@@ -72,10 +80,15 @@ type WrappedHandlerMaker = (originalHandler: OriginalHandler) => WrappedHandler;
72
80
*
73
81
* @param breadcrumbsEnabled Whether or not to record outgoing requests as breadcrumbs
74
82
* @param tracingEnabled Whether or not to record outgoing requests as tracing spans
83
+ * @param shouldCreateSpan Optional hook for controling which rquests get recorded as spans
75
84
*
76
85
* @returns A function which accepts the exiting handler and returns a wrapped handler
77
86
*/
78
- function _createWrappedHandlerMaker ( breadcrumbsEnabled : boolean , tracingEnabled : boolean ) : WrappedHandlerMaker {
87
+ function _createWrappedHandlerMaker (
88
+ breadcrumbsEnabled : boolean ,
89
+ tracingEnabled : boolean ,
90
+ shouldCreateSpan ?: ( url : string ) => boolean ,
91
+ ) : WrappedHandlerMaker {
79
92
return function wrappedHandlerMaker ( originalHandler : OriginalHandler ) : WrappedHandler {
80
93
return function wrappedHandler (
81
94
this : typeof http | typeof https ,
@@ -88,13 +101,27 @@ function _createWrappedHandlerMaker(breadcrumbsEnabled: boolean, tracingEnabled:
88
101
return originalHandler . apply ( this , arguments ) ;
89
102
}
90
103
104
+ // apply user-provided filter (if any) and cache result for next time
105
+ const spanDecisionCache : { [ key : string ] : boolean } = { } ;
106
+ const shouldStartSpan = ( url : string ) : boolean => {
107
+ const cached = spanDecisionCache [ url ] ;
108
+ if ( cached ) {
109
+ return cached ;
110
+ }
111
+
112
+ const spanDecision = typeof shouldCreateSpan === 'function' ? shouldCreateSpan ( url ) : true ;
113
+ spanDecisionCache [ url ] = spanDecision ;
114
+
115
+ return spanDecision ;
116
+ } ;
117
+
91
118
let span : Span | undefined ;
92
119
let transaction : Transaction | undefined ;
93
120
94
121
const scope = getCurrentHub ( ) . getScope ( ) ;
95
122
if ( scope && tracingEnabled ) {
96
123
transaction = scope . getTransaction ( ) ;
97
- if ( transaction ) {
124
+ if ( transaction && shouldStartSpan ( requestUrl ) ) {
98
125
span = transaction . startChild ( {
99
126
description : `${ typeof options === 'string' || ! options . method ? 'GET' : options . method } ${ requestUrl } ` ,
100
127
op : 'request' ,
0 commit comments