@digitraffic/common 2026.10.27-2 → 2026.10.30-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__test__/runtime/http-interceptors.test.d.ts +1 -0
- package/dist/__test__/runtime/http-interceptors.test.js +154 -0
- package/dist/__test__/runtime/http-logger.test.js +49 -26
- package/dist/aws/runtime/logging/http-interceptors.d.ts +9 -0
- package/dist/aws/runtime/logging/http-interceptors.js +87 -0
- package/dist/aws/runtime/logging/http-logger.d.ts +9 -14
- package/dist/aws/runtime/logging/http-logger.js +31 -65
- package/dist/aws/runtime/logging/wide-context.d.ts +3 -1
- package/dist/aws/runtime/logging/wide-context.js +3 -1
- package/dist/types/openapi-schema.d.ts +20 -20
- package/package.json +2 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { HTTPError } from "ky";
|
|
2
|
+
import { describe, expect, test, vi } from "vitest";
|
|
3
|
+
import { createKyHooks, createLoggingInterceptor, } from "../../aws/runtime/logging/http-interceptors.js";
|
|
4
|
+
function mockHttpLogger() {
|
|
5
|
+
return {
|
|
6
|
+
start: vi.fn(),
|
|
7
|
+
logResponse: vi.fn(),
|
|
8
|
+
logError: vi.fn(),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function asHttpLogger(mock) {
|
|
12
|
+
return mock;
|
|
13
|
+
}
|
|
14
|
+
/** Runs the interceptor and returns the handler it passed on to the next dispatch. */
|
|
15
|
+
function intercept(httpLogger, innerHandler) {
|
|
16
|
+
const dispatch = vi.fn(() => true);
|
|
17
|
+
const interceptor = createLoggingInterceptor(asHttpLogger(httpLogger));
|
|
18
|
+
interceptor(dispatch)({}, innerHandler);
|
|
19
|
+
expect(dispatch).toHaveBeenCalledTimes(1);
|
|
20
|
+
return dispatch.mock.calls[0][1];
|
|
21
|
+
}
|
|
22
|
+
// Prototype methods are not own properties, as with undici's real handlers
|
|
23
|
+
class InnerHandler {
|
|
24
|
+
onRequestStart = vi.fn();
|
|
25
|
+
onResponseStart = vi.fn();
|
|
26
|
+
onResponseData = vi.fn();
|
|
27
|
+
onResponseEnd = vi.fn();
|
|
28
|
+
onResponseError = vi.fn();
|
|
29
|
+
}
|
|
30
|
+
describe("createLoggingInterceptor", () => {
|
|
31
|
+
const controller = {};
|
|
32
|
+
test("exposes onRequestStart so undici accepts the handler", () => {
|
|
33
|
+
const wrapped = intercept(mockHttpLogger(), new InnerHandler());
|
|
34
|
+
expect(typeof wrapped.onRequestStart).toBe("function");
|
|
35
|
+
});
|
|
36
|
+
test("starts the timer and delegates onRequestStart", () => {
|
|
37
|
+
const httpLogger = mockHttpLogger();
|
|
38
|
+
const inner = new InnerHandler();
|
|
39
|
+
const wrapped = intercept(httpLogger, inner);
|
|
40
|
+
wrapped.onRequestStart?.(controller, { ctx: 1 });
|
|
41
|
+
expect(httpLogger.start).toHaveBeenCalledTimes(1);
|
|
42
|
+
expect(inner.onRequestStart).toHaveBeenCalledWith(controller, { ctx: 1 });
|
|
43
|
+
});
|
|
44
|
+
test("logs status captured at response start when the response ends", () => {
|
|
45
|
+
const httpLogger = mockHttpLogger();
|
|
46
|
+
const inner = new InnerHandler();
|
|
47
|
+
const wrapped = intercept(httpLogger, inner);
|
|
48
|
+
const headers = {};
|
|
49
|
+
wrapped.onResponseStart?.(controller, 404, headers, "Not Found");
|
|
50
|
+
wrapped.onResponseData?.(controller, Buffer.alloc(42));
|
|
51
|
+
wrapped.onResponseEnd?.(controller, {});
|
|
52
|
+
expect(inner.onResponseStart).toHaveBeenCalledWith(controller, 404, headers, "Not Found");
|
|
53
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(404, 42);
|
|
54
|
+
expect(inner.onResponseEnd).toHaveBeenCalledWith(controller, {});
|
|
55
|
+
});
|
|
56
|
+
test("logs counted bytes regardless of the content-length header", () => {
|
|
57
|
+
const httpLogger = mockHttpLogger();
|
|
58
|
+
const wrapped = intercept(httpLogger, new InnerHandler());
|
|
59
|
+
wrapped.onResponseStart?.(controller, 200, { "content-length": "999" });
|
|
60
|
+
wrapped.onResponseData?.(controller, Buffer.from("abc"));
|
|
61
|
+
wrapped.onResponseEnd?.(controller, {});
|
|
62
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(200, 3);
|
|
63
|
+
});
|
|
64
|
+
test("counts bytes of chunked responses and forwards each chunk", () => {
|
|
65
|
+
const httpLogger = mockHttpLogger();
|
|
66
|
+
const inner = new InnerHandler();
|
|
67
|
+
const wrapped = intercept(httpLogger, inner);
|
|
68
|
+
const first = Buffer.from("hello");
|
|
69
|
+
const second = Buffer.from("world!");
|
|
70
|
+
wrapped.onResponseStart?.(controller, 200, {});
|
|
71
|
+
wrapped.onResponseData?.(controller, first);
|
|
72
|
+
wrapped.onResponseData?.(controller, second);
|
|
73
|
+
wrapped.onResponseEnd?.(controller, {});
|
|
74
|
+
expect(inner.onResponseData).toHaveBeenNthCalledWith(1, controller, first);
|
|
75
|
+
expect(inner.onResponseData).toHaveBeenNthCalledWith(2, controller, second);
|
|
76
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(200, 11);
|
|
77
|
+
});
|
|
78
|
+
test("logs zero length for an empty body without header", () => {
|
|
79
|
+
const httpLogger = mockHttpLogger();
|
|
80
|
+
const wrapped = intercept(httpLogger, new InnerHandler());
|
|
81
|
+
wrapped.onResponseStart?.(controller, 204, {});
|
|
82
|
+
wrapped.onResponseEnd?.(controller, {});
|
|
83
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(204, 0);
|
|
84
|
+
});
|
|
85
|
+
test("logs errors and delegates onResponseError", () => {
|
|
86
|
+
const httpLogger = mockHttpLogger();
|
|
87
|
+
const inner = new InnerHandler();
|
|
88
|
+
const wrapped = intercept(httpLogger, inner);
|
|
89
|
+
const error = new Error("boom");
|
|
90
|
+
wrapped.onResponseError?.(controller, error);
|
|
91
|
+
expect(httpLogger.logError).toHaveBeenCalledWith(error);
|
|
92
|
+
expect(httpLogger.logResponse).not.toHaveBeenCalled();
|
|
93
|
+
expect(inner.onResponseError).toHaveBeenCalledWith(controller, error);
|
|
94
|
+
});
|
|
95
|
+
test("keeps status per handler instance", () => {
|
|
96
|
+
const httpLogger = mockHttpLogger();
|
|
97
|
+
const first = intercept(httpLogger, new InnerHandler());
|
|
98
|
+
const second = intercept(httpLogger, new InnerHandler());
|
|
99
|
+
first.onResponseStart?.(controller, 200, {});
|
|
100
|
+
second.onResponseStart?.(controller, 500, {});
|
|
101
|
+
first.onResponseData?.(controller, Buffer.from("a"));
|
|
102
|
+
second.onResponseData?.(controller, Buffer.from("bb"));
|
|
103
|
+
first.onResponseEnd?.(controller, {});
|
|
104
|
+
second.onResponseEnd?.(controller, {});
|
|
105
|
+
expect(httpLogger.logResponse).toHaveBeenNthCalledWith(1, 200, 1);
|
|
106
|
+
expect(httpLogger.logResponse).toHaveBeenNthCalledWith(2, 500, 2);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
describe("createKyHooks", () => {
|
|
110
|
+
function hook(name) {
|
|
111
|
+
const httpLogger = mockHttpLogger();
|
|
112
|
+
const hooks = createKyHooks(asHttpLogger(httpLogger));
|
|
113
|
+
const fn = hooks[name]?.[0];
|
|
114
|
+
return { httpLogger, fn };
|
|
115
|
+
}
|
|
116
|
+
test.each(["beforeRequest", "beforeRetry"])("%s starts the timer and returns the request", (name) => {
|
|
117
|
+
const { httpLogger, fn } = hook(name);
|
|
118
|
+
const request = new Request("https://example.com");
|
|
119
|
+
expect(fn({ request })).toBe(request);
|
|
120
|
+
expect(httpLogger.start).toHaveBeenCalledTimes(1);
|
|
121
|
+
});
|
|
122
|
+
test("afterResponse uses content-length header without reading the body", async () => {
|
|
123
|
+
const { httpLogger, fn } = hook("afterResponse");
|
|
124
|
+
const response = new Response("hello", {
|
|
125
|
+
status: 200,
|
|
126
|
+
headers: { "content-length": "5" },
|
|
127
|
+
});
|
|
128
|
+
const cloneSpy = vi.spyOn(response, "clone");
|
|
129
|
+
const result = await fn({ response });
|
|
130
|
+
expect(result).toBe(response);
|
|
131
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(200, 5);
|
|
132
|
+
expect(cloneSpy).not.toHaveBeenCalled();
|
|
133
|
+
});
|
|
134
|
+
test("afterResponse measures body length when header is missing and keeps body readable", async () => {
|
|
135
|
+
const { httpLogger, fn } = hook("afterResponse");
|
|
136
|
+
const response = new Response("hello", { status: 201 });
|
|
137
|
+
const result = (await fn({ response }));
|
|
138
|
+
expect(httpLogger.logResponse).toHaveBeenCalledWith(201, 5);
|
|
139
|
+
expect(await result.text()).toBe("hello");
|
|
140
|
+
});
|
|
141
|
+
test("beforeError logs and returns non-HTTP errors", () => {
|
|
142
|
+
const { httpLogger, fn } = hook("beforeError");
|
|
143
|
+
const error = new Error("failed");
|
|
144
|
+
expect(fn({ error })).toBe(error);
|
|
145
|
+
expect(httpLogger.logError).toHaveBeenCalledWith(error);
|
|
146
|
+
});
|
|
147
|
+
test("beforeError does not log HTTPError again after afterResponse logged it", () => {
|
|
148
|
+
const { httpLogger, fn } = hook("beforeError");
|
|
149
|
+
const error = new HTTPError(new Response(null, { status: 500 }), new Request("https://example.com"), {});
|
|
150
|
+
expect(fn({ error })).toBe(error);
|
|
151
|
+
expect(httpLogger.logError).not.toHaveBeenCalled();
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
//# sourceMappingURL=http-interceptors.test.js.map
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { EventEmitter } from "node:events";
|
|
2
1
|
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
3
2
|
import { logger } from "../../aws/runtime/logging/dt-logger-default.js";
|
|
4
3
|
import { HttpLogger, LoggerFactory, } from "../../aws/runtime/logging/http-logger.js";
|
|
@@ -26,19 +25,61 @@ describe("HttpLogger", () => {
|
|
|
26
25
|
urlTemplate: "https://example.test/visits/{id}",
|
|
27
26
|
});
|
|
28
27
|
vi.advanceTimersByTime(1250);
|
|
29
|
-
httpLogger.
|
|
28
|
+
httpLogger.logResponse(200, 512);
|
|
30
29
|
expect(infoSpy).toHaveBeenCalledWith({
|
|
31
30
|
method: "HttpLogger.logStatus",
|
|
32
|
-
[WIDE_CONTEXT.CONTEXT]: "
|
|
31
|
+
[WIDE_CONTEXT.LOGGING.CONTEXT]: "port_call",
|
|
33
32
|
[WIDE_CONTEXT.TRACE_ID]: "trace-123",
|
|
34
33
|
[WIDE_CONTEXT.HTTP_KEYS.URL]: "https://example.test/visits/1",
|
|
35
|
-
[WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: "
|
|
34
|
+
[WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: "/visits/{id}",
|
|
36
35
|
[WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: undefined,
|
|
37
36
|
[WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]: 200,
|
|
38
37
|
[WIDE_CONTEXT.HTTP_KEYS.DURATION]: 1.25,
|
|
39
38
|
[WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]: 512,
|
|
40
39
|
});
|
|
41
40
|
});
|
|
41
|
+
test.each([
|
|
42
|
+
["https://example.test/visits/{id}", "/visits/{id}"],
|
|
43
|
+
["http://example.test:8080/visits/{id}?a=1", "/visits/{id}?a=1"],
|
|
44
|
+
["HTTPS://example.test/visits/{id}", "/visits/{id}"],
|
|
45
|
+
["https://example.test", ""],
|
|
46
|
+
["https://example.test?a=1", "?a=1"],
|
|
47
|
+
["/visits/{id}", "/visits/{id}"],
|
|
48
|
+
["/redirect/https://other.test/{id}", "/redirect/https://other.test/{id}"],
|
|
49
|
+
])("strips scheme and host from url template %s", (template, expected) => {
|
|
50
|
+
const infoSpy = vi
|
|
51
|
+
.spyOn(logger, "info")
|
|
52
|
+
.mockImplementation(() => undefined);
|
|
53
|
+
new HttpLogger({
|
|
54
|
+
context: "Test",
|
|
55
|
+
url: "https://example.test/visits/1",
|
|
56
|
+
urlTemplate: template,
|
|
57
|
+
}).logResponse(200);
|
|
58
|
+
expect(infoSpy).toHaveBeenCalledWith(expect.objectContaining({
|
|
59
|
+
[WIDE_CONTEXT.HTTP_KEYS.URL]: "https://example.test/visits/1",
|
|
60
|
+
[WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: expected,
|
|
61
|
+
}));
|
|
62
|
+
});
|
|
63
|
+
test("strips host from factory and per-request url templates", () => {
|
|
64
|
+
const infoSpy = vi
|
|
65
|
+
.spyOn(logger, "info")
|
|
66
|
+
.mockImplementation(() => undefined);
|
|
67
|
+
infoSpy.mockClear();
|
|
68
|
+
const factory = new LoggerFactory({
|
|
69
|
+
context: "Test",
|
|
70
|
+
urlTemplate: "https://example.test/default/{id}",
|
|
71
|
+
});
|
|
72
|
+
factory.logger("https://example.test/default/1").logResponse(200);
|
|
73
|
+
factory
|
|
74
|
+
.logger("https://example.test/other/1", "https://example.test/other/{id}")
|
|
75
|
+
.logResponse(200);
|
|
76
|
+
expect(infoSpy).toHaveBeenNthCalledWith(1, expect.objectContaining({
|
|
77
|
+
[WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: "/default/{id}",
|
|
78
|
+
}));
|
|
79
|
+
expect(infoSpy).toHaveBeenNthCalledWith(2, expect.objectContaining({
|
|
80
|
+
[WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: "/other/{id}",
|
|
81
|
+
}));
|
|
82
|
+
});
|
|
42
83
|
test("logs error name and keeps status code unset when request fails", () => {
|
|
43
84
|
const infoSpy = vi
|
|
44
85
|
.spyOn(logger, "info")
|
|
@@ -47,12 +88,12 @@ describe("HttpLogger", () => {
|
|
|
47
88
|
context: "Test",
|
|
48
89
|
url: "https://example.test",
|
|
49
90
|
});
|
|
50
|
-
httpLogger.
|
|
91
|
+
httpLogger.logError(new TypeError("request failed"));
|
|
51
92
|
expect(infoSpy).toHaveBeenCalledWith(expect.objectContaining({
|
|
52
93
|
method: "HttpLogger.logStatus",
|
|
53
|
-
[WIDE_CONTEXT.CONTEXT]: "
|
|
94
|
+
[WIDE_CONTEXT.LOGGING.CONTEXT]: "test",
|
|
54
95
|
[WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: "TypeError",
|
|
55
|
-
[WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]:
|
|
96
|
+
[WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]: undefined,
|
|
56
97
|
[WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]: undefined,
|
|
57
98
|
}));
|
|
58
99
|
});
|
|
@@ -67,28 +108,10 @@ describe("HttpLogger", () => {
|
|
|
67
108
|
context: "Test",
|
|
68
109
|
url: "https://example.test",
|
|
69
110
|
});
|
|
70
|
-
httpLogger.
|
|
111
|
+
httpLogger.logError(error);
|
|
71
112
|
expect(infoSpy).toHaveBeenCalledWith(expect.objectContaining({
|
|
72
113
|
[WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: typeof error === "string" ? error : "Unknown",
|
|
73
114
|
}));
|
|
74
115
|
});
|
|
75
|
-
test("logs response status and content length from header", () => {
|
|
76
|
-
const infoSpy = vi
|
|
77
|
-
.spyOn(logger, "info")
|
|
78
|
-
.mockImplementation(() => undefined);
|
|
79
|
-
const body = new EventEmitter();
|
|
80
|
-
const response = {
|
|
81
|
-
statusCode: 204,
|
|
82
|
-
headers: { "content-length": "5" },
|
|
83
|
-
body,
|
|
84
|
-
};
|
|
85
|
-
const httpLogger = new LoggerFactory({ context: "Test" }).logger("https://example.test");
|
|
86
|
-
httpLogger.log(response);
|
|
87
|
-
body.emit("end");
|
|
88
|
-
expect(infoSpy).toHaveBeenCalledWith(expect.objectContaining({
|
|
89
|
-
[WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]: 204,
|
|
90
|
-
[WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]: 5,
|
|
91
|
-
}));
|
|
92
|
-
});
|
|
93
116
|
});
|
|
94
117
|
//# sourceMappingURL=http-logger.test.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Hooks } from "ky";
|
|
2
|
+
import type { Dispatcher } from "undici";
|
|
3
|
+
import type { HttpLogger } from "./http-logger.js";
|
|
4
|
+
/**
|
|
5
|
+
* Create an Undici interceptor that logs HTTP requests and responses using the provided HttpLogger.
|
|
6
|
+
*/
|
|
7
|
+
export declare function createLoggingInterceptor(httpLogger: HttpLogger): Dispatcher.DispatcherComposeInterceptor;
|
|
8
|
+
/** Create hooks for Ky HTTP client using the provided logger */
|
|
9
|
+
export declare function createKyHooks(httpLogger: HttpLogger): Hooks;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { HTTPError } from "ky";
|
|
2
|
+
import { DecoratorHandler } from "undici";
|
|
3
|
+
// undici's DecoratorHandler typings omit the methods it implements at runtime
|
|
4
|
+
const TypedDecoratorHandler = DecoratorHandler;
|
|
5
|
+
function parseContentLengthFromHeader(headerValue) {
|
|
6
|
+
const value = Array.isArray(headerValue) ? headerValue[0] : headerValue;
|
|
7
|
+
const length = value ? parseInt(value, 10) : Number.NaN;
|
|
8
|
+
return Number.isNaN(length) ? undefined : length;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Create an Undici interceptor that logs HTTP requests and responses using the provided HttpLogger.
|
|
12
|
+
*/
|
|
13
|
+
export function createLoggingInterceptor(httpLogger) {
|
|
14
|
+
class LoggingHandler extends TypedDecoratorHandler {
|
|
15
|
+
_statusCode;
|
|
16
|
+
_receivedBytes = 0;
|
|
17
|
+
onRequestStart(controller, context) {
|
|
18
|
+
httpLogger.start();
|
|
19
|
+
super.onRequestStart?.(controller, context);
|
|
20
|
+
}
|
|
21
|
+
onResponseStart(controller, statusCode, headers, statusMessage) {
|
|
22
|
+
this._statusCode = statusCode;
|
|
23
|
+
super.onResponseStart?.(controller, statusCode, headers, statusMessage);
|
|
24
|
+
}
|
|
25
|
+
onResponseData(controller, chunk) {
|
|
26
|
+
this._receivedBytes += chunk.length;
|
|
27
|
+
super.onResponseData?.(controller, chunk);
|
|
28
|
+
}
|
|
29
|
+
onResponseEnd(controller, trailers) {
|
|
30
|
+
if (this._statusCode !== undefined) {
|
|
31
|
+
httpLogger.logResponse(this._statusCode, this._receivedBytes);
|
|
32
|
+
}
|
|
33
|
+
super.onResponseEnd?.(controller, trailers);
|
|
34
|
+
}
|
|
35
|
+
onResponseError(controller, error) {
|
|
36
|
+
httpLogger.logError(error);
|
|
37
|
+
super.onResponseError?.(controller, error);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return (dispatch) => {
|
|
41
|
+
return (options, handler) => {
|
|
42
|
+
return dispatch(options, new LoggingHandler(handler));
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Create hooks for Ky HTTP client using the provided logger */
|
|
47
|
+
export function createKyHooks(httpLogger) {
|
|
48
|
+
return {
|
|
49
|
+
beforeRequest: [
|
|
50
|
+
(state) => {
|
|
51
|
+
httpLogger.start();
|
|
52
|
+
return state.request;
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
beforeRetry: [
|
|
56
|
+
(state) => {
|
|
57
|
+
httpLogger.start();
|
|
58
|
+
return state.request;
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
afterResponse: [
|
|
62
|
+
async (state) => {
|
|
63
|
+
const contentLengthFromHeader = parseContentLengthFromHeader(state.response.headers.get("content-length"));
|
|
64
|
+
// use header when available to determine content length
|
|
65
|
+
if (contentLengthFromHeader !== undefined) {
|
|
66
|
+
httpLogger.logResponse(state.response.status, contentLengthFromHeader);
|
|
67
|
+
return state.response;
|
|
68
|
+
}
|
|
69
|
+
// create clone from the response, so we can read its body without affecting the original response
|
|
70
|
+
const resp = state.response.clone();
|
|
71
|
+
const contentLength = (await resp.bytes()).length;
|
|
72
|
+
httpLogger.logResponse(resp.status, contentLength);
|
|
73
|
+
return state.response;
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
beforeError: [
|
|
77
|
+
(state) => {
|
|
78
|
+
// afterResponse has already logged the HTTP status of this attempt
|
|
79
|
+
if (!(state.error instanceof HTTPError)) {
|
|
80
|
+
httpLogger.logError(state.error);
|
|
81
|
+
}
|
|
82
|
+
return state.error;
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=http-interceptors.js.map
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import type { Hooks } from "ky";
|
|
2
|
-
import type { Dispatcher } from "undici";
|
|
3
1
|
/**
|
|
4
2
|
* Creates request loggers that share integration-level context.
|
|
5
3
|
*
|
|
@@ -20,7 +18,10 @@ export declare class LoggerFactory {
|
|
|
20
18
|
logger(url: string, urlTemplate?: string): HttpLogger;
|
|
21
19
|
}
|
|
22
20
|
/**
|
|
23
|
-
* Logs the outcome and duration of one
|
|
21
|
+
* Logs the outcome and duration of HTTP requests. Usually one, but in case of retries there may be multiple logs for the same logical request.
|
|
22
|
+
*
|
|
23
|
+
* Given context will be reduced to snake_case.
|
|
24
|
+
* Given URL templates will have their scheme and host stripped.
|
|
24
25
|
*
|
|
25
26
|
* Call {@link end} when a response status is available. Call {@link error}
|
|
26
27
|
* when the request fails before a response is received. Each terminal method
|
|
@@ -31,30 +32,24 @@ export declare class HttpLogger {
|
|
|
31
32
|
private readonly _context;
|
|
32
33
|
private readonly _url;
|
|
33
34
|
private readonly _urlTemplate?;
|
|
34
|
-
private readonly _startTime;
|
|
35
35
|
private readonly _traceId?;
|
|
36
|
-
private
|
|
37
|
-
private _contentLength?;
|
|
38
|
-
private _statusCode;
|
|
36
|
+
private _startTime;
|
|
39
37
|
constructor(parameters: {
|
|
40
38
|
context: string;
|
|
41
39
|
url: string;
|
|
42
40
|
traceId?: string;
|
|
43
41
|
urlTemplate?: string;
|
|
44
42
|
});
|
|
43
|
+
start(): void;
|
|
45
44
|
/** Records a received HTTP response and writes the request log entry. */
|
|
46
|
-
|
|
47
|
-
/** Create hooks for Ky HTTP client using the provided logger */
|
|
48
|
-
createKyHooks(): Hooks;
|
|
49
|
-
/** Hook logging into undici response events */
|
|
50
|
-
log(response: Dispatcher.ResponseData<unknown>): void;
|
|
45
|
+
logResponse(statusCode: number, contentLength?: number): void;
|
|
51
46
|
/**
|
|
52
47
|
* Records a failed request and writes the request log entry.
|
|
53
48
|
*
|
|
54
49
|
* Errors are represented by their name, strings are kept as-is, and
|
|
55
50
|
* other values are recorded as `Unknown`. Since no response was received,
|
|
56
|
-
* the status code
|
|
51
|
+
* the status code is left undefined.
|
|
57
52
|
*/
|
|
58
|
-
|
|
53
|
+
logError(error: unknown): void;
|
|
59
54
|
private logStatus;
|
|
60
55
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { snakeCase } from "es-toolkit";
|
|
1
2
|
import { logger } from "./dt-logger-default.js";
|
|
2
3
|
import { WIDE_CONTEXT } from "./wide-context.js";
|
|
3
4
|
const METHOD = "HttpLogger.logStatus";
|
|
@@ -28,7 +29,10 @@ export class LoggerFactory {
|
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
/**
|
|
31
|
-
* Logs the outcome and duration of one
|
|
32
|
+
* Logs the outcome and duration of HTTP requests. Usually one, but in case of retries there may be multiple logs for the same logical request.
|
|
33
|
+
*
|
|
34
|
+
* Given context will be reduced to snake_case.
|
|
35
|
+
* Given URL templates will have their scheme and host stripped.
|
|
32
36
|
*
|
|
33
37
|
* Call {@link end} when a response status is available. Call {@link error}
|
|
34
38
|
* when the request fails before a response is received. Each terminal method
|
|
@@ -39,96 +43,58 @@ export class HttpLogger {
|
|
|
39
43
|
_context;
|
|
40
44
|
_url;
|
|
41
45
|
_urlTemplate;
|
|
42
|
-
_startTime;
|
|
43
46
|
_traceId;
|
|
44
|
-
|
|
45
|
-
_contentLength;
|
|
46
|
-
_statusCode = -1;
|
|
47
|
+
_startTime;
|
|
47
48
|
constructor(parameters) {
|
|
48
|
-
this._context = parameters.context;
|
|
49
|
+
this._context = snakeCase(parameters.context);
|
|
49
50
|
this._url = parameters.url;
|
|
50
51
|
this._traceId = parameters.traceId;
|
|
51
|
-
this._urlTemplate = parameters.urlTemplate;
|
|
52
|
+
this._urlTemplate = parameters.urlTemplate?.replace(/^https?:\/\/[^/?#]+/i, "");
|
|
52
53
|
this._startTime = Date.now();
|
|
53
54
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
this._statusCode = statusCode;
|
|
57
|
-
this._contentLength = contentLength;
|
|
58
|
-
this.logStatus();
|
|
59
|
-
}
|
|
60
|
-
/** Create hooks for Ky HTTP client using the provided logger */
|
|
61
|
-
createKyHooks() {
|
|
62
|
-
return {
|
|
63
|
-
afterResponse: [
|
|
64
|
-
async (state) => {
|
|
65
|
-
const contentLengthHeader = state.response.headers.get("content-length");
|
|
66
|
-
// use header when available to determine content length
|
|
67
|
-
if (contentLengthHeader) {
|
|
68
|
-
const contentLength = parseInt(contentLengthHeader, 10);
|
|
69
|
-
this.end(state.response.status, contentLength);
|
|
70
|
-
return state.response;
|
|
71
|
-
}
|
|
72
|
-
// create clone from the response, so we can read its body without affecting the original response
|
|
73
|
-
const resp = state.response.clone();
|
|
74
|
-
const contentLength = (await resp.bytes()).length;
|
|
75
|
-
this.end(resp.status, contentLength);
|
|
76
|
-
return state.response;
|
|
77
|
-
},
|
|
78
|
-
],
|
|
79
|
-
beforeError: [
|
|
80
|
-
(state) => {
|
|
81
|
-
this.error(state.error);
|
|
82
|
-
return state.error;
|
|
83
|
-
},
|
|
84
|
-
],
|
|
85
|
-
};
|
|
55
|
+
start() {
|
|
56
|
+
this._startTime = Date.now();
|
|
86
57
|
}
|
|
87
|
-
/**
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const contentLength = contentLengthHeader
|
|
92
|
-
? parseInt(contentLengthHeader, 10)
|
|
93
|
-
: undefined;
|
|
94
|
-
response.body.on("error", (err) => {
|
|
95
|
-
this.error(err);
|
|
96
|
-
});
|
|
97
|
-
response.body.on("end", () => {
|
|
98
|
-
this.end(response.statusCode, contentLength);
|
|
99
|
-
});
|
|
58
|
+
/** Records a received HTTP response and writes the request log entry. */
|
|
59
|
+
logResponse(statusCode, contentLength) {
|
|
60
|
+
const endTime = Date.now();
|
|
61
|
+
this.logStatus({ endTime, statusCode, contentLength });
|
|
100
62
|
}
|
|
101
63
|
/**
|
|
102
64
|
* Records a failed request and writes the request log entry.
|
|
103
65
|
*
|
|
104
66
|
* Errors are represented by their name, strings are kept as-is, and
|
|
105
67
|
* other values are recorded as `Unknown`. Since no response was received,
|
|
106
|
-
* the status code
|
|
68
|
+
* the status code is left undefined.
|
|
107
69
|
*/
|
|
108
|
-
|
|
70
|
+
logError(error) {
|
|
71
|
+
let errorType;
|
|
109
72
|
if (typeof error === "string") {
|
|
110
|
-
|
|
73
|
+
errorType = error;
|
|
111
74
|
}
|
|
112
75
|
else if (error instanceof Error) {
|
|
113
|
-
|
|
76
|
+
errorType = error.name;
|
|
114
77
|
}
|
|
115
78
|
else {
|
|
116
|
-
|
|
79
|
+
errorType = "Unknown";
|
|
117
80
|
}
|
|
118
|
-
this.logStatus();
|
|
119
|
-
}
|
|
120
|
-
logStatus() {
|
|
121
81
|
const endTime = Date.now();
|
|
82
|
+
this.logStatus({
|
|
83
|
+
endTime,
|
|
84
|
+
errorType,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
logStatus(parameters) {
|
|
122
88
|
logger.info({
|
|
123
89
|
method: METHOD,
|
|
124
|
-
[WIDE_CONTEXT.CONTEXT]: this._context,
|
|
125
90
|
[WIDE_CONTEXT.TRACE_ID]: this._traceId,
|
|
91
|
+
[WIDE_CONTEXT.LOGGING.CONTEXT]: this._context,
|
|
126
92
|
[WIDE_CONTEXT.HTTP_KEYS.URL]: this._url,
|
|
127
93
|
[WIDE_CONTEXT.HTTP_KEYS.URL_TEMPLATE]: this._urlTemplate,
|
|
128
|
-
[WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]:
|
|
129
|
-
[WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]:
|
|
130
|
-
[WIDE_CONTEXT.HTTP_KEYS.DURATION]: (endTime - this._startTime) / 1000,
|
|
131
|
-
[WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]:
|
|
94
|
+
[WIDE_CONTEXT.HTTP_KEYS.ERROR_TYPE]: parameters.errorType,
|
|
95
|
+
[WIDE_CONTEXT.HTTP_KEYS.STATUS_CODE]: parameters.statusCode,
|
|
96
|
+
[WIDE_CONTEXT.HTTP_KEYS.DURATION]: (parameters.endTime - this._startTime) / 1000,
|
|
97
|
+
[WIDE_CONTEXT.HTTP_KEYS.CONTENT_LENGTH]: parameters.contentLength,
|
|
132
98
|
});
|
|
133
99
|
}
|
|
134
100
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export declare const WIDE_CONTEXT: {
|
|
2
2
|
readonly TRACE_ID: "customTrace_id";
|
|
3
|
-
readonly
|
|
3
|
+
readonly LOGGING: {
|
|
4
|
+
readonly CONTEXT: "customLogging.context";
|
|
5
|
+
};
|
|
4
6
|
readonly HTTP_KEYS: {
|
|
5
7
|
readonly DURATION: "customHttp.client.request.duration";
|
|
6
8
|
readonly ERROR_TYPE: "customRequest.error.type";
|
|
@@ -9,8 +9,8 @@ export declare const parameterObject: z.ZodObject<{
|
|
|
9
9
|
in: z.ZodEnum<{
|
|
10
10
|
path: "path";
|
|
11
11
|
header: "header";
|
|
12
|
-
query: "query";
|
|
13
12
|
cookie: "cookie";
|
|
13
|
+
query: "query";
|
|
14
14
|
}>;
|
|
15
15
|
description: z.ZodOptional<z.ZodString>;
|
|
16
16
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -48,8 +48,8 @@ export declare const openapiOperation: z.ZodObject<{
|
|
|
48
48
|
in: z.ZodEnum<{
|
|
49
49
|
path: "path";
|
|
50
50
|
header: "header";
|
|
51
|
-
query: "query";
|
|
52
51
|
cookie: "cookie";
|
|
52
|
+
query: "query";
|
|
53
53
|
}>;
|
|
54
54
|
description: z.ZodOptional<z.ZodString>;
|
|
55
55
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -96,8 +96,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
96
96
|
in: z.ZodEnum<{
|
|
97
97
|
path: "path";
|
|
98
98
|
header: "header";
|
|
99
|
-
query: "query";
|
|
100
99
|
cookie: "cookie";
|
|
100
|
+
query: "query";
|
|
101
101
|
}>;
|
|
102
102
|
description: z.ZodOptional<z.ZodString>;
|
|
103
103
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -141,8 +141,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
141
141
|
in: z.ZodEnum<{
|
|
142
142
|
path: "path";
|
|
143
143
|
header: "header";
|
|
144
|
-
query: "query";
|
|
145
144
|
cookie: "cookie";
|
|
145
|
+
query: "query";
|
|
146
146
|
}>;
|
|
147
147
|
description: z.ZodOptional<z.ZodString>;
|
|
148
148
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -186,8 +186,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
186
186
|
in: z.ZodEnum<{
|
|
187
187
|
path: "path";
|
|
188
188
|
header: "header";
|
|
189
|
-
query: "query";
|
|
190
189
|
cookie: "cookie";
|
|
190
|
+
query: "query";
|
|
191
191
|
}>;
|
|
192
192
|
description: z.ZodOptional<z.ZodString>;
|
|
193
193
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -231,8 +231,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
231
231
|
in: z.ZodEnum<{
|
|
232
232
|
path: "path";
|
|
233
233
|
header: "header";
|
|
234
|
-
query: "query";
|
|
235
234
|
cookie: "cookie";
|
|
235
|
+
query: "query";
|
|
236
236
|
}>;
|
|
237
237
|
description: z.ZodOptional<z.ZodString>;
|
|
238
238
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -276,8 +276,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
276
276
|
in: z.ZodEnum<{
|
|
277
277
|
path: "path";
|
|
278
278
|
header: "header";
|
|
279
|
-
query: "query";
|
|
280
279
|
cookie: "cookie";
|
|
280
|
+
query: "query";
|
|
281
281
|
}>;
|
|
282
282
|
description: z.ZodOptional<z.ZodString>;
|
|
283
283
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -321,8 +321,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
321
321
|
in: z.ZodEnum<{
|
|
322
322
|
path: "path";
|
|
323
323
|
header: "header";
|
|
324
|
-
query: "query";
|
|
325
324
|
cookie: "cookie";
|
|
325
|
+
query: "query";
|
|
326
326
|
}>;
|
|
327
327
|
description: z.ZodOptional<z.ZodString>;
|
|
328
328
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -366,8 +366,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
366
366
|
in: z.ZodEnum<{
|
|
367
367
|
path: "path";
|
|
368
368
|
header: "header";
|
|
369
|
-
query: "query";
|
|
370
369
|
cookie: "cookie";
|
|
370
|
+
query: "query";
|
|
371
371
|
}>;
|
|
372
372
|
description: z.ZodOptional<z.ZodString>;
|
|
373
373
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -411,8 +411,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
411
411
|
in: z.ZodEnum<{
|
|
412
412
|
path: "path";
|
|
413
413
|
header: "header";
|
|
414
|
-
query: "query";
|
|
415
414
|
cookie: "cookie";
|
|
415
|
+
query: "query";
|
|
416
416
|
}>;
|
|
417
417
|
description: z.ZodOptional<z.ZodString>;
|
|
418
418
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -455,8 +455,8 @@ export declare const openapiPathItem: z.ZodObject<{
|
|
|
455
455
|
in: z.ZodEnum<{
|
|
456
456
|
path: "path";
|
|
457
457
|
header: "header";
|
|
458
|
-
query: "query";
|
|
459
458
|
cookie: "cookie";
|
|
459
|
+
query: "query";
|
|
460
460
|
}>;
|
|
461
461
|
description: z.ZodOptional<z.ZodString>;
|
|
462
462
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -532,8 +532,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
532
532
|
in: z.ZodEnum<{
|
|
533
533
|
path: "path";
|
|
534
534
|
header: "header";
|
|
535
|
-
query: "query";
|
|
536
535
|
cookie: "cookie";
|
|
536
|
+
query: "query";
|
|
537
537
|
}>;
|
|
538
538
|
description: z.ZodOptional<z.ZodString>;
|
|
539
539
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -577,8 +577,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
577
577
|
in: z.ZodEnum<{
|
|
578
578
|
path: "path";
|
|
579
579
|
header: "header";
|
|
580
|
-
query: "query";
|
|
581
580
|
cookie: "cookie";
|
|
581
|
+
query: "query";
|
|
582
582
|
}>;
|
|
583
583
|
description: z.ZodOptional<z.ZodString>;
|
|
584
584
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -622,8 +622,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
622
622
|
in: z.ZodEnum<{
|
|
623
623
|
path: "path";
|
|
624
624
|
header: "header";
|
|
625
|
-
query: "query";
|
|
626
625
|
cookie: "cookie";
|
|
626
|
+
query: "query";
|
|
627
627
|
}>;
|
|
628
628
|
description: z.ZodOptional<z.ZodString>;
|
|
629
629
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -667,8 +667,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
667
667
|
in: z.ZodEnum<{
|
|
668
668
|
path: "path";
|
|
669
669
|
header: "header";
|
|
670
|
-
query: "query";
|
|
671
670
|
cookie: "cookie";
|
|
671
|
+
query: "query";
|
|
672
672
|
}>;
|
|
673
673
|
description: z.ZodOptional<z.ZodString>;
|
|
674
674
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -712,8 +712,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
712
712
|
in: z.ZodEnum<{
|
|
713
713
|
path: "path";
|
|
714
714
|
header: "header";
|
|
715
|
-
query: "query";
|
|
716
715
|
cookie: "cookie";
|
|
716
|
+
query: "query";
|
|
717
717
|
}>;
|
|
718
718
|
description: z.ZodOptional<z.ZodString>;
|
|
719
719
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -757,8 +757,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
757
757
|
in: z.ZodEnum<{
|
|
758
758
|
path: "path";
|
|
759
759
|
header: "header";
|
|
760
|
-
query: "query";
|
|
761
760
|
cookie: "cookie";
|
|
761
|
+
query: "query";
|
|
762
762
|
}>;
|
|
763
763
|
description: z.ZodOptional<z.ZodString>;
|
|
764
764
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -802,8 +802,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
802
802
|
in: z.ZodEnum<{
|
|
803
803
|
path: "path";
|
|
804
804
|
header: "header";
|
|
805
|
-
query: "query";
|
|
806
805
|
cookie: "cookie";
|
|
806
|
+
query: "query";
|
|
807
807
|
}>;
|
|
808
808
|
description: z.ZodOptional<z.ZodString>;
|
|
809
809
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -847,8 +847,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
847
847
|
in: z.ZodEnum<{
|
|
848
848
|
path: "path";
|
|
849
849
|
header: "header";
|
|
850
|
-
query: "query";
|
|
851
850
|
cookie: "cookie";
|
|
851
|
+
query: "query";
|
|
852
852
|
}>;
|
|
853
853
|
description: z.ZodOptional<z.ZodString>;
|
|
854
854
|
required: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -891,8 +891,8 @@ export declare const openapiSchema: z.ZodObject<{
|
|
|
891
891
|
in: z.ZodEnum<{
|
|
892
892
|
path: "path";
|
|
893
893
|
header: "header";
|
|
894
|
-
query: "query";
|
|
895
894
|
cookie: "cookie";
|
|
895
|
+
query: "query";
|
|
896
896
|
}>;
|
|
897
897
|
description: z.ZodOptional<z.ZodString>;
|
|
898
898
|
required: z.ZodOptional<z.ZodBoolean>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@digitraffic/common",
|
|
3
|
-
"version": "2026.10.
|
|
3
|
+
"version": "2026.10.30-1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "",
|
|
6
6
|
"repository": {
|
|
@@ -96,6 +96,7 @@
|
|
|
96
96
|
"./dist/aws/runtime/logging/dt-logger": "./dist/aws/runtime/logging/dt-logger.js",
|
|
97
97
|
"./dist/aws/runtime/logging/http-logger": "./dist/aws/runtime/logging/http-logger.js",
|
|
98
98
|
"./dist/aws/runtime/logging/wide-context": "./dist/aws/runtime/logging/wide-context.js",
|
|
99
|
+
"./dist/aws/runtime/logging/http-interceptors": "./dist/aws/runtime/logging/http-interceptors.js",
|
|
99
100
|
"./dist/aws/runtime/secrets/secret": "./dist/aws/runtime/secrets/secret.js",
|
|
100
101
|
"./dist/aws/runtime/secrets/proxy-holder": "./dist/aws/runtime/secrets/proxy-holder.js",
|
|
101
102
|
"./dist/aws/runtime/secrets/dbsecret": "./dist/aws/runtime/secrets/dbsecret.js",
|