@logbrew/next 0.1.8 → 0.1.10
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 +61 -12
- package/client.cjs +127 -4
- package/client.d.cts +33 -1
- package/client.d.ts +33 -1
- package/client.js +127 -4
- package/edge.cjs +55 -0
- package/edge.js +5 -0
- 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 +57 -161
- package/index.d.cts +7 -0
- package/index.d.ts +7 -0
- package/instrumentation.cjs +9 -1
- package/instrumentation.js +9 -1
- package/package.json +5 -2
- package/request-error-event.cjs +216 -0
package/README.md
CHANGED
|
@@ -125,16 +125,25 @@ export const onRequestError = createLogBrewNextRequestErrorHandler({
|
|
|
125
125
|
Next.js awaits `onRequestError`, and the LogBrew adapter awaits delivery before
|
|
126
126
|
it returns. A delivery or callback failure is reported through
|
|
127
127
|
`onCaptureError` but never replaces the application error that Next.js is
|
|
128
|
-
already handling.
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
128
|
+
already handling. In the Edge runtime (middleware and Edge routes), the hook
|
|
129
|
+
sends the same issue with `fetch` and loads no Node.js modules.
|
|
130
|
+
|
|
131
|
+
Server and Edge issues carry the release and environment from
|
|
132
|
+
`LOGBREW_RELEASE` and `LOGBREW_ENVIRONMENT`, or from Vercel's
|
|
133
|
+
`VERCEL_GIT_COMMIT_SHA` and `VERCEL_ENV` when the LogBrew variables are not
|
|
134
|
+
set. `LOGBREW_SERVICE_NAME` sets the service. Explicit `context` fields win.
|
|
135
|
+
|
|
136
|
+
By default, the issue includes the error message, exception type, structured
|
|
137
|
+
stack frames, Next.js error digest, method, stable route file path, router kind,
|
|
138
|
+
route type, render source, revalidation reason, and render type when Next.js
|
|
139
|
+
supplies them. The exception mechanism is `nextjs.on_request_error`, or
|
|
140
|
+
`nextjs.route_handler` for errors thrown inside `withLogBrewRouteHandler`.
|
|
141
|
+
Frames keep only the file basename, line, column, and function name. The issue
|
|
142
|
+
does not capture request headers, authorization values, cookies, request
|
|
143
|
+
bodies, query strings, URL fragments, concrete dynamic path values, or raw
|
|
144
|
+
stack text. Set `includePathname: true` only when a query-free concrete
|
|
145
|
+
pathname is intentional and acceptable for that application, and
|
|
146
|
+
`includeErrorStack: true` only when raw stack text is acceptable.
|
|
138
147
|
|
|
139
148
|
If the same Route Handler also uses `withLogBrewRouteHandler`, set
|
|
140
149
|
`captureErrors: false` on that wrapper so the global request-error hook is the
|
|
@@ -216,6 +225,10 @@ The metric carries the stable description `Duration of one completed server requ
|
|
|
216
225
|
|
|
217
226
|
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.
|
|
218
227
|
|
|
228
|
+
### Request User
|
|
229
|
+
|
|
230
|
+
Inside `withLogBrewRouteHandler`, call `helpers.setUser(id)` once you know who made the request. The route span and the route error issue of that request carry `context.subject`. Use an opaque app-owned ID, not an email address or name; pass `{ kind: "anonymous" }` for visitors who have not signed in. The ID never reaches another request.
|
|
231
|
+
|
|
219
232
|
## Client Route Spans
|
|
220
233
|
|
|
221
234
|
Use the browser-safe `@logbrew/next/client` subpath from client components. It has no `node:` imports and uses `clientKey` wording for browser setup:
|
|
@@ -232,7 +245,7 @@ import {
|
|
|
232
245
|
const client = createLogBrewNextBrowserClient({
|
|
233
246
|
clientKey: "LOGBREW_CLIENT_KEY",
|
|
234
247
|
sdkName: "my-next-app",
|
|
235
|
-
sdkVersion: "0.1.
|
|
248
|
+
sdkVersion: "0.1.10"
|
|
236
249
|
});
|
|
237
250
|
|
|
238
251
|
const routePatterns = [
|
|
@@ -261,6 +274,42 @@ The client helper requires explicit W3C trace context through `traceparent` or `
|
|
|
261
274
|
|
|
262
275
|
Use `captureNextNavigation(client, input)` when you want to emit a route span from app-owned navigation callbacks instead of the React hook.
|
|
263
276
|
|
|
277
|
+
## Client Errors
|
|
278
|
+
|
|
279
|
+
`createLogBrewNextBrowserClient()` sends events on its own with `fetch` (5
|
|
280
|
+
seconds after the first queued event, to save battery). Pass a `transport` to
|
|
281
|
+
replace it, or `transport: null` to keep the client manual. Browser events
|
|
282
|
+
carry the release and environment from `NEXT_PUBLIC_LOGBREW_RELEASE` and
|
|
283
|
+
`NEXT_PUBLIC_LOGBREW_ENVIRONMENT`, or from `NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA`
|
|
284
|
+
and `NEXT_PUBLIC_VERCEL_ENV`.
|
|
285
|
+
|
|
286
|
+
Report errors that reach an error boundary from `app/error.tsx` or
|
|
287
|
+
`app/global-error.tsx`:
|
|
288
|
+
|
|
289
|
+
```jsx
|
|
290
|
+
"use client";
|
|
291
|
+
|
|
292
|
+
import { useEffect } from "react";
|
|
293
|
+
import { captureNextClientError } from "@logbrew/next/client";
|
|
294
|
+
import { logbrew } from "./logbrew-client";
|
|
295
|
+
|
|
296
|
+
export default function Error({ error, reset }) {
|
|
297
|
+
useEffect(() => {
|
|
298
|
+
void captureNextClientError(logbrew, error, { routeTemplate: "/cart/[cartId]" });
|
|
299
|
+
}, [error]);
|
|
300
|
+
return <button onClick={() => reset()}>Try again</button>;
|
|
301
|
+
}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
The issue has mechanism `nextjs.error_boundary`, the exception type and
|
|
305
|
+
frames, and the Next.js `digest` when present. Pass a stable route template,
|
|
306
|
+
never a concrete path.
|
|
307
|
+
|
|
308
|
+
Call `installLogBrewNextClientErrorCapture({ client })` once in a client
|
|
309
|
+
component to also report uncaught browser errors (`nextjs.window_error`) and
|
|
310
|
+
unhandled rejections (`nextjs.unhandled_rejection`), and to flush queued events
|
|
311
|
+
when the page is hidden. `uninstall()` removes the listeners.
|
|
312
|
+
|
|
264
313
|
## Server Client Helper
|
|
265
314
|
|
|
266
315
|
```js
|
|
@@ -269,7 +318,7 @@ import { createLogBrewNextClient } from "@logbrew/next";
|
|
|
269
318
|
const client = createLogBrewNextClient({
|
|
270
319
|
serverApiKey: "LOGBREW_SERVER_API_KEY",
|
|
271
320
|
sdkName: "my-next-app",
|
|
272
|
-
sdkVersion: "0.1.
|
|
321
|
+
sdkVersion: "0.1.10"
|
|
273
322
|
});
|
|
274
323
|
```
|
|
275
324
|
|
package/client.cjs
CHANGED
|
@@ -4,34 +4,155 @@ const React = require("react");
|
|
|
4
4
|
const {
|
|
5
5
|
LogBrewClient,
|
|
6
6
|
SdkError,
|
|
7
|
+
createIssueAttributesFromError,
|
|
7
8
|
parseTraceparent
|
|
8
9
|
} = require("@logbrew/sdk");
|
|
9
10
|
|
|
11
|
+
const DEFAULT_ENDPOINT = "https://api.logbrew.co/v1/events";
|
|
12
|
+
// Browsers keep a 5-second interval to save battery and radio; pagehide flushes the rest.
|
|
13
|
+
const DEFAULT_BROWSER_DELIVERY_INTERVAL_MS = 5000;
|
|
14
|
+
|
|
10
15
|
function createLogBrewNextBrowserClient({
|
|
11
16
|
apiKey,
|
|
12
17
|
clientKey,
|
|
13
18
|
context,
|
|
19
|
+
deliveryIntervalMs = DEFAULT_BROWSER_DELIVERY_INTERVAL_MS,
|
|
20
|
+
endpoint = DEFAULT_ENDPOINT,
|
|
14
21
|
eventFilter,
|
|
15
22
|
sdkName = "logbrew-next-client",
|
|
16
|
-
sdkVersion = "0.1.
|
|
23
|
+
sdkVersion = "0.1.10",
|
|
17
24
|
maxQueueSize,
|
|
18
25
|
maxRetries = 2,
|
|
19
|
-
onEventDropped
|
|
26
|
+
onEventDropped,
|
|
27
|
+
transport
|
|
20
28
|
} = {}) {
|
|
21
29
|
const authKey = clientKey ?? apiKey;
|
|
22
30
|
if (!authKey) {
|
|
23
31
|
throw new SdkError("configuration_error", "createLogBrewNextBrowserClient requires clientKey or apiKey");
|
|
24
32
|
}
|
|
33
|
+
const deploymentContext = withPublicDeployment(context);
|
|
25
34
|
return LogBrewClient.create({
|
|
26
35
|
apiKey: authKey,
|
|
27
|
-
context,
|
|
36
|
+
context: deploymentContext,
|
|
37
|
+
deliveryIntervalMs,
|
|
28
38
|
eventFilter,
|
|
29
39
|
maxQueueSize,
|
|
30
40
|
maxRetries,
|
|
31
41
|
onEventDropped,
|
|
32
42
|
sdkName,
|
|
33
|
-
sdkVersion
|
|
43
|
+
sdkVersion,
|
|
44
|
+
// Sends on its own; pass transport: null to keep the client manual.
|
|
45
|
+
...(transport === null ? {} : { transport: transport ?? createNextBrowserTransport(endpoint) })
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Report an error from app/error.tsx or app/global-error.tsx, then deliver it.
|
|
50
|
+
function captureNextClientError(client, error, { digest, routeTemplate, flush = true } = {}) {
|
|
51
|
+
const safeDigest = typeof digest === "string" && /^[a-zA-Z0-9._:-]{1,128}$/u.test(digest)
|
|
52
|
+
? digest
|
|
53
|
+
: typeof error?.digest === "string" && /^[a-zA-Z0-9._:-]{1,128}$/u.test(error.digest) ? error.digest : undefined;
|
|
54
|
+
const route = normalizeRouteTemplate(routeTemplate);
|
|
55
|
+
client.issue(`evt_next_client_error_${randomHex(8)}`, new Date().toISOString(), createIssueAttributesFromError(error, {
|
|
56
|
+
handled: false,
|
|
57
|
+
mechanism: "nextjs.error_boundary",
|
|
58
|
+
runtime: "browser",
|
|
59
|
+
source: "nextjs.error_boundary",
|
|
60
|
+
metadata: {
|
|
61
|
+
framework: "nextjs",
|
|
62
|
+
...(safeDigest ? { errorDigest: safeDigest } : {}),
|
|
63
|
+
...(route ? { routeTemplate: route } : {})
|
|
64
|
+
}
|
|
65
|
+
}));
|
|
66
|
+
return flush && client.transport ? client.flush().catch(() => undefined) : Promise.resolve(undefined);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Report uncaught browser errors and unhandled rejections, and flush queued events on pagehide.
|
|
70
|
+
function installLogBrewNextClientErrorCapture({ client, browserWindow = globalThis.window } = {}) {
|
|
71
|
+
if (!client || !browserWindow || typeof browserWindow.addEventListener !== "function") {
|
|
72
|
+
throw new SdkError("configuration_error", "installLogBrewNextClientErrorCapture requires a client and a browser window");
|
|
73
|
+
}
|
|
74
|
+
const capture = (error, mechanism) => {
|
|
75
|
+
try {
|
|
76
|
+
client.issue(`evt_next_client_error_${randomHex(8)}`, new Date().toISOString(), createIssueAttributesFromError(error, {
|
|
77
|
+
handled: false,
|
|
78
|
+
mechanism,
|
|
79
|
+
runtime: "browser",
|
|
80
|
+
source: mechanism,
|
|
81
|
+
metadata: { framework: "nextjs" }
|
|
82
|
+
}));
|
|
83
|
+
} catch {
|
|
84
|
+
// Capture failures never reach the page.
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const listeners = {
|
|
88
|
+
error: (event) => { capture(event?.error ?? event?.message ?? "Uncaught browser error", "nextjs.window_error"); },
|
|
89
|
+
unhandledrejection: (event) => { capture(event?.reason, "nextjs.unhandled_rejection"); },
|
|
90
|
+
pagehide: () => { void client.flush?.().catch(() => undefined); }
|
|
91
|
+
};
|
|
92
|
+
for (const [type, listener] of Object.entries(listeners)) {
|
|
93
|
+
browserWindow.addEventListener(type, listener);
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
uninstall() {
|
|
97
|
+
for (const [type, listener] of Object.entries(listeners)) {
|
|
98
|
+
browserWindow.removeEventListener?.(type, listener);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function createNextBrowserTransport(endpoint) {
|
|
105
|
+
return {
|
|
106
|
+
async send(apiKey, body) {
|
|
107
|
+
const response = await globalThis.fetch(endpoint, {
|
|
108
|
+
body,
|
|
109
|
+
headers: { "content-type": "application/json", authorization: `Bearer ${apiKey}` },
|
|
110
|
+
keepalive: body.length <= 60000,
|
|
111
|
+
method: "POST"
|
|
112
|
+
});
|
|
113
|
+
return { statusCode: response.status, attempts: 1 };
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Next.js inlines NEXT_PUBLIC_* values at build time when they appear literally like this.
|
|
119
|
+
function withPublicDeployment(context) {
|
|
120
|
+
let release;
|
|
121
|
+
let environment;
|
|
122
|
+
try {
|
|
123
|
+
release = process.env.NEXT_PUBLIC_LOGBREW_RELEASE || process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA;
|
|
124
|
+
environment = process.env.NEXT_PUBLIC_LOGBREW_ENVIRONMENT || process.env.NEXT_PUBLIC_VERCEL_ENV;
|
|
125
|
+
} catch {
|
|
126
|
+
return context;
|
|
127
|
+
}
|
|
128
|
+
const deployment = {
|
|
129
|
+
...(boundedValue(environment) ? { environment: boundedValue(environment) } : {}),
|
|
130
|
+
...(boundedValue(release) ? { release: boundedValue(release) } : {})
|
|
131
|
+
};
|
|
132
|
+
if (Object.keys(deployment).length === 0) {
|
|
133
|
+
return context;
|
|
134
|
+
}
|
|
135
|
+
if (context !== undefined && (context === null || typeof context !== "object" || context.schemaVersion !== 1)) {
|
|
136
|
+
return context;
|
|
137
|
+
}
|
|
138
|
+
const resource = context?.resource ?? {};
|
|
139
|
+
return {
|
|
140
|
+
...(context ?? { schemaVersion: 1 }),
|
|
141
|
+
resource: { ...resource, deployment: { ...deployment, ...resource.deployment } }
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function boundedValue(value) {
|
|
146
|
+
if (typeof value !== "string") {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
const normalized = value.trim();
|
|
150
|
+
const characters = Array.from(normalized);
|
|
151
|
+
const hasControl = characters.some((character) => {
|
|
152
|
+
const code = character.codePointAt(0);
|
|
153
|
+
return code <= 31 || (code >= 127 && code <= 159);
|
|
34
154
|
});
|
|
155
|
+
return characters.length > 0 && characters.length <= 256 && !hasControl ? normalized : undefined;
|
|
35
156
|
}
|
|
36
157
|
|
|
37
158
|
function createNextRouteTemplate({
|
|
@@ -308,10 +429,12 @@ function randomHex(bytes) {
|
|
|
308
429
|
}
|
|
309
430
|
|
|
310
431
|
const api = {
|
|
432
|
+
captureNextClientError,
|
|
311
433
|
captureNextNavigation,
|
|
312
434
|
createLogBrewNextBrowserClient,
|
|
313
435
|
createNextNavigationSpanEvent,
|
|
314
436
|
createNextRouteTemplate,
|
|
437
|
+
installLogBrewNextClientErrorCapture,
|
|
315
438
|
useLogBrewNextNavigation
|
|
316
439
|
};
|
|
317
440
|
|
package/client.d.cts
CHANGED
|
@@ -4,7 +4,9 @@ import type {
|
|
|
4
4
|
LogBrewClient,
|
|
5
5
|
Metadata,
|
|
6
6
|
SpanAttributes,
|
|
7
|
-
TelemetryContext
|
|
7
|
+
TelemetryContext,
|
|
8
|
+
Transport,
|
|
9
|
+
TransportResponse
|
|
8
10
|
} from "@logbrew/sdk";
|
|
9
11
|
|
|
10
12
|
export type CreateLogBrewNextBrowserClientConfig = {
|
|
@@ -17,8 +19,36 @@ export type CreateLogBrewNextBrowserClientConfig = {
|
|
|
17
19
|
eventFilter?: EventFilter;
|
|
18
20
|
maxQueueSize?: number;
|
|
19
21
|
onEventDropped?: (drop: DroppedEvent) => void;
|
|
22
|
+
/** Delivery interval after the first queued event. Defaults to 5000 ms to save battery. */
|
|
23
|
+
deliveryIntervalMs?: number;
|
|
24
|
+
/** Ingest endpoint for the built-in fetch transport. */
|
|
25
|
+
endpoint?: string;
|
|
26
|
+
/** Custom transport, or null to keep the client manual. Defaults to a fetch transport that sends automatically. */
|
|
27
|
+
transport?: Transport | null;
|
|
20
28
|
};
|
|
21
29
|
|
|
30
|
+
export type NextClientErrorOptions = {
|
|
31
|
+
/** The `digest` Next.js passes to error boundaries; also read from `error.digest`. */
|
|
32
|
+
digest?: string;
|
|
33
|
+
/** Stable route template such as `/projects/[projectId]`, never a concrete path. */
|
|
34
|
+
routeTemplate?: string;
|
|
35
|
+
/** Deliver right away. Defaults to true. */
|
|
36
|
+
flush?: boolean;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** Report an error from `app/error.tsx` or `app/global-error.tsx` as an unhandled issue. */
|
|
40
|
+
export declare function captureNextClientError(
|
|
41
|
+
client: LogBrewClient,
|
|
42
|
+
error: unknown,
|
|
43
|
+
options?: NextClientErrorOptions
|
|
44
|
+
): Promise<TransportResponse | undefined>;
|
|
45
|
+
|
|
46
|
+
/** Report uncaught browser errors and unhandled rejections, and flush queued events on pagehide. */
|
|
47
|
+
export declare function installLogBrewNextClientErrorCapture(options: {
|
|
48
|
+
client: LogBrewClient;
|
|
49
|
+
browserWindow?: Window;
|
|
50
|
+
}): { uninstall(): void };
|
|
51
|
+
|
|
22
52
|
export type NextRoutePattern = string;
|
|
23
53
|
|
|
24
54
|
export type NextRouteTemplateInput = {
|
|
@@ -87,7 +117,9 @@ export declare function useLogBrewNextNavigation(
|
|
|
87
117
|
): void;
|
|
88
118
|
|
|
89
119
|
declare const defaultExport: {
|
|
120
|
+
captureNextClientError: typeof captureNextClientError;
|
|
90
121
|
captureNextNavigation: typeof captureNextNavigation;
|
|
122
|
+
installLogBrewNextClientErrorCapture: typeof installLogBrewNextClientErrorCapture;
|
|
91
123
|
createLogBrewNextBrowserClient: typeof createLogBrewNextBrowserClient;
|
|
92
124
|
createNextNavigationSpanEvent: typeof createNextNavigationSpanEvent;
|
|
93
125
|
createNextRouteTemplate: typeof createNextRouteTemplate;
|
package/client.d.ts
CHANGED
|
@@ -4,7 +4,9 @@ import type {
|
|
|
4
4
|
LogBrewClient,
|
|
5
5
|
Metadata,
|
|
6
6
|
SpanAttributes,
|
|
7
|
-
TelemetryContext
|
|
7
|
+
TelemetryContext,
|
|
8
|
+
Transport,
|
|
9
|
+
TransportResponse
|
|
8
10
|
} from "@logbrew/sdk";
|
|
9
11
|
|
|
10
12
|
export type CreateLogBrewNextBrowserClientConfig = {
|
|
@@ -17,8 +19,36 @@ export type CreateLogBrewNextBrowserClientConfig = {
|
|
|
17
19
|
eventFilter?: EventFilter;
|
|
18
20
|
maxQueueSize?: number;
|
|
19
21
|
onEventDropped?: (drop: DroppedEvent) => void;
|
|
22
|
+
/** Delivery interval after the first queued event. Defaults to 5000 ms to save battery. */
|
|
23
|
+
deliveryIntervalMs?: number;
|
|
24
|
+
/** Ingest endpoint for the built-in fetch transport. */
|
|
25
|
+
endpoint?: string;
|
|
26
|
+
/** Custom transport, or null to keep the client manual. Defaults to a fetch transport that sends automatically. */
|
|
27
|
+
transport?: Transport | null;
|
|
20
28
|
};
|
|
21
29
|
|
|
30
|
+
export type NextClientErrorOptions = {
|
|
31
|
+
/** The `digest` Next.js passes to error boundaries; also read from `error.digest`. */
|
|
32
|
+
digest?: string;
|
|
33
|
+
/** Stable route template such as `/projects/[projectId]`, never a concrete path. */
|
|
34
|
+
routeTemplate?: string;
|
|
35
|
+
/** Deliver right away. Defaults to true. */
|
|
36
|
+
flush?: boolean;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/** Report an error from `app/error.tsx` or `app/global-error.tsx` as an unhandled issue. */
|
|
40
|
+
export declare function captureNextClientError(
|
|
41
|
+
client: LogBrewClient,
|
|
42
|
+
error: unknown,
|
|
43
|
+
options?: NextClientErrorOptions
|
|
44
|
+
): Promise<TransportResponse | undefined>;
|
|
45
|
+
|
|
46
|
+
/** Report uncaught browser errors and unhandled rejections, and flush queued events on pagehide. */
|
|
47
|
+
export declare function installLogBrewNextClientErrorCapture(options: {
|
|
48
|
+
client: LogBrewClient;
|
|
49
|
+
browserWindow?: Window;
|
|
50
|
+
}): { uninstall(): void };
|
|
51
|
+
|
|
22
52
|
export type NextRoutePattern = string;
|
|
23
53
|
|
|
24
54
|
export type NextRouteTemplateInput = {
|
|
@@ -87,7 +117,9 @@ export declare function useLogBrewNextNavigation(
|
|
|
87
117
|
): void;
|
|
88
118
|
|
|
89
119
|
declare const defaultExport: {
|
|
120
|
+
captureNextClientError: typeof captureNextClientError;
|
|
90
121
|
captureNextNavigation: typeof captureNextNavigation;
|
|
122
|
+
installLogBrewNextClientErrorCapture: typeof installLogBrewNextClientErrorCapture;
|
|
91
123
|
createLogBrewNextBrowserClient: typeof createLogBrewNextBrowserClient;
|
|
92
124
|
createNextNavigationSpanEvent: typeof createNextNavigationSpanEvent;
|
|
93
125
|
createNextRouteTemplate: typeof createNextRouteTemplate;
|
package/client.js
CHANGED
|
@@ -4,34 +4,155 @@ import React from "react";
|
|
|
4
4
|
import {
|
|
5
5
|
LogBrewClient,
|
|
6
6
|
SdkError,
|
|
7
|
+
createIssueAttributesFromError,
|
|
7
8
|
parseTraceparent
|
|
8
9
|
} from "@logbrew/sdk";
|
|
9
10
|
|
|
11
|
+
const DEFAULT_ENDPOINT = "https://api.logbrew.co/v1/events";
|
|
12
|
+
// Browsers keep a 5-second interval to save battery and radio; pagehide flushes the rest.
|
|
13
|
+
const DEFAULT_BROWSER_DELIVERY_INTERVAL_MS = 5000;
|
|
14
|
+
|
|
10
15
|
export function createLogBrewNextBrowserClient({
|
|
11
16
|
apiKey,
|
|
12
17
|
clientKey,
|
|
13
18
|
context,
|
|
19
|
+
deliveryIntervalMs = DEFAULT_BROWSER_DELIVERY_INTERVAL_MS,
|
|
20
|
+
endpoint = DEFAULT_ENDPOINT,
|
|
14
21
|
eventFilter,
|
|
15
22
|
sdkName = "logbrew-next-client",
|
|
16
|
-
sdkVersion = "0.1.
|
|
23
|
+
sdkVersion = "0.1.10",
|
|
17
24
|
maxQueueSize,
|
|
18
25
|
maxRetries = 2,
|
|
19
|
-
onEventDropped
|
|
26
|
+
onEventDropped,
|
|
27
|
+
transport
|
|
20
28
|
} = {}) {
|
|
21
29
|
const authKey = clientKey ?? apiKey;
|
|
22
30
|
if (!authKey) {
|
|
23
31
|
throw new SdkError("configuration_error", "createLogBrewNextBrowserClient requires clientKey or apiKey");
|
|
24
32
|
}
|
|
33
|
+
const deploymentContext = withPublicDeployment(context);
|
|
25
34
|
return LogBrewClient.create({
|
|
26
35
|
apiKey: authKey,
|
|
27
|
-
context,
|
|
36
|
+
context: deploymentContext,
|
|
37
|
+
deliveryIntervalMs,
|
|
28
38
|
eventFilter,
|
|
29
39
|
maxQueueSize,
|
|
30
40
|
maxRetries,
|
|
31
41
|
onEventDropped,
|
|
32
42
|
sdkName,
|
|
33
|
-
sdkVersion
|
|
43
|
+
sdkVersion,
|
|
44
|
+
// Sends on its own; pass transport: null to keep the client manual.
|
|
45
|
+
...(transport === null ? {} : { transport: transport ?? createNextBrowserTransport(endpoint) })
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Report an error from app/error.tsx or app/global-error.tsx, then deliver it.
|
|
50
|
+
export function captureNextClientError(client, error, { digest, routeTemplate, flush = true } = {}) {
|
|
51
|
+
const safeDigest = typeof digest === "string" && /^[a-zA-Z0-9._:-]{1,128}$/u.test(digest)
|
|
52
|
+
? digest
|
|
53
|
+
: typeof error?.digest === "string" && /^[a-zA-Z0-9._:-]{1,128}$/u.test(error.digest) ? error.digest : undefined;
|
|
54
|
+
const route = normalizeRouteTemplate(routeTemplate);
|
|
55
|
+
client.issue(`evt_next_client_error_${randomHex(8)}`, new Date().toISOString(), createIssueAttributesFromError(error, {
|
|
56
|
+
handled: false,
|
|
57
|
+
mechanism: "nextjs.error_boundary",
|
|
58
|
+
runtime: "browser",
|
|
59
|
+
source: "nextjs.error_boundary",
|
|
60
|
+
metadata: {
|
|
61
|
+
framework: "nextjs",
|
|
62
|
+
...(safeDigest ? { errorDigest: safeDigest } : {}),
|
|
63
|
+
...(route ? { routeTemplate: route } : {})
|
|
64
|
+
}
|
|
65
|
+
}));
|
|
66
|
+
return flush && client.transport ? client.flush().catch(() => undefined) : Promise.resolve(undefined);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Report uncaught browser errors and unhandled rejections, and flush queued events on pagehide.
|
|
70
|
+
export function installLogBrewNextClientErrorCapture({ client, browserWindow = globalThis.window } = {}) {
|
|
71
|
+
if (!client || !browserWindow || typeof browserWindow.addEventListener !== "function") {
|
|
72
|
+
throw new SdkError("configuration_error", "installLogBrewNextClientErrorCapture requires a client and a browser window");
|
|
73
|
+
}
|
|
74
|
+
const capture = (error, mechanism) => {
|
|
75
|
+
try {
|
|
76
|
+
client.issue(`evt_next_client_error_${randomHex(8)}`, new Date().toISOString(), createIssueAttributesFromError(error, {
|
|
77
|
+
handled: false,
|
|
78
|
+
mechanism,
|
|
79
|
+
runtime: "browser",
|
|
80
|
+
source: mechanism,
|
|
81
|
+
metadata: { framework: "nextjs" }
|
|
82
|
+
}));
|
|
83
|
+
} catch {
|
|
84
|
+
// Capture failures never reach the page.
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const listeners = {
|
|
88
|
+
error: (event) => { capture(event?.error ?? event?.message ?? "Uncaught browser error", "nextjs.window_error"); },
|
|
89
|
+
unhandledrejection: (event) => { capture(event?.reason, "nextjs.unhandled_rejection"); },
|
|
90
|
+
pagehide: () => { void client.flush?.().catch(() => undefined); }
|
|
91
|
+
};
|
|
92
|
+
for (const [type, listener] of Object.entries(listeners)) {
|
|
93
|
+
browserWindow.addEventListener(type, listener);
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
uninstall() {
|
|
97
|
+
for (const [type, listener] of Object.entries(listeners)) {
|
|
98
|
+
browserWindow.removeEventListener?.(type, listener);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function createNextBrowserTransport(endpoint) {
|
|
105
|
+
return {
|
|
106
|
+
async send(apiKey, body) {
|
|
107
|
+
const response = await globalThis.fetch(endpoint, {
|
|
108
|
+
body,
|
|
109
|
+
headers: { "content-type": "application/json", authorization: `Bearer ${apiKey}` },
|
|
110
|
+
keepalive: body.length <= 60000,
|
|
111
|
+
method: "POST"
|
|
112
|
+
});
|
|
113
|
+
return { statusCode: response.status, attempts: 1 };
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Next.js inlines NEXT_PUBLIC_* values at build time when they appear literally like this.
|
|
119
|
+
function withPublicDeployment(context) {
|
|
120
|
+
let release;
|
|
121
|
+
let environment;
|
|
122
|
+
try {
|
|
123
|
+
release = process.env.NEXT_PUBLIC_LOGBREW_RELEASE || process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA;
|
|
124
|
+
environment = process.env.NEXT_PUBLIC_LOGBREW_ENVIRONMENT || process.env.NEXT_PUBLIC_VERCEL_ENV;
|
|
125
|
+
} catch {
|
|
126
|
+
return context;
|
|
127
|
+
}
|
|
128
|
+
const deployment = {
|
|
129
|
+
...(boundedValue(environment) ? { environment: boundedValue(environment) } : {}),
|
|
130
|
+
...(boundedValue(release) ? { release: boundedValue(release) } : {})
|
|
131
|
+
};
|
|
132
|
+
if (Object.keys(deployment).length === 0) {
|
|
133
|
+
return context;
|
|
134
|
+
}
|
|
135
|
+
if (context !== undefined && (context === null || typeof context !== "object" || context.schemaVersion !== 1)) {
|
|
136
|
+
return context;
|
|
137
|
+
}
|
|
138
|
+
const resource = context?.resource ?? {};
|
|
139
|
+
return {
|
|
140
|
+
...(context ?? { schemaVersion: 1 }),
|
|
141
|
+
resource: { ...resource, deployment: { ...deployment, ...resource.deployment } }
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function boundedValue(value) {
|
|
146
|
+
if (typeof value !== "string") {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
const normalized = value.trim();
|
|
150
|
+
const characters = Array.from(normalized);
|
|
151
|
+
const hasControl = characters.some((character) => {
|
|
152
|
+
const code = character.codePointAt(0);
|
|
153
|
+
return code <= 31 || (code >= 127 && code <= 159);
|
|
34
154
|
});
|
|
155
|
+
return characters.length > 0 && characters.length <= 256 && !hasControl ? normalized : undefined;
|
|
35
156
|
}
|
|
36
157
|
|
|
37
158
|
export function createNextRouteTemplate({
|
|
@@ -308,10 +429,12 @@ function randomHex(bytes) {
|
|
|
308
429
|
}
|
|
309
430
|
|
|
310
431
|
const defaultExport = {
|
|
432
|
+
captureNextClientError,
|
|
311
433
|
captureNextNavigation,
|
|
312
434
|
createLogBrewNextBrowserClient,
|
|
313
435
|
createNextNavigationSpanEvent,
|
|
314
436
|
createNextRouteTemplate,
|
|
437
|
+
installLogBrewNextClientErrorCapture,
|
|
315
438
|
useLogBrewNextNavigation
|
|
316
439
|
};
|
|
317
440
|
|
package/edge.cjs
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Edge runtime request-error capture: Web APIs only, no Node.js imports.
|
|
4
|
+
const { LogBrewClient, SdkError } = require("@logbrew/sdk");
|
|
5
|
+
const {
|
|
6
|
+
createNextRequestErrorEvent,
|
|
7
|
+
nextDeploymentContext,
|
|
8
|
+
readEnvApiKey,
|
|
9
|
+
readEnvServerApiKey
|
|
10
|
+
} = require("./request-error-event.cjs");
|
|
11
|
+
|
|
12
|
+
const DEFAULT_ENDPOINT = "https://api.logbrew.co/v1/events";
|
|
13
|
+
|
|
14
|
+
function createLogBrewNextEdgeRequestErrorHandler(options = {}) {
|
|
15
|
+
return async function logBrewNextEdgeRequestErrorHandler(error, request = {}, context = {}) {
|
|
16
|
+
const authKey = options.serverApiKey ?? options.apiKey ?? readEnvServerApiKey() ?? readEnvApiKey();
|
|
17
|
+
if (!authKey) {
|
|
18
|
+
throw new SdkError(
|
|
19
|
+
"configuration_error",
|
|
20
|
+
"Edge request-error capture requires serverApiKey, apiKey, LOGBREW_SERVER_API_KEY, or LOGBREW_API_KEY"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
const deploymentContext = nextDeploymentContext(options.context);
|
|
24
|
+
const client = LogBrewClient.create({
|
|
25
|
+
apiKey: authKey,
|
|
26
|
+
context: deploymentContext,
|
|
27
|
+
sdkName: options.sdkName ?? "logbrew-next",
|
|
28
|
+
sdkVersion: options.sdkVersion ?? "0.1.10",
|
|
29
|
+
maxRetries: options.maxRetries ?? 2
|
|
30
|
+
});
|
|
31
|
+
const event = typeof options.errorEvent === "function"
|
|
32
|
+
? options.errorEvent(error, { error, request, context, client })
|
|
33
|
+
: createNextRequestErrorEvent(error, request, context, options);
|
|
34
|
+
client.issue(event.id, event.timestamp, event.attributes);
|
|
35
|
+
const response = await client.shutdown(options.transport ?? createEdgeFetchTransport(options));
|
|
36
|
+
if (typeof options.onFlush === "function") {
|
|
37
|
+
await options.onFlush(response, { error, request, context, client });
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function createEdgeFetchTransport({ endpoint = DEFAULT_ENDPOINT, headers = {}, fetchImpl = globalThis.fetch } = {}) {
|
|
43
|
+
return {
|
|
44
|
+
async send(apiKey, body) {
|
|
45
|
+
const response = await fetchImpl(endpoint, {
|
|
46
|
+
body,
|
|
47
|
+
headers: { "content-type": "application/json", authorization: `Bearer ${apiKey}`, ...headers },
|
|
48
|
+
method: "POST"
|
|
49
|
+
});
|
|
50
|
+
return { statusCode: response.status, attempts: 1 };
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
module.exports = { createLogBrewNextEdgeRequestErrorHandler };
|
package/edge.js
ADDED
|
@@ -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.10") {
|
|
28
28
|
throw new Error(`unexpected Next client route span: ${JSON.stringify(event)}`);
|
|
29
29
|
}
|
|
30
30
|
|