Skip to content

Commit

Permalink
Add more propagation functions (#14)
Browse files Browse the repository at this point in the history
* Add more propagation functions

* Format files

* Update readme

* Readme again

* Update version

* Remove getTraceparentObject

* Remove undefined function in index.ts

* Update test equals check

* Minor test refactor
  • Loading branch information
assensamuelsson authored Nov 13, 2024
1 parent 2884d9b commit 8e9a1f2
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 43 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Here is an example server with log tracing enabled.

```js
import {
getHttpTraceHeader,
getTraceparent,
middleware,
logger as buildLogger
} from "@bonniernews/logger";
Expand All @@ -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() },
});

...
Expand All @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -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";
12 changes: 0 additions & 12 deletions lib/http.ts

This file was deleted.

22 changes: 22 additions & 0 deletions lib/propagation.ts
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 0 additions & 25 deletions test/unit/http.test.ts

This file was deleted.

39 changes: 39 additions & 0 deletions test/unit/propagation.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 8e9a1f2

Please sign in to comment.