Skip to content

Commit 07faa5a

Browse files
committed
Move fastify into its own folder.
1 parent 4922148 commit 07faa5a

File tree

9 files changed

+115
-120
lines changed

9 files changed

+115
-120
lines changed

packages/node/src/integrations/tracing/fastify-v3/internal-types.ts

Lines changed: 0 additions & 69 deletions
This file was deleted.

packages/node/src/integrations/tracing/fastify.ts renamed to packages/node/src/integrations/tracing/fastify/index.ts

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,46 @@ import {
1010
spanToJSON,
1111
} from '@sentry/core';
1212
import type { IntegrationFn, Span } from '@sentry/core';
13-
import { generateInstrumentOnce } from '../../otel/instrument';
14-
import { FastifyInstrumentationV3 } from './fastify-v3/instrumentation';
13+
import { generateInstrumentOnce } from '../../../otel/instrument';
14+
import { FastifyInstrumentationV3 } from './v3/instrumentation';
1515
import * as diagnosticsChannel from 'node:diagnostics_channel';
16-
import type { FastifyInstance } from './fastify-v3/internal-types';
17-
import { DEBUG_BUILD } from '../../debug-build';
18-
19-
/**
20-
* Minimal request type containing properties around route information.
21-
* Works for Fastify 3, 4 and presumably 5.
22-
*
23-
* Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/request.d.ts
24-
*/
25-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26-
interface MinimalFastifyRequest extends Record<string, any> {
27-
method?: string;
28-
// since fastify@4.10.0
29-
routeOptions?: {
30-
url?: string;
31-
};
32-
routerPath?: string;
33-
}
34-
35-
/**
36-
* Minimal reply type containing properties needed for error handling.
37-
*
38-
* Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/reply.d.ts
39-
*/
40-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
41-
interface MinimalFastifyReply extends Record<string, any> {
42-
statusCode: number;
43-
}
44-
45-
// We inline the types we care about here
46-
interface Fastify {
47-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
48-
register: (plugin: any) => void;
49-
addHook: (hook: string, handler: (...params: unknown[]) => void) => void;
50-
}
51-
52-
interface FastifyWithHooks extends Omit<Fastify, 'addHook'> {
53-
addHook(
54-
hook: 'onError',
55-
handler: (request: MinimalFastifyRequest, reply: MinimalFastifyReply, error: Error) => void,
56-
): void;
57-
addHook(hook: 'onRequest', handler: (request: MinimalFastifyRequest, reply: MinimalFastifyReply) => void): void;
58-
}
16+
import { DEBUG_BUILD } from '../../../debug-build';
17+
import type { FastifyInstance, FastifyReply, FastifyRequest } from './types';
18+
19+
// /**
20+
// * Minimal request type containing properties around route information.
21+
// * Works for Fastify 3, 4 and presumably 5.
22+
// *
23+
// * Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/request.d.ts
24+
// */
25+
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
26+
// interface MinimalFastifyRequest extends Record<string, any> {
27+
// method?: string;
28+
// // since fastify@4.10.0
29+
// routeOptions?: {
30+
// url?: string;
31+
// };
32+
// routerPath?: string;
33+
// }
34+
35+
// /**
36+
// * Minimal reply type containing properties needed for error handling.
37+
// *
38+
// * Based on https://github.com/fastify/fastify/blob/ce3811f5f718be278bbcd4392c615d64230065a6/types/reply.d.ts
39+
// */
40+
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
41+
// interface MinimalFastifyReply extends Record<string, any> {
42+
// statusCode: number;
43+
// }
44+
45+
// // We inline the types we care about here
46+
// interface Fastify {
47+
// // eslint-disable-next-line @typescript-eslint/no-explicit-any
48+
// version: string;
49+
// register: (plugin: any) => Fastify;
50+
// after: (listener?: (err: Error) => void) => Fastify;
51+
// addHook: (name: string, handler: (...params: unknown[]) => void) => Fastify;
52+
// }
5953

6054
interface FastifyHandlerOptions {
6155
/**
@@ -89,7 +83,7 @@ interface FastifyHandlerOptions {
8983
* });
9084
* ```
9185
*/
92-
shouldHandleError: (error: Error, request: MinimalFastifyRequest, reply: MinimalFastifyReply) => boolean;
86+
shouldHandleError: (error: Error, request: FastifyRequest, reply: FastifyReply) => boolean;
9387
}
9488

9589
const INTEGRATION_NAME = 'Fastify';
@@ -157,7 +151,7 @@ export const fastifyIntegration = defineIntegration(_fastifyIntegration);
157151
*
158152
* 3xx and 4xx errors are not sent by default.
159153
*/
160-
function defaultShouldHandleError(_error: Error, _request: MinimalFastifyRequest, reply: MinimalFastifyReply): boolean {
154+
function defaultShouldHandleError(_error: Error, _request: FastifyRequest, reply: FastifyReply): boolean {
161155
const statusCode = reply.statusCode;
162156
// 3xx and 4xx errors are not sent by default.
163157
return statusCode >= 500 || statusCode <= 299;
@@ -183,11 +177,11 @@ function defaultShouldHandleError(_error: Error, _request: MinimalFastifyRequest
183177
* app.listen({ port: 3000 });
184178
* ```
185179
*/
186-
export function setupFastifyErrorHandler(fastify: Fastify, options?: Partial<FastifyHandlerOptions>): void {
180+
export function setupFastifyErrorHandler(fastify: FastifyInstance, options?: Partial<FastifyHandlerOptions>): void {
187181
const shouldHandleError = options?.shouldHandleError || defaultShouldHandleError;
188182

189183
const plugin = Object.assign(
190-
function (fastify: FastifyWithHooks, _options: unknown, done: () => void): void {
184+
function (fastify: FastifyInstance, _options: unknown, done: () => void): void {
191185
fastify.addHook('onError', async (request, reply, error) => {
192186
if (shouldHandleError(error, request, reply)) {
193187
captureException(error);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
export type HandlerOriginal =
2+
| ((request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => Promise<void>)
3+
| ((request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => void);
4+
5+
export type FastifyError = any;
6+
7+
export type HookHandlerDoneFunction = <TError extends Error = FastifyError>(err?: TError) => void;
8+
9+
export type FastifyErrorCodes = any;
10+
11+
export type FastifyPlugin = (
12+
instance: FastifyInstance,
13+
opts: any,
14+
done: HookHandlerDoneFunction,
15+
) => unknown | Promise<unknown>;
16+
17+
export interface FastifyInstance {
18+
version: string;
19+
register: (plugin: any) => FastifyInstance;
20+
after: (listener?: (err: Error) => void) => FastifyInstance;
21+
addHook(hook: string, handler: HandlerOriginal): FastifyInstance;
22+
addHook(
23+
hook: 'onError',
24+
handler: (request: FastifyRequest, reply: FastifyReply, error: Error) => void,
25+
): FastifyInstance;
26+
addHook(
27+
hook: 'onRequest', handler: (request: FastifyRequest, reply: FastifyReply) => void): FastifyInstance;
28+
29+
}
30+
31+
export interface FastifyReply {
32+
send: () => FastifyReply;
33+
statusCode: number;
34+
}
35+
36+
export interface FastifyRequest {
37+
method?: string;
38+
// since fastify@4.10.0
39+
routeOptions?: {
40+
url?: string;
41+
};
42+
routerPath?: string;
43+
}

packages/node/src/integrations/tracing/fastify-v3/instrumentation.ts renamed to packages/node/src/integrations/tracing/fastify/v3/instrumentation.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,18 @@ import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
3131

3232
import { AttributeNames, FastifyNames, FastifyTypes } from './enums/AttributeNames';
3333

34+
import type {
35+
PluginFastifyReply,
36+
} from './internal-types';
37+
3438
import type {
3539
FastifyErrorCodes,
3640
FastifyInstance,
3741
FastifyReply,
3842
FastifyRequest,
3943
HandlerOriginal,
4044
HookHandlerDoneFunction,
41-
PluginFastifyReply,
42-
} from './internal-types';
45+
} from '../types';
4346

4447
import type { FastifyInstrumentationConfig } from './types';
4548
import { endSpan, safeExecuteInTheMiddleMaybePromise, startSpan } from './utils';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Vendored from: https://github.com/open-telemetry/opentelemetry-js-contrib/blob/407f61591ba69a39a6908264379d4d98a48dbec4/plugins/node/opentelemetry-instrumentation-fastify/src/internal-types.ts
18+
import type { Span } from '@opentelemetry/api';
19+
import type { FastifyReply } from '../types';
20+
import type { spanRequestSymbol } from './constants';
21+
22+
export type PluginFastifyReply = FastifyReply & {
23+
[spanRequestSymbol]?: Span[];
24+
};

0 commit comments

Comments
 (0)