@logbrew/next 0.1.2 → 0.1.4
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 +48 -5
- package/client.cjs +3 -1
- package/client.d.cts +3 -1
- package/client.d.ts +3 -1
- package/client.js +3 -1
- package/examples/client-route-spans.mjs +1 -1
- 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/examples/request-errors.mjs +32 -0
- package/index.cjs +150 -2
- package/index.d.cts +94 -0
- package/index.d.ts +94 -0
- package/index.js +150 -2
- package/instrumentation.cjs +29 -0
- package/instrumentation.d.cts +26 -0
- package/instrumentation.d.ts +26 -0
- package/instrumentation.js +29 -0
- package/package.json +17 -2
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
Next.js App Router helpers for the public LogBrew JavaScript SDK.
|
|
8
8
|
|
|
9
|
-
This package is intentionally thin. It creates server-side LogBrew clients,
|
|
9
|
+
This package is intentionally thin. It creates server-side LogBrew clients, observes App Router request errors through Next.js instrumentation, wraps Route Handlers, captures successful route requests and thrown route errors, and still keeps manual event creation available through the route helpers.
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
@@ -103,6 +103,44 @@ export default withLogBrewNextReleaseArtifacts(
|
|
|
103
103
|
|
|
104
104
|
The helper defaults minified URLs to `app:///_next/static/chunks/...`. Use `minifiedPathPrefix`, `manifestPath`, `repositoryUrl`, `commitSha`, or `stripSourcePrefix` when your deploy needs explicit paths or source-link metadata. Set `LOGBREW_RELEASE_ARTIFACT_TOKEN` in the build environment to a dedicated release-artifact token. Use `tokenEnv` for a different CI variable, or `dryRun: true` to prepare the complete build output without a network request. Existing `compiler.runAfterProductionCompile` work runs first. Upload is disabled when `upload` is omitted, and any unsafe preparation or upload result fails the production build without exposing response text or secret values.
|
|
105
105
|
|
|
106
|
+
## App Router Request Errors
|
|
107
|
+
|
|
108
|
+
Use Next.js's root `instrumentation.ts` file when you want one server error hook
|
|
109
|
+
for React Server Components, Route Handlers, Server Actions, and Proxy. The
|
|
110
|
+
instrumentation subpath stays safe to import in mixed-runtime applications and
|
|
111
|
+
delivers only from the Node.js runtime:
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
// instrumentation.ts
|
|
115
|
+
import { createLogBrewNextRequestErrorHandler } from "@logbrew/next/instrumentation";
|
|
116
|
+
|
|
117
|
+
export const onRequestError = createLogBrewNextRequestErrorHandler({
|
|
118
|
+
serverApiKey: process.env.LOGBREW_SERVER_API_KEY,
|
|
119
|
+
onCaptureError(error) {
|
|
120
|
+
console.error("LogBrew request-error capture failed", error);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Next.js awaits `onRequestError`, and the LogBrew adapter awaits delivery before
|
|
126
|
+
it returns. A delivery or callback failure is reported through
|
|
127
|
+
`onCaptureError` but never replaces the application error that Next.js is
|
|
128
|
+
already handling. The Edge runtime is left unchanged; use a Node.js route or
|
|
129
|
+
server boundary for LogBrew server delivery.
|
|
130
|
+
|
|
131
|
+
By default, the issue includes the error message, Next.js error digest, method,
|
|
132
|
+
stable route file path, router kind, route type, render source, revalidation
|
|
133
|
+
reason, and render type when Next.js supplies them. It does not capture request
|
|
134
|
+
headers, authorization values, cookies, request bodies, query strings, URL
|
|
135
|
+
fragments, concrete dynamic path values, or raw stack text. Set
|
|
136
|
+
`includePathname: true` only when a query-free concrete pathname is intentional
|
|
137
|
+
and acceptable for that application.
|
|
138
|
+
|
|
139
|
+
If the same Route Handler also uses `withLogBrewRouteHandler`, set
|
|
140
|
+
`captureErrors: false` on that wrapper so the global request-error hook is the
|
|
141
|
+
single issue owner. The wrapper can continue to capture successful requests,
|
|
142
|
+
metrics, and trace context.
|
|
143
|
+
|
|
106
144
|
## App Router Route Handler
|
|
107
145
|
|
|
108
146
|
```js
|
|
@@ -174,7 +212,7 @@ export const GET = withLogBrewRouteHandler(handler, {
|
|
|
174
212
|
});
|
|
175
213
|
```
|
|
176
214
|
|
|
177
|
-
The metric
|
|
215
|
+
The metric carries the stable description `Duration of one completed server request.` plus primitive, low-cardinality metadata: `framework`, `method`, `routeTemplate`, `statusCode`, and `statusCodeClass`. Next.js Route Handlers expose standard `Request` and `Response` objects, so pass a stable `routeTemplate` when a route contains dynamic segments. Query strings and hashes are omitted. Avoid user IDs, request payloads, headers, or free-form text in custom metric metadata. Use `metricName`, `metricIdFactory`, or `requestMetricEvent` when your app needs a different naming, description, or metadata policy. Set `captureRequests: false` with `captureRequestMetrics: true` when you only want the duration metric and not the request log/span.
|
|
178
216
|
|
|
179
217
|
The wrapper expects App Router Route Handlers that return standard `Response` objects. It does not call `NextResponse.next()` and does not use the deprecated `middleware` filename. For Next.js 16 request interception, use the framework's `proxy.js` convention separately and keep LogBrew event creation in server-side route code.
|
|
180
218
|
|
|
@@ -194,7 +232,7 @@ import {
|
|
|
194
232
|
const client = createLogBrewNextBrowserClient({
|
|
195
233
|
clientKey: "LOGBREW_CLIENT_KEY",
|
|
196
234
|
sdkName: "my-next-app",
|
|
197
|
-
sdkVersion: "0.1.
|
|
235
|
+
sdkVersion: "0.1.3"
|
|
198
236
|
});
|
|
199
237
|
|
|
200
238
|
const routePatterns = [
|
|
@@ -231,10 +269,15 @@ import { createLogBrewNextClient } from "@logbrew/next";
|
|
|
231
269
|
const client = createLogBrewNextClient({
|
|
232
270
|
serverApiKey: "LOGBREW_SERVER_API_KEY",
|
|
233
271
|
sdkName: "my-next-app",
|
|
234
|
-
sdkVersion: "0.1.
|
|
272
|
+
sdkVersion: "0.1.3"
|
|
235
273
|
});
|
|
236
274
|
```
|
|
237
275
|
|
|
238
276
|
## Example Source
|
|
239
277
|
|
|
240
|
-
The package includes example source for App Router Route Handlers, server-side client creation, and app-owned responses. The packaged server examples use an explicit `RecordingTransport` so they can run offline and print serialized event batches without sending them. Use the default network-delivery snippets above as the starting point for wiring LogBrew into your Next.js application.
|
|
278
|
+
The package includes example source for App Router request-error instrumentation, Route Handlers, server-side client creation, and app-owned responses. The packaged server examples use an explicit `RecordingTransport` so they can run offline and print serialized event batches without sending them. Use the default network-delivery snippets above as the starting point for wiring LogBrew into your Next.js application.
|
|
279
|
+
|
|
280
|
+
```bash
|
|
281
|
+
node node_modules/@logbrew/next/examples/index.mjs request-errors
|
|
282
|
+
npm --prefix node_modules/@logbrew/next/examples run request-errors
|
|
283
|
+
```
|
package/client.cjs
CHANGED
|
@@ -8,11 +8,12 @@ const {
|
|
|
8
8
|
} = require("@logbrew/sdk");
|
|
9
9
|
|
|
10
10
|
const DEFAULT_SDK_NAME = "logbrew-next-client";
|
|
11
|
-
const DEFAULT_SDK_VERSION = "0.1.
|
|
11
|
+
const DEFAULT_SDK_VERSION = "0.1.3";
|
|
12
12
|
|
|
13
13
|
function createLogBrewNextBrowserClient({
|
|
14
14
|
apiKey,
|
|
15
15
|
clientKey,
|
|
16
|
+
context,
|
|
16
17
|
eventFilter,
|
|
17
18
|
sdkName = DEFAULT_SDK_NAME,
|
|
18
19
|
sdkVersion = DEFAULT_SDK_VERSION,
|
|
@@ -26,6 +27,7 @@ function createLogBrewNextBrowserClient({
|
|
|
26
27
|
}
|
|
27
28
|
return LogBrewClient.create({
|
|
28
29
|
apiKey: authKey,
|
|
30
|
+
context,
|
|
29
31
|
eventFilter,
|
|
30
32
|
maxQueueSize,
|
|
31
33
|
maxRetries,
|
package/client.d.cts
CHANGED
|
@@ -3,12 +3,14 @@ import type {
|
|
|
3
3
|
EventFilter,
|
|
4
4
|
LogBrewClient,
|
|
5
5
|
Metadata,
|
|
6
|
-
SpanAttributes
|
|
6
|
+
SpanAttributes,
|
|
7
|
+
TelemetryContext
|
|
7
8
|
} from "@logbrew/sdk";
|
|
8
9
|
|
|
9
10
|
export type CreateLogBrewNextBrowserClientConfig = {
|
|
10
11
|
apiKey?: string;
|
|
11
12
|
clientKey?: string;
|
|
13
|
+
context?: TelemetryContext;
|
|
12
14
|
sdkName?: string;
|
|
13
15
|
sdkVersion?: string;
|
|
14
16
|
maxRetries?: number;
|
package/client.d.ts
CHANGED
|
@@ -3,12 +3,14 @@ import type {
|
|
|
3
3
|
EventFilter,
|
|
4
4
|
LogBrewClient,
|
|
5
5
|
Metadata,
|
|
6
|
-
SpanAttributes
|
|
6
|
+
SpanAttributes,
|
|
7
|
+
TelemetryContext
|
|
7
8
|
} from "@logbrew/sdk";
|
|
8
9
|
|
|
9
10
|
export type CreateLogBrewNextBrowserClientConfig = {
|
|
10
11
|
apiKey?: string;
|
|
11
12
|
clientKey?: string;
|
|
13
|
+
context?: TelemetryContext;
|
|
12
14
|
sdkName?: string;
|
|
13
15
|
sdkVersion?: string;
|
|
14
16
|
maxRetries?: number;
|
package/client.js
CHANGED
|
@@ -8,11 +8,12 @@ import {
|
|
|
8
8
|
} from "@logbrew/sdk";
|
|
9
9
|
|
|
10
10
|
const DEFAULT_SDK_NAME = "logbrew-next-client";
|
|
11
|
-
const DEFAULT_SDK_VERSION = "0.1.
|
|
11
|
+
const DEFAULT_SDK_VERSION = "0.1.3";
|
|
12
12
|
|
|
13
13
|
export function createLogBrewNextBrowserClient({
|
|
14
14
|
apiKey,
|
|
15
15
|
clientKey,
|
|
16
|
+
context,
|
|
16
17
|
eventFilter,
|
|
17
18
|
sdkName = DEFAULT_SDK_NAME,
|
|
18
19
|
sdkVersion = DEFAULT_SDK_VERSION,
|
|
@@ -26,6 +27,7 @@ export function createLogBrewNextBrowserClient({
|
|
|
26
27
|
}
|
|
27
28
|
return LogBrewClient.create({
|
|
28
29
|
apiKey: authKey,
|
|
30
|
+
context,
|
|
29
31
|
eventFilter,
|
|
30
32
|
maxQueueSize,
|
|
31
33
|
maxRetries,
|
|
@@ -13,7 +13,7 @@ const routeTemplate = createNextRouteTemplate({
|
|
|
13
13
|
const client = createLogBrewNextBrowserClient({
|
|
14
14
|
clientKey: "LOGBREW_CLIENT_KEY",
|
|
15
15
|
sdkName: "logbrew-next-client-route-example",
|
|
16
|
-
sdkVersion: "0.1.
|
|
16
|
+
sdkVersion: "0.1.3"
|
|
17
17
|
});
|
|
18
18
|
|
|
19
19
|
const event = captureNextNavigation(client, {
|
package/examples/index.mjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const commands = new Map([
|
|
4
4
|
["client-route-spans", new URL("./client-route-spans.mjs", import.meta.url)],
|
|
5
5
|
["readme-example", new URL("./readme-example.mjs", import.meta.url)],
|
|
6
|
+
["request-errors", new URL("./request-errors.mjs", import.meta.url)],
|
|
6
7
|
["real-user-smoke", new URL("./real-user-smoke.mjs", import.meta.url)]
|
|
7
8
|
]);
|
|
8
9
|
|
|
@@ -25,16 +26,19 @@ function printHelp() {
|
|
|
25
26
|
console.log("node node_modules/@logbrew/next/examples/index.mjs --list");
|
|
26
27
|
console.log("node node_modules/@logbrew/next/examples/index.mjs client-route-spans");
|
|
27
28
|
console.log("node node_modules/@logbrew/next/examples/index.mjs readme-example");
|
|
29
|
+
console.log("node node_modules/@logbrew/next/examples/index.mjs request-errors");
|
|
28
30
|
console.log("node node_modules/@logbrew/next/examples/index.mjs real-user-smoke");
|
|
29
31
|
console.log("node node_modules/@logbrew/next/examples/index.mjs");
|
|
30
32
|
console.log("npm --prefix node_modules/@logbrew/next/examples run list");
|
|
31
33
|
console.log("npm --prefix node_modules/@logbrew/next/examples run client-route-spans");
|
|
32
34
|
console.log("npm --prefix node_modules/@logbrew/next/examples run readme-example");
|
|
35
|
+
console.log("npm --prefix node_modules/@logbrew/next/examples run request-errors");
|
|
33
36
|
console.log("npm --prefix node_modules/@logbrew/next/examples run real-user-smoke");
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
function printList() {
|
|
37
40
|
console.log("client-route-spans -> node node_modules/@logbrew/next/examples/index.mjs client-route-spans");
|
|
38
41
|
console.log("readme-example -> node node_modules/@logbrew/next/examples/index.mjs readme-example");
|
|
42
|
+
console.log("request-errors -> node node_modules/@logbrew/next/examples/index.mjs request-errors");
|
|
39
43
|
console.log("real-user-smoke -> node node_modules/@logbrew/next/examples/index.mjs real-user-smoke");
|
|
40
44
|
}
|
package/examples/package.json
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { RecordingTransport } from "@logbrew/sdk";
|
|
2
|
+
import { createLogBrewNextRequestErrorHandler } from "@logbrew/next/instrumentation";
|
|
3
|
+
|
|
4
|
+
const transport = RecordingTransport.alwaysAccept();
|
|
5
|
+
const onRequestError = createLogBrewNextRequestErrorHandler({
|
|
6
|
+
serverApiKey: "LOGBREW_SERVER_API_KEY",
|
|
7
|
+
transport,
|
|
8
|
+
idFactory: () => "evt_next_request_error_example",
|
|
9
|
+
now: () => "2026-08-01T10:00:00Z"
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const error = Object.assign(new Error("checkout render failed"), {
|
|
13
|
+
digest: "next_digest_example"
|
|
14
|
+
});
|
|
15
|
+
await onRequestError(
|
|
16
|
+
error,
|
|
17
|
+
{
|
|
18
|
+
path: "/orders/example?debug=sample",
|
|
19
|
+
method: "POST",
|
|
20
|
+
headers: {}
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
routerKind: "App Router",
|
|
24
|
+
routePath: "/app/orders/[orderId]/page",
|
|
25
|
+
routeType: "render",
|
|
26
|
+
renderSource: "react-server-components",
|
|
27
|
+
renderType: "dynamic"
|
|
28
|
+
}
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
console.log(transport.lastBody());
|
|
32
|
+
console.error(JSON.stringify({ ok: true, attempts: transport.sentBodies.length }));
|
package/index.cjs
CHANGED
|
@@ -7,12 +7,24 @@ const {
|
|
|
7
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.3";
|
|
11
|
+
const MAX_REQUEST_ERROR_MESSAGE_LENGTH = 2048;
|
|
12
|
+
const MAX_REQUEST_ROUTE_PATH_LENGTH = 512;
|
|
13
|
+
const NEXT_RENDER_SOURCES = new Set([
|
|
14
|
+
"react-server-components",
|
|
15
|
+
"react-server-components-payload",
|
|
16
|
+
"server-rendering"
|
|
17
|
+
]);
|
|
18
|
+
const NEXT_RENDER_TYPES = new Set(["dynamic", "dynamic-resume"]);
|
|
19
|
+
const NEXT_REVALIDATE_REASONS = new Set(["on-demand", "stale"]);
|
|
20
|
+
const NEXT_ROUTE_TYPES = new Set(["action", "middleware", "proxy", "render", "route"]);
|
|
21
|
+
const NEXT_ROUTER_KINDS = new Set(["App Router", "Pages Router"]);
|
|
11
22
|
const activeTraceContext = new AsyncLocalStorage();
|
|
12
23
|
|
|
13
24
|
function createLogBrewNextClient({
|
|
14
25
|
apiKey,
|
|
15
26
|
serverApiKey,
|
|
27
|
+
context,
|
|
16
28
|
sdkName = DEFAULT_SDK_NAME,
|
|
17
29
|
sdkVersion = DEFAULT_SDK_VERSION,
|
|
18
30
|
maxRetries = 2
|
|
@@ -24,7 +36,70 @@ function createLogBrewNextClient({
|
|
|
24
36
|
"createLogBrewNextClient requires serverApiKey, apiKey, LOGBREW_SERVER_API_KEY, or LOGBREW_API_KEY"
|
|
25
37
|
);
|
|
26
38
|
}
|
|
27
|
-
return LogBrewClient.create({ apiKey: authKey, sdkName, sdkVersion, maxRetries });
|
|
39
|
+
return LogBrewClient.create({ apiKey: authKey, context, sdkName, sdkVersion, maxRetries });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function createLogBrewNextRequestErrorHandler(options = {}) {
|
|
43
|
+
const defaultTransport = options.transport === undefined
|
|
44
|
+
? createNodeFetchTransport(options)
|
|
45
|
+
: undefined;
|
|
46
|
+
|
|
47
|
+
return async function logBrewNextRequestErrorHandler(error, request = {}, context = {}) {
|
|
48
|
+
let runtime = { error, request, context };
|
|
49
|
+
try {
|
|
50
|
+
const resolvedClient = resolveRequestErrorClient(options, runtime);
|
|
51
|
+
const client = resolvedClient.client;
|
|
52
|
+
const transport = resolveRequestErrorTransport(options, { ...runtime, client }, defaultTransport);
|
|
53
|
+
runtime = { ...runtime, client, transport };
|
|
54
|
+
const event = typeof options.errorEvent === "function"
|
|
55
|
+
? options.errorEvent(error, runtime)
|
|
56
|
+
: createNextRequestErrorEvent(error, request, context, options);
|
|
57
|
+
|
|
58
|
+
client.issue(event.id, event.timestamp, event.attributes);
|
|
59
|
+
const response = resolvedClient.owned
|
|
60
|
+
? await client.shutdown(transport)
|
|
61
|
+
: await client.flush(transport);
|
|
62
|
+
await notifyFlush(options, response, runtime);
|
|
63
|
+
} catch (captureError) {
|
|
64
|
+
try {
|
|
65
|
+
await notifyFailure(options, captureError, runtime);
|
|
66
|
+
} catch {
|
|
67
|
+
// Observability callbacks must not replace the application error Next.js is handling.
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function createNextRequestErrorEvent(error, request = {}, context = {}, {
|
|
74
|
+
includePathname = false,
|
|
75
|
+
now = () => new Date().toISOString(),
|
|
76
|
+
idFactory = defaultNextRequestErrorId
|
|
77
|
+
} = {}) {
|
|
78
|
+
const method = nextRequestMethod(request);
|
|
79
|
+
const routePath = nextRoutePath(context?.routePath);
|
|
80
|
+
const pathname = includePathname ? nextRoutePath(request?.path) : undefined;
|
|
81
|
+
const errorDigest = nextErrorDigest(error);
|
|
82
|
+
return {
|
|
83
|
+
id: idFactory(error, request, context),
|
|
84
|
+
timestamp: now(),
|
|
85
|
+
attributes: {
|
|
86
|
+
title: `${method} ${routePath} failed`,
|
|
87
|
+
level: "error",
|
|
88
|
+
message: nextErrorMessage(error),
|
|
89
|
+
metadata: {
|
|
90
|
+
framework: "nextjs",
|
|
91
|
+
method,
|
|
92
|
+
routePath,
|
|
93
|
+
...nextCategoricalMetadata("routerKind", context?.routerKind, NEXT_ROUTER_KINDS),
|
|
94
|
+
...nextCategoricalMetadata("routeType", context?.routeType, NEXT_ROUTE_TYPES),
|
|
95
|
+
...nextCategoricalMetadata("renderSource", context?.renderSource, NEXT_RENDER_SOURCES),
|
|
96
|
+
...nextCategoricalMetadata("revalidateReason", context?.revalidateReason, NEXT_REVALIDATE_REASONS),
|
|
97
|
+
...nextCategoricalMetadata("renderType", context?.renderType, NEXT_RENDER_TYPES),
|
|
98
|
+
...(errorDigest ? { errorDigest } : {}),
|
|
99
|
+
...(pathname ? { pathname } : {})
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
28
103
|
}
|
|
29
104
|
|
|
30
105
|
function withLogBrewRouteHandler(handler, options = {}) {
|
|
@@ -147,6 +222,7 @@ function createRequestMetricEvent(request, response, {
|
|
|
147
222
|
timestamp: now(),
|
|
148
223
|
attributes: {
|
|
149
224
|
name: metricName,
|
|
225
|
+
description: "Duration of one completed server request.",
|
|
150
226
|
kind: "histogram",
|
|
151
227
|
value: Math.max(0, Number(durationMs)),
|
|
152
228
|
unit: "ms",
|
|
@@ -179,6 +255,23 @@ function resolveTransport(options, request, context, client, defaultTransport) {
|
|
|
179
255
|
return options.transport ?? defaultTransport;
|
|
180
256
|
}
|
|
181
257
|
|
|
258
|
+
function resolveRequestErrorClient(options, runtime) {
|
|
259
|
+
if (typeof options.client === "function") {
|
|
260
|
+
return { client: options.client(runtime), owned: true };
|
|
261
|
+
}
|
|
262
|
+
if (options.client) {
|
|
263
|
+
return { client: options.client, owned: false };
|
|
264
|
+
}
|
|
265
|
+
return { client: createLogBrewNextClient(options), owned: true };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function resolveRequestErrorTransport(options, runtime, defaultTransport) {
|
|
269
|
+
if (typeof options.transport === "function") {
|
|
270
|
+
return options.transport(runtime);
|
|
271
|
+
}
|
|
272
|
+
return options.transport ?? defaultTransport;
|
|
273
|
+
}
|
|
274
|
+
|
|
182
275
|
function createRouteHelpers(client, transport, trace) {
|
|
183
276
|
return {
|
|
184
277
|
client,
|
|
@@ -302,6 +395,10 @@ function defaultRouteErrorId(request) {
|
|
|
302
395
|
return `evt_next_error_${slug || "route"}`;
|
|
303
396
|
}
|
|
304
397
|
|
|
398
|
+
function defaultNextRequestErrorId() {
|
|
399
|
+
return `evt_next_request_error_${randomHex(8)}`;
|
|
400
|
+
}
|
|
401
|
+
|
|
305
402
|
function defaultRouteMetricId(request, response, routeTemplate = safeUrl(request).pathname) {
|
|
306
403
|
const sanitizedRouteTemplate = routeTemplateOnly(routeTemplate);
|
|
307
404
|
const slug = `${request?.method ?? "GET"}-${sanitizedRouteTemplate}-${response?.status ?? 0}`
|
|
@@ -319,6 +416,55 @@ function safeUrl(request) {
|
|
|
319
416
|
}
|
|
320
417
|
}
|
|
321
418
|
|
|
419
|
+
function nextRequestMethod(request) {
|
|
420
|
+
const method = typeof request?.method === "string" ? request.method.trim().toUpperCase() : "";
|
|
421
|
+
return /^[A-Z]{1,16}$/u.test(method) ? method : "UNKNOWN";
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function nextRoutePath(value) {
|
|
425
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
426
|
+
return "/";
|
|
427
|
+
}
|
|
428
|
+
let pathname;
|
|
429
|
+
try {
|
|
430
|
+
pathname = new URL(value, "http://localhost").pathname;
|
|
431
|
+
} catch {
|
|
432
|
+
pathname = value.split(/[?#]/u)[0];
|
|
433
|
+
}
|
|
434
|
+
if (typeof pathname !== "string" || pathname === "") {
|
|
435
|
+
return "/";
|
|
436
|
+
}
|
|
437
|
+
const normalized = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
438
|
+
return normalized.length <= MAX_REQUEST_ROUTE_PATH_LENGTH ? normalized : "/";
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function nextCategoricalMetadata(key, value, allowed) {
|
|
442
|
+
return typeof value === "string" && allowed.has(value) ? { [key]: value } : {};
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function nextErrorDigest(error) {
|
|
446
|
+
const digest = error && typeof error === "object" ? error.digest : undefined;
|
|
447
|
+
if (typeof digest !== "string" || !/^[a-zA-Z0-9._:-]{1,128}$/u.test(digest)) {
|
|
448
|
+
return undefined;
|
|
449
|
+
}
|
|
450
|
+
return digest;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function nextErrorMessage(error) {
|
|
454
|
+
let message;
|
|
455
|
+
try {
|
|
456
|
+
message = error instanceof Error ? error.message : String(error);
|
|
457
|
+
} catch {
|
|
458
|
+
message = "Next.js request failed";
|
|
459
|
+
}
|
|
460
|
+
const normalized = typeof message === "string" && message.trim() !== ""
|
|
461
|
+
? message
|
|
462
|
+
: "Next.js request failed";
|
|
463
|
+
return normalized.length <= MAX_REQUEST_ERROR_MESSAGE_LENGTH
|
|
464
|
+
? normalized
|
|
465
|
+
: `${normalized.slice(0, MAX_REQUEST_ERROR_MESSAGE_LENGTH - 1)}…`;
|
|
466
|
+
}
|
|
467
|
+
|
|
322
468
|
function getTraceparentHeader(request) {
|
|
323
469
|
if (!request?.headers || typeof request.headers.get !== "function") {
|
|
324
470
|
return undefined;
|
|
@@ -468,6 +614,8 @@ function readEnvServerApiKey() {
|
|
|
468
614
|
|
|
469
615
|
module.exports = {
|
|
470
616
|
createLogBrewNextClient,
|
|
617
|
+
createLogBrewNextRequestErrorHandler,
|
|
618
|
+
createNextRequestErrorEvent,
|
|
471
619
|
createRequestMetricEvent,
|
|
472
620
|
createRouteErrorEvent,
|
|
473
621
|
createRouteRequestEvent,
|
package/index.d.cts
CHANGED
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
LogBrewClient,
|
|
5
5
|
MetricAttributes,
|
|
6
6
|
SpanAttributes,
|
|
7
|
+
TelemetryContext,
|
|
7
8
|
Transport,
|
|
8
9
|
TransportResponse
|
|
9
10
|
} from "@logbrew/sdk";
|
|
@@ -17,11 +18,83 @@ type NodeFetchTransportConfig = {
|
|
|
17
18
|
export type CreateLogBrewNextClientConfig = {
|
|
18
19
|
apiKey?: string;
|
|
19
20
|
serverApiKey?: string;
|
|
21
|
+
context?: TelemetryContext;
|
|
20
22
|
sdkName?: string;
|
|
21
23
|
sdkVersion?: string;
|
|
22
24
|
maxRetries?: number;
|
|
23
25
|
};
|
|
24
26
|
|
|
27
|
+
export type LogBrewNextRequestError = unknown;
|
|
28
|
+
|
|
29
|
+
export type LogBrewNextRequestErrorRequest = Readonly<{
|
|
30
|
+
path: string;
|
|
31
|
+
method: string;
|
|
32
|
+
headers: Readonly<Record<string, string | string[] | undefined>>;
|
|
33
|
+
}>;
|
|
34
|
+
|
|
35
|
+
export type LogBrewNextRequestErrorContext = Readonly<{
|
|
36
|
+
routerKind: "App Router" | "Pages Router";
|
|
37
|
+
routePath: string;
|
|
38
|
+
routeType: "action" | "middleware" | "proxy" | "render" | "route";
|
|
39
|
+
renderSource?: "react-server-components" | "react-server-components-payload" | "server-rendering";
|
|
40
|
+
revalidateReason?: "on-demand" | "stale";
|
|
41
|
+
renderType?: "dynamic" | "dynamic-resume";
|
|
42
|
+
}>;
|
|
43
|
+
|
|
44
|
+
export type LogBrewNextRequestErrorInput = {
|
|
45
|
+
error: LogBrewNextRequestError;
|
|
46
|
+
request: LogBrewNextRequestErrorRequest;
|
|
47
|
+
context: LogBrewNextRequestErrorContext;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type LogBrewNextRequestErrorRuntimeContext = LogBrewNextRequestErrorInput & {
|
|
51
|
+
client?: LogBrewClient;
|
|
52
|
+
transport?: Transport;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type LogBrewNextRequestErrorCaptureContext = LogBrewNextRequestErrorInput & {
|
|
56
|
+
client: LogBrewClient;
|
|
57
|
+
transport: Transport;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type LogBrewNextRequestErrorEvent = {
|
|
61
|
+
id: string;
|
|
62
|
+
timestamp: string;
|
|
63
|
+
attributes: IssueAttributes;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type LogBrewNextRequestErrorHandler = (
|
|
67
|
+
error: LogBrewNextRequestError,
|
|
68
|
+
request: LogBrewNextRequestErrorRequest,
|
|
69
|
+
context: LogBrewNextRequestErrorContext
|
|
70
|
+
) => Promise<void>;
|
|
71
|
+
|
|
72
|
+
export type LogBrewNextRequestErrorOptions = CreateLogBrewNextClientConfig & NodeFetchTransportConfig & {
|
|
73
|
+
/** A fixed client stays caller-owned and is flushed, not shut down. A factory must return a fresh client per error. */
|
|
74
|
+
client?: LogBrewClient | ((input: LogBrewNextRequestErrorInput) => LogBrewClient);
|
|
75
|
+
transport?: Transport | ((context: LogBrewNextRequestErrorInput & { client: LogBrewClient }) => Transport);
|
|
76
|
+
/** Opt into a query-free concrete pathname. Stable routePath metadata remains the default. */
|
|
77
|
+
includePathname?: boolean;
|
|
78
|
+
now?: () => string;
|
|
79
|
+
idFactory?: (
|
|
80
|
+
error: LogBrewNextRequestError,
|
|
81
|
+
request: LogBrewNextRequestErrorRequest,
|
|
82
|
+
context: LogBrewNextRequestErrorContext
|
|
83
|
+
) => string;
|
|
84
|
+
errorEvent?: (
|
|
85
|
+
error: LogBrewNextRequestError,
|
|
86
|
+
context: LogBrewNextRequestErrorCaptureContext
|
|
87
|
+
) => LogBrewNextRequestErrorEvent;
|
|
88
|
+
onFlush?: (
|
|
89
|
+
response: TransportResponse,
|
|
90
|
+
context: LogBrewNextRequestErrorCaptureContext
|
|
91
|
+
) => void | Promise<void>;
|
|
92
|
+
onCaptureError?: (
|
|
93
|
+
error: unknown,
|
|
94
|
+
context: LogBrewNextRequestErrorRuntimeContext
|
|
95
|
+
) => void | Promise<void>;
|
|
96
|
+
};
|
|
97
|
+
|
|
25
98
|
export type LogBrewRouteContext = Record<string, unknown>;
|
|
26
99
|
|
|
27
100
|
export type LogBrewTraceContext = {
|
|
@@ -140,6 +213,25 @@ export declare function createLogBrewNextClient(
|
|
|
140
213
|
config?: CreateLogBrewNextClientConfig
|
|
141
214
|
): LogBrewClient;
|
|
142
215
|
|
|
216
|
+
export declare function createLogBrewNextRequestErrorHandler(
|
|
217
|
+
options?: LogBrewNextRequestErrorOptions
|
|
218
|
+
): LogBrewNextRequestErrorHandler;
|
|
219
|
+
|
|
220
|
+
export declare function createNextRequestErrorEvent(
|
|
221
|
+
error: LogBrewNextRequestError,
|
|
222
|
+
request: LogBrewNextRequestErrorRequest,
|
|
223
|
+
context: LogBrewNextRequestErrorContext,
|
|
224
|
+
options?: {
|
|
225
|
+
includePathname?: boolean;
|
|
226
|
+
now?: () => string;
|
|
227
|
+
idFactory?: (
|
|
228
|
+
error: LogBrewNextRequestError,
|
|
229
|
+
request: LogBrewNextRequestErrorRequest,
|
|
230
|
+
context: LogBrewNextRequestErrorContext
|
|
231
|
+
) => string;
|
|
232
|
+
}
|
|
233
|
+
): LogBrewNextRequestErrorEvent;
|
|
234
|
+
|
|
143
235
|
export declare function withLogBrewRouteHandler<TContext = LogBrewRouteContext>(
|
|
144
236
|
handler: LogBrewRouteHandler<TContext>,
|
|
145
237
|
options?: LogBrewRouteOptions<TContext>
|
|
@@ -184,6 +276,8 @@ export declare function createRouteErrorEvent(
|
|
|
184
276
|
|
|
185
277
|
declare const defaultExport: {
|
|
186
278
|
createLogBrewNextClient: typeof createLogBrewNextClient;
|
|
279
|
+
createLogBrewNextRequestErrorHandler: typeof createLogBrewNextRequestErrorHandler;
|
|
280
|
+
createNextRequestErrorEvent: typeof createNextRequestErrorEvent;
|
|
187
281
|
createRequestMetricEvent: typeof createRequestMetricEvent;
|
|
188
282
|
createRouteErrorEvent: typeof createRouteErrorEvent;
|
|
189
283
|
createRouteRequestEvent: typeof createRouteRequestEvent;
|
package/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
LogBrewClient,
|
|
5
5
|
MetricAttributes,
|
|
6
6
|
SpanAttributes,
|
|
7
|
+
TelemetryContext,
|
|
7
8
|
Transport,
|
|
8
9
|
TransportResponse
|
|
9
10
|
} from "@logbrew/sdk";
|
|
@@ -17,11 +18,83 @@ type NodeFetchTransportConfig = {
|
|
|
17
18
|
export type CreateLogBrewNextClientConfig = {
|
|
18
19
|
apiKey?: string;
|
|
19
20
|
serverApiKey?: string;
|
|
21
|
+
context?: TelemetryContext;
|
|
20
22
|
sdkName?: string;
|
|
21
23
|
sdkVersion?: string;
|
|
22
24
|
maxRetries?: number;
|
|
23
25
|
};
|
|
24
26
|
|
|
27
|
+
export type LogBrewNextRequestError = unknown;
|
|
28
|
+
|
|
29
|
+
export type LogBrewNextRequestErrorRequest = Readonly<{
|
|
30
|
+
path: string;
|
|
31
|
+
method: string;
|
|
32
|
+
headers: Readonly<Record<string, string | string[] | undefined>>;
|
|
33
|
+
}>;
|
|
34
|
+
|
|
35
|
+
export type LogBrewNextRequestErrorContext = Readonly<{
|
|
36
|
+
routerKind: "App Router" | "Pages Router";
|
|
37
|
+
routePath: string;
|
|
38
|
+
routeType: "action" | "middleware" | "proxy" | "render" | "route";
|
|
39
|
+
renderSource?: "react-server-components" | "react-server-components-payload" | "server-rendering";
|
|
40
|
+
revalidateReason?: "on-demand" | "stale";
|
|
41
|
+
renderType?: "dynamic" | "dynamic-resume";
|
|
42
|
+
}>;
|
|
43
|
+
|
|
44
|
+
export type LogBrewNextRequestErrorInput = {
|
|
45
|
+
error: LogBrewNextRequestError;
|
|
46
|
+
request: LogBrewNextRequestErrorRequest;
|
|
47
|
+
context: LogBrewNextRequestErrorContext;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type LogBrewNextRequestErrorRuntimeContext = LogBrewNextRequestErrorInput & {
|
|
51
|
+
client?: LogBrewClient;
|
|
52
|
+
transport?: Transport;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type LogBrewNextRequestErrorCaptureContext = LogBrewNextRequestErrorInput & {
|
|
56
|
+
client: LogBrewClient;
|
|
57
|
+
transport: Transport;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type LogBrewNextRequestErrorEvent = {
|
|
61
|
+
id: string;
|
|
62
|
+
timestamp: string;
|
|
63
|
+
attributes: IssueAttributes;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type LogBrewNextRequestErrorHandler = (
|
|
67
|
+
error: LogBrewNextRequestError,
|
|
68
|
+
request: LogBrewNextRequestErrorRequest,
|
|
69
|
+
context: LogBrewNextRequestErrorContext
|
|
70
|
+
) => Promise<void>;
|
|
71
|
+
|
|
72
|
+
export type LogBrewNextRequestErrorOptions = CreateLogBrewNextClientConfig & NodeFetchTransportConfig & {
|
|
73
|
+
/** A fixed client stays caller-owned and is flushed, not shut down. A factory must return a fresh client per error. */
|
|
74
|
+
client?: LogBrewClient | ((input: LogBrewNextRequestErrorInput) => LogBrewClient);
|
|
75
|
+
transport?: Transport | ((context: LogBrewNextRequestErrorInput & { client: LogBrewClient }) => Transport);
|
|
76
|
+
/** Opt into a query-free concrete pathname. Stable routePath metadata remains the default. */
|
|
77
|
+
includePathname?: boolean;
|
|
78
|
+
now?: () => string;
|
|
79
|
+
idFactory?: (
|
|
80
|
+
error: LogBrewNextRequestError,
|
|
81
|
+
request: LogBrewNextRequestErrorRequest,
|
|
82
|
+
context: LogBrewNextRequestErrorContext
|
|
83
|
+
) => string;
|
|
84
|
+
errorEvent?: (
|
|
85
|
+
error: LogBrewNextRequestError,
|
|
86
|
+
context: LogBrewNextRequestErrorCaptureContext
|
|
87
|
+
) => LogBrewNextRequestErrorEvent;
|
|
88
|
+
onFlush?: (
|
|
89
|
+
response: TransportResponse,
|
|
90
|
+
context: LogBrewNextRequestErrorCaptureContext
|
|
91
|
+
) => void | Promise<void>;
|
|
92
|
+
onCaptureError?: (
|
|
93
|
+
error: unknown,
|
|
94
|
+
context: LogBrewNextRequestErrorRuntimeContext
|
|
95
|
+
) => void | Promise<void>;
|
|
96
|
+
};
|
|
97
|
+
|
|
25
98
|
export type LogBrewRouteContext = Record<string, unknown>;
|
|
26
99
|
|
|
27
100
|
export type LogBrewTraceContext = {
|
|
@@ -140,6 +213,25 @@ export declare function createLogBrewNextClient(
|
|
|
140
213
|
config?: CreateLogBrewNextClientConfig
|
|
141
214
|
): LogBrewClient;
|
|
142
215
|
|
|
216
|
+
export declare function createLogBrewNextRequestErrorHandler(
|
|
217
|
+
options?: LogBrewNextRequestErrorOptions
|
|
218
|
+
): LogBrewNextRequestErrorHandler;
|
|
219
|
+
|
|
220
|
+
export declare function createNextRequestErrorEvent(
|
|
221
|
+
error: LogBrewNextRequestError,
|
|
222
|
+
request: LogBrewNextRequestErrorRequest,
|
|
223
|
+
context: LogBrewNextRequestErrorContext,
|
|
224
|
+
options?: {
|
|
225
|
+
includePathname?: boolean;
|
|
226
|
+
now?: () => string;
|
|
227
|
+
idFactory?: (
|
|
228
|
+
error: LogBrewNextRequestError,
|
|
229
|
+
request: LogBrewNextRequestErrorRequest,
|
|
230
|
+
context: LogBrewNextRequestErrorContext
|
|
231
|
+
) => string;
|
|
232
|
+
}
|
|
233
|
+
): LogBrewNextRequestErrorEvent;
|
|
234
|
+
|
|
143
235
|
export declare function withLogBrewRouteHandler<TContext = LogBrewRouteContext>(
|
|
144
236
|
handler: LogBrewRouteHandler<TContext>,
|
|
145
237
|
options?: LogBrewRouteOptions<TContext>
|
|
@@ -184,6 +276,8 @@ export declare function createRouteErrorEvent(
|
|
|
184
276
|
|
|
185
277
|
declare const defaultExport: {
|
|
186
278
|
createLogBrewNextClient: typeof createLogBrewNextClient;
|
|
279
|
+
createLogBrewNextRequestErrorHandler: typeof createLogBrewNextRequestErrorHandler;
|
|
280
|
+
createNextRequestErrorEvent: typeof createNextRequestErrorEvent;
|
|
187
281
|
createRequestMetricEvent: typeof createRequestMetricEvent;
|
|
188
282
|
createRouteErrorEvent: typeof createRouteErrorEvent;
|
|
189
283
|
createRouteRequestEvent: typeof createRouteRequestEvent;
|
package/index.js
CHANGED
|
@@ -7,12 +7,24 @@ import {
|
|
|
7
7
|
import { createNodeFetchTransport } from "@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.3";
|
|
11
|
+
const MAX_REQUEST_ERROR_MESSAGE_LENGTH = 2048;
|
|
12
|
+
const MAX_REQUEST_ROUTE_PATH_LENGTH = 512;
|
|
13
|
+
const NEXT_RENDER_SOURCES = new Set([
|
|
14
|
+
"react-server-components",
|
|
15
|
+
"react-server-components-payload",
|
|
16
|
+
"server-rendering"
|
|
17
|
+
]);
|
|
18
|
+
const NEXT_RENDER_TYPES = new Set(["dynamic", "dynamic-resume"]);
|
|
19
|
+
const NEXT_REVALIDATE_REASONS = new Set(["on-demand", "stale"]);
|
|
20
|
+
const NEXT_ROUTE_TYPES = new Set(["action", "middleware", "proxy", "render", "route"]);
|
|
21
|
+
const NEXT_ROUTER_KINDS = new Set(["App Router", "Pages Router"]);
|
|
11
22
|
const activeTraceContext = new AsyncLocalStorage();
|
|
12
23
|
|
|
13
24
|
export function createLogBrewNextClient({
|
|
14
25
|
apiKey,
|
|
15
26
|
serverApiKey,
|
|
27
|
+
context,
|
|
16
28
|
sdkName = DEFAULT_SDK_NAME,
|
|
17
29
|
sdkVersion = DEFAULT_SDK_VERSION,
|
|
18
30
|
maxRetries = 2
|
|
@@ -24,7 +36,70 @@ export function createLogBrewNextClient({
|
|
|
24
36
|
"createLogBrewNextClient requires serverApiKey, apiKey, LOGBREW_SERVER_API_KEY, or LOGBREW_API_KEY"
|
|
25
37
|
);
|
|
26
38
|
}
|
|
27
|
-
return LogBrewClient.create({ apiKey: authKey, sdkName, sdkVersion, maxRetries });
|
|
39
|
+
return LogBrewClient.create({ apiKey: authKey, context, sdkName, sdkVersion, maxRetries });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function createLogBrewNextRequestErrorHandler(options = {}) {
|
|
43
|
+
const defaultTransport = options.transport === undefined
|
|
44
|
+
? createNodeFetchTransport(options)
|
|
45
|
+
: undefined;
|
|
46
|
+
|
|
47
|
+
return async function logBrewNextRequestErrorHandler(error, request = {}, context = {}) {
|
|
48
|
+
let runtime = { error, request, context };
|
|
49
|
+
try {
|
|
50
|
+
const resolvedClient = resolveRequestErrorClient(options, runtime);
|
|
51
|
+
const client = resolvedClient.client;
|
|
52
|
+
const transport = resolveRequestErrorTransport(options, { ...runtime, client }, defaultTransport);
|
|
53
|
+
runtime = { ...runtime, client, transport };
|
|
54
|
+
const event = typeof options.errorEvent === "function"
|
|
55
|
+
? options.errorEvent(error, runtime)
|
|
56
|
+
: createNextRequestErrorEvent(error, request, context, options);
|
|
57
|
+
|
|
58
|
+
client.issue(event.id, event.timestamp, event.attributes);
|
|
59
|
+
const response = resolvedClient.owned
|
|
60
|
+
? await client.shutdown(transport)
|
|
61
|
+
: await client.flush(transport);
|
|
62
|
+
await notifyFlush(options, response, runtime);
|
|
63
|
+
} catch (captureError) {
|
|
64
|
+
try {
|
|
65
|
+
await notifyFailure(options, captureError, runtime);
|
|
66
|
+
} catch {
|
|
67
|
+
// Observability callbacks must not replace the application error Next.js is handling.
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function createNextRequestErrorEvent(error, request = {}, context = {}, {
|
|
74
|
+
includePathname = false,
|
|
75
|
+
now = () => new Date().toISOString(),
|
|
76
|
+
idFactory = defaultNextRequestErrorId
|
|
77
|
+
} = {}) {
|
|
78
|
+
const method = nextRequestMethod(request);
|
|
79
|
+
const routePath = nextRoutePath(context?.routePath);
|
|
80
|
+
const pathname = includePathname ? nextRoutePath(request?.path) : undefined;
|
|
81
|
+
const errorDigest = nextErrorDigest(error);
|
|
82
|
+
return {
|
|
83
|
+
id: idFactory(error, request, context),
|
|
84
|
+
timestamp: now(),
|
|
85
|
+
attributes: {
|
|
86
|
+
title: `${method} ${routePath} failed`,
|
|
87
|
+
level: "error",
|
|
88
|
+
message: nextErrorMessage(error),
|
|
89
|
+
metadata: {
|
|
90
|
+
framework: "nextjs",
|
|
91
|
+
method,
|
|
92
|
+
routePath,
|
|
93
|
+
...nextCategoricalMetadata("routerKind", context?.routerKind, NEXT_ROUTER_KINDS),
|
|
94
|
+
...nextCategoricalMetadata("routeType", context?.routeType, NEXT_ROUTE_TYPES),
|
|
95
|
+
...nextCategoricalMetadata("renderSource", context?.renderSource, NEXT_RENDER_SOURCES),
|
|
96
|
+
...nextCategoricalMetadata("revalidateReason", context?.revalidateReason, NEXT_REVALIDATE_REASONS),
|
|
97
|
+
...nextCategoricalMetadata("renderType", context?.renderType, NEXT_RENDER_TYPES),
|
|
98
|
+
...(errorDigest ? { errorDigest } : {}),
|
|
99
|
+
...(pathname ? { pathname } : {})
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
28
103
|
}
|
|
29
104
|
|
|
30
105
|
export function withLogBrewRouteHandler(handler, options = {}) {
|
|
@@ -147,6 +222,7 @@ export function createRequestMetricEvent(request, response, {
|
|
|
147
222
|
timestamp: now(),
|
|
148
223
|
attributes: {
|
|
149
224
|
name: metricName,
|
|
225
|
+
description: "Duration of one completed server request.",
|
|
150
226
|
kind: "histogram",
|
|
151
227
|
value: Math.max(0, Number(durationMs)),
|
|
152
228
|
unit: "ms",
|
|
@@ -179,6 +255,23 @@ function resolveTransport(options, request, context, client, defaultTransport) {
|
|
|
179
255
|
return options.transport ?? defaultTransport;
|
|
180
256
|
}
|
|
181
257
|
|
|
258
|
+
function resolveRequestErrorClient(options, runtime) {
|
|
259
|
+
if (typeof options.client === "function") {
|
|
260
|
+
return { client: options.client(runtime), owned: true };
|
|
261
|
+
}
|
|
262
|
+
if (options.client) {
|
|
263
|
+
return { client: options.client, owned: false };
|
|
264
|
+
}
|
|
265
|
+
return { client: createLogBrewNextClient(options), owned: true };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function resolveRequestErrorTransport(options, runtime, defaultTransport) {
|
|
269
|
+
if (typeof options.transport === "function") {
|
|
270
|
+
return options.transport(runtime);
|
|
271
|
+
}
|
|
272
|
+
return options.transport ?? defaultTransport;
|
|
273
|
+
}
|
|
274
|
+
|
|
182
275
|
function createRouteHelpers(client, transport, trace) {
|
|
183
276
|
return {
|
|
184
277
|
client,
|
|
@@ -302,6 +395,10 @@ function defaultRouteErrorId(request) {
|
|
|
302
395
|
return `evt_next_error_${slug || "route"}`;
|
|
303
396
|
}
|
|
304
397
|
|
|
398
|
+
function defaultNextRequestErrorId() {
|
|
399
|
+
return `evt_next_request_error_${randomHex(8)}`;
|
|
400
|
+
}
|
|
401
|
+
|
|
305
402
|
function defaultRouteMetricId(request, response, routeTemplate = safeUrl(request).pathname) {
|
|
306
403
|
const sanitizedRouteTemplate = routeTemplateOnly(routeTemplate);
|
|
307
404
|
const slug = `${request?.method ?? "GET"}-${sanitizedRouteTemplate}-${response?.status ?? 0}`
|
|
@@ -319,6 +416,55 @@ function safeUrl(request) {
|
|
|
319
416
|
}
|
|
320
417
|
}
|
|
321
418
|
|
|
419
|
+
function nextRequestMethod(request) {
|
|
420
|
+
const method = typeof request?.method === "string" ? request.method.trim().toUpperCase() : "";
|
|
421
|
+
return /^[A-Z]{1,16}$/u.test(method) ? method : "UNKNOWN";
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function nextRoutePath(value) {
|
|
425
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
426
|
+
return "/";
|
|
427
|
+
}
|
|
428
|
+
let pathname;
|
|
429
|
+
try {
|
|
430
|
+
pathname = new URL(value, "http://localhost").pathname;
|
|
431
|
+
} catch {
|
|
432
|
+
pathname = value.split(/[?#]/u)[0];
|
|
433
|
+
}
|
|
434
|
+
if (typeof pathname !== "string" || pathname === "") {
|
|
435
|
+
return "/";
|
|
436
|
+
}
|
|
437
|
+
const normalized = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
438
|
+
return normalized.length <= MAX_REQUEST_ROUTE_PATH_LENGTH ? normalized : "/";
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
function nextCategoricalMetadata(key, value, allowed) {
|
|
442
|
+
return typeof value === "string" && allowed.has(value) ? { [key]: value } : {};
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function nextErrorDigest(error) {
|
|
446
|
+
const digest = error && typeof error === "object" ? error.digest : undefined;
|
|
447
|
+
if (typeof digest !== "string" || !/^[a-zA-Z0-9._:-]{1,128}$/u.test(digest)) {
|
|
448
|
+
return undefined;
|
|
449
|
+
}
|
|
450
|
+
return digest;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function nextErrorMessage(error) {
|
|
454
|
+
let message;
|
|
455
|
+
try {
|
|
456
|
+
message = error instanceof Error ? error.message : String(error);
|
|
457
|
+
} catch {
|
|
458
|
+
message = "Next.js request failed";
|
|
459
|
+
}
|
|
460
|
+
const normalized = typeof message === "string" && message.trim() !== ""
|
|
461
|
+
? message
|
|
462
|
+
: "Next.js request failed";
|
|
463
|
+
return normalized.length <= MAX_REQUEST_ERROR_MESSAGE_LENGTH
|
|
464
|
+
? normalized
|
|
465
|
+
: `${normalized.slice(0, MAX_REQUEST_ERROR_MESSAGE_LENGTH - 1)}…`;
|
|
466
|
+
}
|
|
467
|
+
|
|
322
468
|
function getTraceparentHeader(request) {
|
|
323
469
|
if (!request?.headers || typeof request.headers.get !== "function") {
|
|
324
470
|
return undefined;
|
|
@@ -468,6 +614,8 @@ function readEnvServerApiKey() {
|
|
|
468
614
|
|
|
469
615
|
export default {
|
|
470
616
|
createLogBrewNextClient,
|
|
617
|
+
createLogBrewNextRequestErrorHandler,
|
|
618
|
+
createNextRequestErrorEvent,
|
|
471
619
|
createRequestMetricEvent,
|
|
472
620
|
createRouteErrorEvent,
|
|
473
621
|
createRouteRequestEvent,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
let nodeHandlerPromise;
|
|
2
|
+
|
|
3
|
+
function createLogBrewNextRequestErrorHandler(options = {}) {
|
|
4
|
+
let handler;
|
|
5
|
+
return async function logBrewNextRequestErrorHandler(error, request, context) {
|
|
6
|
+
if (globalThis.process?.env?.NEXT_RUNTIME === "edge") {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
nodeHandlerPromise ??= Promise.resolve().then(() => require("./index.cjs"));
|
|
12
|
+
const module = await nodeHandlerPromise;
|
|
13
|
+
handler ??= module.createLogBrewNextRequestErrorHandler(options);
|
|
14
|
+
await handler(error, request, context);
|
|
15
|
+
} catch (captureError) {
|
|
16
|
+
if (typeof options.onCaptureError === "function") {
|
|
17
|
+
try {
|
|
18
|
+
await options.onCaptureError(captureError, { error, request, context });
|
|
19
|
+
} catch {
|
|
20
|
+
// Observability callbacks must not replace the application error Next.js is handling.
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = {
|
|
28
|
+
createLogBrewNextRequestErrorHandler
|
|
29
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
LogBrewNextRequestErrorHandler,
|
|
3
|
+
LogBrewNextRequestErrorOptions
|
|
4
|
+
} from "./index.cjs";
|
|
5
|
+
|
|
6
|
+
export type {
|
|
7
|
+
LogBrewNextRequestError,
|
|
8
|
+
LogBrewNextRequestErrorCaptureContext,
|
|
9
|
+
LogBrewNextRequestErrorContext,
|
|
10
|
+
LogBrewNextRequestErrorEvent,
|
|
11
|
+
LogBrewNextRequestErrorHandler,
|
|
12
|
+
LogBrewNextRequestErrorInput,
|
|
13
|
+
LogBrewNextRequestErrorOptions,
|
|
14
|
+
LogBrewNextRequestErrorRequest,
|
|
15
|
+
LogBrewNextRequestErrorRuntimeContext
|
|
16
|
+
} from "./index.cjs";
|
|
17
|
+
|
|
18
|
+
export declare function createLogBrewNextRequestErrorHandler(
|
|
19
|
+
options?: LogBrewNextRequestErrorOptions
|
|
20
|
+
): LogBrewNextRequestErrorHandler;
|
|
21
|
+
|
|
22
|
+
declare const defaultExport: {
|
|
23
|
+
createLogBrewNextRequestErrorHandler: typeof createLogBrewNextRequestErrorHandler;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export default defaultExport;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
LogBrewNextRequestErrorHandler,
|
|
3
|
+
LogBrewNextRequestErrorOptions
|
|
4
|
+
} from "./index.js";
|
|
5
|
+
|
|
6
|
+
export type {
|
|
7
|
+
LogBrewNextRequestError,
|
|
8
|
+
LogBrewNextRequestErrorCaptureContext,
|
|
9
|
+
LogBrewNextRequestErrorContext,
|
|
10
|
+
LogBrewNextRequestErrorEvent,
|
|
11
|
+
LogBrewNextRequestErrorHandler,
|
|
12
|
+
LogBrewNextRequestErrorInput,
|
|
13
|
+
LogBrewNextRequestErrorOptions,
|
|
14
|
+
LogBrewNextRequestErrorRequest,
|
|
15
|
+
LogBrewNextRequestErrorRuntimeContext
|
|
16
|
+
} from "./index.js";
|
|
17
|
+
|
|
18
|
+
export declare function createLogBrewNextRequestErrorHandler(
|
|
19
|
+
options?: LogBrewNextRequestErrorOptions
|
|
20
|
+
): LogBrewNextRequestErrorHandler;
|
|
21
|
+
|
|
22
|
+
declare const defaultExport: {
|
|
23
|
+
createLogBrewNextRequestErrorHandler: typeof createLogBrewNextRequestErrorHandler;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export default defaultExport;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
let nodeHandlerPromise;
|
|
2
|
+
|
|
3
|
+
export function createLogBrewNextRequestErrorHandler(options = {}) {
|
|
4
|
+
let handler;
|
|
5
|
+
return async function logBrewNextRequestErrorHandler(error, request, context) {
|
|
6
|
+
if (globalThis.process?.env?.NEXT_RUNTIME === "edge") {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
nodeHandlerPromise ??= import("./index.js");
|
|
12
|
+
const module = await nodeHandlerPromise;
|
|
13
|
+
handler ??= module.createLogBrewNextRequestErrorHandler(options);
|
|
14
|
+
await handler(error, request, context);
|
|
15
|
+
} catch (captureError) {
|
|
16
|
+
if (typeof options.onCaptureError === "function") {
|
|
17
|
+
try {
|
|
18
|
+
await options.onCaptureError(captureError, { error, request, context });
|
|
19
|
+
} catch {
|
|
20
|
+
// Observability callbacks must not replace the application error Next.js is handling.
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default {
|
|
28
|
+
createLogBrewNextRequestErrorHandler
|
|
29
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logbrew/next",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Next.js App Router helpers for the public LogBrew JavaScript SDK.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.cjs",
|
|
@@ -28,6 +28,17 @@
|
|
|
28
28
|
},
|
|
29
29
|
"default": "./client.js"
|
|
30
30
|
},
|
|
31
|
+
"./instrumentation": {
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./instrumentation.d.ts",
|
|
34
|
+
"default": "./instrumentation.js"
|
|
35
|
+
},
|
|
36
|
+
"require": {
|
|
37
|
+
"types": "./instrumentation.d.cts",
|
|
38
|
+
"default": "./instrumentation.cjs"
|
|
39
|
+
},
|
|
40
|
+
"default": "./instrumentation.js"
|
|
41
|
+
},
|
|
31
42
|
"./release-artifacts": {
|
|
32
43
|
"import": {
|
|
33
44
|
"types": "./release-artifacts.d.ts",
|
|
@@ -49,6 +60,10 @@
|
|
|
49
60
|
"client.js",
|
|
50
61
|
"client.d.ts",
|
|
51
62
|
"client.d.cts",
|
|
63
|
+
"instrumentation.cjs",
|
|
64
|
+
"instrumentation.js",
|
|
65
|
+
"instrumentation.d.ts",
|
|
66
|
+
"instrumentation.d.cts",
|
|
52
67
|
"release-artifacts.cjs",
|
|
53
68
|
"release-artifacts.js",
|
|
54
69
|
"release-artifacts.d.ts",
|
|
@@ -82,6 +97,6 @@
|
|
|
82
97
|
"react-dom": ">=18"
|
|
83
98
|
},
|
|
84
99
|
"scripts": {
|
|
85
|
-
"test": "node --check index.js && node --check index.cjs && node --check client.js && node --check client.cjs && node --check release-artifacts.js && node --check release-artifacts.cjs && node --check examples/index.mjs && node --check examples/client-route-spans.mjs && node --check examples/readme-example.mjs && node --check examples/real-user-smoke.mjs && node --test"
|
|
100
|
+
"test": "node --check index.js && node --check index.cjs && node --check client.js && node --check client.cjs && node --check instrumentation.js && node --check instrumentation.cjs && node --check release-artifacts.js && node --check release-artifacts.cjs && node --check examples/index.mjs && node --check examples/client-route-spans.mjs && node --check examples/readme-example.mjs && node --check examples/request-errors.mjs && node --check examples/real-user-smoke.mjs && node --test"
|
|
86
101
|
}
|
|
87
102
|
}
|