@logbrew/next 0.1.0 → 0.1.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/README.md +103 -21
- package/client.cjs +340 -0
- package/client.d.cts +95 -0
- package/client.d.ts +95 -0
- package/client.js +339 -0
- package/examples/client-route-spans.mjs +38 -0
- package/examples/index.mjs +4 -0
- package/examples/package.json +1 -0
- package/index.cjs +209 -59
- package/index.d.cts +45 -2
- package/index.d.ts +45 -2
- package/index.js +209 -59
- package/package.json +32 -2
- package/release-artifacts.cjs +199 -0
- package/release-artifacts.d.cts +35 -0
- package/release-artifacts.d.ts +35 -0
- package/release-artifacts.js +6 -0
package/index.cjs
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
+
const { AsyncLocalStorage } = require("node:async_hooks");
|
|
1
2
|
const {
|
|
2
3
|
LogBrewClient,
|
|
3
4
|
RecordingTransport,
|
|
4
5
|
SdkError,
|
|
5
|
-
parseTraceparent
|
|
6
|
-
spanAttributesFromTraceparent
|
|
6
|
+
parseTraceparent
|
|
7
7
|
} = require("@logbrew/sdk");
|
|
8
8
|
|
|
9
9
|
const DEFAULT_SDK_NAME = "logbrew-next";
|
|
10
10
|
const DEFAULT_SDK_VERSION = "0.1.0";
|
|
11
|
+
const activeTraceContext = new AsyncLocalStorage();
|
|
11
12
|
|
|
12
13
|
function createLogBrewNextClient({
|
|
13
14
|
apiKey,
|
|
@@ -34,40 +35,47 @@ function withLogBrewRouteHandler(handler, options = {}) {
|
|
|
34
35
|
return async function logBrewRouteHandler(request, context = {}) {
|
|
35
36
|
const client = resolveClient(options, request, context);
|
|
36
37
|
const transport = resolveTransport(options, request, context, client);
|
|
37
|
-
const
|
|
38
|
+
const trace = createRouteTraceContext(request, undefined, options);
|
|
39
|
+
const helpers = createRouteHelpers(client, transport, trace);
|
|
38
40
|
const startedAt = nowMs(options);
|
|
39
41
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
42
|
+
return activeTraceContext.run(trace, async () => {
|
|
43
|
+
try {
|
|
44
|
+
const response = await handler(request, context, helpers);
|
|
45
|
+
ensureResponse(response);
|
|
46
|
+
await captureRouteSuccess(options, { request, response, context, client, transport, startedAt, trace });
|
|
47
|
+
return response;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
await recordRouteError(options, error, { request, context, client, transport, trace });
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
49
53
|
};
|
|
50
54
|
}
|
|
51
55
|
|
|
56
|
+
function getActiveLogBrewTrace() {
|
|
57
|
+
return activeTraceContext.getStore();
|
|
58
|
+
}
|
|
59
|
+
|
|
52
60
|
function createRouteRequestEvent(request, response, {
|
|
53
61
|
now = () => new Date().toISOString(),
|
|
54
62
|
durationMs = 0,
|
|
55
63
|
idFactory = defaultRouteRequestId,
|
|
56
|
-
spanIdFactory = defaultSpanIdFactory
|
|
64
|
+
spanIdFactory = defaultSpanIdFactory,
|
|
65
|
+
trace
|
|
57
66
|
} = {}) {
|
|
58
67
|
const url = safeUrl(request);
|
|
59
68
|
const method = request?.method ?? "GET";
|
|
60
69
|
const statusCode = Number(response?.status ?? 0);
|
|
61
70
|
const id = idFactory(request, response);
|
|
62
|
-
const
|
|
63
|
-
const spanEvent =
|
|
64
|
-
? createTraceparentRouteSpan(
|
|
71
|
+
const routeTrace = trace ?? getActiveLogBrewTrace() ?? createRouteTraceContext(request, response, { spanIdFactory });
|
|
72
|
+
const spanEvent = routeTrace
|
|
73
|
+
? createTraceparentRouteSpan(routeTrace, {
|
|
65
74
|
durationMs,
|
|
66
75
|
id,
|
|
67
76
|
method,
|
|
68
77
|
now,
|
|
69
78
|
pathname: url.pathname,
|
|
70
|
-
spanIdFactory: () => spanIdFactory(request, response),
|
|
71
79
|
statusCode
|
|
72
80
|
})
|
|
73
81
|
: undefined;
|
|
@@ -96,11 +104,13 @@ function createRouteRequestEvent(request, response, {
|
|
|
96
104
|
function createRouteErrorEvent(error, request, {
|
|
97
105
|
includeSearchParams = false,
|
|
98
106
|
now = () => new Date().toISOString(),
|
|
99
|
-
idFactory = defaultRouteErrorId
|
|
107
|
+
idFactory = defaultRouteErrorId,
|
|
108
|
+
trace
|
|
100
109
|
} = {}) {
|
|
101
110
|
const url = safeUrl(request);
|
|
102
111
|
const method = request?.method ?? "GET";
|
|
103
112
|
const message = error instanceof Error ? error.message : String(error);
|
|
113
|
+
const routeTrace = trace ?? getActiveLogBrewTrace();
|
|
104
114
|
return {
|
|
105
115
|
id: idFactory(request),
|
|
106
116
|
timestamp: now(),
|
|
@@ -111,7 +121,38 @@ function createRouteErrorEvent(error, request, {
|
|
|
111
121
|
metadata: {
|
|
112
122
|
method,
|
|
113
123
|
pathname: url.pathname,
|
|
114
|
-
...(includeSearchParams ? { search: url.search || null } : {})
|
|
124
|
+
...(includeSearchParams ? { search: url.search || null } : {}),
|
|
125
|
+
...traceMetadata(routeTrace)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function createRequestMetricEvent(request, response, {
|
|
132
|
+
now = () => new Date().toISOString(),
|
|
133
|
+
durationMs = 0,
|
|
134
|
+
idFactory = defaultRouteMetricId,
|
|
135
|
+
metricName = "http.server.duration",
|
|
136
|
+
routeTemplate
|
|
137
|
+
} = {}) {
|
|
138
|
+
const method = request?.method ?? "GET";
|
|
139
|
+
const resolvedRouteTemplate = routeTemplateOnly(routeTemplate ?? safeUrl(request).pathname);
|
|
140
|
+
const statusCode = Number(response?.status ?? 0);
|
|
141
|
+
return {
|
|
142
|
+
id: idFactory(request, response, resolvedRouteTemplate),
|
|
143
|
+
timestamp: now(),
|
|
144
|
+
attributes: {
|
|
145
|
+
name: metricName,
|
|
146
|
+
kind: "histogram",
|
|
147
|
+
value: Math.max(0, Number(durationMs)),
|
|
148
|
+
unit: "ms",
|
|
149
|
+
temporality: "delta",
|
|
150
|
+
metadata: {
|
|
151
|
+
framework: "nextjs",
|
|
152
|
+
method,
|
|
153
|
+
routeTemplate: resolvedRouteTemplate,
|
|
154
|
+
statusCode,
|
|
155
|
+
statusCodeClass: statusCodeClass(statusCode)
|
|
115
156
|
}
|
|
116
157
|
}
|
|
117
158
|
};
|
|
@@ -134,10 +175,11 @@ function resolveTransport(options, request, context, client) {
|
|
|
134
175
|
return options.transport ?? RecordingTransport.alwaysAccept();
|
|
135
176
|
}
|
|
136
177
|
|
|
137
|
-
function createRouteHelpers(client, transport) {
|
|
178
|
+
function createRouteHelpers(client, transport, trace) {
|
|
138
179
|
return {
|
|
139
180
|
client,
|
|
140
181
|
logbrew: client,
|
|
182
|
+
trace,
|
|
141
183
|
previewJson: () => client.previewJson(),
|
|
142
184
|
flush: () => client.flush(transport),
|
|
143
185
|
shutdown: () => client.shutdown(transport)
|
|
@@ -162,43 +204,63 @@ async function notifyFailure(options, error, context) {
|
|
|
162
204
|
}
|
|
163
205
|
}
|
|
164
206
|
|
|
165
|
-
async function captureRouteSuccess(options, { request, response, context, client, transport, startedAt }) {
|
|
207
|
+
async function captureRouteSuccess(options, { request, response, context, client, transport, startedAt, trace }) {
|
|
166
208
|
try {
|
|
209
|
+
const durationMs = (options.captureRequests !== false || options.captureRequestMetrics === true)
|
|
210
|
+
? routeDurationMs(options, startedAt)
|
|
211
|
+
: 0;
|
|
167
212
|
if (options.captureRequests !== false) {
|
|
168
|
-
recordRouteRequest(options, { request, response, context, client,
|
|
213
|
+
recordRouteRequest(options, { request, response, context, client, durationMs, trace });
|
|
214
|
+
}
|
|
215
|
+
if (options.captureRequestMetrics === true) {
|
|
216
|
+
recordRouteMetric(options, { request, response, context, client, durationMs, trace });
|
|
169
217
|
}
|
|
170
218
|
const flushResponse = await client.shutdown(transport);
|
|
171
|
-
await notifyFlush(options, flushResponse, { request, context, client });
|
|
219
|
+
await notifyFlush(options, flushResponse, { request, context, client, trace });
|
|
172
220
|
} catch (error) {
|
|
173
|
-
await notifyFailure(options, error, { request, context, client });
|
|
221
|
+
await notifyFailure(options, error, { request, context, client, trace });
|
|
174
222
|
}
|
|
175
223
|
}
|
|
176
224
|
|
|
177
|
-
function recordRouteRequest(options, { request, response, context, client,
|
|
178
|
-
const durationMs = Math.max(0, Math.round(nowMs(options) - startedAt));
|
|
225
|
+
function recordRouteRequest(options, { request, response, context, client, durationMs, trace }) {
|
|
179
226
|
const event = typeof options.requestEvent === "function"
|
|
180
|
-
? options.requestEvent(request, response, { client, context, durationMs, request, response })
|
|
227
|
+
? options.requestEvent(request, response, { client, context, durationMs, request, response, trace })
|
|
181
228
|
: createRouteRequestEvent(request, response, {
|
|
182
229
|
...options,
|
|
183
230
|
durationMs,
|
|
184
|
-
idFactory: options.requestIdFactory
|
|
231
|
+
idFactory: options.requestIdFactory,
|
|
232
|
+
trace
|
|
185
233
|
});
|
|
186
234
|
captureRouteRequestEvent(client, event);
|
|
187
235
|
}
|
|
188
236
|
|
|
189
|
-
|
|
237
|
+
function recordRouteMetric(options, { request, response, context, client, durationMs, trace }) {
|
|
238
|
+
const routeTemplate = resolveRouteTemplate(options, request, context);
|
|
239
|
+
const event = typeof options.requestMetricEvent === "function"
|
|
240
|
+
? options.requestMetricEvent(request, response, { client, context, durationMs, request, response, trace })
|
|
241
|
+
: createRequestMetricEvent(request, response, {
|
|
242
|
+
...options,
|
|
243
|
+
durationMs,
|
|
244
|
+
idFactory: options.metricIdFactory,
|
|
245
|
+
routeTemplate
|
|
246
|
+
});
|
|
247
|
+
captureRouteMetricEvent(client, event);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function recordRouteError(options, error, { request, context, client, transport, trace }) {
|
|
190
251
|
if (options.captureErrors === false) {
|
|
191
252
|
return;
|
|
192
253
|
}
|
|
254
|
+
const routeTrace = trace ?? getActiveLogBrewTrace();
|
|
193
255
|
const event = typeof options.errorEvent === "function"
|
|
194
|
-
? options.errorEvent(error, { request, context, client })
|
|
195
|
-
: createRouteErrorEvent(error, request, options);
|
|
256
|
+
? options.errorEvent(error, { request, context, client, trace: routeTrace })
|
|
257
|
+
: createRouteErrorEvent(error, request, { ...options, trace: routeTrace });
|
|
196
258
|
try {
|
|
197
259
|
client.issue(event.id, event.timestamp, event.attributes);
|
|
198
260
|
const flushResponse = await client.shutdown(transport);
|
|
199
|
-
await notifyFlush(options, flushResponse, { request, context, client });
|
|
261
|
+
await notifyFlush(options, flushResponse, { request, context, client, trace: routeTrace });
|
|
200
262
|
} catch (captureError) {
|
|
201
|
-
await notifyFailure(options, captureError, { request, context, client });
|
|
263
|
+
await notifyFailure(options, captureError, { request, context, client, trace: routeTrace });
|
|
202
264
|
}
|
|
203
265
|
}
|
|
204
266
|
|
|
@@ -210,6 +272,10 @@ function captureRouteRequestEvent(client, event) {
|
|
|
210
272
|
client.log(event.id, event.timestamp, event.attributes);
|
|
211
273
|
}
|
|
212
274
|
|
|
275
|
+
function captureRouteMetricEvent(client, event) {
|
|
276
|
+
client.metric(event.id, event.timestamp, event.attributes);
|
|
277
|
+
}
|
|
278
|
+
|
|
213
279
|
function defaultRouteRequestId(request, response) {
|
|
214
280
|
const url = safeUrl(request);
|
|
215
281
|
const slug = `${request?.method ?? "GET"}-${url.pathname}-${response?.status ?? 0}`
|
|
@@ -232,6 +298,15 @@ function defaultRouteErrorId(request) {
|
|
|
232
298
|
return `evt_next_error_${slug || "route"}`;
|
|
233
299
|
}
|
|
234
300
|
|
|
301
|
+
function defaultRouteMetricId(request, response, routeTemplate = safeUrl(request).pathname) {
|
|
302
|
+
const sanitizedRouteTemplate = routeTemplateOnly(routeTemplate);
|
|
303
|
+
const slug = `${request?.method ?? "GET"}-${sanitizedRouteTemplate}-${response?.status ?? 0}`
|
|
304
|
+
.toLowerCase()
|
|
305
|
+
.replace(/[^a-z0-9]+/g, "_")
|
|
306
|
+
.replace(/^_+|_+$/g, "");
|
|
307
|
+
return `evt_next_metric_${slug || "route"}`;
|
|
308
|
+
}
|
|
309
|
+
|
|
235
310
|
function safeUrl(request) {
|
|
236
311
|
try {
|
|
237
312
|
return new URL(request?.url ?? "http://localhost/");
|
|
@@ -248,44 +323,117 @@ function getTraceparentHeader(request) {
|
|
|
248
323
|
return typeof value === "string" ? value : undefined;
|
|
249
324
|
}
|
|
250
325
|
|
|
251
|
-
function
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
326
|
+
function resolveRouteTemplate(options, request, context) {
|
|
327
|
+
const value = typeof options.routeTemplate === "function"
|
|
328
|
+
? options.routeTemplate(request, context)
|
|
329
|
+
: options.routeTemplate;
|
|
330
|
+
return routeTemplateOnly(value ?? safeUrl(request).pathname);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function routeTemplateOnly(value) {
|
|
334
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
335
|
+
return "/";
|
|
336
|
+
}
|
|
337
|
+
try {
|
|
338
|
+
return new URL(value, "http://localhost").pathname || "/";
|
|
339
|
+
} catch {
|
|
340
|
+
return value.split(/[?#]/)[0] || "/";
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function statusCodeClass(statusCode) {
|
|
345
|
+
if (!Number.isFinite(statusCode) || statusCode <= 0) {
|
|
346
|
+
return "unknown";
|
|
347
|
+
}
|
|
348
|
+
return `${Math.floor(statusCode / 100)}xx`;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function createRouteTraceContext(request, response, {
|
|
352
|
+
spanIdFactory = defaultSpanIdFactory
|
|
353
|
+
} = {}) {
|
|
354
|
+
const traceparent = getTraceparentHeader(request);
|
|
260
355
|
if (!traceparent) {
|
|
261
356
|
return undefined;
|
|
262
357
|
}
|
|
263
358
|
|
|
264
359
|
try {
|
|
265
|
-
parseTraceparent(traceparent);
|
|
266
|
-
const spanId = spanIdFactory();
|
|
360
|
+
const context = parseTraceparent(traceparent);
|
|
361
|
+
const spanId = normalizeSpanId(spanIdFactory(request, response));
|
|
362
|
+
if (!spanId) {
|
|
363
|
+
return undefined;
|
|
364
|
+
}
|
|
267
365
|
return {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
durationMs,
|
|
273
|
-
metadata: {
|
|
274
|
-
framework: "nextjs",
|
|
275
|
-
method,
|
|
276
|
-
pathname,
|
|
277
|
-
statusCode
|
|
278
|
-
},
|
|
279
|
-
name: `${method} ${pathname}`,
|
|
280
|
-
spanId,
|
|
281
|
-
status: statusCode >= 500 ? "error" : "ok"
|
|
282
|
-
})
|
|
366
|
+
traceId: context.traceId,
|
|
367
|
+
spanId,
|
|
368
|
+
parentSpanId: context.parentSpanId,
|
|
369
|
+
sampled: context.sampled
|
|
283
370
|
};
|
|
284
371
|
} catch {
|
|
285
372
|
return undefined;
|
|
286
373
|
}
|
|
287
374
|
}
|
|
288
375
|
|
|
376
|
+
function normalizeSpanId(value) {
|
|
377
|
+
if (typeof value !== "string") {
|
|
378
|
+
return undefined;
|
|
379
|
+
}
|
|
380
|
+
const spanId = value.toLowerCase();
|
|
381
|
+
if (!/^[0-9a-f]{16}$/u.test(spanId) || spanId === "0000000000000000") {
|
|
382
|
+
return undefined;
|
|
383
|
+
}
|
|
384
|
+
return spanId;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function traceMetadata(trace) {
|
|
388
|
+
if (!trace) {
|
|
389
|
+
return {};
|
|
390
|
+
}
|
|
391
|
+
return {
|
|
392
|
+
parentSpanId: trace.parentSpanId,
|
|
393
|
+
sampled: trace.sampled,
|
|
394
|
+
spanId: trace.spanId,
|
|
395
|
+
traceId: trace.traceId
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function createTraceparentRouteSpan(traceContext, {
|
|
400
|
+
durationMs,
|
|
401
|
+
id,
|
|
402
|
+
method,
|
|
403
|
+
now,
|
|
404
|
+
pathname,
|
|
405
|
+
statusCode
|
|
406
|
+
}) {
|
|
407
|
+
if (!traceContext) {
|
|
408
|
+
return undefined;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return {
|
|
412
|
+
id,
|
|
413
|
+
timestamp: now(),
|
|
414
|
+
type: "span",
|
|
415
|
+
attributes: {
|
|
416
|
+
name: `${method} ${pathname}`,
|
|
417
|
+
traceId: traceContext.traceId,
|
|
418
|
+
spanId: traceContext.spanId,
|
|
419
|
+
parentSpanId: traceContext.parentSpanId,
|
|
420
|
+
status: statusCode >= 500 ? "error" : "ok",
|
|
421
|
+
durationMs,
|
|
422
|
+
metadata: {
|
|
423
|
+
framework: "nextjs",
|
|
424
|
+
method,
|
|
425
|
+
pathname,
|
|
426
|
+
sampled: traceContext.sampled,
|
|
427
|
+
statusCode
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function routeDurationMs(options, startedAt) {
|
|
434
|
+
return Math.max(0, Math.round(nowMs(options) - startedAt));
|
|
435
|
+
}
|
|
436
|
+
|
|
289
437
|
function nowMs(options) {
|
|
290
438
|
if (typeof options.nowMs === "function") {
|
|
291
439
|
return options.nowMs();
|
|
@@ -316,7 +464,9 @@ function readEnvServerApiKey() {
|
|
|
316
464
|
|
|
317
465
|
module.exports = {
|
|
318
466
|
createLogBrewNextClient,
|
|
467
|
+
createRequestMetricEvent,
|
|
319
468
|
createRouteErrorEvent,
|
|
320
469
|
createRouteRequestEvent,
|
|
470
|
+
getActiveLogBrewTrace,
|
|
321
471
|
withLogBrewRouteHandler
|
|
322
472
|
};
|
package/index.d.cts
CHANGED
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
IssueAttributes,
|
|
3
3
|
LogAttributes,
|
|
4
4
|
LogBrewClient,
|
|
5
|
+
MetricAttributes,
|
|
5
6
|
SpanAttributes,
|
|
6
7
|
Transport,
|
|
7
8
|
TransportResponse
|
|
@@ -17,9 +18,17 @@ export type CreateLogBrewNextClientConfig = {
|
|
|
17
18
|
|
|
18
19
|
export type LogBrewRouteContext = Record<string, unknown>;
|
|
19
20
|
|
|
21
|
+
export type LogBrewTraceContext = {
|
|
22
|
+
traceId: string;
|
|
23
|
+
spanId: string;
|
|
24
|
+
parentSpanId: string;
|
|
25
|
+
sampled: boolean;
|
|
26
|
+
};
|
|
27
|
+
|
|
20
28
|
export type LogBrewRouteHelpers = {
|
|
21
29
|
client: LogBrewClient;
|
|
22
30
|
logbrew: LogBrewClient;
|
|
31
|
+
trace?: LogBrewTraceContext;
|
|
23
32
|
previewJson(): string;
|
|
24
33
|
flush(): Promise<TransportResponse>;
|
|
25
34
|
shutdown(): Promise<TransportResponse>;
|
|
@@ -35,6 +44,7 @@ export type LogBrewRouteRuntimeContext<TContext = LogBrewRouteContext> = {
|
|
|
35
44
|
request: Request;
|
|
36
45
|
context: TContext;
|
|
37
46
|
client: LogBrewClient;
|
|
47
|
+
trace?: LogBrewTraceContext;
|
|
38
48
|
};
|
|
39
49
|
|
|
40
50
|
export type LogBrewRouteRequestRuntimeContext<TContext = LogBrewRouteContext> =
|
|
@@ -74,22 +84,37 @@ export type LogBrewRouteSpanRequestEvent = {
|
|
|
74
84
|
export type LogBrewRouteRequestEvent =
|
|
75
85
|
LogBrewRouteLogRequestEvent | LogBrewRouteSpanRequestEvent;
|
|
76
86
|
|
|
87
|
+
export type LogBrewRouteMetricEvent = {
|
|
88
|
+
id: string;
|
|
89
|
+
timestamp: string;
|
|
90
|
+
attributes: MetricAttributes;
|
|
91
|
+
};
|
|
92
|
+
|
|
77
93
|
export type LogBrewRouteOptions<TContext = LogBrewRouteContext> = CreateLogBrewNextClientConfig & {
|
|
78
94
|
client?: LogBrewClient | LogBrewClientFactory<TContext>;
|
|
79
95
|
transport?: Transport | LogBrewTransportFactory<TContext>;
|
|
80
96
|
captureRequests?: boolean;
|
|
97
|
+
captureRequestMetrics?: boolean;
|
|
81
98
|
captureErrors?: boolean;
|
|
82
99
|
includeSearchParams?: boolean;
|
|
100
|
+
metricName?: string;
|
|
101
|
+
routeTemplate?: string | ((request: Request, context: TContext) => string | null | undefined);
|
|
83
102
|
now?: () => string;
|
|
84
103
|
nowMs?: () => number;
|
|
85
104
|
idFactory?: (request: Request) => string;
|
|
86
105
|
requestIdFactory?: (request: Request, response: Response) => string;
|
|
87
|
-
|
|
106
|
+
metricIdFactory?: (request: Request, response: Response) => string;
|
|
107
|
+
spanIdFactory?: (request: Request, response?: Response) => string;
|
|
88
108
|
requestEvent?: (
|
|
89
109
|
request: Request,
|
|
90
110
|
response: Response,
|
|
91
111
|
context: LogBrewRouteRequestRuntimeContext<TContext>
|
|
92
112
|
) => LogBrewRouteRequestEvent;
|
|
113
|
+
requestMetricEvent?: (
|
|
114
|
+
request: Request,
|
|
115
|
+
response: Response,
|
|
116
|
+
context: LogBrewRouteRequestRuntimeContext<TContext>
|
|
117
|
+
) => LogBrewRouteMetricEvent;
|
|
93
118
|
errorEvent?: (
|
|
94
119
|
error: unknown,
|
|
95
120
|
context: LogBrewRouteRuntimeContext<TContext>
|
|
@@ -113,6 +138,8 @@ export declare function withLogBrewRouteHandler<TContext = LogBrewRouteContext>(
|
|
|
113
138
|
options?: LogBrewRouteOptions<TContext>
|
|
114
139
|
): (request: Request, context?: TContext) => Promise<Response>;
|
|
115
140
|
|
|
141
|
+
export declare function getActiveLogBrewTrace(): LogBrewTraceContext | undefined;
|
|
142
|
+
|
|
116
143
|
export declare function createRouteRequestEvent(
|
|
117
144
|
request: Request,
|
|
118
145
|
response: Response,
|
|
@@ -120,10 +147,23 @@ export declare function createRouteRequestEvent(
|
|
|
120
147
|
now?: () => string;
|
|
121
148
|
durationMs?: number;
|
|
122
149
|
idFactory?: (request: Request, response: Response) => string;
|
|
123
|
-
spanIdFactory?: (request: Request, response
|
|
150
|
+
spanIdFactory?: (request: Request, response?: Response) => string;
|
|
151
|
+
trace?: LogBrewTraceContext;
|
|
124
152
|
}
|
|
125
153
|
): LogBrewRouteRequestEvent;
|
|
126
154
|
|
|
155
|
+
export declare function createRequestMetricEvent(
|
|
156
|
+
request: Request,
|
|
157
|
+
response: Response,
|
|
158
|
+
options?: {
|
|
159
|
+
now?: () => string;
|
|
160
|
+
durationMs?: number;
|
|
161
|
+
idFactory?: (request: Request, response: Response) => string;
|
|
162
|
+
metricName?: string;
|
|
163
|
+
routeTemplate?: string;
|
|
164
|
+
}
|
|
165
|
+
): LogBrewRouteMetricEvent;
|
|
166
|
+
|
|
127
167
|
export declare function createRouteErrorEvent(
|
|
128
168
|
error: unknown,
|
|
129
169
|
request: Request,
|
|
@@ -131,13 +171,16 @@ export declare function createRouteErrorEvent(
|
|
|
131
171
|
includeSearchParams?: boolean;
|
|
132
172
|
now?: () => string;
|
|
133
173
|
idFactory?: (request: Request) => string;
|
|
174
|
+
trace?: LogBrewTraceContext;
|
|
134
175
|
}
|
|
135
176
|
): LogBrewRouteErrorEvent;
|
|
136
177
|
|
|
137
178
|
declare const defaultExport: {
|
|
138
179
|
createLogBrewNextClient: typeof createLogBrewNextClient;
|
|
180
|
+
createRequestMetricEvent: typeof createRequestMetricEvent;
|
|
139
181
|
createRouteErrorEvent: typeof createRouteErrorEvent;
|
|
140
182
|
createRouteRequestEvent: typeof createRouteRequestEvent;
|
|
183
|
+
getActiveLogBrewTrace: typeof getActiveLogBrewTrace;
|
|
141
184
|
withLogBrewRouteHandler: typeof withLogBrewRouteHandler;
|
|
142
185
|
};
|
|
143
186
|
|
package/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
IssueAttributes,
|
|
3
3
|
LogAttributes,
|
|
4
4
|
LogBrewClient,
|
|
5
|
+
MetricAttributes,
|
|
5
6
|
SpanAttributes,
|
|
6
7
|
Transport,
|
|
7
8
|
TransportResponse
|
|
@@ -17,9 +18,17 @@ export type CreateLogBrewNextClientConfig = {
|
|
|
17
18
|
|
|
18
19
|
export type LogBrewRouteContext = Record<string, unknown>;
|
|
19
20
|
|
|
21
|
+
export type LogBrewTraceContext = {
|
|
22
|
+
traceId: string;
|
|
23
|
+
spanId: string;
|
|
24
|
+
parentSpanId: string;
|
|
25
|
+
sampled: boolean;
|
|
26
|
+
};
|
|
27
|
+
|
|
20
28
|
export type LogBrewRouteHelpers = {
|
|
21
29
|
client: LogBrewClient;
|
|
22
30
|
logbrew: LogBrewClient;
|
|
31
|
+
trace?: LogBrewTraceContext;
|
|
23
32
|
previewJson(): string;
|
|
24
33
|
flush(): Promise<TransportResponse>;
|
|
25
34
|
shutdown(): Promise<TransportResponse>;
|
|
@@ -35,6 +44,7 @@ export type LogBrewRouteRuntimeContext<TContext = LogBrewRouteContext> = {
|
|
|
35
44
|
request: Request;
|
|
36
45
|
context: TContext;
|
|
37
46
|
client: LogBrewClient;
|
|
47
|
+
trace?: LogBrewTraceContext;
|
|
38
48
|
};
|
|
39
49
|
|
|
40
50
|
export type LogBrewRouteRequestRuntimeContext<TContext = LogBrewRouteContext> =
|
|
@@ -74,22 +84,37 @@ export type LogBrewRouteSpanRequestEvent = {
|
|
|
74
84
|
export type LogBrewRouteRequestEvent =
|
|
75
85
|
LogBrewRouteLogRequestEvent | LogBrewRouteSpanRequestEvent;
|
|
76
86
|
|
|
87
|
+
export type LogBrewRouteMetricEvent = {
|
|
88
|
+
id: string;
|
|
89
|
+
timestamp: string;
|
|
90
|
+
attributes: MetricAttributes;
|
|
91
|
+
};
|
|
92
|
+
|
|
77
93
|
export type LogBrewRouteOptions<TContext = LogBrewRouteContext> = CreateLogBrewNextClientConfig & {
|
|
78
94
|
client?: LogBrewClient | LogBrewClientFactory<TContext>;
|
|
79
95
|
transport?: Transport | LogBrewTransportFactory<TContext>;
|
|
80
96
|
captureRequests?: boolean;
|
|
97
|
+
captureRequestMetrics?: boolean;
|
|
81
98
|
captureErrors?: boolean;
|
|
82
99
|
includeSearchParams?: boolean;
|
|
100
|
+
metricName?: string;
|
|
101
|
+
routeTemplate?: string | ((request: Request, context: TContext) => string | null | undefined);
|
|
83
102
|
now?: () => string;
|
|
84
103
|
nowMs?: () => number;
|
|
85
104
|
idFactory?: (request: Request) => string;
|
|
86
105
|
requestIdFactory?: (request: Request, response: Response) => string;
|
|
87
|
-
|
|
106
|
+
metricIdFactory?: (request: Request, response: Response) => string;
|
|
107
|
+
spanIdFactory?: (request: Request, response?: Response) => string;
|
|
88
108
|
requestEvent?: (
|
|
89
109
|
request: Request,
|
|
90
110
|
response: Response,
|
|
91
111
|
context: LogBrewRouteRequestRuntimeContext<TContext>
|
|
92
112
|
) => LogBrewRouteRequestEvent;
|
|
113
|
+
requestMetricEvent?: (
|
|
114
|
+
request: Request,
|
|
115
|
+
response: Response,
|
|
116
|
+
context: LogBrewRouteRequestRuntimeContext<TContext>
|
|
117
|
+
) => LogBrewRouteMetricEvent;
|
|
93
118
|
errorEvent?: (
|
|
94
119
|
error: unknown,
|
|
95
120
|
context: LogBrewRouteRuntimeContext<TContext>
|
|
@@ -113,6 +138,8 @@ export declare function withLogBrewRouteHandler<TContext = LogBrewRouteContext>(
|
|
|
113
138
|
options?: LogBrewRouteOptions<TContext>
|
|
114
139
|
): (request: Request, context?: TContext) => Promise<Response>;
|
|
115
140
|
|
|
141
|
+
export declare function getActiveLogBrewTrace(): LogBrewTraceContext | undefined;
|
|
142
|
+
|
|
116
143
|
export declare function createRouteRequestEvent(
|
|
117
144
|
request: Request,
|
|
118
145
|
response: Response,
|
|
@@ -120,10 +147,23 @@ export declare function createRouteRequestEvent(
|
|
|
120
147
|
now?: () => string;
|
|
121
148
|
durationMs?: number;
|
|
122
149
|
idFactory?: (request: Request, response: Response) => string;
|
|
123
|
-
spanIdFactory?: (request: Request, response
|
|
150
|
+
spanIdFactory?: (request: Request, response?: Response) => string;
|
|
151
|
+
trace?: LogBrewTraceContext;
|
|
124
152
|
}
|
|
125
153
|
): LogBrewRouteRequestEvent;
|
|
126
154
|
|
|
155
|
+
export declare function createRequestMetricEvent(
|
|
156
|
+
request: Request,
|
|
157
|
+
response: Response,
|
|
158
|
+
options?: {
|
|
159
|
+
now?: () => string;
|
|
160
|
+
durationMs?: number;
|
|
161
|
+
idFactory?: (request: Request, response: Response) => string;
|
|
162
|
+
metricName?: string;
|
|
163
|
+
routeTemplate?: string;
|
|
164
|
+
}
|
|
165
|
+
): LogBrewRouteMetricEvent;
|
|
166
|
+
|
|
127
167
|
export declare function createRouteErrorEvent(
|
|
128
168
|
error: unknown,
|
|
129
169
|
request: Request,
|
|
@@ -131,13 +171,16 @@ export declare function createRouteErrorEvent(
|
|
|
131
171
|
includeSearchParams?: boolean;
|
|
132
172
|
now?: () => string;
|
|
133
173
|
idFactory?: (request: Request) => string;
|
|
174
|
+
trace?: LogBrewTraceContext;
|
|
134
175
|
}
|
|
135
176
|
): LogBrewRouteErrorEvent;
|
|
136
177
|
|
|
137
178
|
declare const defaultExport: {
|
|
138
179
|
createLogBrewNextClient: typeof createLogBrewNextClient;
|
|
180
|
+
createRequestMetricEvent: typeof createRequestMetricEvent;
|
|
139
181
|
createRouteErrorEvent: typeof createRouteErrorEvent;
|
|
140
182
|
createRouteRequestEvent: typeof createRouteRequestEvent;
|
|
183
|
+
getActiveLogBrewTrace: typeof getActiveLogBrewTrace;
|
|
141
184
|
withLogBrewRouteHandler: typeof withLogBrewRouteHandler;
|
|
142
185
|
};
|
|
143
186
|
|