Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #166 from BonnierNews/feature/bump-lu-logger
Browse files Browse the repository at this point in the history
Bump `lu-logger` and take corr-ID automatically in HTTP requests
  • Loading branch information
MattiasOlla authored Jul 17, 2024
2 parents 1368d2c + 0deb6d6 commit ba66f93
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 16.x, 18.x, 20.x ]
node-version: [ 18.x, 20.x ]

steps:
- uses: actions/checkout@v2
Expand Down
25 changes: 9 additions & 16 deletions lib/utils/http.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import util from "util";
import config from "exp-config";
import { buildLogger } from "lu-logger";
import axios from "axios";
import assert from "assert";
import axios from "axios";
import config from "exp-config";
import { debugMeta, logger } from "lu-logger";
import util from "util";

import { getGcpAuthHeaders } from "./gcp-auth.js";
import getUrl from "./get-url.js";
Expand All @@ -12,16 +12,9 @@ const backends = buildBackends();
async function performRequest(method, params) {
assert(config.appName, "appName must be set in config");

const logger = buildLogger({
meta: {
correlationId: params.correlationId,
requesterName: config.appName,
routingKey: params.routingKey,
key: params.key,
},
});
const correlationId = params.correlationId || debugMeta.getDebugMeta().correlationId;
const url = getUrl(params);
logger.info(`HTTP ${method}, ${url}, params: ${JSON.stringify(params)}`);
logger.info(`HTTP ${method}, ${url}, params: ${JSON.stringify(params)}`, { correlationId });

// default to not let axios validate status, we do that ourselves, but allow override via opts
let validateStatus = () => true;
Expand All @@ -30,7 +23,7 @@ async function performRequest(method, params) {
const opts = {
method,
params: { ...params.qs, ...params.query },
headers: await buildHeaders(params, { ...(params.headers || {}) }),
headers: await buildHeaders({ ...params, correlationId }, { ...(params.headers || {}) }),
validateStatus,
maxBodyLength: Infinity,
maxContentLength: Infinity,
Expand All @@ -56,9 +49,9 @@ async function performRequest(method, params) {
let response;
try {
response = await axios(url, opts);
logger.info(`HTTP response for ${method} ${url}, ${response.status}, ${JSON.stringify(response.data)}`);
logger.info(`HTTP response for ${method} ${url}, ${response.status}, ${JSON.stringify(response.data)}`, { correlationId });
} catch (err) {
logger.warning(`HTTP ${method}:${url} , error: ${err}`);
logger.warning(`HTTP ${method}:${url} , error: ${err}`, { correlationId });
throw err;
}

Expand Down
86 changes: 12 additions & 74 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "lu-common",
"version": "8.9.0",
"version": "9.0.0",
"description": "",
"main": "index.js",
"engines": {
"node": ">=16"
"node": ">=18"
},
"type": "module",
"scripts": {
Expand All @@ -28,7 +28,7 @@
"google-auth-library": "^9.4.1",
"joi": "^17.11.0",
"joi-postalcode": "^2.0.0",
"lu-logger": "github:BonnierNews/lu-logger#semver:^7.3.1",
"lu-logger": "github:BonnierNews/lu-logger#semver:^8.0.0",
"moment": "^2.29.4",
"nock": "^13.4.0",
"sinon": "^17.0.1",
Expand Down
32 changes: 31 additions & 1 deletion test/unit/utils/http-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { fakeApi as fakeApiInit, fakeGcpAuth } from "@bonniernews/lu-test";
import config from "exp-config";
import { debugMeta } from "lu-logger";
import nock from "nock";
import urlencode from "urlencode";
import { fakeGcpAuth, fakeApi as fakeApiInit } from "@bonniernews/lu-test";

import http from "../../../lib/utils/http.js";

Expand Down Expand Up @@ -375,5 +376,34 @@ describe("http", () => {
});
});

describe("correlation ID", () => {
beforeEach(fakeGcpAuth.authenticated);
after(fakeGcpAuth.reset);
const correlationId = "some-epic-id";
const middleware = debugMeta.initMiddleware((req) => req.debugMeta);

it("should take correlation ID from argument if ran without middleware", async () => {
fakeApi.get("/some/path").matchHeader("correlation-id", correlationId).reply(200, { ok: true });

await http.get({ path: "/some/path", correlationId });
});

it("should take correlation ID from logger middleware if no argument provided", () => {
fakeApi.get("/some/path").matchHeader("correlation-id", correlationId).reply(200, { ok: true });

middleware({ debugMeta: { correlationId } }, {}, async () => {
await http.get({ path: "/some/path" });
});
});

it("should take correlation ID from argument even if ran in middleware", () => {
fakeApi.get("/some/path").matchHeader("correlation-id", correlationId).reply(200, { ok: true });

middleware({ debugMeta: { correlationId: "some-less-epic-id" } }, {}, async () => {
await http.get({ path: "/some/path", correlationId });
});
});
});

afterEach(fakeApi.reset);
});

0 comments on commit ba66f93

Please sign in to comment.