@honeybadger-io/nextjs 5.11.6 → 6.0.0
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 +5 -42
- package/dist/capture-request-error.d.ts +35 -0
- package/dist/capture-request-error.test.d.ts +2 -0
- package/dist/capture-router-transition-start.d.ts +21 -0
- package/dist/client.d.ts +2 -0
- package/dist/copy-config-files.js +106 -34
- package/dist/copy-config-files.js.map +1 -1
- package/dist/edge.d.ts +2 -1
- package/dist/flush.d.ts +24 -0
- package/dist/honeybadger-nextjs-client.cjs.js +35 -0
- package/dist/honeybadger-nextjs-client.cjs.js.map +1 -0
- package/dist/honeybadger-nextjs-client.esm.js +27 -0
- package/dist/honeybadger-nextjs-client.esm.js.map +1 -0
- package/dist/honeybadger-nextjs-edge.cjs.js +320 -240
- package/dist/honeybadger-nextjs-edge.cjs.js.map +1 -1
- package/dist/honeybadger-nextjs-edge.esm.js +317 -240
- package/dist/honeybadger-nextjs-edge.esm.js.map +1 -1
- package/dist/honeybadger-nextjs.cjs.js +835 -387
- package/dist/honeybadger-nextjs.cjs.js.map +1 -1
- package/dist/honeybadger-nextjs.esm.js +829 -386
- package/dist/honeybadger-nextjs.esm.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/insights-instrumentation.d.ts +30 -2
- package/dist/insights.d.ts +72 -0
- package/dist/insights.test.d.ts +2 -0
- package/dist/setup.d.ts +11 -0
- package/dist/setup.test.d.ts +2 -0
- package/dist/source-maps.d.ts +55 -0
- package/dist/source-maps.test.d.ts +2 -0
- package/dist/types.d.ts +20 -8
- package/package.json +33 -11
- package/src/capture-request-error.test.ts +245 -0
- package/src/capture-request-error.ts +92 -0
- package/src/capture-router-transition-start.ts +33 -0
- package/src/client.ts +4 -0
- package/src/copy-config-files.test.ts +250 -16
- package/src/copy-config-files.ts +117 -34
- package/src/edge.ts +12 -1
- package/src/flush.ts +58 -0
- package/src/index.ts +7 -2
- package/src/insights-instrumentation.ts +104 -11
- package/src/insights.test.ts +192 -0
- package/src/insights.ts +225 -0
- package/src/setup.test.ts +214 -0
- package/src/setup.ts +172 -0
- package/src/source-maps.test.ts +803 -0
- package/src/source-maps.ts +515 -0
- package/src/types.ts +38 -21
- package/templates/_error.js +14 -10
- package/templates/_error_app_router.js +1 -1
- package/templates/_error_app_router.tsx +1 -1
- package/templates/honeybadger.server.config.js +5 -16
- package/templates/instrumentation-client.js +21 -0
- package/templates/instrumentation.js +29 -0
- package/dist/webpack.d.ts +0 -3
- package/dist/with-honeybadger.d.ts +0 -32
- package/dist/with-honeybadger.test.d.ts +0 -2
- package/src/webpack.ts +0 -228
- package/src/with-honeybadger.test.ts +0 -527
- package/src/with-honeybadger.ts +0 -286
- package/templates/honeybadger.browser.config.js +0 -13
- package/templates/honeybadger.edge.config.js +0 -13
|
@@ -1,10 +1,60 @@
|
|
|
1
1
|
import Honeybadger from '@honeybadger-io/js';
|
|
2
2
|
import * as nextServer from 'next/server';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* The `waitUntil` primitive the hosting platform injects per request. Next.js
|
|
6
|
+
* resolves `after()` through this same accessor, and it is the only channel
|
|
7
|
+
* available in Pages Router API routes, which are invoked as `(req, res)` with
|
|
8
|
+
* no context argument to read it from.
|
|
9
|
+
*/
|
|
10
|
+
function requestContextWaitUntil() {
|
|
11
|
+
var _a, _b;
|
|
12
|
+
const context = globalThis[Symbol.for('@next/request-context')];
|
|
13
|
+
const waitUntil = (_b = (_a = context === null || context === void 0 ? void 0 : context.get) === null || _a === void 0 ? void 0 : _a.call(context)) === null || _b === void 0 ? void 0 : _b.waitUntil;
|
|
14
|
+
return typeof waitUntil === 'function' ? waitUntil : undefined;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Ensure events are delivered before a serverless runtime freezes, without
|
|
18
|
+
* delaying the response where the runtime lets us avoid it.
|
|
19
|
+
*
|
|
20
|
+
* In order of preference: Next.js `after()` (App Router only — it needs App Router
|
|
21
|
+
* request context, so Pages Router must not call it), then a `waitUntil` from the
|
|
22
|
+
* platform request context, then a blocking `flushAsync()` when the runtime offers
|
|
23
|
+
* neither.
|
|
24
|
+
*
|
|
25
|
+
* Delivery failures are logged by the events worker and must not break the handler.
|
|
26
|
+
*/
|
|
27
|
+
function scheduleFlush(options = {}) {
|
|
28
|
+
var _a;
|
|
29
|
+
const flush = () => Honeybadger.flushAsync().catch(() => { });
|
|
30
|
+
if (options.useAfter) {
|
|
31
|
+
const after = nextServer.after;
|
|
32
|
+
if (typeof after === 'function') {
|
|
33
|
+
// Exported but still refusable: `after()` throws outside a supported
|
|
34
|
+
// context. Fall through to the remaining strategies rather than failing
|
|
35
|
+
// the request.
|
|
36
|
+
try {
|
|
37
|
+
after(flush);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
41
|
+
catch (error) {
|
|
42
|
+
// try waitUntil / blocking flush below
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const waitUntil = (_a = options.waitUntil) !== null && _a !== void 0 ? _a : requestContextWaitUntil();
|
|
47
|
+
if (waitUntil) {
|
|
48
|
+
waitUntil(flush());
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
return flush();
|
|
52
|
+
}
|
|
53
|
+
|
|
4
54
|
/**
|
|
5
55
|
* Edge-safe equivalents of the inbound instrumentation helpers in
|
|
6
56
|
* `@honeybadger-io/js` (src/server/instrumentation/http_event.ts). They are
|
|
7
|
-
* duplicated here because this module must
|
|
57
|
+
* duplicated here because this module must load in every runtime where
|
|
8
58
|
* Node builtins (the `crypto` module, `process.hrtime`) are unavailable. Keep
|
|
9
59
|
* the header names and the `request_id` / `correlation_id` contract in sync
|
|
10
60
|
* with that file.
|
|
@@ -33,14 +83,6 @@ function generateId() {
|
|
|
33
83
|
return v.toString(16);
|
|
34
84
|
});
|
|
35
85
|
}
|
|
36
|
-
function readHeader(headers, name) {
|
|
37
|
-
const value = headers.get(name);
|
|
38
|
-
if (typeof value !== 'string') {
|
|
39
|
-
return undefined;
|
|
40
|
-
}
|
|
41
|
-
const trimmed = value.trim();
|
|
42
|
-
return trimmed.length ? trimmed : undefined;
|
|
43
|
-
}
|
|
44
86
|
function readNodeHeader(headers, name) {
|
|
45
87
|
if (!headers) {
|
|
46
88
|
return undefined;
|
|
@@ -64,168 +106,102 @@ function readNodeHeader(headers, name) {
|
|
|
64
106
|
const trimmed = value.trim();
|
|
65
107
|
return trimmed.length ? trimmed : undefined;
|
|
66
108
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
function
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
// the per-source flag must both be on.
|
|
88
|
-
function insightsHttpEnabled() {
|
|
89
|
-
const insights = Honeybadger.config.insights;
|
|
90
|
-
return (insights === null || insights === void 0 ? void 0 : insights.enabled) === true && (insights === null || insights === void 0 ? void 0 : insights.http) === true;
|
|
91
|
-
}
|
|
92
|
-
// The ids are embedded directly in the payload (instead of relying on the
|
|
93
|
-
// store's eventContext merge) so the event carries them even on the edge
|
|
94
|
-
// runtime, where there is no per-request store isolation. On the Node.js
|
|
95
|
-
// runtime they match the seeded event context, so embedding is a no-op.
|
|
96
|
-
function emitHandledEvent(method, path, status, start, ids) {
|
|
97
|
-
const payload = {
|
|
98
|
-
method,
|
|
99
|
-
duration: Math.round(now() - start),
|
|
100
|
-
...ids,
|
|
101
|
-
};
|
|
102
|
-
if (typeof path === 'string') {
|
|
103
|
-
payload.path = path;
|
|
109
|
+
/**
|
|
110
|
+
* Shared id precedence. Kept in one place (rather than once per request shape)
|
|
111
|
+
* so the header-name contract documented above is only spelled out once.
|
|
112
|
+
*
|
|
113
|
+
* The span ids do double duty. They are emitted as `trace_id` / `span_id`, and they also
|
|
114
|
+
* back-fill `request_id` / `correlation_id` when no header supplied them — a trace id
|
|
115
|
+
* already has exactly the semantics we want for a correlation id, since W3C trace context
|
|
116
|
+
* reuses an inbound `traceparent` and mints a new one when there is none.
|
|
117
|
+
*
|
|
118
|
+
* That double duty is what makes a fault joinable to its request's events: both paths seed
|
|
119
|
+
* from the same span, so with no id headers present a fault and its `request.handled` event
|
|
120
|
+
* carry identical values for all four.
|
|
121
|
+
*/
|
|
122
|
+
function seedIds(read, span = {}) {
|
|
123
|
+
var _a, _b, _c, _d, _e, _f;
|
|
124
|
+
const requestId = (_c = (_b = (_a = read('x-request-id')) !== null && _a !== void 0 ? _a : read('request-id')) !== null && _b !== void 0 ? _b : span.spanId) !== null && _c !== void 0 ? _c : generateId();
|
|
125
|
+
const correlationId = (_f = (_e = (_d = read('x-correlation-id')) !== null && _d !== void 0 ? _d : read('x-amzn-trace-id')) !== null && _e !== void 0 ? _e : span.traceId) !== null && _f !== void 0 ? _f : requestId;
|
|
126
|
+
const ids = { request_id: requestId, correlation_id: correlationId };
|
|
127
|
+
if (span.traceId) {
|
|
128
|
+
ids.trace_id = span.traceId;
|
|
104
129
|
}
|
|
105
|
-
if (
|
|
106
|
-
|
|
130
|
+
if (span.spanId) {
|
|
131
|
+
ids.span_id = span.spanId;
|
|
107
132
|
}
|
|
108
|
-
|
|
133
|
+
return ids;
|
|
109
134
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
135
|
+
/**
|
|
136
|
+
* The active span's ids, or an empty object when OpenTelemetry is not in play.
|
|
137
|
+
*
|
|
138
|
+
* `@opentelemetry/api` is an optional peer dependency, and this module is reachable from
|
|
139
|
+
* every runtime entry point, so it is loaded on demand and any failure to resolve it is
|
|
140
|
+
* treated as "no tracing configured" rather than an error.
|
|
141
|
+
*/
|
|
142
|
+
async function activeSpanIds() {
|
|
143
|
+
var _a;
|
|
113
144
|
try {
|
|
114
|
-
|
|
145
|
+
const { trace } = await import('@opentelemetry/api');
|
|
146
|
+
const spanContext = (_a = trace.getActiveSpan()) === null || _a === void 0 ? void 0 : _a.spanContext();
|
|
147
|
+
if (!spanContext) {
|
|
148
|
+
return {};
|
|
149
|
+
}
|
|
150
|
+
return { traceId: spanContext.traceId, spanId: spanContext.spanId };
|
|
115
151
|
}
|
|
116
152
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
117
153
|
catch (error) {
|
|
118
|
-
|
|
154
|
+
return {};
|
|
119
155
|
}
|
|
120
|
-
emitHandledEvent(req.method, path, status, start, ids);
|
|
121
156
|
}
|
|
122
|
-
// Pages Router:
|
|
123
|
-
function
|
|
124
|
-
|
|
125
|
-
emitHandledEvent(req.method, path, status, start, ids);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* The `waitUntil` primitive the hosting platform injects per request. Next.js
|
|
130
|
-
* resolves `after()` through this same accessor, and it is the only channel
|
|
131
|
-
* available in Pages Router API routes, which are invoked as `(req, res)` with
|
|
132
|
-
* no context argument to read it from.
|
|
133
|
-
*/
|
|
134
|
-
function requestContextWaitUntil() {
|
|
135
|
-
var _a, _b;
|
|
136
|
-
const context = globalThis[Symbol.for('@next/request-context')];
|
|
137
|
-
const waitUntil = (_b = (_a = context === null || context === void 0 ? void 0 : context.get) === null || _a === void 0 ? void 0 : _a.call(context)) === null || _b === void 0 ? void 0 : _b.waitUntil;
|
|
138
|
-
return typeof waitUntil === 'function' ? waitUntil : undefined;
|
|
157
|
+
// Pages Router: headers are a Node bag (Pages routes are Node-only, never edge).
|
|
158
|
+
function seedNodeRequestEventContext(headers, span = {}) {
|
|
159
|
+
return seedIds((name) => readNodeHeader(headers, name), span);
|
|
139
160
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
function eventWaitUntil(event) {
|
|
146
|
-
const waitUntil = event === null || event === void 0 ? void 0 : event.waitUntil;
|
|
147
|
-
return typeof waitUntil === 'function' ? waitUntil.bind(event) : undefined;
|
|
161
|
+
// Mirrors Util.resolveInsights from @honeybadger-io/core: the master gate and
|
|
162
|
+
// the per-source flag must both be on.
|
|
163
|
+
function insightsHttpEnabled() {
|
|
164
|
+
const insights = Honeybadger.config.insights;
|
|
165
|
+
return (insights === null || insights === void 0 ? void 0 : insights.enabled) === true && (insights === null || insights === void 0 ? void 0 : insights.http) === true;
|
|
148
166
|
}
|
|
149
167
|
/**
|
|
150
|
-
*
|
|
151
|
-
*
|
|
168
|
+
* OpenTelemetry path. Header values are not on the span by default; they arrive as
|
|
169
|
+
* `http.request.header.*` attributes when the HTTP instrumentation is configured with
|
|
170
|
+
* `headersToSpanAttributes`. Hyphens become underscores under the older semantic
|
|
171
|
+
* conventions and are preserved under the stable ones, so both spellings are read.
|
|
152
172
|
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
* then a blocking `flushAsync()` when the runtime offers neither. Blocking is
|
|
157
|
-
* correct in that last case: no `waitUntil` means nothing is going to freeze
|
|
158
|
-
* the invocation out from under us.
|
|
159
|
-
*
|
|
160
|
-
* Delivery failures are logged by the events worker and must not break the handler.
|
|
173
|
+
* When no header is present the span and trace ids are used, so the ids stay
|
|
174
|
+
* meaningful — and joinable to a trace — on platforms where the HTTP layer is not
|
|
175
|
+
* visible, such as serverless platforms.
|
|
161
176
|
*/
|
|
162
|
-
function
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
176
|
-
catch (error) {
|
|
177
|
-
// try waitUntil / blocking flush below
|
|
177
|
+
function seedSpanEventContext(attributes, spanContext) {
|
|
178
|
+
const read = (name) => {
|
|
179
|
+
const lower = name.toLowerCase();
|
|
180
|
+
for (const key of [
|
|
181
|
+
`http.request.header.${lower}`,
|
|
182
|
+
`http.request.header.${lower.replace(/-/g, '_')}`,
|
|
183
|
+
]) {
|
|
184
|
+
const value = attributes[key];
|
|
185
|
+
const first = Array.isArray(value) ? value[0] : value;
|
|
186
|
+
if (typeof first === 'string' && first.trim().length) {
|
|
187
|
+
return first.trim();
|
|
178
188
|
}
|
|
179
189
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
return flush();
|
|
187
|
-
}
|
|
188
|
-
function configure(overrides) {
|
|
189
|
-
var _a;
|
|
190
|
-
if (((_a = Honeybadger.config.apiKey) === null || _a === void 0 ? void 0 : _a.length) > 0) {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
let projectRoot = undefined;
|
|
194
|
-
try {
|
|
195
|
-
// not available on edge runtime
|
|
196
|
-
projectRoot = process.cwd();
|
|
197
|
-
}
|
|
198
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
199
|
-
catch (error) {
|
|
200
|
-
// do nothing
|
|
201
|
-
}
|
|
202
|
-
Honeybadger
|
|
203
|
-
.configure({
|
|
204
|
-
apiKey: process.env.NEXT_PUBLIC_HONEYBADGER_API_KEY,
|
|
205
|
-
environment: process.env.NEXT_PUBLIC_VERCEL_ENV || process.env.VERCEL_ENV || process.env.NODE_ENV,
|
|
206
|
-
revision: process.env.NEXT_PUBLIC_HONEYBADGER_REVISION,
|
|
207
|
-
projectRoot: 'webpack://_N_E/./',
|
|
208
|
-
...overrides,
|
|
209
|
-
})
|
|
210
|
-
.beforeNotify((notice) => {
|
|
211
|
-
if (!projectRoot) {
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
notice === null || notice === void 0 ? void 0 : notice.backtrace.forEach((line) => {
|
|
215
|
-
if (line.file) {
|
|
216
|
-
line.file = line.file.replace(`${projectRoot}/.next/server`, `${process.env.NEXT_PUBLIC_HONEYBADGER_ASSETS_URL}/..`);
|
|
217
|
-
}
|
|
218
|
-
return line;
|
|
219
|
-
});
|
|
190
|
+
return undefined;
|
|
191
|
+
};
|
|
192
|
+
return seedIds(read, {
|
|
193
|
+
traceId: spanContext.traceId || undefined,
|
|
194
|
+
spanId: spanContext.spanId || undefined,
|
|
220
195
|
});
|
|
221
196
|
}
|
|
197
|
+
|
|
222
198
|
/**
|
|
223
199
|
* Next.js uses thrown errors for control flow: `redirect()`, `notFound()`,
|
|
224
200
|
* `forbidden()` and `unauthorized()` all throw an error carrying a `digest`
|
|
225
201
|
* string (`NEXT_REDIRECT;...`, `NEXT_NOT_FOUND`, `NEXT_HTTP_ERROR_FALLBACK;...`).
|
|
226
202
|
* These are not real failures — the framework catches them upstream to produce
|
|
227
|
-
* the redirect/404/etc. — so we must let them
|
|
228
|
-
*
|
|
203
|
+
* the redirect/404/etc. — so we must let them pass without reporting, otherwise
|
|
204
|
+
* every redirect shows up as an error in Honeybadger.
|
|
229
205
|
*
|
|
230
206
|
* We match on the `NEXT_` prefix rather than an exhaustive list so that any
|
|
231
207
|
* present or future framework control-flow digest is covered. This is safe:
|
|
@@ -238,120 +214,221 @@ function isNextControlFlowError(error) {
|
|
|
238
214
|
return typeof digest === 'string' && digest.startsWith('NEXT_');
|
|
239
215
|
}
|
|
240
216
|
/**
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
*
|
|
217
|
+
* Reports errors passed to Next.js's `onRequestError` instrumentation hook.
|
|
218
|
+
*
|
|
219
|
+
* Wire it up in `instrumentation.ts`:
|
|
220
|
+
*
|
|
221
|
+
* ```ts
|
|
222
|
+
* export const onRequestError = captureRequestError
|
|
223
|
+
* ```
|
|
224
|
+
*
|
|
225
|
+
* Reaches Server Components, Route Handlers, Server Actions and middleware, and receives
|
|
226
|
+
* the real error rather than the generic message Next.js hands to an error component.
|
|
245
227
|
*/
|
|
246
|
-
function
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
228
|
+
async function captureRequestError(error, request, context) {
|
|
229
|
+
if (isNextControlFlowError(error)) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
// Same header precedence *and* the same span as the Insights path, so a fault and the
|
|
233
|
+
// events from the request it failed in carry matching ids. Next.js still has the request
|
|
234
|
+
// span active when it calls this hook, and it is the very span the Insights processor
|
|
235
|
+
// maps to `request.handled` — so with no id headers present all four ids are identical on
|
|
236
|
+
// both, and the fault joins its request's events and trace.
|
|
237
|
+
//
|
|
238
|
+
// Deliberately not written to the event context: outside a `Honeybadger.run()` that
|
|
239
|
+
// lands on a store shared by the whole process or isolate, so the ids would leak into
|
|
240
|
+
// later requests' events. `onRequestError` also fires after the request has already
|
|
241
|
+
// failed, so it is too late to decorate that request's own events anyway.
|
|
242
|
+
const ids = seedNodeRequestEventContext(request.headers, await activeSpanIds());
|
|
243
|
+
await Honeybadger.notifyAsync(error, {
|
|
244
|
+
context: {
|
|
245
|
+
...ids,
|
|
246
|
+
path: request.path,
|
|
247
|
+
method: request.method,
|
|
248
|
+
router_kind: context.routerKind,
|
|
249
|
+
route_path: context.routePath,
|
|
250
|
+
route_type: context.routeType,
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
await scheduleFlush({ useAfter: true });
|
|
251
254
|
}
|
|
255
|
+
|
|
252
256
|
/**
|
|
253
|
-
*
|
|
254
|
-
* `
|
|
257
|
+
* Next.js instruments itself with OpenTelemetry and emits one root span per request,
|
|
258
|
+
* `[http.method] [next.route]`, tagged `next.span_type: BaseServer.handleRequest`. Its
|
|
259
|
+
* attributes carry the method, route, target and status, so mapping that span to a
|
|
260
|
+
* `request.handled` event reports every request without wrapping any handler.
|
|
255
261
|
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
262
|
+
* Next instruments but does not export: spans only reach us once a tracer provider with
|
|
263
|
+
* this processor is registered. See `registerHoneybadgerInsights`.
|
|
258
264
|
*/
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
265
|
+
const NEXT_REQUEST_SPAN_TYPE = 'BaseServer.handleRequest';
|
|
266
|
+
/**
|
|
267
|
+
* Request headers to copy onto the root span, so `request_id` and `correlation_id` can be
|
|
268
|
+
* reused from an inbound request instead of falling back to the span and trace ids.
|
|
269
|
+
*
|
|
270
|
+
* Header values are not span attributes by default. `@vercel/otel` applies this map when
|
|
271
|
+
* it creates the root span, which is the span this processor reads — and, unlike Node's
|
|
272
|
+
* HTTP instrumentation, it works on serverless platforms too.
|
|
273
|
+
*
|
|
274
|
+
* Spread it into your own `registerOTel` call if you register OpenTelemetry yourself:
|
|
275
|
+
*
|
|
276
|
+
* ```ts
|
|
277
|
+
* registerOTel({
|
|
278
|
+
* attributesFromHeaders: { ...HONEYBADGER_HEADER_ATTRIBUTES },
|
|
279
|
+
* spanProcessors: [honeybadgerSpanProcessor()],
|
|
280
|
+
* })
|
|
281
|
+
* ```
|
|
282
|
+
*
|
|
283
|
+
* Keep the header names in sync with `seedIds` in ./insights-instrumentation.
|
|
284
|
+
*/
|
|
285
|
+
const HONEYBADGER_HEADER_ATTRIBUTES = {
|
|
286
|
+
'http.request.header.x-request-id': 'x-request-id',
|
|
287
|
+
'http.request.header.request-id': 'request-id',
|
|
288
|
+
'http.request.header.x-correlation-id': 'x-correlation-id',
|
|
289
|
+
'http.request.header.x-amzn-trace-id': 'x-amzn-trace-id',
|
|
290
|
+
};
|
|
291
|
+
function readString(attributes, ...names) {
|
|
292
|
+
for (const name of names) {
|
|
293
|
+
const value = attributes[name];
|
|
294
|
+
if (typeof value === 'string' && value.length) {
|
|
295
|
+
return value;
|
|
270
296
|
}
|
|
271
|
-
return response;
|
|
272
297
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
298
|
+
return undefined;
|
|
299
|
+
}
|
|
300
|
+
function readNumber(attributes, ...names) {
|
|
301
|
+
for (const name of names) {
|
|
302
|
+
const value = attributes[name];
|
|
303
|
+
if (typeof value === 'number') {
|
|
304
|
+
return value;
|
|
276
305
|
}
|
|
277
|
-
if (
|
|
278
|
-
|
|
279
|
-
await scheduleFlush({ useAfter: true, waitUntil });
|
|
306
|
+
if (typeof value === 'string' && value.trim().length && Number.isFinite(Number(value))) {
|
|
307
|
+
return Number(value);
|
|
280
308
|
}
|
|
281
|
-
await Honeybadger.notifyAsync(error);
|
|
282
|
-
throw error;
|
|
283
309
|
}
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
312
|
+
function durationMs(duration) {
|
|
313
|
+
if (!Array.isArray(duration) || duration.length !== 2) {
|
|
314
|
+
return 0;
|
|
315
|
+
}
|
|
316
|
+
const [seconds, nanos] = duration;
|
|
317
|
+
return Math.round(seconds * 1000 + nanos / 1e6);
|
|
284
318
|
}
|
|
285
319
|
/**
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
* `res.statusCode` once it resolves.
|
|
320
|
+
* Path without the query string. `http.target` includes the query under the older
|
|
321
|
+
* semantic conventions; `url.path` is already bare under the stable ones.
|
|
289
322
|
*/
|
|
290
|
-
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
323
|
+
function readPath(attributes) {
|
|
324
|
+
const target = readString(attributes, 'url.path', 'http.target');
|
|
325
|
+
return typeof target === 'string' ? target.split('?')[0] : undefined;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Maps Next.js's per-request span to a Honeybadger `request.handled` Insights event.
|
|
329
|
+
*
|
|
330
|
+
* Pass it to whatever registers OpenTelemetry in your `instrumentation.ts`:
|
|
331
|
+
*
|
|
332
|
+
* ```ts
|
|
333
|
+
* import { registerOTel } from '@vercel/otel'
|
|
334
|
+
* import { honeybadgerSpanProcessor } from '@honeybadger-io/nextjs'
|
|
335
|
+
*
|
|
336
|
+
* export function register() {
|
|
337
|
+
* registerOTel({ serviceName: 'my-app', spanProcessors: [honeybadgerSpanProcessor()] })
|
|
338
|
+
* }
|
|
339
|
+
* ```
|
|
340
|
+
*
|
|
341
|
+
* `registerHoneybadgerInsights()` is the one-line equivalent when you have no other
|
|
342
|
+
* OpenTelemetry setup of your own.
|
|
343
|
+
*/
|
|
344
|
+
function honeybadgerSpanProcessor() {
|
|
345
|
+
return {
|
|
346
|
+
onStart() {
|
|
347
|
+
// Nothing to do: the event is emitted once the request span is complete, which is
|
|
348
|
+
// when the status and duration are known.
|
|
349
|
+
},
|
|
350
|
+
onEnd(span) {
|
|
351
|
+
var _a;
|
|
352
|
+
const attributes = (_a = span.attributes) !== null && _a !== void 0 ? _a : {};
|
|
353
|
+
if (attributes['next.span_type'] !== NEXT_REQUEST_SPAN_TYPE) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
// Checked per event rather than at registration: configuration is loaded by
|
|
357
|
+
// `register()` importing the honeybadger config files, which may happen after the
|
|
358
|
+
// processor is constructed.
|
|
359
|
+
if (!insightsHttpEnabled()) {
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
const spanContext = typeof span.spanContext === 'function' ? span.spanContext() : {};
|
|
363
|
+
const ids = seedSpanEventContext(attributes, spanContext !== null && spanContext !== void 0 ? spanContext : {});
|
|
364
|
+
const payload = {
|
|
365
|
+
method: readString(attributes, 'http.request.method', 'http.method'),
|
|
366
|
+
duration: durationMs(span.duration),
|
|
367
|
+
...ids,
|
|
368
|
+
};
|
|
369
|
+
const path = readPath(attributes);
|
|
370
|
+
if (path) {
|
|
371
|
+
payload.path = path;
|
|
372
|
+
}
|
|
373
|
+
const status = readNumber(attributes, 'http.response.status_code', 'http.status_code');
|
|
374
|
+
if (typeof status === 'number') {
|
|
375
|
+
payload.status = status;
|
|
376
|
+
}
|
|
377
|
+
// Emitted alongside the ids rather than replacing them, so an event can be joined
|
|
378
|
+
// to a trace in another tool without changing what existing consumers read.
|
|
379
|
+
if (spanContext === null || spanContext === void 0 ? void 0 : spanContext.traceId) {
|
|
380
|
+
payload.trace_id = spanContext.traceId;
|
|
381
|
+
}
|
|
382
|
+
if (spanContext === null || spanContext === void 0 ? void 0 : spanContext.spanId) {
|
|
383
|
+
payload.span_id = spanContext.spanId;
|
|
384
|
+
}
|
|
385
|
+
Honeybadger.event('request.handled', payload);
|
|
386
|
+
// A span processor batches on its own lifecycle, which is not enough on a platform
|
|
387
|
+
// that can freeze the invocation as soon as the response is sent.
|
|
388
|
+
scheduleFlush({ useAfter: true });
|
|
389
|
+
},
|
|
390
|
+
async forceFlush() {
|
|
391
|
+
await Honeybadger.flushAsync().catch(() => { });
|
|
392
|
+
},
|
|
393
|
+
async shutdown() {
|
|
394
|
+
await Honeybadger.flushAsync().catch(() => { });
|
|
395
|
+
},
|
|
396
|
+
};
|
|
317
397
|
}
|
|
318
398
|
/**
|
|
319
|
-
*
|
|
320
|
-
*
|
|
399
|
+
* Registers OpenTelemetry with only Honeybadger's span processor attached, for apps that
|
|
400
|
+
* have no OpenTelemetry setup of their own. Call it from `register()` in
|
|
401
|
+
* `instrumentation.ts`.
|
|
402
|
+
*
|
|
403
|
+
* `@vercel/otel` is an optional peer dependency: it is only needed for Insights, and it
|
|
404
|
+
* loads where the full Node SDK cannot, such as serverless platforms. If you
|
|
405
|
+
* already call `registerOTel` yourself, add `honeybadgerSpanProcessor()` to your
|
|
406
|
+
* `spanProcessors` instead of calling this — registering twice would compete for the
|
|
407
|
+
* global provider.
|
|
321
408
|
*/
|
|
322
|
-
async function
|
|
409
|
+
async function registerHoneybadgerInsights(options = {}) {
|
|
410
|
+
var _a, _b;
|
|
411
|
+
let registerOTel;
|
|
323
412
|
try {
|
|
324
|
-
|
|
413
|
+
({ registerOTel } = await import('@vercel/otel'));
|
|
325
414
|
}
|
|
415
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
326
416
|
catch (error) {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
await Honeybadger.notifyAsync(error);
|
|
331
|
-
throw error;
|
|
417
|
+
Honeybadger.logger.error('[Honeybadger] Insights request events need @vercel/otel. Install it, or drop the ' +
|
|
418
|
+
'registerHoneybadgerInsights() call — error reporting works without it.');
|
|
419
|
+
return;
|
|
332
420
|
}
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
const invoke = () => {
|
|
341
|
-
// App Router / middleware first: a web Request as the first argument.
|
|
342
|
-
if (typeof Request !== 'undefined' && args[0] instanceof Request) {
|
|
343
|
-
return handleAppRouterRequest(call, args[0], canIsolate, eventWaitUntil(args[1]));
|
|
344
|
-
}
|
|
345
|
-
// Pages Router API route: a Node req/res pair.
|
|
346
|
-
if (isPagesApiInvocation(args)) {
|
|
347
|
-
return handlePagesApiRequest(call, args[0], args[1], canIsolate);
|
|
348
|
-
}
|
|
349
|
-
return handleUninstrumented(call);
|
|
350
|
-
};
|
|
351
|
-
return canIsolate ? Honeybadger.run(invoke) : invoke();
|
|
421
|
+
registerOTel({
|
|
422
|
+
serviceName: (_b = (_a = options.serviceName) !== null && _a !== void 0 ? _a : process.env.NEXT_PUBLIC_HONEYBADGER_SERVICE_NAME) !== null && _b !== void 0 ? _b : 'nextjs-app',
|
|
423
|
+
// Without this the id headers never reach the span, so request_id and
|
|
424
|
+
// correlation_id would always fall back to the span and trace ids.
|
|
425
|
+
attributesFromHeaders: {
|
|
426
|
+
...HONEYBADGER_HEADER_ATTRIBUTES,
|
|
427
|
+
...options.attributesFromHeaders,
|
|
352
428
|
},
|
|
429
|
+
spanProcessors: [honeybadgerSpanProcessor()],
|
|
353
430
|
});
|
|
354
431
|
}
|
|
355
432
|
|
|
356
|
-
export {
|
|
433
|
+
export { HONEYBADGER_HEADER_ATTRIBUTES, captureRequestError, honeybadgerSpanProcessor, registerHoneybadgerInsights };
|
|
357
434
|
//# sourceMappingURL=honeybadger-nextjs-edge.esm.js.map
|