Skip to content

ref(node): Make naming of http method wrappers clearer #2883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions packages/node/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,45 @@ export class Http implements Integration {
return;
}

const handlerWrapper = createHandlerWrapper(this._breadcrumbs, this._tracing);
const wrappedHandlerMaker = _createWrappedHandlerMaker(this._breadcrumbs, this._tracing);

const httpModule = require('http');
fill(httpModule, 'get', handlerWrapper);
fill(httpModule, 'request', handlerWrapper);
fill(httpModule, 'get', wrappedHandlerMaker);
fill(httpModule, 'request', wrappedHandlerMaker);

// NOTE: Prior to Node 9, `https` used internals of `http` module, thus we don't patch it.
// If we do, we'd get double breadcrumbs and double spans for `https` calls.
// It has been changed in Node 9, so for all versions equal and above, we patch `https` separately.
if (NODE_VERSION.major && NODE_VERSION.major > 8) {
const httpsModule = require('https');
fill(httpsModule, 'get', handlerWrapper);
fill(httpsModule, 'request', handlerWrapper);
fill(httpsModule, 'get', wrappedHandlerMaker);
fill(httpsModule, 'request', wrappedHandlerMaker);
}
}
}

type OriginalHandler = () => http.ClientRequest;
type WrappedHandler = (options: string | http.ClientRequestArgs) => http.ClientRequest;
type WrappedHandlerMaker = (originalHandler: OriginalHandler) => WrappedHandler;

/**
* Wrapper function for internal `request` and `get` calls within `http` and `https` modules
* Function which creates a function which creates wrapped versions of internal `request` and `get` calls within `http`
* and `https` modules. (NB: Not a typo - this is a creator^2!)
*
* @param breadcrumbsEnabled Whether or not to record outgoing requests as breadcrumbs
* @param tracingEnabled Whether or not to record outgoing requests as tracing spans
*
* @returns A function which accepts the exiting handler and returns a wrapped handler
*/
function createHandlerWrapper(
breadcrumbsEnabled: boolean,
tracingEnabled: boolean,
): (originalHandler: () => http.ClientRequest) => (options: string | http.ClientRequestArgs) => http.ClientRequest {
return function handlerWrapper(
originalHandler: () => http.ClientRequest,
): (options: string | http.ClientRequestArgs) => http.ClientRequest {
return function(this: typeof http | typeof https, options: string | http.ClientRequestArgs): http.ClientRequest {
function _createWrappedHandlerMaker(breadcrumbsEnabled: boolean, tracingEnabled: boolean): WrappedHandlerMaker {
return function wrappedHandlerMaker(originalHandler: OriginalHandler): WrappedHandler {
return function wrappedHandler(
this: typeof http | typeof https,
options: string | http.ClientRequestArgs,
): http.ClientRequest {
const requestUrl = extractUrl(options);

// we don't want to record requests to Sentry as either breadcrumbs or spans, so just use the original handler
if (isSentryRequest(requestUrl)) {
return originalHandler.apply(this, arguments);
}
Expand Down