autotel-adapters 0.2.19 → 0.3.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/dist/chunk-2YPL66HM.js +79 -0
- package/dist/chunk-2YPL66HM.js.map +1 -0
- package/dist/chunk-6TOW47TB.js +70 -0
- package/dist/chunk-6TOW47TB.js.map +1 -0
- package/dist/{chunk-GBSB7IX7.js → chunk-DJ2OU3S6.js} +26 -4
- package/dist/chunk-DJ2OU3S6.js.map +1 -0
- package/dist/{chunk-AD3QBS5J.js → chunk-FAHH33UI.js} +13 -4
- package/dist/chunk-FAHH33UI.js.map +1 -0
- package/dist/{chunk-GPWONOFN.js → chunk-HTZFHBTN.js} +13 -4
- package/dist/chunk-HTZFHBTN.js.map +1 -0
- package/dist/{chunk-UUDK5U3L.js → chunk-JH5ZTTLF.js} +3 -3
- package/dist/{chunk-UUDK5U3L.js.map → chunk-JH5ZTTLF.js.map} +1 -1
- package/dist/{chunk-JC3TNPQD.js → chunk-U3U4WH42.js} +3 -3
- package/dist/{chunk-JC3TNPQD.js.map → chunk-U3U4WH42.js.map} +1 -1
- package/dist/{chunk-CITNYQ2G.js → chunk-VJZDW2DS.js} +6 -3
- package/dist/chunk-VJZDW2DS.js.map +1 -0
- package/dist/cloudflare.cjs +3 -0
- package/dist/cloudflare.cjs.map +1 -1
- package/dist/cloudflare.d.cts +3 -0
- package/dist/cloudflare.d.ts +3 -0
- package/dist/cloudflare.js +2 -2
- package/dist/core.cjs +23 -0
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +16 -1
- package/dist/core.d.ts +16 -1
- package/dist/core.js +1 -1
- package/dist/express.cjs +146 -0
- package/dist/express.cjs.map +1 -0
- package/dist/express.d.cts +43 -0
- package/dist/express.d.ts +43 -0
- package/dist/express.js +4 -0
- package/dist/express.js.map +1 -0
- package/dist/fastify.cjs +137 -0
- package/dist/fastify.cjs.map +1 -0
- package/dist/fastify.d.cts +43 -0
- package/dist/fastify.d.ts +43 -0
- package/dist/fastify.js +4 -0
- package/dist/fastify.js.map +1 -0
- package/dist/hono.cjs.map +1 -1
- package/dist/hono.d.cts +1 -0
- package/dist/hono.d.ts +1 -0
- package/dist/hono.js +2 -2
- package/dist/index.cjs +174 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +8 -6
- package/dist/next.cjs +10 -1
- package/dist/next.cjs.map +1 -1
- package/dist/next.d.cts +3 -0
- package/dist/next.d.ts +3 -0
- package/dist/next.js +2 -2
- package/dist/nitro.cjs +10 -1
- package/dist/nitro.cjs.map +1 -1
- package/dist/nitro.d.cts +3 -0
- package/dist/nitro.d.ts +3 -0
- package/dist/nitro.js +2 -2
- package/dist/tanstack.cjs.map +1 -1
- package/dist/tanstack.d.cts +1 -0
- package/dist/tanstack.d.ts +1 -0
- package/dist/tanstack.js +2 -2
- package/package.json +12 -2
- package/src/cloudflare.test.ts +26 -1
- package/src/cloudflare.ts +5 -0
- package/src/core.ts +44 -0
- package/src/express.test.ts +85 -0
- package/src/express.ts +169 -0
- package/src/fastify.test.ts +79 -0
- package/src/fastify.ts +151 -0
- package/src/index.ts +8 -0
- package/src/next.test.ts +26 -1
- package/src/next.ts +11 -1
- package/src/nitro.test.ts +26 -1
- package/src/nitro.ts +11 -1
- package/dist/chunk-AD3QBS5J.js.map +0 -1
- package/dist/chunk-CITNYQ2G.js.map +0 -1
- package/dist/chunk-GBSB7IX7.js.map +0 -1
- package/dist/chunk-GPWONOFN.js.map +0 -1
package/src/express.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import {
|
|
3
|
+
createDrainPipeline,
|
|
4
|
+
createStructuredError,
|
|
5
|
+
parseError,
|
|
6
|
+
type DrainPipelineOptions,
|
|
7
|
+
type ParsedError,
|
|
8
|
+
type PipelineDrainFn,
|
|
9
|
+
type RequestLogger,
|
|
10
|
+
type RequestLoggerOptions,
|
|
11
|
+
type StructuredError,
|
|
12
|
+
type StructuredErrorInput,
|
|
13
|
+
} from 'autotel';
|
|
14
|
+
import {
|
|
15
|
+
createAdapterToolkit,
|
|
16
|
+
createRequestRunner,
|
|
17
|
+
createUseLogger,
|
|
18
|
+
} from './core';
|
|
19
|
+
|
|
20
|
+
export interface ExpressRequestLike {
|
|
21
|
+
method?: string;
|
|
22
|
+
originalUrl?: string;
|
|
23
|
+
url?: string;
|
|
24
|
+
path?: string;
|
|
25
|
+
route?: { path?: string };
|
|
26
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ExpressResponseLike {
|
|
30
|
+
statusCode?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type ExpressNext = (err?: unknown) => void;
|
|
34
|
+
|
|
35
|
+
export interface ExpressWithAutotelOptions {
|
|
36
|
+
spanName?: string | ((request: ExpressRequestLike) => string);
|
|
37
|
+
requestLoggerOptions?: RequestLoggerOptions;
|
|
38
|
+
enrich?: (request: ExpressRequestLike) => Record<string, unknown> | undefined;
|
|
39
|
+
/** Emit one wide event automatically when the handler settles. Default `true`. */
|
|
40
|
+
autoEmit?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const expressLoggerStorage = new AsyncLocalStorage<RequestLogger>();
|
|
44
|
+
|
|
45
|
+
function headerValue(
|
|
46
|
+
headers: ExpressRequestLike['headers'],
|
|
47
|
+
name: string,
|
|
48
|
+
): string | undefined {
|
|
49
|
+
if (!headers) return undefined;
|
|
50
|
+
const value = headers[name] ?? headers[name.toLowerCase()];
|
|
51
|
+
if (Array.isArray(value)) return value[0];
|
|
52
|
+
return typeof value === 'string' ? value : undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function enrichFromRequest(
|
|
56
|
+
request?: ExpressRequestLike,
|
|
57
|
+
): Record<string, unknown> | undefined {
|
|
58
|
+
if (!request) return undefined;
|
|
59
|
+
|
|
60
|
+
const url = request.originalUrl ?? request.url;
|
|
61
|
+
const route = request.route?.path ?? request.path ?? url;
|
|
62
|
+
const requestId = headerValue(request.headers, 'x-request-id');
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
...(request.method ? { 'http.request.method': request.method } : {}),
|
|
66
|
+
...(url ? { 'url.full': url } : {}),
|
|
67
|
+
...(route ? { 'http.route': route } : {}),
|
|
68
|
+
...(requestId ? { 'http.request.id': requestId } : {}),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const baseUseLogger = createUseLogger<ExpressRequestLike>({
|
|
73
|
+
adapterName: 'express',
|
|
74
|
+
enrich: enrichFromRequest,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
export function useLogger(
|
|
78
|
+
request?: ExpressRequestLike,
|
|
79
|
+
requestLoggerOptions?: RequestLoggerOptions,
|
|
80
|
+
): RequestLogger {
|
|
81
|
+
const stored = expressLoggerStorage.getStore();
|
|
82
|
+
if (stored) return stored;
|
|
83
|
+
return baseUseLogger(request, requestLoggerOptions);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const runRequest = createRequestRunner(expressLoggerStorage);
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Wrap an Express route handler. Each request opens a span, gets a
|
|
90
|
+
* request-scoped logger (via `useLogger(req)`), and emits one wide event when
|
|
91
|
+
* the handler settles. Thrown errors are recorded and forwarded to `next`.
|
|
92
|
+
*/
|
|
93
|
+
export function withAutotel<
|
|
94
|
+
TReq extends ExpressRequestLike,
|
|
95
|
+
TRes extends ExpressResponseLike,
|
|
96
|
+
TReturn,
|
|
97
|
+
>(
|
|
98
|
+
handler: (req: TReq, res: TRes, next?: ExpressNext) => TReturn | Promise<TReturn>,
|
|
99
|
+
options?: ExpressWithAutotelOptions,
|
|
100
|
+
): (req: TReq, res: TRes, next?: ExpressNext) => Promise<TReturn | undefined> {
|
|
101
|
+
return async (
|
|
102
|
+
req: TReq,
|
|
103
|
+
res: TRes,
|
|
104
|
+
next?: ExpressNext,
|
|
105
|
+
): Promise<TReturn | undefined> => {
|
|
106
|
+
const spanName =
|
|
107
|
+
typeof options?.spanName === 'function'
|
|
108
|
+
? options.spanName(req)
|
|
109
|
+
: (options?.spanName ?? `express.${req.method ?? 'request'}`);
|
|
110
|
+
|
|
111
|
+
try {
|
|
112
|
+
return await runRequest<TReturn>(
|
|
113
|
+
spanName,
|
|
114
|
+
(log) => {
|
|
115
|
+
const auto = enrichFromRequest(req);
|
|
116
|
+
if (auto && Object.keys(auto).length > 0) log.set(auto);
|
|
117
|
+
const custom = options?.enrich?.(req);
|
|
118
|
+
if (custom && Object.keys(custom).length > 0) log.set(custom);
|
|
119
|
+
},
|
|
120
|
+
() => handler(req, res, next),
|
|
121
|
+
{
|
|
122
|
+
requestLoggerOptions: options?.requestLoggerOptions,
|
|
123
|
+
autoEmit: options?.autoEmit,
|
|
124
|
+
finalize: () =>
|
|
125
|
+
res.statusCode
|
|
126
|
+
? { 'http.response.status_code': res.statusCode }
|
|
127
|
+
: undefined,
|
|
128
|
+
},
|
|
129
|
+
);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if (typeof next === 'function') {
|
|
132
|
+
next(error);
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function createExpressAdapter(options?: ExpressWithAutotelOptions) {
|
|
141
|
+
return {
|
|
142
|
+
withAutotel: <
|
|
143
|
+
TReq extends ExpressRequestLike,
|
|
144
|
+
TRes extends ExpressResponseLike,
|
|
145
|
+
TReturn,
|
|
146
|
+
>(
|
|
147
|
+
handler: (
|
|
148
|
+
req: TReq,
|
|
149
|
+
res: TRes,
|
|
150
|
+
next?: ExpressNext,
|
|
151
|
+
) => TReturn | Promise<TReturn>,
|
|
152
|
+
) => withAutotel(handler, options),
|
|
153
|
+
useLogger,
|
|
154
|
+
parseError: (error: unknown): ParsedError => parseError(error),
|
|
155
|
+
createStructuredError: (input: StructuredErrorInput): StructuredError =>
|
|
156
|
+
createStructuredError(input),
|
|
157
|
+
createDrainPipeline: <T = unknown>(
|
|
158
|
+
drainOptions?: DrainPipelineOptions<T>,
|
|
159
|
+
): ((batchDrain: (batch: T[]) => void | Promise<void>) => PipelineDrainFn<T>) =>
|
|
160
|
+
createDrainPipeline(drainOptions),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export const expressToolkit = createAdapterToolkit<ExpressRequestLike>({
|
|
165
|
+
adapterName: 'express',
|
|
166
|
+
enrich: enrichFromRequest,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
export { parseError, createDrainPipeline, createStructuredError };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { useLogger, withAutotel } from './fastify';
|
|
3
|
+
import type { FastifyRequestLike, FastifyReplyLike } from './fastify';
|
|
4
|
+
|
|
5
|
+
const req = (over: Partial<FastifyRequestLike> = {}): FastifyRequestLike => ({
|
|
6
|
+
method: 'GET',
|
|
7
|
+
url: '/api/orders',
|
|
8
|
+
routeOptions: { url: '/api/orders/:id' },
|
|
9
|
+
id: 'req-1',
|
|
10
|
+
...over,
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe('fastify adapter', () => {
|
|
14
|
+
it('throws a clear error when useLogger is called outside a traced context', () => {
|
|
15
|
+
expect(() => useLogger(req())).toThrow(
|
|
16
|
+
'[autotel-adapters/fastify] No active trace context.',
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('runs the handler with a request-scoped logger', async () => {
|
|
21
|
+
const handler = withAutotel((request, reply: FastifyReplyLike) => {
|
|
22
|
+
useLogger(request).set({ feature: 'checkout' });
|
|
23
|
+
reply.statusCode = 201;
|
|
24
|
+
return { ok: true };
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
await expect(handler(req(), { statusCode: 200 })).resolves.toEqual({
|
|
28
|
+
ok: true,
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('auto-emits one wide event with route, request id, and status', async () => {
|
|
33
|
+
const onEmit = vi.fn();
|
|
34
|
+
const handler = withAutotel(
|
|
35
|
+
(request, reply: FastifyReplyLike) => {
|
|
36
|
+
useLogger(request).set({ user: 'u1' });
|
|
37
|
+
reply.statusCode = 200;
|
|
38
|
+
return 'done';
|
|
39
|
+
},
|
|
40
|
+
{ requestLoggerOptions: { onEmit } },
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
await handler(req({ method: 'POST' }), { statusCode: 200 });
|
|
44
|
+
|
|
45
|
+
expect(onEmit).toHaveBeenCalledTimes(1);
|
|
46
|
+
const snapshot = onEmit.mock.calls[0]?.[0] as {
|
|
47
|
+
context: Record<string, unknown>;
|
|
48
|
+
};
|
|
49
|
+
expect(snapshot.context.user).toBe('u1');
|
|
50
|
+
expect(snapshot.context['http.route']).toBe('/api/orders/:id');
|
|
51
|
+
expect(snapshot.context['http.request.id']).toBe('req-1');
|
|
52
|
+
expect(snapshot.context['http.response.status_code']).toBe(200);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('does not emit when autoEmit is false', async () => {
|
|
56
|
+
const onEmit = vi.fn();
|
|
57
|
+
const handler = withAutotel(() => 'x', {
|
|
58
|
+
autoEmit: false,
|
|
59
|
+
requestLoggerOptions: { onEmit },
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
await handler(req(), { statusCode: 200 });
|
|
63
|
+
expect(onEmit).not.toHaveBeenCalled();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('records the error, emits, and rethrows for fastify to handle', async () => {
|
|
67
|
+
const onEmit = vi.fn();
|
|
68
|
+
const boom = new Error('boom');
|
|
69
|
+
const handler = withAutotel(
|
|
70
|
+
() => {
|
|
71
|
+
throw boom;
|
|
72
|
+
},
|
|
73
|
+
{ requestLoggerOptions: { onEmit } },
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
await expect(handler(req(), { statusCode: 500 })).rejects.toThrow('boom');
|
|
77
|
+
expect(onEmit).toHaveBeenCalledTimes(1);
|
|
78
|
+
});
|
|
79
|
+
});
|
package/src/fastify.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import {
|
|
3
|
+
createDrainPipeline,
|
|
4
|
+
createStructuredError,
|
|
5
|
+
parseError,
|
|
6
|
+
type DrainPipelineOptions,
|
|
7
|
+
type ParsedError,
|
|
8
|
+
type PipelineDrainFn,
|
|
9
|
+
type RequestLogger,
|
|
10
|
+
type RequestLoggerOptions,
|
|
11
|
+
type StructuredError,
|
|
12
|
+
type StructuredErrorInput,
|
|
13
|
+
} from 'autotel';
|
|
14
|
+
import {
|
|
15
|
+
createAdapterToolkit,
|
|
16
|
+
createRequestRunner,
|
|
17
|
+
createUseLogger,
|
|
18
|
+
} from './core';
|
|
19
|
+
|
|
20
|
+
export interface FastifyRequestLike {
|
|
21
|
+
method?: string;
|
|
22
|
+
url?: string;
|
|
23
|
+
routeOptions?: { url?: string };
|
|
24
|
+
routerPath?: string;
|
|
25
|
+
id?: string;
|
|
26
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface FastifyReplyLike {
|
|
30
|
+
statusCode?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface FastifyWithAutotelOptions {
|
|
34
|
+
spanName?: string | ((request: FastifyRequestLike) => string);
|
|
35
|
+
requestLoggerOptions?: RequestLoggerOptions;
|
|
36
|
+
enrich?: (request: FastifyRequestLike) => Record<string, unknown> | undefined;
|
|
37
|
+
/** Emit one wide event automatically when the handler settles. Default `true`. */
|
|
38
|
+
autoEmit?: boolean;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const fastifyLoggerStorage = new AsyncLocalStorage<RequestLogger>();
|
|
42
|
+
|
|
43
|
+
function headerValue(
|
|
44
|
+
headers: FastifyRequestLike['headers'],
|
|
45
|
+
name: string,
|
|
46
|
+
): string | undefined {
|
|
47
|
+
if (!headers) return undefined;
|
|
48
|
+
const value = headers[name] ?? headers[name.toLowerCase()];
|
|
49
|
+
if (Array.isArray(value)) return value[0];
|
|
50
|
+
return typeof value === 'string' ? value : undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function enrichFromRequest(
|
|
54
|
+
request?: FastifyRequestLike,
|
|
55
|
+
): Record<string, unknown> | undefined {
|
|
56
|
+
if (!request) return undefined;
|
|
57
|
+
|
|
58
|
+
const route = request.routeOptions?.url ?? request.routerPath ?? request.url;
|
|
59
|
+
const requestId = request.id ?? headerValue(request.headers, 'x-request-id');
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
...(request.method ? { 'http.request.method': request.method } : {}),
|
|
63
|
+
...(request.url ? { 'url.full': request.url } : {}),
|
|
64
|
+
...(route ? { 'http.route': route } : {}),
|
|
65
|
+
...(requestId ? { 'http.request.id': requestId } : {}),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const baseUseLogger = createUseLogger<FastifyRequestLike>({
|
|
70
|
+
adapterName: 'fastify',
|
|
71
|
+
enrich: enrichFromRequest,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export function useLogger(
|
|
75
|
+
request?: FastifyRequestLike,
|
|
76
|
+
requestLoggerOptions?: RequestLoggerOptions,
|
|
77
|
+
): RequestLogger {
|
|
78
|
+
const stored = fastifyLoggerStorage.getStore();
|
|
79
|
+
if (stored) return stored;
|
|
80
|
+
return baseUseLogger(request, requestLoggerOptions);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const runRequest = createRequestRunner(fastifyLoggerStorage);
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Wrap a Fastify route handler. Each request opens a span, gets a
|
|
87
|
+
* request-scoped logger (via `useLogger(request)`), and emits one wide event
|
|
88
|
+
* when the handler settles. Thrown errors are recorded, then rethrown for
|
|
89
|
+
* Fastify's error handling.
|
|
90
|
+
*/
|
|
91
|
+
export function withAutotel<
|
|
92
|
+
TReq extends FastifyRequestLike,
|
|
93
|
+
TRes extends FastifyReplyLike,
|
|
94
|
+
TReturn,
|
|
95
|
+
>(
|
|
96
|
+
handler: (request: TReq, reply: TRes) => TReturn | Promise<TReturn>,
|
|
97
|
+
options?: FastifyWithAutotelOptions,
|
|
98
|
+
): (request: TReq, reply: TRes) => Promise<TReturn> {
|
|
99
|
+
return (request: TReq, reply: TRes): Promise<TReturn> => {
|
|
100
|
+
const spanName =
|
|
101
|
+
typeof options?.spanName === 'function'
|
|
102
|
+
? options.spanName(request)
|
|
103
|
+
: (options?.spanName ?? `fastify.${request.method ?? 'request'}`);
|
|
104
|
+
|
|
105
|
+
return runRequest<TReturn>(
|
|
106
|
+
spanName,
|
|
107
|
+
(log) => {
|
|
108
|
+
const auto = enrichFromRequest(request);
|
|
109
|
+
if (auto && Object.keys(auto).length > 0) log.set(auto);
|
|
110
|
+
const custom = options?.enrich?.(request);
|
|
111
|
+
if (custom && Object.keys(custom).length > 0) log.set(custom);
|
|
112
|
+
},
|
|
113
|
+
() => handler(request, reply),
|
|
114
|
+
{
|
|
115
|
+
requestLoggerOptions: options?.requestLoggerOptions,
|
|
116
|
+
autoEmit: options?.autoEmit,
|
|
117
|
+
finalize: () =>
|
|
118
|
+
reply.statusCode
|
|
119
|
+
? { 'http.response.status_code': reply.statusCode }
|
|
120
|
+
: undefined,
|
|
121
|
+
},
|
|
122
|
+
);
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function createFastifyAdapter(options?: FastifyWithAutotelOptions) {
|
|
127
|
+
return {
|
|
128
|
+
withAutotel: <
|
|
129
|
+
TReq extends FastifyRequestLike,
|
|
130
|
+
TRes extends FastifyReplyLike,
|
|
131
|
+
TReturn,
|
|
132
|
+
>(
|
|
133
|
+
handler: (request: TReq, reply: TRes) => TReturn | Promise<TReturn>,
|
|
134
|
+
) => withAutotel(handler, options),
|
|
135
|
+
useLogger,
|
|
136
|
+
parseError: (error: unknown): ParsedError => parseError(error),
|
|
137
|
+
createStructuredError: (input: StructuredErrorInput): StructuredError =>
|
|
138
|
+
createStructuredError(input),
|
|
139
|
+
createDrainPipeline: <T = unknown>(
|
|
140
|
+
drainOptions?: DrainPipelineOptions<T>,
|
|
141
|
+
): ((batchDrain: (batch: T[]) => void | Promise<void>) => PipelineDrainFn<T>) =>
|
|
142
|
+
createDrainPipeline(drainOptions),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export const fastifyToolkit = createAdapterToolkit<FastifyRequestLike>({
|
|
147
|
+
adapterName: 'fastify',
|
|
148
|
+
enrich: enrichFromRequest,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
export { parseError, createDrainPipeline, createStructuredError };
|
package/src/index.ts
CHANGED
|
@@ -2,5 +2,13 @@ export * from './core';
|
|
|
2
2
|
export { createNextAdapter, withAutotel } from './next';
|
|
3
3
|
export { createNitroAdapter, withAutotelEventHandler } from './nitro';
|
|
4
4
|
export { createCloudflareAdapter, withAutotelFetch } from './cloudflare';
|
|
5
|
+
export {
|
|
6
|
+
createExpressAdapter,
|
|
7
|
+
withAutotel as withAutotelExpress,
|
|
8
|
+
} from './express';
|
|
9
|
+
export {
|
|
10
|
+
createFastifyAdapter,
|
|
11
|
+
withAutotel as withAutotelFastify,
|
|
12
|
+
} from './fastify';
|
|
5
13
|
export { honoToolkit } from './hono';
|
|
6
14
|
export { tanstackToolkit } from './tanstack';
|
package/src/next.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
2
|
import { useLogger, withAutotel } from './next';
|
|
3
3
|
|
|
4
4
|
describe('next adapter', () => {
|
|
@@ -19,4 +19,29 @@ describe('next adapter', () => {
|
|
|
19
19
|
'ok',
|
|
20
20
|
);
|
|
21
21
|
});
|
|
22
|
+
|
|
23
|
+
it('auto-emits one wide event by default', async () => {
|
|
24
|
+
const onEmit = vi.fn();
|
|
25
|
+
const handler = withAutotel(
|
|
26
|
+
async (request: { url: string }) => {
|
|
27
|
+
useLogger(request).set({ feature: 'checkout' });
|
|
28
|
+
return 'ok';
|
|
29
|
+
},
|
|
30
|
+
{ requestLoggerOptions: { onEmit } },
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
await handler({ url: 'https://example.com/orders' });
|
|
34
|
+
expect(onEmit).toHaveBeenCalledTimes(1);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('does not emit when autoEmit is false', async () => {
|
|
38
|
+
const onEmit = vi.fn();
|
|
39
|
+
const handler = withAutotel(async () => 'ok', {
|
|
40
|
+
autoEmit: false,
|
|
41
|
+
requestLoggerOptions: { onEmit },
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
await handler();
|
|
45
|
+
expect(onEmit).not.toHaveBeenCalled();
|
|
46
|
+
});
|
|
22
47
|
});
|
package/src/next.ts
CHANGED
|
@@ -27,6 +27,8 @@ export interface NextWithAutotelOptions {
|
|
|
27
27
|
spanName?: string | ((request?: NextRequestLike) => string);
|
|
28
28
|
requestLoggerOptions?: RequestLoggerOptions;
|
|
29
29
|
enrich?: (request?: NextRequestLike) => Record<string, unknown> | undefined;
|
|
30
|
+
/** Emit one wide event automatically when the handler settles. Default `true`. */
|
|
31
|
+
autoEmit?: boolean;
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
const nextLoggerStorage = new AsyncLocalStorage<RequestLogger>();
|
|
@@ -92,7 +94,15 @@ export function withAutotel<TArgs extends unknown[], TReturn>(
|
|
|
92
94
|
if (custom && Object.keys(custom).length > 0) {
|
|
93
95
|
log.set(custom);
|
|
94
96
|
}
|
|
95
|
-
|
|
97
|
+
try {
|
|
98
|
+
return await nextLoggerStorage.run(log, async () =>
|
|
99
|
+
handler(...innerArgs),
|
|
100
|
+
);
|
|
101
|
+
} finally {
|
|
102
|
+
if (options?.autoEmit !== false) {
|
|
103
|
+
log.emitNow();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
96
106
|
},
|
|
97
107
|
);
|
|
98
108
|
return await wrapped(...args);
|
package/src/nitro.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, expect, it } from 'vitest';
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
2
|
import { useLogger, withAutotelEventHandler } from './nitro';
|
|
3
3
|
|
|
4
4
|
describe('nitro adapter', () => {
|
|
@@ -21,4 +21,29 @@ describe('nitro adapter', () => {
|
|
|
21
21
|
handler({ path: '/orders', context: {} }),
|
|
22
22
|
).resolves.toMatchObject({ ok: true });
|
|
23
23
|
});
|
|
24
|
+
|
|
25
|
+
it('auto-emits one wide event by default', async () => {
|
|
26
|
+
const onEmit = vi.fn();
|
|
27
|
+
const handler = withAutotelEventHandler(
|
|
28
|
+
async (event: { path: string; context: Record<string, unknown> }) => {
|
|
29
|
+
useLogger(event).set({ route: event.path });
|
|
30
|
+
return { ok: true };
|
|
31
|
+
},
|
|
32
|
+
{ requestLoggerOptions: { onEmit } },
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
await handler({ path: '/orders', context: {} });
|
|
36
|
+
expect(onEmit).toHaveBeenCalledTimes(1);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('does not emit when autoEmit is false', async () => {
|
|
40
|
+
const onEmit = vi.fn();
|
|
41
|
+
const handler = withAutotelEventHandler(async () => ({ ok: true }), {
|
|
42
|
+
autoEmit: false,
|
|
43
|
+
requestLoggerOptions: { onEmit },
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
await handler({ path: '/x', context: {} });
|
|
47
|
+
expect(onEmit).not.toHaveBeenCalled();
|
|
48
|
+
});
|
|
24
49
|
});
|
package/src/nitro.ts
CHANGED
|
@@ -17,6 +17,8 @@ export interface NitroWithAutotelOptions {
|
|
|
17
17
|
spanName?: string | ((event: NitroEventLike) => string);
|
|
18
18
|
requestLoggerOptions?: RequestLoggerOptions;
|
|
19
19
|
enrich?: (event: NitroEventLike) => Record<string, unknown> | undefined;
|
|
20
|
+
/** Emit one wide event automatically when the handler settles. Default `true`. */
|
|
21
|
+
autoEmit?: boolean;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
function enrichFromEvent(
|
|
@@ -78,7 +80,15 @@ export function withAutotelEventHandler<TEvent extends NitroEventLike, TReturn>(
|
|
|
78
80
|
if (custom && Object.keys(custom).length > 0) {
|
|
79
81
|
log.set(custom);
|
|
80
82
|
}
|
|
81
|
-
|
|
83
|
+
try {
|
|
84
|
+
return await nitroLoggerStorage.run(log, async () =>
|
|
85
|
+
handler(innerEvent),
|
|
86
|
+
);
|
|
87
|
+
} finally {
|
|
88
|
+
if (options?.autoEmit !== false) {
|
|
89
|
+
log.emitNow();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
82
92
|
});
|
|
83
93
|
|
|
84
94
|
return await wrapped(event);
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/nitro.ts"],"names":[],"mappings":";;;;AAqBA,SAAS,gBACP,KAAA,EACqC;AACrC,EAAA,IAAI,CAAC,OAAO,OAAO,MAAA;AAEnB,EAAA,OAAO;AAAA,IACL,GAAI,MAAM,MAAA,GAAS,EAAE,uBAAuB,KAAA,CAAM,MAAA,KAAW,EAAC;AAAA,IAC9D,GAAI,MAAM,IAAA,GAAO,EAAE,cAAc,KAAA,CAAM,IAAA,KAAS,EAAC;AAAA,IACjD,GAAI,OAAO,KAAA,CAAM,OAAA,EAAS,SAAA,KAAc,QAAA,GACpC,EAAE,iBAAA,EAAmB,KAAA,CAAM,OAAA,CAAQ,SAAA,EAAU,GAC7C;AAAC,GACP;AACF;AAEA,IAAM,gBAA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MAAA,GAAS,OAAA,EAAS,MAAA,GAAS,UAAU,CAAA;AAC3C,MAAA,IAAI,UAAU,MAAA,CAAO,IAAA,CAAK,MAAM,CAAA,CAAE,SAAS,CAAA,EAAG;AAC5C,QAAA,GAAA,CAAI,IAAI,MAAM,CAAA;AAAA,MAChB;AACA,MAAA,OAAO,MAAM,kBAAA,CAAmB,GAAA,CAAI,KAAK,YAAY,OAAA,CAAQ,UAAU,CAAC,CAAA;AAAA,IAC1E,CAAC,CAAA;AAED,IAAA,OAAO,MAAM,QAAQ,KAAK,CAAA;AAAA,EAC5B,CAAA;AACF;AAEO,SAAS,mBAAmB,OAAA,EAAmC;AACpE,EAAA,MAAM,UAAU,oBAAA,CAAqC;AAAA,IACnD,WAAA,EAAa,OAAA;AAAA,IACb,MAAA,EAAQ,CAAC,KAAA,MAAW;AAAA,MAClB,GAAG,gBAAgB,KAAK,CAAA;AAAA,MACxB,GAAI,OAAA,EAAS,MAAA,GAAS,KAAK,KAAK;AAAC,KACnC;AAAA,GACD,CAAA;AAED,EAAA,OAAO;AAAA,IACL,uBAAA,EAAyB,CACvB,OAAA,KACG,uBAAA,CAAwB,SAAS,OAAO,CAAA;AAAA,IAC7C,SAAA;AAAA,IACA,YAAY,OAAA,CAAQ,UAAA;AAAA,IACpB,uBAAuB,OAAA,CAAQ,qBAAA;AAAA,IAC/B,qBAAqB,OAAA,CAAQ;AAAA,GAC/B;AACF;AAEO,IAAM,eAAe,oBAAA,CAAqC;AAAA,EAC/D,WAAA,EAAa,OAAA;AAAA,EACb,MAAA,EAAQ;AACV,CAAC","file":"chunk-AD3QBS5J.js","sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport {\n getRequestLogger,\n trace,\n type RequestLogger,\n type RequestLoggerOptions,\n} from 'autotel';\nimport { createAdapterToolkit, createUseLogger } from './core';\n\nexport interface NitroEventLike {\n method?: string;\n path?: string;\n context?: Record<string, unknown>;\n}\n\nexport interface NitroWithAutotelOptions {\n spanName?: string | ((event: NitroEventLike) => string);\n requestLoggerOptions?: RequestLoggerOptions;\n enrich?: (event: NitroEventLike) => Record<string, unknown> | undefined;\n}\n\nfunction enrichFromEvent(\n event?: NitroEventLike,\n): Record<string, unknown> | undefined {\n if (!event) return undefined;\n\n return {\n ...(event.method ? { 'http.request.method': event.method } : {}),\n ...(event.path ? { 'http.route': event.path } : {}),\n ...(typeof event.context?.requestId === 'string'\n ? { 'http.request.id': event.context.requestId }\n : {}),\n };\n}\n\nconst baseUseLogger = createUseLogger<NitroEventLike>({\n adapterName: 'nitro',\n enrich: enrichFromEvent,\n});\nconst nitroLoggerStorage = new AsyncLocalStorage<RequestLogger>();\n\nexport function useLogger(\n event?: NitroEventLike,\n serviceOrOptions?: string | RequestLoggerOptions,\n): RequestLogger {\n const stored = nitroLoggerStorage.getStore();\n const logger = stored ??\n (\n typeof serviceOrOptions === 'string'\n ? baseUseLogger(event)\n : baseUseLogger(event, serviceOrOptions)\n );\n\n if (typeof serviceOrOptions === 'string' && serviceOrOptions.length > 0) {\n logger.set({ service: serviceOrOptions });\n }\n\n return logger;\n}\n\nexport function withAutotelEventHandler<TEvent extends NitroEventLike, TReturn>(\n handler: (event: TEvent) => TReturn | Promise<TReturn>,\n options?: NitroWithAutotelOptions,\n): (event: TEvent) => Promise<TReturn> {\n return async (event: TEvent): Promise<TReturn> => {\n const spanName =\n typeof options?.spanName === 'function'\n ? options.spanName(event)\n : (options?.spanName ?? `nitro.${event.method ?? 'request'}`);\n\n const wrapped = trace({ name: spanName }, (ctx) => async (innerEvent: TEvent) => {\n const log = getRequestLogger(ctx, options?.requestLoggerOptions);\n const auto = enrichFromEvent(innerEvent);\n if (auto && Object.keys(auto).length > 0) {\n log.set(auto);\n }\n const custom = options?.enrich?.(innerEvent);\n if (custom && Object.keys(custom).length > 0) {\n log.set(custom);\n }\n return await nitroLoggerStorage.run(log, async () => handler(innerEvent));\n });\n\n return await wrapped(event);\n };\n}\n\nexport function createNitroAdapter(options?: NitroWithAutotelOptions) {\n const toolkit = createAdapterToolkit<NitroEventLike>({\n adapterName: 'nitro',\n enrich: (event) => ({\n ...enrichFromEvent(event),\n ...(options?.enrich?.(event) ?? {}),\n }),\n });\n\n return {\n withAutotelEventHandler: <TEvent extends NitroEventLike, TReturn>(\n handler: (event: TEvent) => TReturn | Promise<TReturn>,\n ) => withAutotelEventHandler(handler, options),\n useLogger,\n parseError: toolkit.parseError,\n createStructuredError: toolkit.createStructuredError,\n createDrainPipeline: toolkit.createDrainPipeline,\n };\n}\n\nexport const nitroToolkit = createAdapterToolkit<NitroEventLike>({\n adapterName: 'nitro',\n enrich: enrichFromEvent,\n});\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/cloudflare.ts"],"names":[],"mappings":";;;;AAwCA,IAAM,cAAA,uBAAqB,OAAA,EAA+B;AAE1D,SAAS,kBACP,OAAA,EACqC;AACrC,EAAA,IAAI,CAAC,SAAS,OAAO,MAAA;AAErB,EAAA,IAAI,KAAA,GAAQ,GAAA;AACZ,EAAA,IAAI,QAAQ,GAAA,EAAK;AACf,IAAA,IAAI;AACF,MAAA,KAAA,GAAQ,IAAI,GAAA,CAAI,OAAA,CAAQ,GAAG,CAAA,CAAE,QAAA;AAAA,IAC/B,CAAA,CAAA,MAAQ;AACN,MAAA,KAAA,GAAQ,OAAA,CAAQ,GAAA;AAAA,IAClB;AAAA,EACF;AAEA,EAAA,MAAM,SAAA,GACJ,UAAU,OAAA,CAAQ,OAAA,EAAS,cAAc,CAAA,IACzC,SAAA,CAAU,OAAA,CAAQ,OAAA,EAAS,QAAQ,CAAA;AAErC,EAAA,OAAO;AAAA,IACL,GAAI,QAAQ,MAAA,GAAS,EAAE,uBAAuB,OAAA,CAAQ,MAAA,KAAW,EAAC;AAAA,IAClE,GAAI,QAAQ,GAAA,GAAM,EAAE,YAAY,OAAA,CAAQ,GAAA,KAAQ,EAAC;AAAA,IACjD,GAAI,KAAA,GAAQ,EAAE,YAAA,EAAc,KAAA,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oB,oBAAA,CAA4C;AAAA,EAC3E,WAAA,EAAa,YAAA;AAAA,EACb,MAAA,EAAQ;AACV,CAAC","file":"chunk-CITNYQ2G.js","sourcesContent":["import {\n createDrainPipeline,\n createStructuredError,\n getRequestLogger,\n parseError,\n trace,\n type DrainPipelineOptions,\n type ParsedError,\n type PipelineDrainFn,\n type RequestLogger,\n type RequestLoggerOptions,\n type StructuredError,\n type StructuredErrorInput,\n} from 'autotel';\nimport { createAdapterToolkit, createUseLogger, getHeader } from './core';\n\nexport interface CloudflareRequestLike {\n method?: string;\n url?: string;\n headers?:\n | { get(name: string): string | null }\n | Record<string, string | undefined>;\n cf?: Record<string, unknown>;\n}\n\nexport interface CloudflareExecutionContextLike {\n waitUntil?: (promise: Promise<unknown>) => void;\n passThroughOnException?: () => void;\n}\n\nexport interface CloudflareWithAutotelOptions<TEnv = unknown> {\n spanName?: string | ((request: CloudflareRequestLike, env: TEnv) => string);\n requestLoggerOptions?: RequestLoggerOptions;\n enrich?: (\n request: CloudflareRequestLike,\n env: TEnv,\n ctx: CloudflareExecutionContextLike,\n ) => Record<string, unknown> | undefined;\n}\n\nconst requestLoggers = new WeakMap<object, RequestLogger>();\n\nfunction enrichFromRequest(\n request?: CloudflareRequestLike,\n): Record<string, unknown> | undefined {\n if (!request) return undefined;\n\n let route = '/';\n if (request.url) {\n try {\n route = new URL(request.url).pathname;\n } catch {\n route = request.url;\n }\n }\n\n const requestId =\n getHeader(request.headers, 'x-request-id') ??\n getHeader(request.headers, 'cf-ray');\n\n return {\n ...(request.method ? { 'http.request.method': request.method } : {}),\n ...(request.url ? { 'url.full': request.url } : {}),\n ...(route ? { 'http.route': route } : {}),\n ...(requestId ? { 'http.request.id': requestId } : {}),\n ...(request.cf?.country ? { 'cloudflare.country': request.cf.country } : {}),\n ...(request.cf?.colo ? { 'cloudflare.colo': request.cf.colo } : {}),\n ...(request.cf?.city ? { 'cloudflare.city': request.cf.city } : {}),\n };\n}\n\nconst baseUseLogger = createUseLogger<CloudflareRequestLike>({\n adapterName: 'cloudflare',\n enrich: enrichFromRequest,\n});\n\nexport function useLogger(\n request?: CloudflareRequestLike,\n requestLoggerOptions?: RequestLoggerOptions,\n): RequestLogger {\n if (request) {\n const existing = requestLoggers.get(request as object);\n if (existing) return existing;\n }\n return baseUseLogger(request, requestLoggerOptions);\n}\n\nexport function withAutotelFetch<\n TEnv,\n TRequest extends CloudflareRequestLike,\n TContext extends CloudflareExecutionContextLike,\n TReturn,\n>(\n handler: (\n request: TRequest,\n env: TEnv,\n ctx: TContext,\n ) => TReturn | Promise<TReturn>,\n options?: CloudflareWithAutotelOptions<TEnv>,\n): (\n request: TRequest,\n env: TEnv,\n ctx: TContext,\n) => Promise<TReturn> {\n return async (\n request: TRequest,\n env: TEnv,\n executionContext: TContext,\n ): Promise<TReturn> => {\n const spanName =\n typeof options?.spanName === 'function'\n ? options.spanName(request, env)\n : (options?.spanName ?? `cloudflare.${request.method ?? 'request'}`);\n\n const wrapped = trace(\n { name: spanName },\n (ctx) => async (\n innerRequest: TRequest,\n innerEnv: TEnv,\n innerExecutionContext: TContext,\n ) => {\n const log = getRequestLogger(ctx, options?.requestLoggerOptions);\n const auto = enrichFromRequest(innerRequest);\n if (auto && Object.keys(auto).length > 0) {\n log.set(auto);\n }\n const custom = options?.enrich?.(\n innerRequest,\n innerEnv,\n innerExecutionContext,\n );\n if (custom && Object.keys(custom).length > 0) {\n log.set(custom);\n }\n\n requestLoggers.set(innerRequest as object, log);\n try {\n return await handler(innerRequest, innerEnv, innerExecutionContext);\n } finally {\n requestLoggers.delete(innerRequest as object);\n }\n },\n );\n\n return await wrapped(request, env, executionContext);\n };\n}\n\nexport function createCloudflareAdapter<TEnv = unknown>(\n options?: CloudflareWithAutotelOptions<TEnv>,\n) {\n return {\n withAutotelFetch: <\n TRequest extends CloudflareRequestLike,\n TContext extends CloudflareExecutionContextLike,\n TReturn,\n >(\n handler: (\n request: TRequest,\n env: TEnv,\n ctx: TContext,\n ) => TReturn | Promise<TReturn>,\n ) => withAutotelFetch(handler, options),\n useLogger,\n parseError: (error: unknown): ParsedError => parseError(error),\n createStructuredError: (\n input: StructuredErrorInput,\n ): StructuredError => createStructuredError(input),\n createDrainPipeline: <T = unknown>(\n drainOptions?: DrainPipelineOptions<T>,\n ): ((batchDrain: (batch: T[]) => void | Promise<void>) => PipelineDrainFn<T>) =>\n createDrainPipeline(drainOptions),\n };\n}\n\nexport const cloudflareToolkit = createAdapterToolkit<CloudflareRequestLike>({\n adapterName: 'cloudflare',\n enrich: enrichFromRequest,\n});\n\nexport { parseError, createDrainPipeline, createStructuredError };\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/core.ts"],"names":[],"mappings":";;;AAgCO,SAAS,gBACd,OAAA,EACA;AACA,EAAA,OAAO,SAAS,SAAA,CACd,OAAA,EACA,oBAAA,EACe;AACf,IAAA,IAAI,MAAA;AACJ,IAAA,IAAI;AACF,MAAA,MAAA,GAAS,gBAAA,CAAiB,QAAW,oBAAoB,CAAA;AAAA,IAC3D,CAAA,CAAA,MAAQ;AACN,MAAA,MAAM,IAAI,KAAA;AAAA,QACR,CAAA,kBAAA,EAAqB,QAAQ,WAAW,CAAA,2GAAA;AAAA,OAE1C;AAAA,IACF;AAEA,IAAA,IAAI,OAAA,IAAW,QAAQ,MAAA,EAAQ;AAC7B,MAAA,MAAM,KAAA,GAAQ,OAAA,CAAQ,MAAA,CAAO,OAAO,CAAA;AACpC,MAAA,IAAI,SAAS,MAAA,CAAO,IAAA,CAAK,KAAK,CAAA,CAAE,SAAS,CAAA,EAAG;AAC1C,QAAA,MAAA,CAAO,IAAI,KAAK,CAAA;AAAA,MAClB;AAAA,IACF;AAEA,IAAA,OAAO,MAAA;AAAA,EACT,CAAA;AACF;AAEO,SAAS,qBACd,OAAA,EAC0B;AAC1B,EAAA,OAAO;AAAA,IACL,SAAA,EAAW,gBAAgB,OAAO,CAAA;AAAA,IAClC,UAAA;AAAA,IACA,qBAAA;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EAAA,IAAI,CAAC,SAAS,OAAO,MAAA;AACrB,EAAA,IAAI,KAAA,IAAS,OAAA,IAAW,OAAO,OAAA,CAAQ,QAAQ,UAAA,EAAY;AACzD,IAAA,OAAO,OAAA,CAAQ,GAAA,CAAI,IAAI,CAAA,IAAK,MAAA;AAAA,EAC9B;AACA,EAAA,MAAM,UAAA,GAAa,OAAA;AACnB,EAAA,MAAM,QAAQ,UAAA,CAAW,IAAI,KAAK,UAAA,CAAW,IAAA,CAAK,aAAa,CAAA;AAC/D,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA,GAAW,KAAA,GAAQ,MAAA;AAC7C","file":"chunk-GBSB7IX7.js","sourcesContent":["import {\n createDrainPipeline,\n createStructuredError,\n getRequestLogger,\n parseError,\n type ParsedError,\n type RequestLogger,\n type RequestLoggerOptions,\n type RequestLogSnapshot,\n type DrainPipelineOptions,\n type PipelineDrainFn,\n type StructuredError,\n type StructuredErrorInput,\n} from 'autotel';\n\nexport interface AdapterUseLoggerOptions<TContext> {\n adapterName: string;\n enrich?: (context: TContext) => Record<string, unknown> | undefined;\n}\n\nexport interface AdapterToolkit<TContext> {\n useLogger: (\n context?: TContext,\n options?: RequestLoggerOptions,\n ) => RequestLogger;\n parseError: (error: unknown) => ParsedError;\n createStructuredError: (input: StructuredErrorInput) => StructuredError;\n createDrainPipeline: <T = unknown>(\n options?: DrainPipelineOptions<T>,\n ) => (drain: (batch: T[]) => void | Promise<void>) => PipelineDrainFn<T>;\n}\n\nexport function createUseLogger<TContext = unknown>(\n options: AdapterUseLoggerOptions<TContext>,\n) {\n return function useLogger(\n context?: TContext,\n requestLoggerOptions?: RequestLoggerOptions,\n ): RequestLogger {\n let logger: RequestLogger;\n try {\n logger = getRequestLogger(undefined, requestLoggerOptions);\n } catch {\n throw new Error(\n `[autotel-adapters/${options.adapterName}] No active trace context. ` +\n `Wrap your handler with autotel trace instrumentation before calling useLogger().`,\n );\n }\n\n if (context && options.enrich) {\n const extra = options.enrich(context);\n if (extra && Object.keys(extra).length > 0) {\n logger.set(extra);\n }\n }\n\n return logger;\n };\n}\n\nexport function createAdapterToolkit<TContext = unknown>(\n options: AdapterUseLoggerOptions<TContext>,\n): AdapterToolkit<TContext> {\n return {\n useLogger: createUseLogger(options),\n parseError,\n createStructuredError,\n createDrainPipeline,\n };\n}\n\n/**\n * Description of a single adapter config field. `env` is the ordered list of\n * environment variables to fall back to.\n */\nexport interface ConfigField<T> {\n key: keyof T & string;\n env?: string[];\n}\n\nfunction resolveEnv(envKeys?: string[]): string | undefined {\n if (!envKeys) return undefined;\n for (const key of envKeys) {\n const value = process.env[key];\n if (value) return value;\n }\n return undefined;\n}\n\n/**\n * Returns true when at least one env-backed field is not provided via\n * `overrides`, meaning runtime config may still contribute and should be\n * probed to preserve precedence (`overrides > runtime > env`).\n *\n * @example\n * ```ts\n * const FIELDS: ConfigField<MyAdapterConfig>[] = [\n * { key: 'token', env: ['MY_ADAPTER_TOKEN'] },\n * { key: 'endpoint', env: ['MY_ADAPTER_URL'] },\n * ]\n *\n * if (shouldProbeRuntime(FIELDS, overrides)) {\n * runtimeConfig = await loadRuntimeConfig()\n * }\n * ```\n */\nexport function shouldProbeRuntime<T>(\n fields: ConfigField<T>[],\n overrides?: Partial<T>,\n): boolean {\n return fields.some(({ key, env }) => {\n if (!env || env.length === 0) return false;\n if (overrides?.[key] !== undefined) return false;\n return true;\n });\n}\n\n/**\n * Resolve adapter configuration with the standard priority chain:\n *\n * 1. `overrides` passed to the adapter factory\n * 2. `runtimeConfig.autotel.{namespace}.{key}` (if a probe was performed)\n * 3. `runtimeConfig.{namespace}.{key}` (if a probe was performed)\n * 4. `process.env[envKey]` for each env in `field.env`\n *\n * Pass an async `probe` to defer the runtime config lookup so it is only\n * invoked when runtime resolution is needed (i.e. at least one env-backed\n * field is not set by overrides). Adapters that have no probe target may pass\n * `() => Promise.resolve(undefined)`.\n */\nexport async function resolveAdapterConfig<T>(\n namespace: string,\n fields: ConfigField<T>[],\n overrides: Partial<T> | undefined,\n probe: () => Promise<Record<string, any> | undefined>,\n): Promise<Partial<T>> {\n const runtimeConfig = shouldProbeRuntime(fields, overrides)\n ? await probe()\n : undefined;\n const autotelNs = runtimeConfig?.autotel?.[namespace];\n const rootNs = runtimeConfig?.[namespace];\n\n const config: Record<string, unknown> = {};\n for (const { key, env } of fields) {\n config[key] =\n overrides?.[key] ??\n autotelNs?.[key] ??\n rootNs?.[key] ??\n resolveEnv(env);\n }\n\n return config as Partial<T>;\n}\n\nexport type HeadersLike =\n | { get(name: string): string | null }\n | Record<string, string | undefined>;\n\nexport function getHeader(\n headers: HeadersLike | undefined,\n name: string,\n): string | undefined {\n if (!headers) return undefined;\n if ('get' in headers && typeof headers.get === 'function') {\n return headers.get(name) ?? undefined;\n }\n const dictionary = headers as Record<string, string | undefined>;\n const value = dictionary[name] ?? dictionary[name.toLowerCase()];\n return typeof value === 'string' ? value : undefined;\n}\n\nexport type {\n RequestLogger,\n RequestLoggerOptions,\n RequestLogSnapshot,\n ParsedError,\n StructuredError,\n StructuredErrorInput,\n DrainPipelineOptions,\n PipelineDrainFn,\n};\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/next.ts"],"names":[],"mappings":";;;;;AA+BA,IAAM,iBAAA,GAAoB,IAAI,iBAAA,EAAiC;AAE/D,SAAS,kBACP,OAAA,EACqC;AACrC,EAAA,IAAI,CAAC,SAAS,OAAO,MAAA;AAErB,EAAA,IAAI,KAAA,GAAQ,GAAA;AACZ,EAAA,IAAI,QAAQ,GAAA,EAAK;AACf,IAAA,IAAI;AACF,MAAA,KAAA,GAAQ,IAAI,GAAA,CAAI,OAAA,CAAQ,GAAG,CAAA,CAAE,QAAA;AAAA,IAC/B,CAAA,CAAA,MAAQ;AACN,MAAA,KAAA,GAAQ,OAAA,CAAQ,GAAA;AAAA,IAClB;AAAA,EACF;AACA,EAAA,MAAM,SAAA,GAAY,SAAA,CAAU,OAAA,CAAQ,OAAA,EAAS,cAAc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A,CAAO,IAAA,CAAK,IAAI,CAAA,CAAE,SAAS,CAAA,EAAG;AACxC,UAAA,GAAA,CAAI,IAAI,IAAI,CAAA;AAAA,QACd;AACA,QAAA,MAAM,MAAA,GAAS,OAAA,EAAS,MAAA,GAAS,YAAY,CAAA;AAC7C,QAAA,IAAI,UAAU,MAAA,CAAO,IAAA,CAAK,MAAM,CAAA,CAAE,SAAS,CAAA,EAAG;AAC5C,UAAA,GAAA,CAAI,IAAI,MAAM,CAAA;AAAA,QAChB;AACA,QAAA,OAAO,MAAM,kBAAkB,GAAA,CAAI,GAAA,EAAK,YAAY,OAAA,CAAQ,GAAG,SAAS,CAAC,CAAA;AAAA,MAC3E;AAAA,KACF;AACA,IAAA,OAAO,MAAM,OAAA,CAAQ,GAAG,IAAI,CAAA;AAAA,EAC9B,CAAA;AACF;AAEO,SAAS,kBAAkB,OAAA,EAAkC;AAClE,EAAA,OAAO;AAAA,IACL,WAAA,EAAa,CACX,OAAA,KACG,WAAA,CAAY,SAAS,OAAO,CAAA;AAAA,IACjC,WAAW,CACT,OAAA,EACA,oBAAA,KACkB,SAAA,CAAU,SAAS,oBAAoB,CAAA;AAAA,IAC3D,UAAA,EAAY,CAAC,KAAA,KAAgC,UAAA,CAAW,KAAK,CAAA;AAAA,IAC7D,qBAAA,EAAuB,CACrB,KAAA,KACoB,qBAAA,CAAsB,KAAK,CAAA;AAAA,IACjD,mBAAA,EAAqB,CACnB,YAAA,KAEA,mBAAA,CAAoB,YAAY;AAAA,GACpC;AACF;AAEO,IAAM,cAAc,oBAAA,CAAsC;AAAA,EAC/D,WAAA,EAAa,MAAA;AAAA,EACb,MAAA,EAAQ;AACV,CAAC","file":"chunk-GPWONOFN.js","sourcesContent":["import { AsyncLocalStorage } from 'node:async_hooks';\nimport {\n createDrainPipeline,\n getRequestLogger,\n parseError,\n trace,\n createStructuredError,\n type RequestLogger,\n type RequestLoggerOptions,\n type ParsedError,\n type DrainPipelineOptions,\n type PipelineDrainFn,\n type StructuredError,\n type StructuredErrorInput,\n} from 'autotel';\nimport { createAdapterToolkit, createUseLogger, getHeader } from './core';\n\nexport interface NextRequestLike {\n method?: string;\n url?: string;\n headers?:\n | { get(name: string): string | null }\n | Record<string, string | undefined>;\n}\n\nexport interface NextWithAutotelOptions {\n spanName?: string | ((request?: NextRequestLike) => string);\n requestLoggerOptions?: RequestLoggerOptions;\n enrich?: (request?: NextRequestLike) => Record<string, unknown> | undefined;\n}\n\nconst nextLoggerStorage = new AsyncLocalStorage<RequestLogger>();\n\nfunction enrichFromRequest(\n request?: NextRequestLike,\n): Record<string, unknown> | undefined {\n if (!request) return undefined;\n\n let route = '/';\n if (request.url) {\n try {\n route = new URL(request.url).pathname;\n } catch {\n route = request.url;\n }\n }\n const requestId = getHeader(request.headers, 'x-request-id');\n\n return {\n ...(request.method ? { 'http.request.method': request.method } : {}),\n ...(request.url ? { 'url.full': request.url } : {}),\n ...(route ? { 'http.route': route } : {}),\n ...(requestId ? { 'http.request.header.x-request-id': requestId } : {}),\n };\n}\n\nconst baseUseLogger = createUseLogger<NextRequestLike>({\n adapterName: 'next',\n enrich: enrichFromRequest,\n});\n\nexport function useLogger(\n request?: NextRequestLike,\n requestLoggerOptions?: RequestLoggerOptions,\n): RequestLogger {\n const logger = nextLoggerStorage.getStore();\n if (logger) return logger;\n return baseUseLogger(request, requestLoggerOptions);\n}\n\nexport function withAutotel<TArgs extends unknown[], TReturn>(\n handler: (...args: TArgs) => TReturn | Promise<TReturn>,\n options?: NextWithAutotelOptions,\n): (...args: TArgs) => Promise<TReturn> {\n return async (...args: TArgs): Promise<TReturn> => {\n const request = args[0] as NextRequestLike | undefined;\n const spanName =\n typeof options?.spanName === 'function'\n ? options.spanName(request)\n : (options?.spanName ?? 'next.request');\n\n const wrapped = trace(\n { name: spanName },\n (ctx) => async (...innerArgs: TArgs) => {\n const innerRequest = innerArgs[0] as NextRequestLike | undefined;\n const log = getRequestLogger(ctx, options?.requestLoggerOptions);\n const auto = enrichFromRequest(innerRequest);\n if (auto && Object.keys(auto).length > 0) {\n log.set(auto);\n }\n const custom = options?.enrich?.(innerRequest);\n if (custom && Object.keys(custom).length > 0) {\n log.set(custom);\n }\n return await nextLoggerStorage.run(log, async () => handler(...innerArgs));\n },\n );\n return await wrapped(...args);\n };\n}\n\nexport function createNextAdapter(options?: NextWithAutotelOptions) {\n return {\n withAutotel: <TArgs extends unknown[], TReturn>(\n handler: (...args: TArgs) => TReturn | Promise<TReturn>,\n ) => withAutotel(handler, options),\n useLogger: (\n request?: NextRequestLike,\n requestLoggerOptions?: RequestLoggerOptions,\n ): RequestLogger => useLogger(request, requestLoggerOptions),\n parseError: (error: unknown): ParsedError => parseError(error),\n createStructuredError: (\n input: StructuredErrorInput,\n ): StructuredError => createStructuredError(input),\n createDrainPipeline: <T = unknown>(\n drainOptions?: DrainPipelineOptions<T>,\n ): ((batchDrain: (batch: T[]) => void | Promise<void>) => PipelineDrainFn<T>) =>\n createDrainPipeline(drainOptions),\n };\n}\n\nexport const nextToolkit = createAdapterToolkit<NextRequestLike>({\n adapterName: 'next',\n enrich: enrichFromRequest,\n});\n\nexport { parseError, createDrainPipeline, createStructuredError };\n"]}
|