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