Skip to content

Commit

Permalink
feat: add x-github-request-id into logging message (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed May 2, 2024
1 parent 25434e2 commit 2431de5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
9 changes: 6 additions & 3 deletions src/index.ts
Expand Up @@ -15,17 +15,20 @@ export function requestLog(octokit: Octokit) {

return (request as typeof octokit.request)(options)
.then((response) => {
const requestId = response.headers["x-github-request-id"];
octokit.log.info(
`${requestOptions.method} ${path} - ${response.status} in ${
`${requestOptions.method} ${path} - ${response.status} with id ${requestId} in ${
Date.now() - start
}ms`,
);
return response;
})

.catch((error) => {
octokit.log.info(
`${requestOptions.method} ${path} - ${error.status} in ${
const requestId =
error.response.headers["x-github-request-id"] || "UNKNOWN";
octokit.log.error(
`${requestOptions.method} ${path} - ${error.status} with id ${requestId} in ${
Date.now() - start
}ms`,
);
Expand Down
69 changes: 58 additions & 11 deletions test/request-log.test.ts
Expand Up @@ -5,10 +5,16 @@ import { jest } from "@jest/globals";
import { requestLog } from "../src/index.js";

describe("logging", () => {
it("logs sucessful 'GET /'", async () => {
const mock = fetchMock
.sandbox()
.getOnce("https://api.github.com/", { ok: true });
it("logs successful 'GET /'", async () => {
const mock = fetchMock.sandbox().getOnce("https://api.github.com/", {
status: 200,
body: {
ok: true,
},
headers: {
"x-github-request-id": "1234",
},
});

const mockLogInfo = jest.fn();
const mockDebugInfo = jest.fn();
Expand All @@ -31,15 +37,22 @@ describe("logging", () => {
method: "GET",
url: "/",
});
expect(mockLogInfo.mock.calls[0][0]).toMatch(/GET \/ - 200 in \d+ms/);
expect(mockLogInfo.mock.calls[0][0]).toMatch(
/GET \/ - 200 with id 1234 in \d+ms/,
);
});

it("logs 404 for 'GET /unknown'", async () => {
const mock = fetchMock
.sandbox()
.getOnce("https://api.github.com/unknown", 404);
const mock = fetchMock.sandbox().getOnce("https://api.github.com/unknown", {
status: 404,
body: { message: "Not Found" },
headers: {
"x-github-request-id": "1234",
},
});

const mockLogInfo = jest.fn();
const mockErrorInfo = jest.fn();
const mockDebugInfo = jest.fn();
const MyOctokit = Octokit.plugin(requestLog);
const octokit = new MyOctokit({
Expand All @@ -50,15 +63,49 @@ describe("logging", () => {
debug: mockDebugInfo,
info: mockLogInfo,
warn() {},
error() {},
error: mockErrorInfo,
},
});

try {
await octokit.request("GET /unknown");
} catch (error) {
expect(mockErrorInfo.mock.calls[0][0]).toMatch(
/GET \/unknown - 404 with id 1234 in \d+ms/,
);
return;
}

throw new Error('"GET /unknown" should not resolve');
});

it("logs malformed error response for 'GET /unknown'", async () => {
const mock = fetchMock.sandbox().getOnce("https://api.github.com/unknown", {
status: 500,
body: "Internal Server Error",
});

const mockLogInfo = jest.fn();
const mockErrorInfo = jest.fn();
const mockDebugInfo = jest.fn();
const MyOctokit = Octokit.plugin(requestLog);
const octokit = new MyOctokit({
request: {
fetch: mock,
},
log: {
debug: mockDebugInfo,
info: mockLogInfo,
warn() {},
error: mockErrorInfo,
},
});

try {
await octokit.request("GET /unknown");
} catch (error) {
expect(mockLogInfo.mock.calls[0][0]).toMatch(
/GET \/unknown - 404 in \d+ms/,
expect(mockErrorInfo.mock.calls[0][0]).toMatch(
/GET \/unknown - 500 with id UNKNOWN in \d+ms/,
);
return;
}
Expand Down

0 comments on commit 2431de5

Please sign in to comment.