@logbrew/next 0.1.7 → 0.1.8
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 +2 -2
- package/client.cjs +1 -1
- package/client.js +1 -1
- package/examples/client-route-spans.mjs +1 -1
- package/examples/readme-example.mjs +1 -1
- package/examples/real-user-smoke.mjs +1 -1
- package/index.cjs +79 -44
- package/index.d.cts +2 -0
- package/index.d.ts +2 -0
- package/instrumentation.cjs +6 -8
- package/instrumentation.js +6 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -232,7 +232,7 @@ import {
|
|
|
232
232
|
const client = createLogBrewNextBrowserClient({
|
|
233
233
|
clientKey: "LOGBREW_CLIENT_KEY",
|
|
234
234
|
sdkName: "my-next-app",
|
|
235
|
-
sdkVersion: "0.1.
|
|
235
|
+
sdkVersion: "0.1.8"
|
|
236
236
|
});
|
|
237
237
|
|
|
238
238
|
const routePatterns = [
|
|
@@ -269,7 +269,7 @@ import { createLogBrewNextClient } from "@logbrew/next";
|
|
|
269
269
|
const client = createLogBrewNextClient({
|
|
270
270
|
serverApiKey: "LOGBREW_SERVER_API_KEY",
|
|
271
271
|
sdkName: "my-next-app",
|
|
272
|
-
sdkVersion: "0.1.
|
|
272
|
+
sdkVersion: "0.1.8"
|
|
273
273
|
});
|
|
274
274
|
```
|
|
275
275
|
|
package/client.cjs
CHANGED
package/client.js
CHANGED
|
@@ -24,7 +24,7 @@ const event = captureNextNavigation(client, {
|
|
|
24
24
|
});
|
|
25
25
|
|
|
26
26
|
const payload = JSON.parse(client.previewJson());
|
|
27
|
-
if (!event || event.attributes.metadata.routeTemplate !== "/projects/[projectId]/settings" || event.attributes.metadata.operation !== "browser.navigation" || payload.sdk.version !== "0.1.
|
|
27
|
+
if (!event || event.attributes.metadata.routeTemplate !== "/projects/[projectId]/settings" || event.attributes.metadata.operation !== "browser.navigation" || payload.sdk.version !== "0.1.8") {
|
|
28
28
|
throw new Error(`unexpected Next client route span: ${JSON.stringify(event)}`);
|
|
29
29
|
}
|
|
30
30
|
|
package/index.cjs
CHANGED
|
@@ -24,7 +24,7 @@ function createLogBrewNextClient({
|
|
|
24
24
|
serverApiKey,
|
|
25
25
|
context,
|
|
26
26
|
sdkName = "logbrew-next",
|
|
27
|
-
sdkVersion = "0.1.
|
|
27
|
+
sdkVersion = "0.1.8",
|
|
28
28
|
maxRetries = 2
|
|
29
29
|
} = {}) {
|
|
30
30
|
const authKey = serverApiKey ?? apiKey ?? readEnvServerApiKey() ?? readEnvApiKey();
|
|
@@ -49,11 +49,14 @@ function createLogBrewNextRequestErrorHandler(options = {}) {
|
|
|
49
49
|
const client = resolvedClient.client;
|
|
50
50
|
const transport = resolveTransport(options, { ...runtime, client }, defaultTransport);
|
|
51
51
|
runtime = { ...runtime, client, transport };
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
try {
|
|
53
|
+
const event = typeof options.errorEvent === "function"
|
|
54
|
+
? options.errorEvent(error, runtime)
|
|
55
|
+
: createNextRequestErrorEvent(error, request, context, options);
|
|
56
|
+
client.issue(event.id, event.timestamp, event.attributes);
|
|
57
|
+
} catch (captureError) {
|
|
58
|
+
await notifyFailure(options, captureError, runtime);
|
|
59
|
+
}
|
|
57
60
|
const response = resolvedClient.owned
|
|
58
61
|
? await client.shutdown(transport)
|
|
59
62
|
: await client.flush(transport);
|
|
@@ -93,6 +96,7 @@ function createNextRequestErrorEvent(error, request = {}, context = {}, {
|
|
|
93
96
|
...nextCategoricalMetadata("renderSource", context?.renderSource, NEXT_RENDER_SOURCES),
|
|
94
97
|
...nextCategoricalMetadata("revalidateReason", context?.revalidateReason, NEXT_REVALIDATE_REASONS),
|
|
95
98
|
...nextCategoricalMetadata("renderType", context?.renderType, NEXT_RENDER_TYPES),
|
|
99
|
+
...nextRequestTraceMetadata(request),
|
|
96
100
|
...(errorDigest ? { errorDigest } : {}),
|
|
97
101
|
...(pathname ? { pathname } : {})
|
|
98
102
|
}
|
|
@@ -110,7 +114,7 @@ function withLogBrewRouteHandler(handler, options = {}) {
|
|
|
110
114
|
: undefined;
|
|
111
115
|
|
|
112
116
|
return async function logBrewRouteHandler(request, context = {}) {
|
|
113
|
-
const client = resolveClient(options, request, context);
|
|
117
|
+
const { client, owned } = resolveClient(options, request, context);
|
|
114
118
|
const transport = resolveTransport(options, { request, context, client }, defaultTransport);
|
|
115
119
|
const trace = createRouteTraceContext(request, undefined, options);
|
|
116
120
|
const helpers = createRouteHelpers(client, transport, trace);
|
|
@@ -120,10 +124,10 @@ function withLogBrewRouteHandler(handler, options = {}) {
|
|
|
120
124
|
try {
|
|
121
125
|
const response = await handler(request, context, helpers);
|
|
122
126
|
ensureResponse(response);
|
|
123
|
-
await captureRouteSuccess(options, { request, response, context, client, transport, startedAt, trace });
|
|
127
|
+
await captureRouteSuccess(options, { request, response, context, client, owned, transport, startedAt, trace });
|
|
124
128
|
return response;
|
|
125
129
|
} catch (error) {
|
|
126
|
-
await recordRouteError(options, error, { request, context, client, transport, trace });
|
|
130
|
+
await recordRouteError(options, error, { request, context, client, owned, transport, trace });
|
|
127
131
|
throw error;
|
|
128
132
|
}
|
|
129
133
|
});
|
|
@@ -142,7 +146,7 @@ function createRouteRequestEvent(request, response, {
|
|
|
142
146
|
spanIdFactory = defaultSpanIdFactory,
|
|
143
147
|
trace
|
|
144
148
|
} = {}) {
|
|
145
|
-
const routePath = routeTemplateOnly(routeTemplate
|
|
149
|
+
const routePath = routeTemplateOnly(routeTemplate);
|
|
146
150
|
const method = request?.method ?? "GET";
|
|
147
151
|
const statusCode = Number(response?.status ?? 0);
|
|
148
152
|
const id = idFactory(request, response, routePath);
|
|
@@ -187,7 +191,7 @@ function createRouteErrorEvent(error, request, {
|
|
|
187
191
|
trace
|
|
188
192
|
} = {}) {
|
|
189
193
|
const url = safeUrl(request);
|
|
190
|
-
const routePath = routeTemplateOnly(routeTemplate
|
|
194
|
+
const routePath = routeTemplateOnly(routeTemplate);
|
|
191
195
|
const method = request?.method ?? "GET";
|
|
192
196
|
const message = error instanceof Error ? error.message : String(error);
|
|
193
197
|
const routeTrace = trace ?? getActiveLogBrewTrace();
|
|
@@ -218,7 +222,7 @@ function createRequestMetricEvent(request, response, {
|
|
|
218
222
|
routeTemplate
|
|
219
223
|
} = {}) {
|
|
220
224
|
const method = request?.method ?? "GET";
|
|
221
|
-
const resolvedRouteTemplate = routeTemplateOnly(routeTemplate
|
|
225
|
+
const resolvedRouteTemplate = routeTemplateOnly(routeTemplate);
|
|
222
226
|
const statusCode = Number(response?.status ?? 0);
|
|
223
227
|
return {
|
|
224
228
|
id: idFactory(request, response, resolvedRouteTemplate),
|
|
@@ -243,12 +247,12 @@ function createRequestMetricEvent(request, response, {
|
|
|
243
247
|
|
|
244
248
|
function resolveClient(options, request, context) {
|
|
245
249
|
if (typeof options.client === "function") {
|
|
246
|
-
return options.client({ request, context });
|
|
250
|
+
return { client: options.client({ request, context }), owned: true };
|
|
247
251
|
}
|
|
248
252
|
if (options.client) {
|
|
249
|
-
return options.client;
|
|
253
|
+
return { client: options.client, owned: false };
|
|
250
254
|
}
|
|
251
|
-
return createLogBrewNextClient(options);
|
|
255
|
+
return { client: createLogBrewNextClient(options), owned: true };
|
|
252
256
|
}
|
|
253
257
|
|
|
254
258
|
function resolveTransport(options, runtime, defaultTransport) {
|
|
@@ -293,11 +297,16 @@ async function notifyFlush(options, flushResponse, context) {
|
|
|
293
297
|
|
|
294
298
|
async function notifyFailure(options, error, context) {
|
|
295
299
|
if (typeof options.onCaptureError === "function") {
|
|
296
|
-
|
|
300
|
+
try {
|
|
301
|
+
await options.onCaptureError(error, context);
|
|
302
|
+
} catch {
|
|
303
|
+
// A capture callback must not change the application's response or error.
|
|
304
|
+
}
|
|
297
305
|
}
|
|
298
306
|
}
|
|
299
307
|
|
|
300
|
-
async function captureRouteSuccess(options,
|
|
308
|
+
async function captureRouteSuccess(options, runtime) {
|
|
309
|
+
const { request, response, context, client, startedAt, trace } = runtime;
|
|
301
310
|
try {
|
|
302
311
|
const durationMs = (options.captureRequests !== false || options.captureRequestMetrics === true)
|
|
303
312
|
? routeDurationMs(options, startedAt)
|
|
@@ -308,7 +317,17 @@ async function captureRouteSuccess(options, { request, response, context, client
|
|
|
308
317
|
if (options.captureRequestMetrics === true) {
|
|
309
318
|
recordRouteMetric(options, { request, response, context, client, durationMs, trace });
|
|
310
319
|
}
|
|
311
|
-
|
|
320
|
+
} catch (error) {
|
|
321
|
+
await notifyFailure(options, error, { request, context, client, trace });
|
|
322
|
+
}
|
|
323
|
+
await finalizeRouteCapture(options, runtime);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
async function finalizeRouteCapture(options, { request, context, client, owned, transport, trace }) {
|
|
327
|
+
try {
|
|
328
|
+
const flushResponse = owned
|
|
329
|
+
? await client.shutdown(transport)
|
|
330
|
+
: await client.flush(transport);
|
|
312
331
|
await notifyFlush(options, flushResponse, { request, context, client, trace });
|
|
313
332
|
} catch (error) {
|
|
314
333
|
await notifyFailure(options, error, { request, context, client, trace });
|
|
@@ -341,25 +360,24 @@ function recordRouteMetric(options, { request, response, context, client, durati
|
|
|
341
360
|
captureRouteMetricEvent(client, event);
|
|
342
361
|
}
|
|
343
362
|
|
|
344
|
-
async function recordRouteError(options, error,
|
|
345
|
-
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
363
|
+
async function recordRouteError(options, error, runtime) {
|
|
364
|
+
const { request, context, client, trace } = runtime;
|
|
348
365
|
const routeTrace = trace ?? getActiveLogBrewTrace();
|
|
349
|
-
const event = typeof options.errorEvent === "function"
|
|
350
|
-
? options.errorEvent(error, { request, context, client, trace: routeTrace })
|
|
351
|
-
: createRouteErrorEvent(error, request, {
|
|
352
|
-
...options,
|
|
353
|
-
routeTemplate: resolveRouteTemplate(options, request, context),
|
|
354
|
-
trace: routeTrace
|
|
355
|
-
});
|
|
356
366
|
try {
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
367
|
+
if (options.captureErrors !== false) {
|
|
368
|
+
const event = typeof options.errorEvent === "function"
|
|
369
|
+
? options.errorEvent(error, { request, context, client, trace: routeTrace })
|
|
370
|
+
: createRouteErrorEvent(error, request, {
|
|
371
|
+
...options,
|
|
372
|
+
routeTemplate: resolveRouteTemplate(options, request, context),
|
|
373
|
+
trace: routeTrace
|
|
374
|
+
});
|
|
375
|
+
client.issue(event.id, event.timestamp, event.attributes);
|
|
376
|
+
}
|
|
360
377
|
} catch (captureError) {
|
|
361
378
|
await notifyFailure(options, captureError, { request, context, client, trace: routeTrace });
|
|
362
379
|
}
|
|
380
|
+
await finalizeRouteCapture(options, { ...runtime, trace: routeTrace });
|
|
363
381
|
}
|
|
364
382
|
|
|
365
383
|
function captureRouteRequestEvent(client, event) {
|
|
@@ -415,7 +433,7 @@ function nextRoutePath(value) {
|
|
|
415
433
|
try {
|
|
416
434
|
pathname = new URL(value, "http://localhost").pathname;
|
|
417
435
|
} catch {
|
|
418
|
-
|
|
436
|
+
return "/";
|
|
419
437
|
}
|
|
420
438
|
if (typeof pathname !== "string" || pathname === "") {
|
|
421
439
|
return "/";
|
|
@@ -451,11 +469,35 @@ function nextErrorMessage(error) {
|
|
|
451
469
|
: `${normalized.slice(0, MAX_REQUEST_ERROR_MESSAGE_LENGTH - 1)}…`;
|
|
452
470
|
}
|
|
453
471
|
|
|
472
|
+
function nextRequestTraceMetadata(request) {
|
|
473
|
+
try {
|
|
474
|
+
const traceparent = getTraceparentHeader(request);
|
|
475
|
+
if (!traceparent) {
|
|
476
|
+
return {};
|
|
477
|
+
}
|
|
478
|
+
const trace = parseTraceparent(traceparent);
|
|
479
|
+
return {
|
|
480
|
+
traceId: trace.traceId,
|
|
481
|
+
parentSpanId: trace.parentSpanId,
|
|
482
|
+
sampled: trace.sampled
|
|
483
|
+
};
|
|
484
|
+
} catch {
|
|
485
|
+
return {};
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
454
489
|
function getTraceparentHeader(request) {
|
|
455
|
-
|
|
490
|
+
const headers = request?.headers;
|
|
491
|
+
if (!headers) {
|
|
456
492
|
return undefined;
|
|
457
493
|
}
|
|
458
|
-
|
|
494
|
+
let value;
|
|
495
|
+
if (typeof headers.get === "function") {
|
|
496
|
+
value = headers.get("traceparent");
|
|
497
|
+
} else {
|
|
498
|
+
const names = Object.keys(headers).filter(name => name.toLowerCase() === "traceparent");
|
|
499
|
+
value = names.length === 1 ? headers[names[0]] : undefined;
|
|
500
|
+
}
|
|
459
501
|
return typeof value === "string" ? value : undefined;
|
|
460
502
|
}
|
|
461
503
|
|
|
@@ -463,18 +505,11 @@ function resolveRouteTemplate(options, request, context) {
|
|
|
463
505
|
const value = typeof options.routeTemplate === "function"
|
|
464
506
|
? options.routeTemplate(request, context)
|
|
465
507
|
: options.routeTemplate;
|
|
466
|
-
return routeTemplateOnly(value
|
|
508
|
+
return routeTemplateOnly(value);
|
|
467
509
|
}
|
|
468
510
|
|
|
469
511
|
function routeTemplateOnly(value) {
|
|
470
|
-
|
|
471
|
-
return "/";
|
|
472
|
-
}
|
|
473
|
-
try {
|
|
474
|
-
return new URL(value, "http://localhost").pathname || "/";
|
|
475
|
-
} catch {
|
|
476
|
-
return value.split(/[?#]/)[0] || "/";
|
|
477
|
-
}
|
|
512
|
+
return nextRoutePath(value);
|
|
478
513
|
}
|
|
479
514
|
|
|
480
515
|
function statusCodeClass(statusCode) {
|
package/index.d.cts
CHANGED
|
@@ -170,6 +170,7 @@ export type LogBrewRouteMetricEvent = {
|
|
|
170
170
|
};
|
|
171
171
|
|
|
172
172
|
export type LogBrewRouteOptions<TContext = LogBrewRouteContext> = CreateLogBrewNextClientConfig & NodeFetchTransportConfig & {
|
|
173
|
+
/** A fixed client stays caller-owned and is flushed, not shut down. A factory must return a fresh client per request. */
|
|
173
174
|
client?: LogBrewClient | LogBrewClientFactory<TContext>;
|
|
174
175
|
/** Override the default Node fetch transport, optionally per request. */
|
|
175
176
|
transport?: Transport | LogBrewTransportFactory<TContext>;
|
|
@@ -178,6 +179,7 @@ export type LogBrewRouteOptions<TContext = LogBrewRouteContext> = CreateLogBrewN
|
|
|
178
179
|
captureErrors?: boolean;
|
|
179
180
|
includeSearchParams?: boolean;
|
|
180
181
|
metricName?: string;
|
|
182
|
+
/** Supply a stable route template. Missing or invalid templates use "/" without reading the request pathname. */
|
|
181
183
|
routeTemplate?: string | ((request: Request, context: TContext) => string | null | undefined);
|
|
182
184
|
now?: () => string;
|
|
183
185
|
nowMs?: () => number;
|
package/index.d.ts
CHANGED
|
@@ -170,6 +170,7 @@ export type LogBrewRouteMetricEvent = {
|
|
|
170
170
|
};
|
|
171
171
|
|
|
172
172
|
export type LogBrewRouteOptions<TContext = LogBrewRouteContext> = CreateLogBrewNextClientConfig & NodeFetchTransportConfig & {
|
|
173
|
+
/** A fixed client stays caller-owned and is flushed, not shut down. A factory must return a fresh client per request. */
|
|
173
174
|
client?: LogBrewClient | LogBrewClientFactory<TContext>;
|
|
174
175
|
/** Override the default Node fetch transport, optionally per request. */
|
|
175
176
|
transport?: Transport | LogBrewTransportFactory<TContext>;
|
|
@@ -178,6 +179,7 @@ export type LogBrewRouteOptions<TContext = LogBrewRouteContext> = CreateLogBrewN
|
|
|
178
179
|
captureErrors?: boolean;
|
|
179
180
|
includeSearchParams?: boolean;
|
|
180
181
|
metricName?: string;
|
|
182
|
+
/** Supply a stable route template. Missing or invalid templates use "/" without reading the request pathname. */
|
|
181
183
|
routeTemplate?: string | ((request: Request, context: TContext) => string | null | undefined);
|
|
182
184
|
now?: () => string;
|
|
183
185
|
nowMs?: () => number;
|
package/instrumentation.cjs
CHANGED
|
@@ -3,15 +3,13 @@ let nodeHandlerPromise;
|
|
|
3
3
|
function createLogBrewNextRequestErrorHandler(options = {}) {
|
|
4
4
|
let handler;
|
|
5
5
|
return async function logBrewNextRequestErrorHandler(error, request, context) {
|
|
6
|
-
if (globalThis.process?.env?.NEXT_RUNTIME === "edge") {
|
|
7
|
-
return;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
6
|
try {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
if (process.env.NEXT_RUNTIME !== "edge") {
|
|
8
|
+
nodeHandlerPromise ??= Promise.resolve().then(() => require("./index.cjs"));
|
|
9
|
+
const nodeModule = await nodeHandlerPromise;
|
|
10
|
+
handler ??= nodeModule.createLogBrewNextRequestErrorHandler(options);
|
|
11
|
+
await handler(error, request, context);
|
|
12
|
+
}
|
|
15
13
|
} catch (captureError) {
|
|
16
14
|
if (typeof options.onCaptureError === "function") {
|
|
17
15
|
try {
|
package/instrumentation.js
CHANGED
|
@@ -3,15 +3,13 @@ let nodeHandlerPromise;
|
|
|
3
3
|
export function createLogBrewNextRequestErrorHandler(options = {}) {
|
|
4
4
|
let handler;
|
|
5
5
|
return async function logBrewNextRequestErrorHandler(error, request, context) {
|
|
6
|
-
if (globalThis.process?.env?.NEXT_RUNTIME === "edge") {
|
|
7
|
-
return;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
6
|
try {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
7
|
+
if (process.env.NEXT_RUNTIME !== "edge") {
|
|
8
|
+
nodeHandlerPromise ??= import("./index.js");
|
|
9
|
+
const module = await nodeHandlerPromise;
|
|
10
|
+
handler ??= module.createLogBrewNextRequestErrorHandler(options);
|
|
11
|
+
await handler(error, request, context);
|
|
12
|
+
}
|
|
15
13
|
} catch (captureError) {
|
|
16
14
|
if (typeof options.onCaptureError === "function") {
|
|
17
15
|
try {
|