@sentry/node
Advanced tools
Comparing version
@@ -10,3 +10,27 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); | ||
/** | ||
* Options for the Fastify integration. | ||
* | ||
* `shouldHandleError` - Callback method deciding whether error should be captured and sent to Sentry | ||
* This is used on Fastify v5 where Sentry handles errors in the diagnostics channel. | ||
* Fastify v3 and v4 use `setupFastifyErrorHandler` instead. | ||
* | ||
* @example | ||
* | ||
* ```javascript | ||
* Sentry.init({ | ||
* integrations: [ | ||
* Sentry.fastifyIntegration({ | ||
* shouldHandleError(_error, _request, reply) { | ||
* return reply.statusCode >= 500; | ||
* }, | ||
* }); | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
*/ | ||
const INTEGRATION_NAME = 'Fastify'; | ||
const INTEGRATION_NAME_V5 = 'Fastify-V5'; | ||
const INTEGRATION_NAME_V3 = 'Fastify-V3'; | ||
@@ -16,2 +40,11 @@ | ||
function getFastifyIntegration() { | ||
const client = core.getClient(); | ||
if (!client) { | ||
return undefined; | ||
} else { | ||
return client.getIntegrationByName(INTEGRATION_NAME) ; | ||
} | ||
} | ||
function handleFastifyError( | ||
@@ -22,5 +55,5 @@ | ||
reply, | ||
shouldHandleError, | ||
handlerOrigin, | ||
) { | ||
const shouldHandleError = getFastifyIntegration()?.getShouldHandleError() || defaultShouldHandleError; | ||
// Diagnostics channel runs before the onError hook, so we can use it to check if the handler was already registered | ||
@@ -35,3 +68,3 @@ if (handlerOrigin === 'diagnostics-channel') { | ||
'Fastify error handler was already registered via diagnostics channel.', | ||
'You can safely remove `setupFastifyErrorHandler` call.', | ||
'You can safely remove `setupFastifyErrorHandler` call and set `shouldHandleError` on the integration options.', | ||
); | ||
@@ -48,7 +81,5 @@ | ||
const instrumentFastify = nodeCore.generateInstrumentOnce(INTEGRATION_NAME, () => { | ||
const instrumentFastify = nodeCore.generateInstrumentOnce(INTEGRATION_NAME_V5, () => { | ||
const fastifyOtelInstrumentationInstance = new index.FastifyOtelInstrumentation(); | ||
const plugin = fastifyOtelInstrumentationInstance.plugin(); | ||
const options = fastifyOtelInstrumentationInstance.getConfig(); | ||
const shouldHandleError = (options )?.shouldHandleError || defaultShouldHandleError; | ||
@@ -79,3 +110,3 @@ // This message handler works for Fastify versions 3, 4 and 5 | ||
handleFastifyError.call(handleFastifyError, error, request, reply, shouldHandleError, 'diagnostics-channel'); | ||
handleFastifyError.call(handleFastifyError, error, request, reply, 'diagnostics-channel'); | ||
}); | ||
@@ -87,9 +118,19 @@ | ||
const _fastifyIntegration = (() => { | ||
const _fastifyIntegration = (({ shouldHandleError }) => { | ||
let _shouldHandleError; | ||
return { | ||
name: INTEGRATION_NAME, | ||
setupOnce() { | ||
_shouldHandleError = shouldHandleError || defaultShouldHandleError; | ||
instrumentFastifyV3(); | ||
instrumentFastify(); | ||
}, | ||
getShouldHandleError() { | ||
return _shouldHandleError; | ||
}, | ||
setShouldHandleError(fn) { | ||
_shouldHandleError = fn; | ||
}, | ||
}; | ||
@@ -114,3 +155,5 @@ }) ; | ||
*/ | ||
const fastifyIntegration = core.defineIntegration(_fastifyIntegration); | ||
const fastifyIntegration = core.defineIntegration((options = {}) => | ||
_fastifyIntegration(options), | ||
); | ||
@@ -149,7 +192,10 @@ /** | ||
function setupFastifyErrorHandler(fastify, options) { | ||
const shouldHandleError = options?.shouldHandleError || defaultShouldHandleError; | ||
if (options?.shouldHandleError) { | ||
getFastifyIntegration()?.setShouldHandleError(options.shouldHandleError); | ||
} | ||
const plugin = Object.assign( | ||
function (fastify, _options, done) { | ||
fastify.addHook('onError', async (request, reply, error) => { | ||
handleFastifyError.call(handleFastifyError, error, request, reply, shouldHandleError, 'onError-hook'); | ||
handleFastifyError.call(handleFastifyError, error, request, reply, 'onError-hook'); | ||
}); | ||
@@ -156,0 +202,0 @@ done(); |
@@ -120,4 +120,17 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); | ||
// @ts-expect-error we know that the method exists | ||
return originalMethod.apply(this, args); | ||
return core.handleCallbackErrors( | ||
() => { | ||
// @ts-expect-error we know that the method exists | ||
return originalMethod.apply(this, args); | ||
}, | ||
error => { | ||
// This error bubbles up to unhandledrejection handler (if not handled before), | ||
// where we do not know the active span anymore | ||
// So to circumvent this, we set the active span on the error object | ||
// which is picked up by the unhandledrejection handler | ||
if (error && typeof error === 'object') { | ||
core.addNonEnumerableProperty(error, '_sentry_active_span', core.getActiveSpan()); | ||
} | ||
}, | ||
); | ||
}; | ||
@@ -124,0 +137,0 @@ } |
@@ -8,3 +8,27 @@ import * as dc from 'node:diagnostics_channel'; | ||
/** | ||
* Options for the Fastify integration. | ||
* | ||
* `shouldHandleError` - Callback method deciding whether error should be captured and sent to Sentry | ||
* This is used on Fastify v5 where Sentry handles errors in the diagnostics channel. | ||
* Fastify v3 and v4 use `setupFastifyErrorHandler` instead. | ||
* | ||
* @example | ||
* | ||
* ```javascript | ||
* Sentry.init({ | ||
* integrations: [ | ||
* Sentry.fastifyIntegration({ | ||
* shouldHandleError(_error, _request, reply) { | ||
* return reply.statusCode >= 500; | ||
* }, | ||
* }); | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
*/ | ||
const INTEGRATION_NAME = 'Fastify'; | ||
const INTEGRATION_NAME_V5 = 'Fastify-V5'; | ||
const INTEGRATION_NAME_V3 = 'Fastify-V3'; | ||
@@ -14,2 +38,11 @@ | ||
function getFastifyIntegration() { | ||
const client = getClient(); | ||
if (!client) { | ||
return undefined; | ||
} else { | ||
return client.getIntegrationByName(INTEGRATION_NAME) ; | ||
} | ||
} | ||
function handleFastifyError( | ||
@@ -20,5 +53,5 @@ | ||
reply, | ||
shouldHandleError, | ||
handlerOrigin, | ||
) { | ||
const shouldHandleError = getFastifyIntegration()?.getShouldHandleError() || defaultShouldHandleError; | ||
// Diagnostics channel runs before the onError hook, so we can use it to check if the handler was already registered | ||
@@ -33,3 +66,3 @@ if (handlerOrigin === 'diagnostics-channel') { | ||
'Fastify error handler was already registered via diagnostics channel.', | ||
'You can safely remove `setupFastifyErrorHandler` call.', | ||
'You can safely remove `setupFastifyErrorHandler` call and set `shouldHandleError` on the integration options.', | ||
); | ||
@@ -46,7 +79,5 @@ | ||
const instrumentFastify = generateInstrumentOnce(INTEGRATION_NAME, () => { | ||
const instrumentFastify = generateInstrumentOnce(INTEGRATION_NAME_V5, () => { | ||
const fastifyOtelInstrumentationInstance = new FastifyOtelInstrumentation(); | ||
const plugin = fastifyOtelInstrumentationInstance.plugin(); | ||
const options = fastifyOtelInstrumentationInstance.getConfig(); | ||
const shouldHandleError = (options )?.shouldHandleError || defaultShouldHandleError; | ||
@@ -77,3 +108,3 @@ // This message handler works for Fastify versions 3, 4 and 5 | ||
handleFastifyError.call(handleFastifyError, error, request, reply, shouldHandleError, 'diagnostics-channel'); | ||
handleFastifyError.call(handleFastifyError, error, request, reply, 'diagnostics-channel'); | ||
}); | ||
@@ -85,9 +116,19 @@ | ||
const _fastifyIntegration = (() => { | ||
const _fastifyIntegration = (({ shouldHandleError }) => { | ||
let _shouldHandleError; | ||
return { | ||
name: INTEGRATION_NAME, | ||
setupOnce() { | ||
_shouldHandleError = shouldHandleError || defaultShouldHandleError; | ||
instrumentFastifyV3(); | ||
instrumentFastify(); | ||
}, | ||
getShouldHandleError() { | ||
return _shouldHandleError; | ||
}, | ||
setShouldHandleError(fn) { | ||
_shouldHandleError = fn; | ||
}, | ||
}; | ||
@@ -112,3 +153,5 @@ }) ; | ||
*/ | ||
const fastifyIntegration = defineIntegration(_fastifyIntegration); | ||
const fastifyIntegration = defineIntegration((options = {}) => | ||
_fastifyIntegration(options), | ||
); | ||
@@ -147,7 +190,10 @@ /** | ||
function setupFastifyErrorHandler(fastify, options) { | ||
const shouldHandleError = options?.shouldHandleError || defaultShouldHandleError; | ||
if (options?.shouldHandleError) { | ||
getFastifyIntegration()?.setShouldHandleError(options.shouldHandleError); | ||
} | ||
const plugin = Object.assign( | ||
function (fastify, _options, done) { | ||
fastify.addHook('onError', async (request, reply, error) => { | ||
handleFastifyError.call(handleFastifyError, error, request, reply, shouldHandleError, 'onError-hook'); | ||
handleFastifyError.call(handleFastifyError, error, request, reply, 'onError-hook'); | ||
}); | ||
@@ -154,0 +200,0 @@ done(); |
import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation'; | ||
import { SDK_VERSION, getCurrentScope } from '@sentry/core'; | ||
import { SDK_VERSION, getCurrentScope, handleCallbackErrors, addNonEnumerableProperty, getActiveSpan } from '@sentry/core'; | ||
import { INTEGRATION_NAME } from './constants.js'; | ||
@@ -118,4 +118,17 @@ | ||
// @ts-expect-error we know that the method exists | ||
return originalMethod.apply(this, args); | ||
return handleCallbackErrors( | ||
() => { | ||
// @ts-expect-error we know that the method exists | ||
return originalMethod.apply(this, args); | ||
}, | ||
error => { | ||
// This error bubbles up to unhandledrejection handler (if not handled before), | ||
// where we do not know the active span anymore | ||
// So to circumvent this, we set the active span on the error object | ||
// which is picked up by the unhandledrejection handler | ||
if (error && typeof error === 'object') { | ||
addNonEnumerableProperty(error, '_sentry_active_span', getActiveSpan()); | ||
} | ||
}, | ||
); | ||
}; | ||
@@ -122,0 +135,0 @@ } |
@@ -1,1 +0,1 @@ | ||
{"type":"module","version":"10.0.0-alpha.1","sideEffects":false} | ||
{"type":"module","version":"10.0.0-alpha.2","sideEffects":false} |
import { Instrumentation, InstrumentationConfig } from '@opentelemetry/instrumentation'; | ||
import { FastifyInstance, FastifyReply, FastifyRequest } from './types'; | ||
import { FastifyInstrumentationV3 } from './v3/instrumentation'; | ||
/** | ||
* Options for the Fastify integration. | ||
* | ||
* `shouldHandleError` - Callback method deciding whether error should be captured and sent to Sentry | ||
* This is used on Fastify v5 where Sentry handles errors in the diagnostics channel. | ||
* Fastify v3 and v4 use `setupFastifyErrorHandler` instead. | ||
* | ||
* @example | ||
* | ||
* ```javascript | ||
* Sentry.init({ | ||
* integrations: [ | ||
* Sentry.fastifyIntegration({ | ||
* shouldHandleError(_error, _request, reply) { | ||
* return reply.statusCode >= 500; | ||
* }, | ||
* }); | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
*/ | ||
interface FastifyIntegrationOptions { | ||
/** | ||
* Callback method deciding whether error should be captured and sent to Sentry | ||
* This is used on Fastify v5 where Sentry handles errors in the diagnostics channel. | ||
* Fastify v3 and v4 use `setupFastifyErrorHandler` instead. | ||
* | ||
* @param error Captured Fastify error | ||
* @param request Fastify request (or any object containing at least method, routeOptions.url, and routerPath) | ||
* @param reply Fastify reply (or any object containing at least statusCode) | ||
*/ | ||
shouldHandleError: (error: Error, request: FastifyRequest, reply: FastifyReply) => boolean; | ||
} | ||
interface FastifyHandlerOptions { | ||
@@ -14,2 +48,3 @@ /** | ||
* | ||
* | ||
* ```javascript | ||
@@ -23,2 +58,3 @@ * setupFastifyErrorHandler(app, { | ||
* | ||
* | ||
* If using TypeScript, you can cast the request and reply to get full type safety. | ||
@@ -43,3 +79,3 @@ * | ||
}; | ||
export declare const instrumentFastify: ((options?: unknown) => Instrumentation<InstrumentationConfig & FastifyHandlerOptions>) & { | ||
export declare const instrumentFastify: ((options?: unknown) => Instrumentation<InstrumentationConfig & FastifyIntegrationOptions>) & { | ||
id: string; | ||
@@ -63,3 +99,3 @@ }; | ||
*/ | ||
export declare const fastifyIntegration: () => import("@sentry/core").Integration; | ||
export declare const fastifyIntegration: (options?: Partial<FastifyIntegrationOptions> | undefined) => import("@sentry/core").Integration; | ||
/** | ||
@@ -66,0 +102,0 @@ * Add an Fastify error handler to capture errors to Sentry. |
import type { Instrumentation, InstrumentationConfig } from '@opentelemetry/instrumentation'; | ||
import type { FastifyInstance, FastifyReply, FastifyRequest } from './types'; | ||
import { FastifyInstrumentationV3 } from './v3/instrumentation'; | ||
/** | ||
* Options for the Fastify integration. | ||
* | ||
* `shouldHandleError` - Callback method deciding whether error should be captured and sent to Sentry | ||
* This is used on Fastify v5 where Sentry handles errors in the diagnostics channel. | ||
* Fastify v3 and v4 use `setupFastifyErrorHandler` instead. | ||
* | ||
* @example | ||
* | ||
* ```javascript | ||
* Sentry.init({ | ||
* integrations: [ | ||
* Sentry.fastifyIntegration({ | ||
* shouldHandleError(_error, _request, reply) { | ||
* return reply.statusCode >= 500; | ||
* }, | ||
* }); | ||
* }, | ||
* }); | ||
* ``` | ||
* | ||
*/ | ||
interface FastifyIntegrationOptions { | ||
/** | ||
* Callback method deciding whether error should be captured and sent to Sentry | ||
* This is used on Fastify v5 where Sentry handles errors in the diagnostics channel. | ||
* Fastify v3 and v4 use `setupFastifyErrorHandler` instead. | ||
* | ||
* @param error Captured Fastify error | ||
* @param request Fastify request (or any object containing at least method, routeOptions.url, and routerPath) | ||
* @param reply Fastify reply (or any object containing at least statusCode) | ||
*/ | ||
shouldHandleError: (error: Error, request: FastifyRequest, reply: FastifyReply) => boolean; | ||
} | ||
interface FastifyHandlerOptions { | ||
@@ -14,2 +48,3 @@ /** | ||
* | ||
* | ||
* ```javascript | ||
@@ -23,2 +58,3 @@ * setupFastifyErrorHandler(app, { | ||
* | ||
* | ||
* If using TypeScript, you can cast the request and reply to get full type safety. | ||
@@ -43,3 +79,3 @@ * | ||
}; | ||
export declare const instrumentFastify: ((options?: unknown) => Instrumentation<InstrumentationConfig & FastifyHandlerOptions>) & { | ||
export declare const instrumentFastify: ((options?: unknown) => Instrumentation<InstrumentationConfig & FastifyIntegrationOptions>) & { | ||
id: string; | ||
@@ -63,3 +99,3 @@ }; | ||
*/ | ||
export declare const fastifyIntegration: () => import("@sentry/core").Integration; | ||
export declare const fastifyIntegration: (options?: Partial<FastifyIntegrationOptions> | undefined) => import("@sentry/core").Integration; | ||
/** | ||
@@ -66,0 +102,0 @@ * Add an Fastify error handler to capture errors to Sentry. |
{ | ||
"name": "@sentry/node", | ||
"version": "10.0.0-alpha.1", | ||
"version": "10.0.0-alpha.2", | ||
"description": "Sentry Node SDK using OpenTelemetry for performance instrumentation", | ||
@@ -97,6 +97,6 @@ "repository": "git://github.com/getsentry/sentry-javascript.git", | ||
"@opentelemetry/semantic-conventions": "^1.34.0", | ||
"@prisma/instrumentation": "6.11.1", | ||
"@sentry/core": "10.0.0-alpha.1", | ||
"@sentry/node-core": "10.0.0-alpha.1", | ||
"@sentry/opentelemetry": "10.0.0-alpha.1", | ||
"@prisma/instrumentation": "6.12.0", | ||
"@sentry/core": "10.0.0-alpha.2", | ||
"@sentry/node-core": "10.0.0-alpha.2", | ||
"@sentry/opentelemetry": "10.0.0-alpha.2", | ||
"import-in-the-middle": "^1.14.2", | ||
@@ -103,0 +103,0 @@ "minimatch": "^9.0.0" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
1053549
1.25%11862
1.54%+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
Updated