@@ -6,6 +6,18 @@ import { wrapHandler } from "./fastify/wrapHandler";
66import { wrapNewInstance } from "../agent/hooks/wrapNewInstance" ;
77import { wrapExport } from "../agent/hooks/wrapExport" ;
88import { 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
1022export 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}
0 commit comments