Skip to content

Fix requests patching edge case #146

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 5 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions src/trace/patch-http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LogLevel, setLogLevel } from "../utils";
import { parentIDHeader, SampleMode, samplingPriorityHeader, traceIDHeader, Source } from "./constants";
import { patchHttp, unpatchHttp } from "./patch-http";
import { TraceContextService } from "./trace-context-service";
import { URL } from "url";

describe("patchHttp", () => {
let traceWrapper = {
Expand Down Expand Up @@ -131,4 +132,22 @@ describe("patchHttp", () => {
expect(headers[parentIDHeader]).toBeUndefined();
expect(headers[samplingPriorityHeader]).toBeUndefined();
});
it("injects tracing headers when using the new WHATWG URL object", () => {
nock("http://www.example.com").get("/").reply(200, {});
patchHttp(contextService);
const url = new URL("http://www.example.com");
const req = http.request(url);
expectHeaders(req);
});
it("injects tracing headers when using the new WHATWG URL object and callback", (done) => {
nock("http://www.example.com").get("/").reply(200, {});
patchHttp(contextService);
const url = new URL("http://www.example.com");
const req = http.request(url, {}, () => {
done();
});
req.end();

expectHeaders(req);
});
});
55 changes: 37 additions & 18 deletions src/trace/patch-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ export function unpatchHttp() {
function patchMethod(mod: typeof http | typeof https, method: "get" | "request", contextService: TraceContextService) {
shimmer.wrap(mod, method, (original) => {
const fn = (arg1: any, arg2: any, arg3: any) => {
const { options, callback } = normalizeArgs(arg1, arg2, arg3);
const requestOpts = getRequestOptionsWithTraceContext(options, contextService);
[arg1, arg2, arg3] = addTraceContextToArgs(contextService, arg1, arg2, arg3);

if (isIntegrationTest()) {
_logHttpRequest(requestOpts);
if (arg3 === undefined || arg3 === null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a comment here explaining why this is necessary

return original(arg1, arg2);
} else {
return original(arg1, arg2, arg3);
}

return original(requestOpts, callback);
};
return fn as any;
});
Expand All @@ -54,23 +53,37 @@ function unpatchMethod(mod: typeof http | typeof https, method: "get" | "request
}

/**
* The input into the http.request function has 6 different overloads. This method normalized the inputs
* into a consistent format.
* Finds the RequestOptions in the args and injects context into headers
*/
function normalizeArgs(
function addTraceContextToArgs(
contextService: TraceContextService,
arg1: string | URL | http.RequestOptions,
arg2?: RequestCallback | http.RequestOptions,
arg3?: RequestCallback,
) {
let options: http.RequestOptions = typeof arg1 === "string" ? parse(arg1) : { ...arg1 };
options.headers = options.headers || {};
let callback = arg3;
if (typeof arg2 === "function") {
callback = arg2;
} else if (typeof arg2 === "object") {
options = { ...options, ...arg2 };
let requestOpts: http.RequestOptions | undefined;
if (typeof arg1 === "string" || arg1 instanceof URL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise I might add comments here to describe the different cases

if (arg2 === undefined || arg2 === null) {
requestOpts = {
method: "GET",
};
requestOpts = getRequestOptionsWithTraceContext(requestOpts, contextService);
return [arg1, requestOpts, arg3];
} else if (typeof arg2 === "function") {
requestOpts = {
method: "GET",
};
requestOpts = getRequestOptionsWithTraceContext(requestOpts, contextService);
return [arg1, requestOpts, arg2];
} else {
requestOpts = arg2 as http.RequestOptions;
requestOpts = getRequestOptionsWithTraceContext(requestOpts, contextService);
return [arg1, requestOpts, arg3];
}
} else {
requestOpts = getRequestOptionsWithTraceContext(arg1, contextService);
return [requestOpts, arg2, arg3];
}
return { options, callback };
}

function getRequestOptionsWithTraceContext(
Expand All @@ -86,10 +99,16 @@ function getRequestOptionsWithTraceContext(
...headers,
...traceHeaders,
};
return {
const requestOpts = {
...options,
headers,
};
// Logging all http requests during integration tests let's
// us track traffic in our test snapshots
if (isIntegrationTest()) {
_logHttpRequest(requestOpts);
}
return requestOpts;
}

function isIntegrationTest() {
Expand Down