Skip to content

Commit 6e8fa0b

Browse files
committed
first pass at shouldCreateSpanForRequest option
1 parent 334691d commit 6e8fa0b

File tree

1 file changed

+33
-6
lines changed
  • packages/node/src/integrations

1 file changed

+33
-6
lines changed

packages/node/src/integrations/http.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,29 @@ export class Http implements Integration {
1919
public name: string = Http.id;
2020

2121
/**
22-
* @inheritDoc
22+
* Whether or not to record outgoing requests as breadcrumbs
2323
*/
2424
private readonly _breadcrumbs: boolean;
2525

2626
/**
27-
* @inheritDoc
27+
* Whether or not to record outgoing requests as tracing spans
2828
*/
2929
private readonly _tracing: boolean;
3030

31+
/**
32+
* Hook allowing filering of request spans
33+
*/
34+
private readonly _shouldCreateSpan?: (url: string) => boolean;
35+
3136
/**
3237
* @inheritDoc
3338
*/
34-
public constructor(options: { breadcrumbs?: boolean; tracing?: boolean } = {}) {
39+
public constructor(
40+
options: { breadcrumbs?: boolean; tracing?: boolean; shouldCreateSpanForRequest?: (url: string) => boolean } = {},
41+
) {
3542
this._breadcrumbs = typeof options.breadcrumbs === 'undefined' ? true : options.breadcrumbs;
3643
this._tracing = typeof options.tracing === 'undefined' ? false : options.tracing;
44+
this._shouldCreateSpan = options.shouldCreateSpanForRequest;
3745
}
3846

3947
/**
@@ -45,7 +53,7 @@ export class Http implements Integration {
4553
return;
4654
}
4755

48-
const wrappedHandlerMaker = _createWrappedHandlerMaker(this._breadcrumbs, this._tracing);
56+
const wrappedHandlerMaker = _createWrappedHandlerMaker(this._breadcrumbs, this._tracing, this._shouldCreateSpan);
4957

5058
const httpModule = require('http');
5159
fill(httpModule, 'get', wrappedHandlerMaker);
@@ -72,10 +80,15 @@ type WrappedHandlerMaker = (originalHandler: OriginalHandler) => WrappedHandler;
7280
*
7381
* @param breadcrumbsEnabled Whether or not to record outgoing requests as breadcrumbs
7482
* @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
7584
*
7685
* @returns A function which accepts the exiting handler and returns a wrapped handler
7786
*/
78-
function _createWrappedHandlerMaker(breadcrumbsEnabled: boolean, tracingEnabled: boolean): WrappedHandlerMaker {
87+
function _createWrappedHandlerMaker(
88+
breadcrumbsEnabled: boolean,
89+
tracingEnabled: boolean,
90+
shouldCreateSpan?: (url: string) => boolean,
91+
): WrappedHandlerMaker {
7992
return function wrappedHandlerMaker(originalHandler: OriginalHandler): WrappedHandler {
8093
return function wrappedHandler(
8194
this: typeof http | typeof https,
@@ -88,13 +101,27 @@ function _createWrappedHandlerMaker(breadcrumbsEnabled: boolean, tracingEnabled:
88101
return originalHandler.apply(this, arguments);
89102
}
90103

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+
91118
let span: Span | undefined;
92119
let transaction: Transaction | undefined;
93120

94121
const scope = getCurrentHub().getScope();
95122
if (scope && tracingEnabled) {
96123
transaction = scope.getTransaction();
97-
if (transaction) {
124+
if (transaction && shouldStartSpan(requestUrl)) {
98125
span = transaction.startChild({
99126
description: `${typeof options === 'string' || !options.method ? 'GET' : options.method} ${requestUrl}`,
100127
op: 'request',

0 commit comments

Comments
 (0)