diff --git a/README.md b/README.md index 45fe84e..4b0a9eb 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Here is an example server with log tracing enabled. ```js import { - getHttpTraceHeader, + getTraceparent, middleware, logger as buildLogger } from "@bonniernews/logger"; @@ -28,8 +28,9 @@ app.use(middleware()); app.get("/", async (req, res) => { logger.info("Hello, world!"); - const response = await fetch("https://some.service.bn.nr/some/endpoint", { - headers: { ...getHttpTraceHeader() }, + // Propagate traceparent to other services + const response = await fetch("https://some.other.service.bn.nr/some/endpoint", { + headers: { traceparent: getTraceparent() }, }); ... @@ -38,7 +39,8 @@ app.get("/", async (req, res) => { The `middleware` should be put as early as possible, since only logs after this middleware will get the tracing data. The middleware will lookup the active project ID from GCP. Alternatively, you can set the `GCP_PROJECT` environment variable for this purpose. -Use the `getHttpTraceHeader` function to pass tracing headers to downstream services. +Use the `getTraceparent` function to pass tracing headers to downstream services. +Use `getTraceId` if you only want to know the current trace-id. If you want to decorate logs with custom data, use the exported `decorateLogs` function. In order to use this, the middleware needs to be installed first. diff --git a/index.ts b/index.ts index 60c0c29..ac882a2 100644 --- a/index.ts +++ b/index.ts @@ -1,4 +1,4 @@ -export { getHttpTraceHeader } from "./lib/http"; +export { getTraceparent, getTraceId } from "./lib/propagation"; export { decorateLogs, logger, Logger, LoggerOptions, DestinationStream } from "./lib/logging"; export { middleware } from "./lib/middleware"; export { createTraceparent } from "./lib/traceparent"; diff --git a/lib/http.ts b/lib/http.ts deleted file mode 100644 index 198e817..0000000 --- a/lib/http.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { getStore } from "./middleware"; - -/** - * Retrieves an object from the request context store with headers that can be used - * for requests to downstream services. - * - * Make sure you are using the `middleware` when using this. - */ -export function getHttpTraceHeader(): Record { - const { traceparent } = getStore() || {}; - return traceparent ? { traceparent } : {}; -} diff --git a/lib/propagation.ts b/lib/propagation.ts new file mode 100644 index 0000000..5d92a1f --- /dev/null +++ b/lib/propagation.ts @@ -0,0 +1,22 @@ +import { getStore } from "./middleware"; +import { getTraceFromTraceparent } from "./traceparent"; + +/** + * Retrieves traceparent string from the request context store that can be used + * for requests to downstream services. + * + * Make sure you are using the `middleware` when using this. + */ +export function getTraceparent(): string | undefined { + return getStore()?.traceparent; +} + +/** + * Retrieves traceId from the request context store + * + * Make sure you are using the `middleware` when using this. + */ +export function getTraceId(): string | undefined { + const { traceparent } = getStore() || {}; + return traceparent ? getTraceFromTraceparent(traceparent)?.traceId : undefined; +} diff --git a/package.json b/package.json index 0ffc87b..84faa91 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bonniernews/logger", - "version": "0.0.7", + "version": "0.0.8", "description": "Some simple functions to use Trace Context for correlating logs", "type": "module", "main": "./dist/index.cjs", diff --git a/test/unit/http.test.ts b/test/unit/http.test.ts deleted file mode 100644 index ab93c82..0000000 --- a/test/unit/http.test.ts +++ /dev/null @@ -1,25 +0,0 @@ -import type { RequestHandler } from "express"; - -import { getHttpTraceHeader } from "../../lib/http"; -import { middleware as createMiddleware } from "../../lib/middleware"; - -describe("HTTP helper", () => { - let middleware: RequestHandler; - - beforeEach(() => { - middleware = createMiddleware(); - }); - - it("should get the traceparent header from the middleware context", async () => { - const traceparent = "00-abcdef0123456789abcdef0123456789-abcdef0123456789-01"; - - // @ts-expect-error - We don't need the full Express Request object - await middleware({ header: () => traceparent }, {}, () => { - expect(getHttpTraceHeader()).to.deep.equal({ traceparent }); - }); - }); - - it("should return an empty object if run outside the middleware context", () => { - expect(getHttpTraceHeader()).to.deep.equal({}); - }); -}); diff --git a/test/unit/propagation.test.ts b/test/unit/propagation.test.ts new file mode 100644 index 0000000..6611dd3 --- /dev/null +++ b/test/unit/propagation.test.ts @@ -0,0 +1,39 @@ +import type { RequestHandler } from "express"; + +import { getTraceparent, getTraceId } from "../../lib/propagation"; +import { middleware as createMiddleware } from "../../lib/middleware"; + +describe("Propagations", () => { + let middleware: RequestHandler; + const traceparent = "00-abcdef0123456789abcdef0123456789-abcdef0123456789-01"; + + beforeEach(() => { + middleware = createMiddleware(); + }); + + describe("getTraceparent", () => { + it("should get the traceparent string from the middleware context", async () => { + // @ts-expect-error - We don't need the full Express Request object + await middleware({ header: () => traceparent }, {}, () => { + expect(getTraceparent()).to.equal(traceparent); + }); + }); + + it("should return undefined if run outside the middleware context", () => { + expect(getTraceparent()).to.equal(undefined); + }); + }); + + describe("getTraceId", () => { + it("should get the traceId from the middleware context", async () => { + // @ts-expect-error - We don't need the full Express Request object + await middleware({ header: () => traceparent }, {}, () => { + expect(getTraceId()).to.equal("abcdef0123456789abcdef0123456789"); + }); + }); + + it("should return undefined if run outside the middleware context", () => { + expect(getTraceId()).to.equal(undefined); + }); + }); +});