@sentry/node 9.16.1 → 9.17.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.
Files changed (64) hide show
  1. package/build/cjs/index.js +18 -18
  2. package/build/cjs/integrations/anr/index.js +1 -1
  3. package/build/cjs/integrations/local-variables/local-variables-async.js +1 -1
  4. package/build/cjs/integrations/tracing/fastify/index.js +187 -0
  5. package/build/cjs/integrations/tracing/fastify/index.js.map +1 -0
  6. package/build/cjs/integrations/tracing/fastify/v3/constants.js +23 -0
  7. package/build/cjs/integrations/tracing/fastify/v3/constants.js.map +1 -0
  8. package/build/cjs/integrations/tracing/fastify/v3/enums/AttributeNames.js +37 -0
  9. package/build/cjs/integrations/tracing/fastify/v3/enums/AttributeNames.js.map +1 -0
  10. package/build/cjs/integrations/tracing/fastify/v3/instrumentation.js +303 -0
  11. package/build/cjs/integrations/tracing/fastify/v3/instrumentation.js.map +1 -0
  12. package/build/cjs/integrations/tracing/fastify/v3/utils.js +135 -0
  13. package/build/cjs/integrations/tracing/fastify/v3/utils.js.map +1 -0
  14. package/build/cjs/integrations/tracing/index.js +11 -10
  15. package/build/cjs/integrations/tracing/index.js.map +1 -1
  16. package/build/esm/index.js +1 -1
  17. package/build/esm/integrations/anr/index.js +1 -1
  18. package/build/esm/integrations/anr/worker-script.js +2 -2
  19. package/build/esm/integrations/local-variables/local-variables-async.js +1 -1
  20. package/build/esm/integrations/local-variables/worker-script.js +2 -2
  21. package/build/esm/integrations/tracing/fastify/index.js +182 -0
  22. package/build/esm/integrations/tracing/fastify/index.js.map +1 -0
  23. package/build/esm/integrations/tracing/fastify/v3/constants.js +21 -0
  24. package/build/esm/integrations/tracing/fastify/v3/constants.js.map +1 -0
  25. package/build/esm/integrations/tracing/fastify/v3/enums/AttributeNames.js +37 -0
  26. package/build/esm/integrations/tracing/fastify/v3/enums/AttributeNames.js.map +1 -0
  27. package/build/esm/integrations/tracing/fastify/v3/instrumentation.js +301 -0
  28. package/build/esm/integrations/tracing/fastify/v3/instrumentation.js.map +1 -0
  29. package/build/esm/integrations/tracing/fastify/v3/utils.js +131 -0
  30. package/build/esm/integrations/tracing/fastify/v3/utils.js.map +1 -0
  31. package/build/esm/integrations/tracing/index.js +2 -1
  32. package/build/esm/integrations/tracing/index.js.map +1 -1
  33. package/build/esm/package.json +1 -1
  34. package/build/{types-ts3.8/integrations/tracing/fastify.d.ts → types/integrations/tracing/fastify/index.d.ts} +83 -103
  35. package/build/types/integrations/tracing/fastify/index.d.ts.map +1 -0
  36. package/build/types/integrations/tracing/fastify/types.d.ts +25 -0
  37. package/build/types/integrations/tracing/fastify/types.d.ts.map +1 -0
  38. package/build/types/integrations/tracing/fastify/v3/constants.d.ts +3 -0
  39. package/build/types/integrations/tracing/fastify/v3/constants.d.ts.map +1 -0
  40. package/build/types/integrations/tracing/fastify/v3/enums/AttributeNames.d.ts +15 -0
  41. package/build/types/integrations/tracing/fastify/v3/enums/AttributeNames.d.ts.map +1 -0
  42. package/build/types/integrations/tracing/fastify/v3/instrumentation.d.ts +16 -0
  43. package/build/types/integrations/tracing/fastify/v3/instrumentation.d.ts.map +1 -0
  44. package/build/types/integrations/tracing/fastify/v3/internal-types.d.ts +7 -0
  45. package/build/types/integrations/tracing/fastify/v3/internal-types.d.ts.map +1 -0
  46. package/build/types/integrations/tracing/fastify/v3/types.d.ts +21 -0
  47. package/build/types/integrations/tracing/fastify/v3/types.d.ts.map +1 -0
  48. package/build/types/integrations/tracing/fastify/v3/utils.d.ts +28 -0
  49. package/build/types/integrations/tracing/fastify/v3/utils.d.ts.map +1 -0
  50. package/build/types/integrations/tracing/index.d.ts.map +1 -1
  51. package/build/{types/integrations/tracing/fastify.d.ts → types-ts3.8/integrations/tracing/fastify/index.d.ts} +83 -103
  52. package/build/types-ts3.8/integrations/tracing/fastify/types.d.ts +25 -0
  53. package/build/types-ts3.8/integrations/tracing/fastify/v3/constants.d.ts +3 -0
  54. package/build/types-ts3.8/integrations/tracing/fastify/v3/enums/AttributeNames.d.ts +15 -0
  55. package/build/types-ts3.8/integrations/tracing/fastify/v3/instrumentation.d.ts +16 -0
  56. package/build/types-ts3.8/integrations/tracing/fastify/v3/internal-types.d.ts +7 -0
  57. package/build/types-ts3.8/integrations/tracing/fastify/v3/types.d.ts +21 -0
  58. package/build/types-ts3.8/integrations/tracing/fastify/v3/utils.d.ts +28 -0
  59. package/package.json +4 -4
  60. package/build/cjs/integrations/tracing/fastify.js +0 -160
  61. package/build/cjs/integrations/tracing/fastify.js.map +0 -1
  62. package/build/esm/integrations/tracing/fastify.js +0 -156
  63. package/build/esm/integrations/tracing/fastify.js.map +0 -1
  64. package/build/types/integrations/tracing/fastify.d.ts.map +0 -1
@@ -1,103 +1,83 @@
1
- import { FastifyInstrumentation } from '@opentelemetry/instrumentation-fastify';
2
- /**
3
- * Minimal request type containing properties around route information.
4
- * Works for Fastify 3, 4 and presumably 5.
5
- *
6
- * Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/request.d.ts
7
- */
8
- interface MinimalFastifyRequest extends Record<string, any> {
9
- method?: string;
10
- routeOptions?: {
11
- url?: string;
12
- };
13
- routerPath?: string;
14
- }
15
- /**
16
- * Minimal reply type containing properties needed for error handling.
17
- *
18
- * Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/reply.d.ts
19
- */
20
- interface MinimalFastifyReply extends Record<string, any> {
21
- statusCode: number;
22
- }
23
- interface Fastify {
24
- register: (plugin: any) => void;
25
- addHook: (hook: string, handler: (...params: unknown[]) => void) => void;
26
- }
27
- interface FastifyHandlerOptions {
28
- /**
29
- * Callback method deciding whether error should be captured and sent to Sentry
30
- *
31
- * @param error Captured Fastify error
32
- * @param request Fastify request (or any object containing at least method, routeOptions.url, and routerPath)
33
- * @param reply Fastify reply (or any object containing at least statusCode)
34
- *
35
- * @example
36
- *
37
- * ```javascript
38
- * setupFastifyErrorHandler(app, {
39
- * shouldHandleError(_error, _request, reply) {
40
- * return reply.statusCode >= 400;
41
- * },
42
- * });
43
- * ```
44
- *
45
- * If using TypeScript, you can cast the request and reply to get full type safety.
46
- *
47
- * ```typescript
48
- * import type { FastifyRequest, FastifyReply } from 'fastify';
49
- *
50
- * setupFastifyErrorHandler(app, {
51
- * shouldHandleError(error, minimalRequest, minimalReply) {
52
- * const request = minimalRequest as FastifyRequest;
53
- * const reply = minimalReply as FastifyReply;
54
- * return reply.statusCode >= 500;
55
- * },
56
- * });
57
- * ```
58
- */
59
- shouldHandleError: (error: Error, request: MinimalFastifyRequest, reply: MinimalFastifyReply) => boolean;
60
- }
61
- export declare const instrumentFastify: ((options?: unknown) => FastifyInstrumentation) & {
62
- id: string;
63
- };
64
- /**
65
- * Adds Sentry tracing instrumentation for [Fastify](https://fastify.dev/).
66
- *
67
- * If you also want to capture errors, you need to call `setupFastifyErrorHandler(app)` after you set up your Fastify server.
68
- *
69
- * For more information, see the [fastify documentation](https://docs.sentry.io/platforms/javascript/guides/fastify/).
70
- *
71
- * @example
72
- * ```javascript
73
- * const Sentry = require('@sentry/node');
74
- *
75
- * Sentry.init({
76
- * integrations: [Sentry.fastifyIntegration()],
77
- * })
78
- * ```
79
- */
80
- export declare const fastifyIntegration: () => import("@sentry/core").Integration;
81
- /**
82
- * Add an Fastify error handler to capture errors to Sentry.
83
- *
84
- * @param fastify The Fastify instance to which to add the error handler
85
- * @param options Configuration options for the handler
86
- *
87
- * @example
88
- * ```javascript
89
- * const Sentry = require('@sentry/node');
90
- * const Fastify = require("fastify");
91
- *
92
- * const app = Fastify();
93
- *
94
- * Sentry.setupFastifyErrorHandler(app);
95
- *
96
- * // Add your routes, etc.
97
- *
98
- * app.listen({ port: 3000 });
99
- * ```
100
- */
101
- export declare function setupFastifyErrorHandler(fastify: Fastify, options?: Partial<FastifyHandlerOptions>): void;
102
- export {};
103
- //# sourceMappingURL=fastify.d.ts.map
1
+ import { Instrumentation, InstrumentationConfig } from '@opentelemetry/instrumentation';
2
+ import { FastifyInstance, FastifyReply, FastifyRequest } from './types';
3
+ import { FastifyInstrumentationV3 } from './v3/instrumentation';
4
+ interface FastifyHandlerOptions {
5
+ /**
6
+ * Callback method deciding whether error should be captured and sent to Sentry
7
+ *
8
+ * @param error Captured Fastify error
9
+ * @param request Fastify request (or any object containing at least method, routeOptions.url, and routerPath)
10
+ * @param reply Fastify reply (or any object containing at least statusCode)
11
+ *
12
+ * @example
13
+ *
14
+ * ```javascript
15
+ * setupFastifyErrorHandler(app, {
16
+ * shouldHandleError(_error, _request, reply) {
17
+ * return reply.statusCode >= 400;
18
+ * },
19
+ * });
20
+ * ```
21
+ *
22
+ * If using TypeScript, you can cast the request and reply to get full type safety.
23
+ *
24
+ * ```typescript
25
+ * import type { FastifyRequest, FastifyReply } from 'fastify';
26
+ *
27
+ * setupFastifyErrorHandler(app, {
28
+ * shouldHandleError(error, minimalRequest, minimalReply) {
29
+ * const request = minimalRequest as FastifyRequest;
30
+ * const reply = minimalReply as FastifyReply;
31
+ * return reply.statusCode >= 500;
32
+ * },
33
+ * });
34
+ * ```
35
+ */
36
+ shouldHandleError: (error: Error, request: FastifyRequest, reply: FastifyReply) => boolean;
37
+ }
38
+ export declare const instrumentFastifyV3: ((options?: unknown) => FastifyInstrumentationV3) & {
39
+ id: string;
40
+ };
41
+ export declare const instrumentFastify: ((options?: unknown) => Instrumentation<InstrumentationConfig>) & {
42
+ id: string;
43
+ };
44
+ /**
45
+ * Adds Sentry tracing instrumentation for [Fastify](https://fastify.dev/).
46
+ *
47
+ * If you also want to capture errors, you need to call `setupFastifyErrorHandler(app)` after you set up your Fastify server.
48
+ *
49
+ * For more information, see the [fastify documentation](https://docs.sentry.io/platforms/javascript/guides/fastify/).
50
+ *
51
+ * @example
52
+ * ```javascript
53
+ * const Sentry = require('@sentry/node');
54
+ *
55
+ * Sentry.init({
56
+ * integrations: [Sentry.fastifyIntegration()],
57
+ * })
58
+ * ```
59
+ */
60
+ export declare const fastifyIntegration: () => import("@sentry/core").Integration;
61
+ /**
62
+ * Add an Fastify error handler to capture errors to Sentry.
63
+ *
64
+ * @param fastify The Fastify instance to which to add the error handler
65
+ * @param options Configuration options for the handler
66
+ *
67
+ * @example
68
+ * ```javascript
69
+ * const Sentry = require('@sentry/node');
70
+ * const Fastify = require("fastify");
71
+ *
72
+ * const app = Fastify();
73
+ *
74
+ * Sentry.setupFastifyErrorHandler(app);
75
+ *
76
+ * // Add your routes, etc.
77
+ *
78
+ * app.listen({ port: 3000 });
79
+ * ```
80
+ */
81
+ export declare function setupFastifyErrorHandler(fastify: FastifyInstance, options?: Partial<FastifyHandlerOptions>): void;
82
+ export {};
83
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,25 @@
1
+ export type HandlerOriginal = ((request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => Promise<void>) | ((request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => void);
2
+ export type FastifyError = any;
3
+ export type HookHandlerDoneFunction = <TError extends Error = FastifyError>(err?: TError) => void;
4
+ export type FastifyErrorCodes = any;
5
+ export type FastifyPlugin = (instance: FastifyInstance, opts: any, done: HookHandlerDoneFunction) => unknown | Promise<unknown>;
6
+ export interface FastifyInstance {
7
+ version: string;
8
+ register: (plugin: any) => FastifyInstance;
9
+ after: (listener?: (err: Error) => void) => FastifyInstance;
10
+ addHook(hook: string, handler: HandlerOriginal): FastifyInstance;
11
+ addHook(hook: 'onError', handler: (request: FastifyRequest, reply: FastifyReply, error: Error) => void): FastifyInstance;
12
+ addHook(hook: 'onRequest', handler: (request: FastifyRequest, reply: FastifyReply) => void): FastifyInstance;
13
+ }
14
+ export interface FastifyReply {
15
+ send: () => FastifyReply;
16
+ statusCode: number;
17
+ }
18
+ export interface FastifyRequest {
19
+ method?: string;
20
+ routeOptions?: {
21
+ url?: string;
22
+ };
23
+ routerPath?: string;
24
+ }
25
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,3 @@
1
+ export declare const spanRequestSymbol: unique symbol;
2
+ export declare const hooksNamesToWrap: Set<string>;
3
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1,15 @@
1
+ export declare enum AttributeNames {
2
+ FASTIFY_NAME = "fastify.name",
3
+ FASTIFY_TYPE = "fastify.type",
4
+ HOOK_NAME = "hook.name",
5
+ PLUGIN_NAME = "plugin.name"
6
+ }
7
+ export declare enum FastifyTypes {
8
+ MIDDLEWARE = "middleware",
9
+ REQUEST_HANDLER = "request_handler"
10
+ }
11
+ export declare enum FastifyNames {
12
+ MIDDLEWARE = "middleware",
13
+ REQUEST_HANDLER = "request handler"
14
+ }
15
+ //# sourceMappingURL=AttributeNames.d.ts.map
@@ -0,0 +1,16 @@
1
+ import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';
2
+ import { FastifyInstrumentationConfig } from './types';
3
+ /**
4
+ * Fastify instrumentation for OpenTelemetry
5
+ */
6
+ export declare class FastifyInstrumentationV3 extends InstrumentationBase<FastifyInstrumentationConfig> {
7
+ constructor(config?: FastifyInstrumentationConfig);
8
+ init(): InstrumentationNodeModuleDefinition[];
9
+ private _hookOnRequest;
10
+ private _wrapHandler;
11
+ private _wrapAddHook;
12
+ private _patchConstructor;
13
+ private _patchSend;
14
+ private _hookPreHandler;
15
+ }
16
+ //# sourceMappingURL=instrumentation.d.ts.map
@@ -0,0 +1,7 @@
1
+ import { Span } from '@opentelemetry/api';
2
+ import { FastifyReply } from '../types';
3
+ import { spanRequestSymbol } from './constants';
4
+ export type PluginFastifyReply = FastifyReply & {
5
+ [spanRequestSymbol]?: Span[];
6
+ };
7
+ //# sourceMappingURL=internal-types.d.ts.map
@@ -0,0 +1,21 @@
1
+ import { Span } from '@opentelemetry/api';
2
+ import { InstrumentationConfig } from '@opentelemetry/instrumentation';
3
+ export interface FastifyRequestInfo {
4
+ request: any;
5
+ }
6
+ /**
7
+ * Function that can be used to add custom attributes to the current span
8
+ * @param span - The Fastify handler span.
9
+ * @param info - The Fastify request info object.
10
+ */
11
+ export interface FastifyCustomAttributeFunction {
12
+ (span: Span, info: FastifyRequestInfo): void;
13
+ }
14
+ /**
15
+ * Options available for the Fastify Instrumentation
16
+ */
17
+ export interface FastifyInstrumentationConfig extends InstrumentationConfig {
18
+ /** Function for adding custom attributes to each handler span */
19
+ requestHook?: FastifyCustomAttributeFunction;
20
+ }
21
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1,28 @@
1
+ import { Attributes, Span, Tracer } from '@opentelemetry/api';
2
+ import { PluginFastifyReply } from './internal-types';
3
+ /**
4
+ * Starts Span
5
+ * @param reply - reply function
6
+ * @param tracer - tracer
7
+ * @param spanName - span name
8
+ * @param spanAttributes - span attributes
9
+ */
10
+ export declare function startSpan(reply: PluginFastifyReply, tracer: Tracer, spanName: string, spanAttributes?: Attributes): Span;
11
+ /**
12
+ * Ends span
13
+ * @param reply - reply function
14
+ * @param err - error
15
+ */
16
+ export declare function endSpan(reply: PluginFastifyReply, err?: any): void;
17
+ /**
18
+ * This function handles the missing case from instrumentation package when
19
+ * execute can either return a promise or void. And using async is not an
20
+ * option as it is producing unwanted side effects.
21
+ * @param execute - function to be executed
22
+ * @param onFinish - function called when function executed
23
+ * @param preventThrowingError - prevent to throw error when execute
24
+ * function fails
25
+ */
26
+ export declare function safeExecuteInTheMiddleMaybePromise<T>(execute: () => Promise<T>, onFinish: (e: unknown, result?: T) => void, preventThrowingError?: boolean): Promise<T>;
27
+ export declare function safeExecuteInTheMiddleMaybePromise<T>(execute: () => T, onFinish: (e: unknown, result?: T) => void, preventThrowingError?: boolean): T;
28
+ //# sourceMappingURL=utils.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sentry/node",
3
- "version": "9.16.1",
3
+ "version": "9.17.0",
4
4
  "description": "Sentry Node SDK using OpenTelemetry for performance instrumentation",
5
5
  "repository": "git://github.com/getsentry/sentry-javascript.git",
6
6
  "homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/node",
@@ -65,6 +65,7 @@
65
65
  "access": "public"
66
66
  },
67
67
  "dependencies": {
68
+ "@fastify/otel": "0.6.0",
68
69
  "@opentelemetry/api": "^1.9.0",
69
70
  "@opentelemetry/context-async-hooks": "^1.30.1",
70
71
  "@opentelemetry/core": "^1.30.1",
@@ -73,7 +74,6 @@
73
74
  "@opentelemetry/instrumentation-connect": "0.43.1",
74
75
  "@opentelemetry/instrumentation-dataloader": "0.16.1",
75
76
  "@opentelemetry/instrumentation-express": "0.47.1",
76
- "@opentelemetry/instrumentation-fastify": "0.44.2",
77
77
  "@opentelemetry/instrumentation-fs": "0.19.1",
78
78
  "@opentelemetry/instrumentation-generic-pool": "0.43.1",
79
79
  "@opentelemetry/instrumentation-graphql": "0.47.1",
@@ -96,8 +96,8 @@
96
96
  "@opentelemetry/sdk-trace-base": "^1.30.1",
97
97
  "@opentelemetry/semantic-conventions": "^1.30.0",
98
98
  "@prisma/instrumentation": "6.7.0",
99
- "@sentry/core": "9.16.1",
100
- "@sentry/opentelemetry": "9.16.1",
99
+ "@sentry/core": "9.17.0",
100
+ "@sentry/opentelemetry": "9.17.0",
101
101
  "import-in-the-middle": "^1.13.0"
102
102
  },
103
103
  "devDependencies": {
@@ -1,160 +0,0 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
-
3
- const instrumentationFastify = require('@opentelemetry/instrumentation-fastify');
4
- const core = require('@sentry/core');
5
- const instrument = require('../../otel/instrument.js');
6
- const ensureIsWrapped = require('../../utils/ensureIsWrapped.js');
7
-
8
- /**
9
- * Minimal request type containing properties around route information.
10
- * Works for Fastify 3, 4 and presumably 5.
11
- *
12
- * Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/request.d.ts
13
- */
14
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
15
-
16
- const INTEGRATION_NAME = 'Fastify';
17
-
18
- const instrumentFastify = instrument.generateInstrumentOnce(
19
- INTEGRATION_NAME,
20
- () =>
21
- // eslint-disable-next-line deprecation/deprecation
22
- new instrumentationFastify.FastifyInstrumentation({
23
- requestHook(span) {
24
- addFastifySpanAttributes(span);
25
- },
26
- }),
27
- );
28
-
29
- const _fastifyIntegration = (() => {
30
- return {
31
- name: INTEGRATION_NAME,
32
- setupOnce() {
33
- instrumentFastify();
34
- },
35
- };
36
- }) ;
37
-
38
- /**
39
- * Adds Sentry tracing instrumentation for [Fastify](https://fastify.dev/).
40
- *
41
- * If you also want to capture errors, you need to call `setupFastifyErrorHandler(app)` after you set up your Fastify server.
42
- *
43
- * For more information, see the [fastify documentation](https://docs.sentry.io/platforms/javascript/guides/fastify/).
44
- *
45
- * @example
46
- * ```javascript
47
- * const Sentry = require('@sentry/node');
48
- *
49
- * Sentry.init({
50
- * integrations: [Sentry.fastifyIntegration()],
51
- * })
52
- * ```
53
- */
54
- const fastifyIntegration = core.defineIntegration(_fastifyIntegration);
55
-
56
- /**
57
- * Default function to determine if an error should be sent to Sentry
58
- *
59
- * 3xx and 4xx errors are not sent by default.
60
- */
61
- function defaultShouldHandleError(_error, _request, reply) {
62
- const statusCode = reply.statusCode;
63
- // 3xx and 4xx errors are not sent by default.
64
- return statusCode >= 500 || statusCode <= 299;
65
- }
66
-
67
- /**
68
- * Add an Fastify error handler to capture errors to Sentry.
69
- *
70
- * @param fastify The Fastify instance to which to add the error handler
71
- * @param options Configuration options for the handler
72
- *
73
- * @example
74
- * ```javascript
75
- * const Sentry = require('@sentry/node');
76
- * const Fastify = require("fastify");
77
- *
78
- * const app = Fastify();
79
- *
80
- * Sentry.setupFastifyErrorHandler(app);
81
- *
82
- * // Add your routes, etc.
83
- *
84
- * app.listen({ port: 3000 });
85
- * ```
86
- */
87
- function setupFastifyErrorHandler(fastify, options) {
88
- const shouldHandleError = options?.shouldHandleError || defaultShouldHandleError;
89
-
90
- const plugin = Object.assign(
91
- function (fastify, _options, done) {
92
- fastify.addHook('onError', async (request, reply, error) => {
93
- if (shouldHandleError(error, request, reply)) {
94
- core.captureException(error);
95
- }
96
- });
97
-
98
- // registering `onRequest` hook here instead of using Otel `onRequest` callback b/c `onRequest` hook
99
- // is ironically called in the fastify `preHandler` hook which is called later in the lifecycle:
100
- // https://fastify.dev/docs/latest/Reference/Lifecycle/
101
- fastify.addHook('onRequest', async (request, _reply) => {
102
- // Taken from Otel Fastify instrumentation:
103
- // https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts#L94-L96
104
- const routeName = request.routeOptions?.url || request.routerPath;
105
- const method = request.method || 'GET';
106
-
107
- core.getIsolationScope().setTransactionName(`${method} ${routeName}`);
108
- });
109
-
110
- done();
111
- },
112
- {
113
- [Symbol.for('skip-override')]: true,
114
- [Symbol.for('fastify.display-name')]: 'sentry-fastify-error-handler',
115
- },
116
- );
117
-
118
- fastify.register(plugin);
119
-
120
- // Sadly, middleware spans do not go through `requestHook`, so we handle those here
121
- // We register this hook in this method, because if we register it in the integration `setup`,
122
- // it would always run even for users that are not even using fastify
123
- const client = core.getClient();
124
- if (client) {
125
- client.on('spanStart', span => {
126
- addFastifySpanAttributes(span);
127
- });
128
- }
129
-
130
- ensureIsWrapped.ensureIsWrapped(fastify.addHook, 'fastify');
131
- }
132
-
133
- function addFastifySpanAttributes(span) {
134
- const attributes = core.spanToJSON(span).data;
135
-
136
- // this is one of: middleware, request_handler
137
- const type = attributes['fastify.type'];
138
-
139
- // If this is already set, or we have no fastify span, no need to process again...
140
- if (attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_OP] || !type) {
141
- return;
142
- }
143
-
144
- span.setAttributes({
145
- [core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.otel.fastify',
146
- [core.SEMANTIC_ATTRIBUTE_SENTRY_OP]: `${type}.fastify`,
147
- });
148
-
149
- // Also update the name, we don't need to "middleware - " prefix
150
- const name = attributes['fastify.name'] || attributes['plugin.name'] || attributes['hook.name'];
151
- if (typeof name === 'string') {
152
- // Also remove `fastify -> ` prefix
153
- span.updateName(name.replace(/^fastify -> /, ''));
154
- }
155
- }
156
-
157
- exports.fastifyIntegration = fastifyIntegration;
158
- exports.instrumentFastify = instrumentFastify;
159
- exports.setupFastifyErrorHandler = setupFastifyErrorHandler;
160
- //# sourceMappingURL=fastify.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fastify.js","sources":["../../../../src/integrations/tracing/fastify.ts"],"sourcesContent":["import { FastifyInstrumentation } from '@opentelemetry/instrumentation-fastify';\nimport type { IntegrationFn, Span } from '@sentry/core';\nimport {\n captureException,\n defineIntegration,\n getClient,\n getIsolationScope,\n SEMANTIC_ATTRIBUTE_SENTRY_OP,\n SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,\n spanToJSON,\n} from '@sentry/core';\nimport { generateInstrumentOnce } from '../../otel/instrument';\nimport { ensureIsWrapped } from '../../utils/ensureIsWrapped';\n\n/**\n * Minimal request type containing properties around route information.\n * Works for Fastify 3, 4 and presumably 5.\n *\n * Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/request.d.ts\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ninterface MinimalFastifyRequest extends Record<string, any> {\n method?: string;\n // since fastify@4.10.0\n routeOptions?: {\n url?: string;\n };\n routerPath?: string;\n}\n\n/**\n * Minimal reply type containing properties needed for error handling.\n *\n * Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/reply.d.ts\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ninterface MinimalFastifyReply extends Record<string, any> {\n statusCode: number;\n}\n\n// We inline the types we care about here\ninterface Fastify {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n register: (plugin: any) => void;\n addHook: (hook: string, handler: (...params: unknown[]) => void) => void;\n}\n\ninterface FastifyWithHooks extends Omit<Fastify, 'addHook'> {\n addHook(\n hook: 'onError',\n handler: (request: MinimalFastifyRequest, reply: MinimalFastifyReply, error: Error) => void,\n ): void;\n addHook(hook: 'onRequest', handler: (request: MinimalFastifyRequest, reply: MinimalFastifyReply) => void): void;\n}\n\ninterface FastifyHandlerOptions {\n /**\n * Callback method deciding whether error should be captured and sent to Sentry\n *\n * @param error Captured Fastify error\n * @param request Fastify request (or any object containing at least method, routeOptions.url, and routerPath)\n * @param reply Fastify reply (or any object containing at least statusCode)\n *\n * @example\n *\n * ```javascript\n * setupFastifyErrorHandler(app, {\n * shouldHandleError(_error, _request, reply) {\n * return reply.statusCode >= 400;\n * },\n * });\n * ```\n *\n * If using TypeScript, you can cast the request and reply to get full type safety.\n *\n * ```typescript\n * import type { FastifyRequest, FastifyReply } from 'fastify';\n *\n * setupFastifyErrorHandler(app, {\n * shouldHandleError(error, minimalRequest, minimalReply) {\n * const request = minimalRequest as FastifyRequest;\n * const reply = minimalReply as FastifyReply;\n * return reply.statusCode >= 500;\n * },\n * });\n * ```\n */\n shouldHandleError: (error: Error, request: MinimalFastifyRequest, reply: MinimalFastifyReply) => boolean;\n}\n\nconst INTEGRATION_NAME = 'Fastify';\n\nexport const instrumentFastify = generateInstrumentOnce(\n INTEGRATION_NAME,\n () =>\n // eslint-disable-next-line deprecation/deprecation\n new FastifyInstrumentation({\n requestHook(span) {\n addFastifySpanAttributes(span);\n },\n }),\n);\n\nconst _fastifyIntegration = (() => {\n return {\n name: INTEGRATION_NAME,\n setupOnce() {\n instrumentFastify();\n },\n };\n}) satisfies IntegrationFn;\n\n/**\n * Adds Sentry tracing instrumentation for [Fastify](https://fastify.dev/).\n *\n * If you also want to capture errors, you need to call `setupFastifyErrorHandler(app)` after you set up your Fastify server.\n *\n * For more information, see the [fastify documentation](https://docs.sentry.io/platforms/javascript/guides/fastify/).\n *\n * @example\n * ```javascript\n * const Sentry = require('@sentry/node');\n *\n * Sentry.init({\n * integrations: [Sentry.fastifyIntegration()],\n * })\n * ```\n */\nexport const fastifyIntegration = defineIntegration(_fastifyIntegration);\n\n/**\n * Default function to determine if an error should be sent to Sentry\n *\n * 3xx and 4xx errors are not sent by default.\n */\nfunction defaultShouldHandleError(_error: Error, _request: MinimalFastifyRequest, reply: MinimalFastifyReply): boolean {\n const statusCode = reply.statusCode;\n // 3xx and 4xx errors are not sent by default.\n return statusCode >= 500 || statusCode <= 299;\n}\n\n/**\n * Add an Fastify error handler to capture errors to Sentry.\n *\n * @param fastify The Fastify instance to which to add the error handler\n * @param options Configuration options for the handler\n *\n * @example\n * ```javascript\n * const Sentry = require('@sentry/node');\n * const Fastify = require(\"fastify\");\n *\n * const app = Fastify();\n *\n * Sentry.setupFastifyErrorHandler(app);\n *\n * // Add your routes, etc.\n *\n * app.listen({ port: 3000 });\n * ```\n */\nexport function setupFastifyErrorHandler(fastify: Fastify, options?: Partial<FastifyHandlerOptions>): void {\n const shouldHandleError = options?.shouldHandleError || defaultShouldHandleError;\n\n const plugin = Object.assign(\n function (fastify: FastifyWithHooks, _options: unknown, done: () => void): void {\n fastify.addHook('onError', async (request, reply, error) => {\n if (shouldHandleError(error, request, reply)) {\n captureException(error);\n }\n });\n\n // registering `onRequest` hook here instead of using Otel `onRequest` callback b/c `onRequest` hook\n // is ironically called in the fastify `preHandler` hook which is called later in the lifecycle:\n // https://fastify.dev/docs/latest/Reference/Lifecycle/\n fastify.addHook('onRequest', async (request, _reply) => {\n // Taken from Otel Fastify instrumentation:\n // https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts#L94-L96\n const routeName = request.routeOptions?.url || request.routerPath;\n const method = request.method || 'GET';\n\n getIsolationScope().setTransactionName(`${method} ${routeName}`);\n });\n\n done();\n },\n {\n [Symbol.for('skip-override')]: true,\n [Symbol.for('fastify.display-name')]: 'sentry-fastify-error-handler',\n },\n );\n\n fastify.register(plugin);\n\n // Sadly, middleware spans do not go through `requestHook`, so we handle those here\n // We register this hook in this method, because if we register it in the integration `setup`,\n // it would always run even for users that are not even using fastify\n const client = getClient();\n if (client) {\n client.on('spanStart', span => {\n addFastifySpanAttributes(span);\n });\n }\n\n ensureIsWrapped(fastify.addHook, 'fastify');\n}\n\nfunction addFastifySpanAttributes(span: Span): void {\n const attributes = spanToJSON(span).data;\n\n // this is one of: middleware, request_handler\n const type = attributes['fastify.type'];\n\n // If this is already set, or we have no fastify span, no need to process again...\n if (attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] || !type) {\n return;\n }\n\n span.setAttributes({\n [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.otel.fastify',\n [SEMANTIC_ATTRIBUTE_SENTRY_OP]: `${type}.fastify`,\n });\n\n // Also update the name, we don't need to \"middleware - \" prefix\n const name = attributes['fastify.name'] || attributes['plugin.name'] || attributes['hook.name'];\n if (typeof name === 'string') {\n // Also remove `fastify -> ` prefix\n span.updateName(name.replace(/^fastify -> /, ''));\n }\n}\n"],"names":["generateInstrumentOnce","FastifyInstrumentation","defineIntegration","captureException","getIsolationScope","getClient","ensureIsWrapped","spanToJSON","SEMANTIC_ATTRIBUTE_SENTRY_OP","SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN"],"mappings":";;;;;;;AAcA;AACA;AACA;AACA;AACA;AACA;AACA;;AAsEA,MAAM,gBAAA,GAAmB,SAAS;;AAE3B,MAAM,iBAAkB,GAAEA,iCAAsB;AACvD,EAAE,gBAAgB;AAClB,EAAE;AACF;AACA,IAAI,IAAIC,6CAAsB,CAAC;AAC/B,MAAM,WAAW,CAAC,IAAI,EAAE;AACxB,QAAQ,wBAAwB,CAAC,IAAI,CAAC;AACtC,OAAO;AACP,KAAK,CAAC;AACN;;AAEA,MAAM,mBAAoB,IAAG,MAAM;AACnC,EAAE,OAAO;AACT,IAAI,IAAI,EAAE,gBAAgB;AAC1B,IAAI,SAAS,GAAG;AAChB,MAAM,iBAAiB,EAAE;AACzB,KAAK;AACL,GAAG;AACH,CAAC,CAAE;;AAEH;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA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