Skip to content

Commit da27a09

Browse files
committed
fix(instrumentation-fastify): do not wrap preClose and onRequestAbort hooks
Fixes: open-telemetry#1762
1 parent 1b0caa6 commit da27a09

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

plugins/node/opentelemetry-instrumentation-fastify/src/constants.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@ export const spanRequestSymbol = Symbol(
1818
'opentelemetry.instrumentation.fastify.request_active_span'
1919
);
2020

21-
export const applicationHookNames = [
22-
'onRegister',
23-
'onRoute',
24-
'onReady',
25-
'onClose',
26-
];
21+
// The instrumentation creates a span for invocations of lifecycle hook handlers
22+
// that take `(request, reply, ...[, done])` arguments. Currently this is all
23+
// lifecycle hooks except `onRequestAbort`.
24+
// https://fastify.dev/docs/latest/Reference/Hooks
25+
export const hooksNamesToWrap = new Set([
26+
'onTimeout',
27+
'onRequest',
28+
'preParsing',
29+
'preValidation',
30+
'preSerialization',
31+
'preHandler',
32+
'onSend',
33+
'onResponse',
34+
'onError',
35+
]);

plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import type {
3333
FastifyRequest,
3434
FastifyReply,
3535
} from 'fastify';
36-
import { applicationHookNames } from './constants';
36+
import { hooksNamesToWrap } from './constants';
3737
import {
3838
AttributeNames,
3939
FastifyNames,
@@ -178,8 +178,8 @@ export class FastifyInstrumentation extends InstrumentationBase {
178178
const name = args[0] as string;
179179
const handler = args[1] as HandlerOriginal;
180180
const pluginName = this.pluginName;
181-
if (applicationHookNames.includes(name)) {
182-
return original.apply(this, [name, handler] as never);
181+
if (!hooksNamesToWrap.has(name)) {
182+
return original.apply(this, args);
183183
}
184184

185185
const syncFunctionWithDone =

plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ describe('fastify', () => {
424424
await startServer();
425425
});
426426

427+
it('preClose is not instrumented', async () => {
428+
app.addHook('preClose', () => {
429+
assertRootContextActive();
430+
});
431+
432+
await startServer();
433+
});
434+
427435
it('onClose is not instrumented', async () => {
428436
app.addHook('onClose', () => {
429437
assertRootContextActive();

0 commit comments

Comments
 (0)