Skip to content
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

Ensure fastify instrumentation honors the httpParserHook config #287

Merged
merged 1 commit into from
Jan 21, 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
41 changes: 41 additions & 0 deletions lib/instrumentation/express.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const request = require("supertest"),
schema = require("../schema"),
api = require("../api"),
tracker = require("../async_tracker"),
hooks = require("../propagation/hooks"),
pkg = require(path.join(__dirname, "..", "..", "package.json"));

describe("userContext", () => {
Expand Down Expand Up @@ -285,12 +286,52 @@ describe("Trace ids from X-Request-ID and X-Amzn-trace-id headers", () => {
});
});

describe("trace parser hook", () => {
const express = instrumentExpress(require("express"));

let app;

function initializeTestApp() {
app = express();
app.get("/", function (req, res) {
res.status(200).send("ok");
});
}

beforeEach(() => {
api.configure({ impl: "mock" });
hooks.configure({
httpTraceParserHook: () => {
return { traceId: "my-special-trace" };
},
});
initializeTestApp();
});
afterEach(() => {
api._resetForTesting();
hooks.configure({});
});

test("honors the hook configuration over default parsing", (done) => {
request(app)
.get("/")
.set("X-Amzn-Trace-Id", "Root=1-67891233-abcdef012345678912345678")
.expect(200, () => {
expect(api._apiForTesting().sentEvents.length).toBe(1);
let ev = api._apiForTesting().sentEvents[0];
expect(ev[schema.TRACE_ID]).toBe("my-special-trace");
done();
});
});
});

describe("trace id callback", () => {
const express = instrumentExpress(require("express"), {
traceIdSource: (req) => req.get("X-Request-ID") || "efgh456",
});

let app;

function initializeTestApp() {
app = express();
app.get("/", function (req, res) {
Expand Down
2 changes: 1 addition & 1 deletion lib/instrumentation/fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const instrumentFastify = function (fastify, opts = {}) {
// start our trace handling

// parse incoming trace headers into a span context
let spanContext = traceUtil.getTraceContext(traceIdSource, request);
let spanContext = traceUtil.parseTraceHeader(traceIdSource, request);
Copy link
Member

Choose a reason for hiding this comment

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

Yay! ✨ consistency ✨


// parentSpanId refers to the span id of the parent span
let parentSpanId = traceUtil.getParentSourceId(parentIdSource, request);
Expand Down
45 changes: 45 additions & 0 deletions lib/instrumentation/fastify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const request = require("supertest"),
cases = require("jest-in-case"),
schema = require("../schema"),
api = require("../api"),
hooks = require("../propagation/hooks"),
pkg = require(path.join(__dirname, "..", "..", "package.json"));

describe("userContext", () => {
Expand Down Expand Up @@ -203,6 +204,50 @@ describe("request id from http headers", () => {
]);
});

describe("trace parser hook", () => {
const fastify = instrumentFastify(require("fastify"));

function initializeTestServer() {
return new Promise((resolve, reject) => {
const app = fastify();
app.get("/", function (req, res) {
// don't add req.user here
res.code(200).send("ok");
});

app.ready().then(() => resolve(app), reject);
});
}

beforeEach(() => {
api.configure({ impl: "mock" });
hooks.configure({
httpTraceParserHook: () => {
return { traceId: "my-special-trace" };
},
});
});
afterEach(() => {
api._resetForTesting();
hooks.configure({});
});

test("honors the hook configuration over default parsing", (done) => {
initializeTestServer().then((app) => {
request(app.server)
.get("/")
.set("X-Amzn-Trace-Id", "Root=1-67891233-abcdef012345678912345678")
.expect(200, () => {
const sentEvents = api._apiForTesting().sentEvents;
expect(sentEvents.length).toBe(2);
let ev = api._apiForTesting().sentEvents[1];
expect(ev[schema.TRACE_ID]).toBe("my-special-trace");
app.close(done);
});
});
});
});

describe("trace id callback", () => {
function runCase(opts, done) {
const fastify = instrumentFastify(require("fastify"), {
Expand Down