autotel-adapters 2.1.1 → 3.0.0
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/README.md +21 -0
- package/dist/cloudflare.cjs +3 -9
- package/dist/cloudflare.d.cts +1 -1
- package/dist/cloudflare.d.ts +1 -1
- package/dist/cloudflare.js +3 -9
- package/dist/core.cjs +4 -2
- package/dist/core.d.cts +1 -1
- package/dist/core.d.ts +1 -1
- package/dist/core.js +4 -2
- package/dist/elysia.cjs +4 -2
- package/dist/elysia.d.cts +3 -1
- package/dist/elysia.d.ts +3 -1
- package/dist/elysia.js +4 -2
- package/dist/express.cjs +16 -4
- package/dist/express.d.cts +1 -1
- package/dist/express.d.ts +1 -1
- package/dist/express.js +16 -4
- package/dist/fastify.cjs +6 -4
- package/dist/fastify.d.cts +1 -1
- package/dist/fastify.d.ts +1 -1
- package/dist/fastify.js +6 -4
- package/dist/hono.cjs +17 -3
- package/dist/hono.d.cts +1 -1
- package/dist/hono.d.ts +1 -1
- package/dist/hono.js +17 -3
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/{integration-CQMpu5E4.d.ts → integration-B6OoKr7O.d.ts} +23 -3
- package/dist/{integration-D9g_qI5y.d.cts → integration-CwqWnPKn.d.cts} +23 -3
- package/dist/{integration-jKj-jgtX.js → integration-D_ABolGu.js} +64 -16
- package/dist/{integration-rv0DU916.cjs → integration-Danzss1R.cjs} +74 -14
- package/dist/nestjs.cjs +13 -15
- package/dist/nestjs.d.cts +9 -1
- package/dist/nestjs.d.ts +9 -1
- package/dist/nestjs.js +13 -15
- package/dist/next.cjs +3 -9
- package/dist/next.d.cts +1 -1
- package/dist/next.d.ts +1 -1
- package/dist/next.js +3 -9
- package/dist/nitro.cjs +9 -3
- package/dist/nitro.d.cts +9 -2
- package/dist/nitro.d.ts +9 -2
- package/dist/nitro.js +9 -3
- package/dist/sveltekit.cjs +4 -2
- package/dist/sveltekit.d.cts +1 -1
- package/dist/sveltekit.d.ts +1 -1
- package/dist/sveltekit.js +4 -2
- package/dist/tanstack.cjs +2 -1
- package/dist/tanstack.d.cts +2 -0
- package/dist/tanstack.d.ts +2 -0
- package/dist/tanstack.js +2 -1
- package/dist/toolkit/index.cjs +1 -1
- package/dist/toolkit/index.d.cts +1 -1
- package/dist/toolkit/index.d.ts +1 -1
- package/dist/toolkit/index.js +1 -1
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ForkLifecycle, ForkLifecycle as ForkLifecycle$1, RequestLogSnapshot, RequestLogger, RequestLoggerOptions, TraceContext } from "autotel";
|
|
1
|
+
import { ForkLifecycle, ForkLifecycle as ForkLifecycle$1, RequestLogSnapshot, RequestLogger, RequestLoggerOptions, TraceContext, recordStructuredError } from "autotel";
|
|
2
2
|
import { RouteFilterOptions, RouteServiceConfig, getServiceForPath, matchesRoutePattern, shouldInstrumentPath } from "autotel-edge";
|
|
3
3
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
4
|
//#region src/toolkit/middleware.d.ts
|
|
@@ -33,8 +33,20 @@ interface MiddlewareLoggerOptions extends RouteAdapterOptions {
|
|
|
33
33
|
path: string;
|
|
34
34
|
requestId?: string;
|
|
35
35
|
}
|
|
36
|
+
/** How a request ended, as far as the span is concerned. */
|
|
37
|
+
interface RequestOutcome {
|
|
38
|
+
status?: number;
|
|
39
|
+
error?: Error;
|
|
40
|
+
}
|
|
36
41
|
interface MiddlewareLoggerResult {
|
|
37
42
|
logger: RequestLogger;
|
|
43
|
+
/**
|
|
44
|
+
* Record the outcome on the span now: the status code, ERROR for a 5xx, and
|
|
45
|
+
* the error as its exception. Separate from emitting the snapshot, so it
|
|
46
|
+
* happens with `autoEmit: false` too, and before a streamed body (whose
|
|
47
|
+
* snapshot waits for the stream) outlives the span.
|
|
48
|
+
*/
|
|
49
|
+
markOutcome: (outcome: RequestOutcome) => void;
|
|
38
50
|
finish: (opts?: {
|
|
39
51
|
status?: number;
|
|
40
52
|
error?: Error;
|
|
@@ -42,21 +54,27 @@ interface MiddlewareLoggerResult {
|
|
|
42
54
|
}) => Promise<RequestLogSnapshot | null>;
|
|
43
55
|
finishResponse: (response: Response, opts?: {
|
|
44
56
|
status?: number;
|
|
57
|
+
/** What the framework caught on the way to this response, if anything. */
|
|
58
|
+
error?: Error;
|
|
45
59
|
overrides?: Record<string, unknown>;
|
|
46
60
|
}) => Promise<Response>;
|
|
47
61
|
skipped: boolean;
|
|
48
62
|
}
|
|
49
63
|
declare function mergeRequestLoggerOptions(base: RequestLoggerOptions | undefined, waitUntil?: (promise: Promise<unknown>) => void): RequestLoggerOptions | undefined;
|
|
64
|
+
/** The request span, for the status a log line cannot set. */
|
|
65
|
+
type RequestSpan = Parameters<typeof recordStructuredError>[0];
|
|
50
66
|
/**
|
|
51
67
|
* Create a request logger handle for use inside an active autotel trace span.
|
|
52
68
|
* Call `finish()` or `finishResponse()` when the request completes.
|
|
53
69
|
*/
|
|
54
|
-
declare function createMiddlewareLogger(logger: RequestLogger, options: MiddlewareLoggerOptions): MiddlewareLoggerResult;
|
|
70
|
+
declare function createMiddlewareLogger(logger: RequestLogger, options: MiddlewareLoggerOptions, span?: RequestSpan): MiddlewareLoggerResult;
|
|
55
71
|
//#endregion
|
|
56
72
|
//#region src/toolkit/integration.d.ts
|
|
57
73
|
interface ExtractedRequest {
|
|
58
74
|
method: string;
|
|
59
75
|
path: string;
|
|
76
|
+
/** Matched route template (`/users/:id`), never the raw path. Omit when unknown. */
|
|
77
|
+
route?: string;
|
|
60
78
|
headers?: Headers | Record<string, string | string[] | undefined> | {
|
|
61
79
|
get(name: string): string | null;
|
|
62
80
|
};
|
|
@@ -86,6 +104,8 @@ interface FrameworkIntegrationSpec<TCtx> {
|
|
|
86
104
|
interface FrameworkRequestHandle extends MiddlewareLoggerResult {
|
|
87
105
|
middlewareOptions: MiddlewareLoggerOptions;
|
|
88
106
|
runWith: <T>(fn: () => T | Promise<T>) => Promise<T>;
|
|
107
|
+
/** Record a route template only known after routing (sets `http.route`, renames a default-named span). */
|
|
108
|
+
setRoute: (route: string | undefined) => void;
|
|
89
109
|
}
|
|
90
110
|
interface FrameworkIntegrationHelpers<TCtx> {
|
|
91
111
|
begin: (traceCtx: TraceContext, ctx: TCtx, options?: FrameworkHandlerOptions) => FrameworkRequestHandle;
|
|
@@ -95,7 +115,7 @@ declare function applyLoggerEnrichment(logger: RequestLogger, ...attributeSets:
|
|
|
95
115
|
declare function completeIntegratedRequest<T>(handle: FrameworkRequestHandle, options: IntegratedCompletionOptions | undefined, result: T): Promise<T>;
|
|
96
116
|
declare function runWithIntegratedHandle<T>(handle: FrameworkRequestHandle, options: IntegratedCompletionOptions | undefined, fn: () => T | Promise<T>): Promise<T>;
|
|
97
117
|
declare function toRouteAdapterOptions(options?: Pick<FrameworkHandlerOptions, keyof RouteAdapterOptions | 'requestLoggerOptions'>): RouteAdapterOptions;
|
|
98
|
-
declare function buildTracedOptions(options: TracedOptionInput | undefined, spanName: string): FrameworkHandlerOptions;
|
|
118
|
+
declare function buildTracedOptions(options: TracedOptionInput | undefined, spanName: string | undefined): FrameworkHandlerOptions;
|
|
99
119
|
declare function defineFrameworkIntegration<TCtx>(spec: FrameworkIntegrationSpec<TCtx>): FrameworkIntegrationHelpers<TCtx>;
|
|
100
120
|
//#endregion
|
|
101
121
|
export { matchesRoutePattern as C, getServiceForPath as S, shouldInstrumentPath as T, RouteAdapterOptions as _, FrameworkRequestHandle as a, TailSamplingContext as b, buildTracedOptions as c, runWithIntegratedHandle as d, toRouteAdapterOptions as f, MiddlewareLoggerResult as g, MiddlewareLoggerOptions as h, FrameworkIntegrationSpec as i, completeIntegratedRequest as l, ForkLifecycle$1 as m, FrameworkHandlerOptions as n, IntegratedCompletionOptions as o, BaseAdapterOptions as p, FrameworkIntegrationHelpers as r, applyLoggerEnrichment as s, ExtractedRequest as t, defineFrameworkIntegration as u, RouteFilterOptions as v, mergeRequestLoggerOptions as w, createMiddlewareLogger as x, RouteServiceConfig as y };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ForkLifecycle, ForkLifecycle as ForkLifecycle$1, RequestLogSnapshot, RequestLogger, RequestLoggerOptions, TraceContext } from "autotel";
|
|
1
|
+
import { ForkLifecycle, ForkLifecycle as ForkLifecycle$1, RequestLogSnapshot, RequestLogger, RequestLoggerOptions, TraceContext, recordStructuredError } from "autotel";
|
|
2
2
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
3
|
import { RouteFilterOptions, RouteServiceConfig, getServiceForPath, matchesRoutePattern, shouldInstrumentPath } from "autotel-edge";
|
|
4
4
|
//#region src/toolkit/middleware.d.ts
|
|
@@ -33,8 +33,20 @@ interface MiddlewareLoggerOptions extends RouteAdapterOptions {
|
|
|
33
33
|
path: string;
|
|
34
34
|
requestId?: string;
|
|
35
35
|
}
|
|
36
|
+
/** How a request ended, as far as the span is concerned. */
|
|
37
|
+
interface RequestOutcome {
|
|
38
|
+
status?: number;
|
|
39
|
+
error?: Error;
|
|
40
|
+
}
|
|
36
41
|
interface MiddlewareLoggerResult {
|
|
37
42
|
logger: RequestLogger;
|
|
43
|
+
/**
|
|
44
|
+
* Record the outcome on the span now: the status code, ERROR for a 5xx, and
|
|
45
|
+
* the error as its exception. Separate from emitting the snapshot, so it
|
|
46
|
+
* happens with `autoEmit: false` too, and before a streamed body (whose
|
|
47
|
+
* snapshot waits for the stream) outlives the span.
|
|
48
|
+
*/
|
|
49
|
+
markOutcome: (outcome: RequestOutcome) => void;
|
|
38
50
|
finish: (opts?: {
|
|
39
51
|
status?: number;
|
|
40
52
|
error?: Error;
|
|
@@ -42,21 +54,27 @@ interface MiddlewareLoggerResult {
|
|
|
42
54
|
}) => Promise<RequestLogSnapshot | null>;
|
|
43
55
|
finishResponse: (response: Response, opts?: {
|
|
44
56
|
status?: number;
|
|
57
|
+
/** What the framework caught on the way to this response, if anything. */
|
|
58
|
+
error?: Error;
|
|
45
59
|
overrides?: Record<string, unknown>;
|
|
46
60
|
}) => Promise<Response>;
|
|
47
61
|
skipped: boolean;
|
|
48
62
|
}
|
|
49
63
|
declare function mergeRequestLoggerOptions(base: RequestLoggerOptions | undefined, waitUntil?: (promise: Promise<unknown>) => void): RequestLoggerOptions | undefined;
|
|
64
|
+
/** The request span, for the status a log line cannot set. */
|
|
65
|
+
type RequestSpan = Parameters<typeof recordStructuredError>[0];
|
|
50
66
|
/**
|
|
51
67
|
* Create a request logger handle for use inside an active autotel trace span.
|
|
52
68
|
* Call `finish()` or `finishResponse()` when the request completes.
|
|
53
69
|
*/
|
|
54
|
-
declare function createMiddlewareLogger(logger: RequestLogger, options: MiddlewareLoggerOptions): MiddlewareLoggerResult;
|
|
70
|
+
declare function createMiddlewareLogger(logger: RequestLogger, options: MiddlewareLoggerOptions, span?: RequestSpan): MiddlewareLoggerResult;
|
|
55
71
|
//#endregion
|
|
56
72
|
//#region src/toolkit/integration.d.ts
|
|
57
73
|
interface ExtractedRequest {
|
|
58
74
|
method: string;
|
|
59
75
|
path: string;
|
|
76
|
+
/** Matched route template (`/users/:id`), never the raw path. Omit when unknown. */
|
|
77
|
+
route?: string;
|
|
60
78
|
headers?: Headers | Record<string, string | string[] | undefined> | {
|
|
61
79
|
get(name: string): string | null;
|
|
62
80
|
};
|
|
@@ -86,6 +104,8 @@ interface FrameworkIntegrationSpec<TCtx> {
|
|
|
86
104
|
interface FrameworkRequestHandle extends MiddlewareLoggerResult {
|
|
87
105
|
middlewareOptions: MiddlewareLoggerOptions;
|
|
88
106
|
runWith: <T>(fn: () => T | Promise<T>) => Promise<T>;
|
|
107
|
+
/** Record a route template only known after routing (sets `http.route`, renames a default-named span). */
|
|
108
|
+
setRoute: (route: string | undefined) => void;
|
|
89
109
|
}
|
|
90
110
|
interface FrameworkIntegrationHelpers<TCtx> {
|
|
91
111
|
begin: (traceCtx: TraceContext, ctx: TCtx, options?: FrameworkHandlerOptions) => FrameworkRequestHandle;
|
|
@@ -95,7 +115,7 @@ declare function applyLoggerEnrichment(logger: RequestLogger, ...attributeSets:
|
|
|
95
115
|
declare function completeIntegratedRequest<T>(handle: FrameworkRequestHandle, options: IntegratedCompletionOptions | undefined, result: T): Promise<T>;
|
|
96
116
|
declare function runWithIntegratedHandle<T>(handle: FrameworkRequestHandle, options: IntegratedCompletionOptions | undefined, fn: () => T | Promise<T>): Promise<T>;
|
|
97
117
|
declare function toRouteAdapterOptions(options?: Pick<FrameworkHandlerOptions, keyof RouteAdapterOptions | 'requestLoggerOptions'>): RouteAdapterOptions;
|
|
98
|
-
declare function buildTracedOptions(options: TracedOptionInput | undefined, spanName: string): FrameworkHandlerOptions;
|
|
118
|
+
declare function buildTracedOptions(options: TracedOptionInput | undefined, spanName: string | undefined): FrameworkHandlerOptions;
|
|
99
119
|
declare function defineFrameworkIntegration<TCtx>(spec: FrameworkIntegrationSpec<TCtx>): FrameworkIntegrationHelpers<TCtx>;
|
|
100
120
|
//#endregion
|
|
101
121
|
export { matchesRoutePattern as C, getServiceForPath as S, shouldInstrumentPath as T, RouteAdapterOptions as _, FrameworkRequestHandle as a, TailSamplingContext as b, buildTracedOptions as c, runWithIntegratedHandle as d, toRouteAdapterOptions as f, MiddlewareLoggerResult as g, MiddlewareLoggerOptions as h, FrameworkIntegrationSpec as i, completeIntegratedRequest as l, ForkLifecycle$1 as m, FrameworkHandlerOptions as n, IntegratedCompletionOptions as o, BaseAdapterOptions as p, FrameworkIntegrationHelpers as r, applyLoggerEnrichment as s, ExtractedRequest as t, defineFrameworkIntegration as u, RouteFilterOptions as v, mergeRequestLoggerOptions as w, createMiddlewareLogger as x, RouteServiceConfig as y };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createStorageForkLifecycle } from "./toolkit/storage.js";
|
|
2
|
-
import { createNoopRequestLogger, getRequestLogger, trace } from "autotel";
|
|
2
|
+
import { SpanStatusCode, createNoopRequestLogger, getRequestLogger, recordStructuredError, trace } from "autotel";
|
|
3
3
|
import { getServiceForPath, matchesRoutePattern, shouldInstrumentPath } from "autotel-edge";
|
|
4
4
|
|
|
5
5
|
//#region src/toolkit/deferred-drain.ts
|
|
@@ -108,6 +108,7 @@ function bindStreamingResponseLifecycle(response, onComplete) {
|
|
|
108
108
|
//#region src/toolkit/middleware.ts
|
|
109
109
|
const noopResult = {
|
|
110
110
|
logger: createNoopRequestLogger(),
|
|
111
|
+
markOutcome: () => {},
|
|
111
112
|
finish: () => Promise.resolve(null),
|
|
112
113
|
finishResponse: (response) => Promise.resolve(response),
|
|
113
114
|
skipped: true
|
|
@@ -123,15 +124,31 @@ function mergeRequestLoggerOptions(base, waitUntil) {
|
|
|
123
124
|
}
|
|
124
125
|
};
|
|
125
126
|
}
|
|
126
|
-
|
|
127
|
-
|
|
127
|
+
/**
|
|
128
|
+
* A server span with a 5xx response is an error, whether or not anything was
|
|
129
|
+
* thrown (OpenTelemetry HTTP conventions); a 4xx is the client's and is left
|
|
130
|
+
* unset. Without this, error-based sampling and alerts miss every request
|
|
131
|
+
* that answered 500 without an exception reaching the adapter.
|
|
132
|
+
*/
|
|
133
|
+
function markServerError(span, status) {
|
|
134
|
+
if (status !== void 0 && status >= 500) span?.setStatus({ code: SpanStatusCode.ERROR });
|
|
135
|
+
}
|
|
136
|
+
function recordOutcome(logger, span, { status, error }, emitted) {
|
|
128
137
|
if (error) {
|
|
129
|
-
|
|
138
|
+
if (span && emitted()) recordStructuredError(span, error);
|
|
139
|
+
else logger.error(error);
|
|
130
140
|
logger.set({
|
|
131
141
|
"http.response.status_code": status ?? 500,
|
|
132
142
|
error_message: error.message
|
|
133
143
|
});
|
|
134
|
-
} else if (status !== void 0)
|
|
144
|
+
} else if (status !== void 0) {
|
|
145
|
+
logger.set({ "http.response.status_code": status });
|
|
146
|
+
markServerError(span, status);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async function performFinish(logger, options, startedAt, span, emitted, opts) {
|
|
150
|
+
const { status, overrides } = opts ?? {};
|
|
151
|
+
recordOutcome(logger, span, opts ?? {}, emitted);
|
|
135
152
|
const durationMs = Date.now() - startedAt;
|
|
136
153
|
logger.set({ duration_ms: durationMs });
|
|
137
154
|
const tailCtx = {
|
|
@@ -164,27 +181,40 @@ async function performFinish(logger, options, startedAt, opts) {
|
|
|
164
181
|
* Create a request logger handle for use inside an active autotel trace span.
|
|
165
182
|
* Call `finish()` or `finishResponse()` when the request completes.
|
|
166
183
|
*/
|
|
167
|
-
function createMiddlewareLogger(logger, options) {
|
|
184
|
+
function createMiddlewareLogger(logger, options, span) {
|
|
168
185
|
if (!shouldInstrumentPath(options.path, options)) return noopResult;
|
|
169
186
|
const routeService = getServiceForPath(options.path, options.routes);
|
|
170
187
|
if (routeService) logger.set({ service: routeService });
|
|
171
188
|
if (options.requestId) logger.set({ "http.request.id": options.requestId });
|
|
172
189
|
const startedAt = Date.now();
|
|
190
|
+
let hasEmitted = false;
|
|
191
|
+
const emitNow = logger.emitNow.bind(logger);
|
|
192
|
+
logger.emitNow = (overrides) => {
|
|
193
|
+
hasEmitted = true;
|
|
194
|
+
return emitNow(overrides);
|
|
195
|
+
};
|
|
196
|
+
const emitted = () => hasEmitted;
|
|
173
197
|
return {
|
|
174
198
|
logger,
|
|
175
199
|
skipped: false,
|
|
176
|
-
|
|
200
|
+
markOutcome: (outcome) => recordOutcome(logger, span, outcome, emitted),
|
|
201
|
+
finish: (opts) => performFinish(logger, options, startedAt, span, emitted, opts),
|
|
177
202
|
finishResponse: async (response, opts) => {
|
|
178
203
|
const status = opts?.status ?? response.status;
|
|
179
204
|
if (!shouldDeferEmitForResponse(response)) {
|
|
180
|
-
await performFinish(logger, options, startedAt, {
|
|
205
|
+
await performFinish(logger, options, startedAt, span, emitted, {
|
|
181
206
|
status,
|
|
207
|
+
error: opts?.error,
|
|
182
208
|
overrides: opts?.overrides
|
|
183
209
|
});
|
|
184
210
|
return response;
|
|
185
211
|
}
|
|
212
|
+
recordOutcome(logger, span, {
|
|
213
|
+
status,
|
|
214
|
+
error: opts?.error
|
|
215
|
+
}, emitted);
|
|
186
216
|
return bindStreamingResponseLifecycle(response, async (meta) => {
|
|
187
|
-
await performFinish(logger, options, startedAt, {
|
|
217
|
+
await performFinish(logger, options, startedAt, span, emitted, {
|
|
188
218
|
status: meta.status ?? status,
|
|
189
219
|
error: meta.error,
|
|
190
220
|
overrides: opts?.overrides
|
|
@@ -222,7 +252,11 @@ function applyLoggerEnrichment(logger, ...attributeSets) {
|
|
|
222
252
|
for (const attrs of attributeSets) if (attrs && Object.keys(attrs).length > 0) logger.set(attrs);
|
|
223
253
|
}
|
|
224
254
|
async function completeIntegratedRequest(handle, options, result) {
|
|
225
|
-
if (handle.skipped
|
|
255
|
+
if (handle.skipped) return result;
|
|
256
|
+
if (options?.autoEmit === false) {
|
|
257
|
+
if (result instanceof Response) handle.markOutcome({ status: result.status });
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
226
260
|
if (result instanceof Response) return handle.finishResponse(result);
|
|
227
261
|
await handle.finish({});
|
|
228
262
|
return result;
|
|
@@ -262,9 +296,14 @@ function buildTracedOptions(options, spanName) {
|
|
|
262
296
|
function resolveSpanName(spec, ctx, request) {
|
|
263
297
|
if (typeof spec.spanName === "function") return spec.spanName(ctx, request);
|
|
264
298
|
if (typeof spec.spanName === "string") return spec.spanName;
|
|
265
|
-
return
|
|
299
|
+
return defaultSpanName(request.method, request.route);
|
|
266
300
|
}
|
|
267
|
-
|
|
301
|
+
/** OTel HTTP server span name: `{method} {http.route}`, or `{method}` without a route. */
|
|
302
|
+
function defaultSpanName(method, route) {
|
|
303
|
+
const m = method || "HTTP";
|
|
304
|
+
return route ? `${m} ${route}` : m;
|
|
305
|
+
}
|
|
306
|
+
function buildHandle(spec, ctx, traceCtx, options = {}, defaultNamed = false) {
|
|
268
307
|
const extracted = spec.extractRequest(ctx);
|
|
269
308
|
const waitUntil = options.waitUntil ?? spec.extractWaitUntil?.(ctx);
|
|
270
309
|
const middlewareOptions = {
|
|
@@ -278,12 +317,13 @@ function buildHandle(spec, ctx, traceCtx, options = {}) {
|
|
|
278
317
|
const log = getRequestLogger(traceCtx, middlewareOptions.requestLoggerOptions);
|
|
279
318
|
log.set({
|
|
280
319
|
"http.request.method": extracted.method,
|
|
281
|
-
"
|
|
320
|
+
"url.path": extracted.path,
|
|
321
|
+
"http.route": extracted.route,
|
|
282
322
|
"http.request.id": extracted.requestId
|
|
283
323
|
});
|
|
284
324
|
if (spec.storage) attachForkToLogger(log, spec.forkLifecycle ?? createStorageForkLifecycle(spec.storage));
|
|
285
325
|
spec.attachLogger(ctx, log);
|
|
286
|
-
const result = createMiddlewareLogger(log, middlewareOptions);
|
|
326
|
+
const result = createMiddlewareLogger(log, middlewareOptions, traceCtx);
|
|
287
327
|
const storage = spec.storage;
|
|
288
328
|
return {
|
|
289
329
|
...result,
|
|
@@ -291,6 +331,11 @@ function buildHandle(spec, ctx, traceCtx, options = {}) {
|
|
|
291
331
|
runWith: async (fn) => {
|
|
292
332
|
if (!storage || result.skipped) return fn();
|
|
293
333
|
return storage.run(log, fn);
|
|
334
|
+
},
|
|
335
|
+
setRoute: (route) => {
|
|
336
|
+
if (!route) return;
|
|
337
|
+
log.set({ "http.route": route });
|
|
338
|
+
if (defaultNamed) traceCtx.updateName(defaultSpanName(extracted.method, route));
|
|
294
339
|
}
|
|
295
340
|
};
|
|
296
341
|
}
|
|
@@ -309,8 +354,10 @@ function buildSkippedHandle(spec, ctx, extracted, options = {}) {
|
|
|
309
354
|
logger,
|
|
310
355
|
middlewareOptions,
|
|
311
356
|
skipped: true,
|
|
357
|
+
markOutcome: () => {},
|
|
312
358
|
finish: async () => null,
|
|
313
359
|
finishResponse: async (response) => response,
|
|
360
|
+
setRoute: () => {},
|
|
314
361
|
runWith: async (fn) => {
|
|
315
362
|
if (!spec.storage) return fn();
|
|
316
363
|
return spec.storage.run(logger, fn);
|
|
@@ -328,15 +375,16 @@ function defineFrameworkIntegration(spec) {
|
|
|
328
375
|
const extracted = spec.extractRequest(ctx);
|
|
329
376
|
if (!shouldInstrumentPath(extracted.path, toRouteAdapterOptions(options))) return handler(buildSkippedHandle(spec, ctx, extracted, options));
|
|
330
377
|
const spanName = options?.spanName ?? resolveSpanName(spec, ctx, extracted);
|
|
378
|
+
const defaultNamed = !options?.spanName && !spec.spanName;
|
|
331
379
|
return trace.run({
|
|
332
380
|
name: spanName,
|
|
333
381
|
isError: completion?.isError
|
|
334
382
|
}, (traceCtx) => {
|
|
335
|
-
return handler(buildHandle(spec, ctx, traceCtx, options ?? {}));
|
|
383
|
+
return handler(buildHandle(spec, ctx, traceCtx, options ?? {}, defaultNamed));
|
|
336
384
|
});
|
|
337
385
|
}
|
|
338
386
|
};
|
|
339
387
|
}
|
|
340
388
|
|
|
341
389
|
//#endregion
|
|
342
|
-
export {
|
|
390
|
+
export { shouldDeferEmitForResponse as _, defineFrameworkIntegration as a, attachForkToLogger as c, markServerError as d, matchesRoutePattern as f, isStreamingResponse as g, bindStreamingResponseLifecycle as h, defaultSpanName as i, createMiddlewareLogger as l, shouldInstrumentPath as m, buildTracedOptions as n, runWithIntegratedHandle as o, mergeRequestLoggerOptions as p, completeIntegratedRequest as r, toRouteAdapterOptions as s, applyLoggerEnrichment as t, getServiceForPath as u, extendDeferredDrain as v };
|
|
@@ -108,6 +108,7 @@ function bindStreamingResponseLifecycle(response, onComplete) {
|
|
|
108
108
|
//#region src/toolkit/middleware.ts
|
|
109
109
|
const noopResult = {
|
|
110
110
|
logger: (0, autotel.createNoopRequestLogger)(),
|
|
111
|
+
markOutcome: () => {},
|
|
111
112
|
finish: () => Promise.resolve(null),
|
|
112
113
|
finishResponse: (response) => Promise.resolve(response),
|
|
113
114
|
skipped: true
|
|
@@ -123,15 +124,31 @@ function mergeRequestLoggerOptions(base, waitUntil) {
|
|
|
123
124
|
}
|
|
124
125
|
};
|
|
125
126
|
}
|
|
126
|
-
|
|
127
|
-
|
|
127
|
+
/**
|
|
128
|
+
* A server span with a 5xx response is an error, whether or not anything was
|
|
129
|
+
* thrown (OpenTelemetry HTTP conventions); a 4xx is the client's and is left
|
|
130
|
+
* unset. Without this, error-based sampling and alerts miss every request
|
|
131
|
+
* that answered 500 without an exception reaching the adapter.
|
|
132
|
+
*/
|
|
133
|
+
function markServerError(span, status) {
|
|
134
|
+
if (status !== void 0 && status >= 500) span?.setStatus({ code: autotel.SpanStatusCode.ERROR });
|
|
135
|
+
}
|
|
136
|
+
function recordOutcome(logger, span, { status, error }, emitted) {
|
|
128
137
|
if (error) {
|
|
129
|
-
|
|
138
|
+
if (span && emitted()) (0, autotel.recordStructuredError)(span, error);
|
|
139
|
+
else logger.error(error);
|
|
130
140
|
logger.set({
|
|
131
141
|
"http.response.status_code": status ?? 500,
|
|
132
142
|
error_message: error.message
|
|
133
143
|
});
|
|
134
|
-
} else if (status !== void 0)
|
|
144
|
+
} else if (status !== void 0) {
|
|
145
|
+
logger.set({ "http.response.status_code": status });
|
|
146
|
+
markServerError(span, status);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
async function performFinish(logger, options, startedAt, span, emitted, opts) {
|
|
150
|
+
const { status, overrides } = opts ?? {};
|
|
151
|
+
recordOutcome(logger, span, opts ?? {}, emitted);
|
|
135
152
|
const durationMs = Date.now() - startedAt;
|
|
136
153
|
logger.set({ duration_ms: durationMs });
|
|
137
154
|
const tailCtx = {
|
|
@@ -164,27 +181,40 @@ async function performFinish(logger, options, startedAt, opts) {
|
|
|
164
181
|
* Create a request logger handle for use inside an active autotel trace span.
|
|
165
182
|
* Call `finish()` or `finishResponse()` when the request completes.
|
|
166
183
|
*/
|
|
167
|
-
function createMiddlewareLogger(logger, options) {
|
|
184
|
+
function createMiddlewareLogger(logger, options, span) {
|
|
168
185
|
if (!(0, autotel_edge.shouldInstrumentPath)(options.path, options)) return noopResult;
|
|
169
186
|
const routeService = (0, autotel_edge.getServiceForPath)(options.path, options.routes);
|
|
170
187
|
if (routeService) logger.set({ service: routeService });
|
|
171
188
|
if (options.requestId) logger.set({ "http.request.id": options.requestId });
|
|
172
189
|
const startedAt = Date.now();
|
|
190
|
+
let hasEmitted = false;
|
|
191
|
+
const emitNow = logger.emitNow.bind(logger);
|
|
192
|
+
logger.emitNow = (overrides) => {
|
|
193
|
+
hasEmitted = true;
|
|
194
|
+
return emitNow(overrides);
|
|
195
|
+
};
|
|
196
|
+
const emitted = () => hasEmitted;
|
|
173
197
|
return {
|
|
174
198
|
logger,
|
|
175
199
|
skipped: false,
|
|
176
|
-
|
|
200
|
+
markOutcome: (outcome) => recordOutcome(logger, span, outcome, emitted),
|
|
201
|
+
finish: (opts) => performFinish(logger, options, startedAt, span, emitted, opts),
|
|
177
202
|
finishResponse: async (response, opts) => {
|
|
178
203
|
const status = opts?.status ?? response.status;
|
|
179
204
|
if (!shouldDeferEmitForResponse(response)) {
|
|
180
|
-
await performFinish(logger, options, startedAt, {
|
|
205
|
+
await performFinish(logger, options, startedAt, span, emitted, {
|
|
181
206
|
status,
|
|
207
|
+
error: opts?.error,
|
|
182
208
|
overrides: opts?.overrides
|
|
183
209
|
});
|
|
184
210
|
return response;
|
|
185
211
|
}
|
|
212
|
+
recordOutcome(logger, span, {
|
|
213
|
+
status,
|
|
214
|
+
error: opts?.error
|
|
215
|
+
}, emitted);
|
|
186
216
|
return bindStreamingResponseLifecycle(response, async (meta) => {
|
|
187
|
-
await performFinish(logger, options, startedAt, {
|
|
217
|
+
await performFinish(logger, options, startedAt, span, emitted, {
|
|
188
218
|
status: meta.status ?? status,
|
|
189
219
|
error: meta.error,
|
|
190
220
|
overrides: opts?.overrides
|
|
@@ -222,7 +252,11 @@ function applyLoggerEnrichment(logger, ...attributeSets) {
|
|
|
222
252
|
for (const attrs of attributeSets) if (attrs && Object.keys(attrs).length > 0) logger.set(attrs);
|
|
223
253
|
}
|
|
224
254
|
async function completeIntegratedRequest(handle, options, result) {
|
|
225
|
-
if (handle.skipped
|
|
255
|
+
if (handle.skipped) return result;
|
|
256
|
+
if (options?.autoEmit === false) {
|
|
257
|
+
if (result instanceof Response) handle.markOutcome({ status: result.status });
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
226
260
|
if (result instanceof Response) return handle.finishResponse(result);
|
|
227
261
|
await handle.finish({});
|
|
228
262
|
return result;
|
|
@@ -262,9 +296,14 @@ function buildTracedOptions(options, spanName) {
|
|
|
262
296
|
function resolveSpanName(spec, ctx, request) {
|
|
263
297
|
if (typeof spec.spanName === "function") return spec.spanName(ctx, request);
|
|
264
298
|
if (typeof spec.spanName === "string") return spec.spanName;
|
|
265
|
-
return
|
|
299
|
+
return defaultSpanName(request.method, request.route);
|
|
266
300
|
}
|
|
267
|
-
|
|
301
|
+
/** OTel HTTP server span name: `{method} {http.route}`, or `{method}` without a route. */
|
|
302
|
+
function defaultSpanName(method, route) {
|
|
303
|
+
const m = method || "HTTP";
|
|
304
|
+
return route ? `${m} ${route}` : m;
|
|
305
|
+
}
|
|
306
|
+
function buildHandle(spec, ctx, traceCtx, options = {}, defaultNamed = false) {
|
|
268
307
|
const extracted = spec.extractRequest(ctx);
|
|
269
308
|
const waitUntil = options.waitUntil ?? spec.extractWaitUntil?.(ctx);
|
|
270
309
|
const middlewareOptions = {
|
|
@@ -278,12 +317,13 @@ function buildHandle(spec, ctx, traceCtx, options = {}) {
|
|
|
278
317
|
const log = (0, autotel.getRequestLogger)(traceCtx, middlewareOptions.requestLoggerOptions);
|
|
279
318
|
log.set({
|
|
280
319
|
"http.request.method": extracted.method,
|
|
281
|
-
"
|
|
320
|
+
"url.path": extracted.path,
|
|
321
|
+
"http.route": extracted.route,
|
|
282
322
|
"http.request.id": extracted.requestId
|
|
283
323
|
});
|
|
284
324
|
if (spec.storage) attachForkToLogger(log, spec.forkLifecycle ?? require_toolkit_storage.createStorageForkLifecycle(spec.storage));
|
|
285
325
|
spec.attachLogger(ctx, log);
|
|
286
|
-
const result = createMiddlewareLogger(log, middlewareOptions);
|
|
326
|
+
const result = createMiddlewareLogger(log, middlewareOptions, traceCtx);
|
|
287
327
|
const storage = spec.storage;
|
|
288
328
|
return {
|
|
289
329
|
...result,
|
|
@@ -291,6 +331,11 @@ function buildHandle(spec, ctx, traceCtx, options = {}) {
|
|
|
291
331
|
runWith: async (fn) => {
|
|
292
332
|
if (!storage || result.skipped) return fn();
|
|
293
333
|
return storage.run(log, fn);
|
|
334
|
+
},
|
|
335
|
+
setRoute: (route) => {
|
|
336
|
+
if (!route) return;
|
|
337
|
+
log.set({ "http.route": route });
|
|
338
|
+
if (defaultNamed) traceCtx.updateName(defaultSpanName(extracted.method, route));
|
|
294
339
|
}
|
|
295
340
|
};
|
|
296
341
|
}
|
|
@@ -309,8 +354,10 @@ function buildSkippedHandle(spec, ctx, extracted, options = {}) {
|
|
|
309
354
|
logger,
|
|
310
355
|
middlewareOptions,
|
|
311
356
|
skipped: true,
|
|
357
|
+
markOutcome: () => {},
|
|
312
358
|
finish: async () => null,
|
|
313
359
|
finishResponse: async (response) => response,
|
|
360
|
+
setRoute: () => {},
|
|
314
361
|
runWith: async (fn) => {
|
|
315
362
|
if (!spec.storage) return fn();
|
|
316
363
|
return spec.storage.run(logger, fn);
|
|
@@ -328,11 +375,12 @@ function defineFrameworkIntegration(spec) {
|
|
|
328
375
|
const extracted = spec.extractRequest(ctx);
|
|
329
376
|
if (!(0, autotel_edge.shouldInstrumentPath)(extracted.path, toRouteAdapterOptions(options))) return handler(buildSkippedHandle(spec, ctx, extracted, options));
|
|
330
377
|
const spanName = options?.spanName ?? resolveSpanName(spec, ctx, extracted);
|
|
378
|
+
const defaultNamed = !options?.spanName && !spec.spanName;
|
|
331
379
|
return autotel.trace.run({
|
|
332
380
|
name: spanName,
|
|
333
381
|
isError: completion?.isError
|
|
334
382
|
}, (traceCtx) => {
|
|
335
|
-
return handler(buildHandle(spec, ctx, traceCtx, options ?? {}));
|
|
383
|
+
return handler(buildHandle(spec, ctx, traceCtx, options ?? {}, defaultNamed));
|
|
336
384
|
});
|
|
337
385
|
}
|
|
338
386
|
};
|
|
@@ -375,6 +423,12 @@ Object.defineProperty(exports, "createMiddlewareLogger", {
|
|
|
375
423
|
return createMiddlewareLogger;
|
|
376
424
|
}
|
|
377
425
|
});
|
|
426
|
+
Object.defineProperty(exports, "defaultSpanName", {
|
|
427
|
+
enumerable: true,
|
|
428
|
+
get: function () {
|
|
429
|
+
return defaultSpanName;
|
|
430
|
+
}
|
|
431
|
+
});
|
|
378
432
|
Object.defineProperty(exports, "defineFrameworkIntegration", {
|
|
379
433
|
enumerable: true,
|
|
380
434
|
get: function () {
|
|
@@ -393,6 +447,12 @@ Object.defineProperty(exports, "isStreamingResponse", {
|
|
|
393
447
|
return isStreamingResponse;
|
|
394
448
|
}
|
|
395
449
|
});
|
|
450
|
+
Object.defineProperty(exports, "markServerError", {
|
|
451
|
+
enumerable: true,
|
|
452
|
+
get: function () {
|
|
453
|
+
return markServerError;
|
|
454
|
+
}
|
|
455
|
+
});
|
|
396
456
|
Object.defineProperty(exports, "mergeRequestLoggerOptions", {
|
|
397
457
|
enumerable: true,
|
|
398
458
|
get: function () {
|
package/dist/nestjs.cjs
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_integration = require("./integration-
|
|
2
|
+
const require_integration = require("./integration-Danzss1R.cjs");
|
|
3
3
|
const require_toolkit_storage = require("./toolkit/storage.cjs");
|
|
4
4
|
let autotel = require("autotel");
|
|
5
5
|
let rxjs = require("rxjs");
|
|
6
6
|
|
|
7
7
|
//#region src/nestjs.ts
|
|
8
|
+
function routeTemplate(request) {
|
|
9
|
+
return request.route?.path ?? request.routeOptions?.url;
|
|
10
|
+
}
|
|
8
11
|
const { storage: nestLoggerStorage, useLogger: storageUseLogger } = require_toolkit_storage.createLoggerStorage("request context. Register AutotelInterceptor globally or on controllers.");
|
|
9
12
|
function enrichFromRequest(request) {
|
|
10
13
|
if (!request) return void 0;
|
|
11
14
|
const url = request.originalUrl ?? request.url;
|
|
12
|
-
const route = request.path ?? url;
|
|
13
15
|
return {
|
|
14
16
|
"http.request.method": request.method,
|
|
15
17
|
"url.full": url,
|
|
16
|
-
"
|
|
18
|
+
"url.path": request.path,
|
|
19
|
+
"http.route": routeTemplate(request)
|
|
17
20
|
};
|
|
18
21
|
}
|
|
19
22
|
const integration = require_integration.defineFrameworkIntegration({
|
|
@@ -25,14 +28,11 @@ const integration = require_integration.defineFrameworkIntegration({
|
|
|
25
28
|
return {
|
|
26
29
|
method: request.method ?? "GET",
|
|
27
30
|
path,
|
|
31
|
+
route: routeTemplate(request),
|
|
28
32
|
headers: request.headers
|
|
29
33
|
};
|
|
30
34
|
},
|
|
31
|
-
attachLogger: () => {}
|
|
32
|
-
spanName: (executionCtx) => {
|
|
33
|
-
const handler = executionCtx.getHandler().name;
|
|
34
|
-
return `nestjs.${executionCtx.getClass().name}.${handler}`;
|
|
35
|
-
}
|
|
35
|
+
attachLogger: () => {}
|
|
36
36
|
});
|
|
37
37
|
function useLogger() {
|
|
38
38
|
return storageUseLogger();
|
|
@@ -50,7 +50,7 @@ var AutotelInterceptor = class {
|
|
|
50
50
|
integration.runTraced(executionCtx, this.options, async (handle) => {
|
|
51
51
|
if (!handle.skipped) {
|
|
52
52
|
const request = executionCtx.switchToHttp().getRequest();
|
|
53
|
-
require_integration.applyLoggerEnrichment(handle.logger, enrichFromRequest(request), this.options.enrichRequest?.(request));
|
|
53
|
+
require_integration.applyLoggerEnrichment(handle.logger, enrichFromRequest(request), { "code.function.name": `${executionCtx.getClass().name}.${executionCtx.getHandler().name}` }, this.options.enrichRequest?.(request));
|
|
54
54
|
}
|
|
55
55
|
await handle.runWith(() => new Promise((resolve, reject) => {
|
|
56
56
|
let settled = false;
|
|
@@ -58,12 +58,10 @@ var AutotelInterceptor = class {
|
|
|
58
58
|
if (settled) return;
|
|
59
59
|
settled = true;
|
|
60
60
|
try {
|
|
61
|
-
if (!handle.skipped
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
await handle.finish({ status: response.statusCode });
|
|
66
|
-
}
|
|
61
|
+
if (!handle.skipped) {
|
|
62
|
+
const outcome = error === void 0 ? { status: executionCtx.switchToHttp().getResponse().statusCode } : { error: error instanceof Error ? error : new Error(String(error)) };
|
|
63
|
+
if (this.options.autoEmit === false) handle.markOutcome(outcome);
|
|
64
|
+
else await handle.finish(outcome);
|
|
67
65
|
}
|
|
68
66
|
} catch (finishError) {
|
|
69
67
|
reject(finishError);
|
package/dist/nestjs.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as FrameworkHandlerOptions } from "./integration-
|
|
1
|
+
import { n as FrameworkHandlerOptions } from "./integration-CwqWnPKn.cjs";
|
|
2
2
|
import "./core.cjs";
|
|
3
3
|
import { RequestLogger, createDrainPipeline, createStructuredError, parseError } from "autotel";
|
|
4
4
|
import { Observable } from "rxjs";
|
|
@@ -21,6 +21,14 @@ export interface NestRequestLike {
|
|
|
21
21
|
url?: string;
|
|
22
22
|
originalUrl?: string;
|
|
23
23
|
path?: string;
|
|
24
|
+
/** Set by the Express platform once routing has run. */
|
|
25
|
+
route?: {
|
|
26
|
+
path?: string;
|
|
27
|
+
};
|
|
28
|
+
/** Set by the Fastify platform. */
|
|
29
|
+
routeOptions?: {
|
|
30
|
+
url?: string;
|
|
31
|
+
};
|
|
24
32
|
headers?: Record<string, string | string[] | undefined>;
|
|
25
33
|
}
|
|
26
34
|
export interface NestResponseLike {
|
package/dist/nestjs.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as FrameworkHandlerOptions } from "./integration-
|
|
1
|
+
import { n as FrameworkHandlerOptions } from "./integration-B6OoKr7O.js";
|
|
2
2
|
import "./core.js";
|
|
3
3
|
import { RequestLogger, createDrainPipeline, createStructuredError, parseError } from "autotel";
|
|
4
4
|
import { Observable } from "rxjs";
|
|
@@ -21,6 +21,14 @@ export interface NestRequestLike {
|
|
|
21
21
|
url?: string;
|
|
22
22
|
originalUrl?: string;
|
|
23
23
|
path?: string;
|
|
24
|
+
/** Set by the Express platform once routing has run. */
|
|
25
|
+
route?: {
|
|
26
|
+
path?: string;
|
|
27
|
+
};
|
|
28
|
+
/** Set by the Fastify platform. */
|
|
29
|
+
routeOptions?: {
|
|
30
|
+
url?: string;
|
|
31
|
+
};
|
|
24
32
|
headers?: Record<string, string | string[] | undefined>;
|
|
25
33
|
}
|
|
26
34
|
export interface NestResponseLike {
|