Skip to content

Commit 39eb7f4

Browse files
committed
Instrument Fastify
1 parent e258bdf commit 39eb7f4

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

library/sources/Fastify.ts

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ import { wrapHandler } from "./fastify/wrapHandler";
66
import { wrapNewInstance } from "../agent/hooks/wrapNewInstance";
77
import { wrapExport } from "../agent/hooks/wrapExport";
88
import { WrapPackageInfo } from "../agent/hooks/WrapPackageInfo";
9+
import { PackageFunctionInstrumentationInstruction } from "../agent/hooks/instrumentation/types";
10+
11+
const requestFunctions = [
12+
"get",
13+
"head",
14+
"post",
15+
"put",
16+
"delete",
17+
"options",
18+
"patch",
19+
"all",
20+
];
921

1022
export class Fastify implements Wrapper {
1123
private wrapRequestArgs(args: unknown[]) {
@@ -103,17 +115,6 @@ export class Fastify implements Wrapper {
103115
}
104116

105117
private wrapFastifyInstance(instance: any, pkgInfo: WrapPackageInfo) {
106-
const requestFunctions = [
107-
"get",
108-
"head",
109-
"post",
110-
"put",
111-
"delete",
112-
"options",
113-
"patch",
114-
"all",
115-
];
116-
117118
for (const func of requestFunctions) {
118119
// Check if the function exists - new functions in Fastify 5
119120
if (typeof instance[func] === "function") {
@@ -144,6 +145,42 @@ export class Fastify implements Wrapper {
144145
}
145146
}
146147

148+
private getFunctionInstructions(): PackageFunctionInstrumentationInstruction[] {
149+
return [
150+
{
151+
name: "addHook",
152+
nodeType: "FunctionDeclaration",
153+
operationKind: undefined,
154+
modifyArgs: this.wrapAddHookArgs,
155+
},
156+
{
157+
name: "addHttpMethod",
158+
nodeType: "FunctionDeclaration",
159+
operationKind: undefined,
160+
modifyReturnValue: (args, returnValue) =>
161+
this.wrapNewRouteMethod(args, returnValue, {
162+
name: "fastify",
163+
type: "external",
164+
}),
165+
},
166+
{
167+
name: "_route",
168+
nodeType: "FunctionExpression",
169+
operationKind: undefined,
170+
modifyArgs: this.wrapRouteMethod,
171+
},
172+
...requestFunctions.map(
173+
(func) =>
174+
({
175+
name: `_${func}`,
176+
nodeType: "FunctionExpression",
177+
operationKind: undefined,
178+
modifyArgs: this.wrapRequestArgs,
179+
}) as PackageFunctionInstrumentationInstruction
180+
),
181+
];
182+
}
183+
147184
wrap(hooks: Hooks) {
148185
hooks
149186
.addPackage("fastify")
@@ -161,6 +198,10 @@ export class Fastify implements Wrapper {
161198
return wrapNewInstance(exports, undefined, pkgInfo, (exports) =>
162199
this.wrapFastifyInstance(exports, pkgInfo)
163200
);
201+
})
202+
.addFileInstrumentation({
203+
path: "fastify.js",
204+
functions: this.getFunctionInstructions(),
164205
});
165206
}
166207
}

scripts/run-tap.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ if (process.argv.includes("--test-new-instrumentation")) {
4040
"**/sources/GraphQL.test.ts",
4141
"**/sources/GraphQL.schema.test.ts",
4242
"**/sources/GraphQL.tools.test.ts",
43-
"**/sources/Fastify.test.ts",
4443
];
4544

4645
for (const exclude of excludedTestFilesForNewInstrumentation) {

0 commit comments

Comments
 (0)