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