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

Create new traceparent if no header was provided #8

Merged
merged 3 commits into from
Nov 8, 2024
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
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { fetchGcpProjectId } from "./lib/gcp";
export { getHttpTraceHeader } from "./lib/http";
export { getLoggingTraceData, logger } from "./lib/logging";
export { middleware } from "./lib/middleware";
export { createTraceparent } from "./lib/traceparent";
3 changes: 2 additions & 1 deletion lib/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { RequestHandler } from "express";
import { AsyncLocalStorage } from "node:async_hooks";
import { createTraceparent } from "./traceparent";

type Store = {
traceparent?: string;
Expand All @@ -10,7 +11,7 @@ type Store = {
const storage = new AsyncLocalStorage<Store>();

export const middleware: RequestHandler = (req, _res, next) => {
const traceparent = req.header("traceparent");
const traceparent = req.header("traceparent") || createTraceparent();

storage.run({ traceparent }, () => {
next();
Expand Down
11 changes: 11 additions & 0 deletions lib/traceparent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import crypto from "crypto";

/**
* Traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00
*
Expand All @@ -17,3 +19,12 @@ export function getTraceFromTraceparent(traceHeader: string) {
isSampled: parts[3] !== "00",
};
}

export function createTraceparent(isSampled: boolean = false) {
const version = "00";
const traceId = crypto.randomBytes(16).toString("hex");
const parentId = crypto.randomBytes(8).toString("hex");
const flags = isSampled ? "01" : "00";

return `${version}-${traceId}-${parentId}-${flags}`;
}
10 changes: 10 additions & 0 deletions test/unit/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ describe("Express middleware", () => {
});
});

it("should create a new traceparent and store it in the store if no traceparent header was provided", () => {
const req = { header: () => {} };

// @ts-expect-error - We don't need the full Express Request object
middleware(req, {}, () => {
const traceparent = getStore().traceparent || "";
expect(new RegExp(/^[\da-f-]{55}$/).test(traceparent)).to.equal(true);
});
});

it("should return an empty object if the middleware is not used", () => {
expect(getStore()).to.deep.equal({});
});
Expand Down
24 changes: 23 additions & 1 deletion test/unit/traceparent.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTraceFromTraceparent } from "../../lib/traceparent";
import { getTraceFromTraceparent, createTraceparent } from "../../lib/traceparent";

describe("Traceparent parsing", () => {
it("should parse a traceparent header correctly", async () => {
Expand All @@ -21,3 +21,25 @@ describe("Traceparent parsing", () => {
expect(trace).to.equal(undefined);
});
});

describe("Traceparent creation", () => {
it("should create a traceparent header correctly", async () => {
const traceparent = createTraceparent();
const [version, traceId, parentId, isSampled] = traceparent.split("-");

expect(version).to.equal("00"); // we only support version 00
expect(isSampled).to.equal("00"); // default is not sampled

expect(new RegExp(/^[\da-f]{2}$/).test(version)).to.equal(true, version);
expect(new RegExp(/^[\da-f]{32}$/).test(traceId)).to.equal(true, traceId);
expect(new RegExp(/^[\da-f]{16}$/).test(parentId)).to.equal(true, parentId);
expect(new RegExp(/^[\da-f]{2}$/).test(isSampled)).to.equal(true, isSampled);
});

it("Sets flags if passed isSampled true", async () => {
const traceparent = createTraceparent(true);
const [, , , isSampled] = traceparent.split("-");

expect(isSampled).to.equal("01");
});
});