diff --git a/plugins/node/opentelemetry-instrumentation-fastify/README.md b/plugins/node/opentelemetry-instrumentation-fastify/README.md index 9835aa225b..89c4a6e1d0 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/README.md +++ b/plugins/node/opentelemetry-instrumentation-fastify/README.md @@ -17,7 +17,7 @@ npm install --save @opentelemetry/instrumentation-http @opentelemetry/instrument ### Supported Versions -- `^3.0.0` +- fastify: `^3.0.0 || ^4.0.0` ## Usage diff --git a/plugins/node/opentelemetry-instrumentation-fastify/package.json b/plugins/node/opentelemetry-instrumentation-fastify/package.json index 65d3a7f959..672c06fbcf 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/package.json +++ b/plugins/node/opentelemetry-instrumentation-fastify/package.json @@ -45,15 +45,15 @@ "@opentelemetry/api": "^1.0.0" }, "devDependencies": { + "@fastify/express": "^2.0.2", "@opentelemetry/api": "^1.0.0", "@opentelemetry/context-async-hooks": "^1.3.1", "@opentelemetry/instrumentation-http": "0.30.0", - "@opentelemetry/sdk-trace-node": "^1.3.1", "@opentelemetry/sdk-trace-base": "^1.3.1", + "@opentelemetry/sdk-trace-node": "^1.3.1", "@types/express": "4.17.13", "@types/mocha": "7.0.2", "@types/node": "16.11.21", - "fastify-express": "0.3.3", "gts": "3.1.0", "mocha": "7.2.0", "nyc": "15.1.0", @@ -65,7 +65,7 @@ "@opentelemetry/core": "^1.0.0", "@opentelemetry/instrumentation": "^0.32.0", "@opentelemetry/semantic-conventions": "^1.0.0", - "fastify": "^3.19.2" + "fastify": "^4.5.3" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-fastify#readme" } diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts index cfe666c559..b267f03336 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -62,7 +62,7 @@ export class FastifyInstrumentation extends InstrumentationBase { return [ new InstrumentationNodeModuleDefinition( 'fastify', - ['^3.0.0'], + ['^3.0.0', '^4.0.0'], (moduleExports, moduleVersion) => { this._diag.debug(`Applying patch for fastify@${moduleVersion}`); return this._patchConstructor(moduleExports); @@ -100,6 +100,8 @@ export class FastifyInstrumentation extends InstrumentationBase { syncFunctionWithDone: boolean ): () => Promise { const instrumentation = this; + this._diag.debug('Patching fastify route.handler function'); + return function (this: any, ...args: unknown[]): Promise { if (!instrumentation.isEnabled()) { return original.apply(this, args); @@ -156,6 +158,8 @@ export class FastifyInstrumentation extends InstrumentationBase { original: FastifyInstance['addHook'] ) => () => FastifyInstance { const instrumentation = this; + this._diag.debug('Patching fastify server.addHook function'); + return function ( original: FastifyInstance['addHook'] ): () => FastifyInstance { @@ -188,6 +192,7 @@ export class FastifyInstrumentation extends InstrumentationBase { original: () => FastifyInstance ): () => FastifyInstance { const instrumentation = this; + this._diag.debug('Patching fastify constructor function'); function fastify(this: FastifyInstance, ...args: any) { const app: FastifyInstance = original.apply(this, args); @@ -206,6 +211,8 @@ export class FastifyInstrumentation extends InstrumentationBase { public _patchSend() { const instrumentation = this; + this._diag.debug('Patching fastify reply.send function'); + return function patchSend( original: () => FastifyReply ): () => FastifyReply { @@ -233,6 +240,8 @@ export class FastifyInstrumentation extends InstrumentationBase { public _hookPreHandler() { const instrumentation = this; + this._diag.debug('Patching fastify preHandler function'); + return function preHandler( this: any, request: FastifyRequest, diff --git a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts index 8588c24c60..aa17f3b246 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts @@ -67,7 +67,7 @@ provider.addSpanProcessor(spanProcessor); instrumentation.enable(); httpInstrumentation.enable(); -import 'fastify-express'; +import '@fastify/express'; import { FastifyInstance } from 'fastify/types/instance'; const Fastify = require('fastify'); @@ -94,7 +94,7 @@ describe('fastify', () => { let app: FastifyInstance; async function startServer(): Promise { - const address = await app.listen(0); + const address = await app.listen({ port: 0 }); const url = new URL(address); PORT = parseInt(url.port, 10); } @@ -102,7 +102,7 @@ describe('fastify', () => { beforeEach(async () => { instrumentation.enable(); app = Fastify(); - app.register(require('fastify-express')); + app.register(require('@fastify/express')); }); afterEach(async () => { @@ -145,7 +145,7 @@ describe('fastify', () => { const span = spans[3]; assert.deepStrictEqual(span.attributes, { 'fastify.type': 'request_handler', - 'plugin.name': 'fastify-express', + 'plugin.name': 'fastify -> @fastify/express', [SemanticAttributes.HTTP_ROUTE]: '/test', }); assert.strictEqual(span.name, `request handler - ${ANONYMOUS_NAME}`); @@ -168,7 +168,7 @@ describe('fastify', () => { assert.deepStrictEqual(span.attributes, { 'fastify.type': 'request_handler', 'fastify.name': 'namedHandler', - 'plugin.name': 'fastify-express', + 'plugin.name': 'fastify -> @fastify/express', [SemanticAttributes.HTTP_ROUTE]: '/test', }); assert.strictEqual(span.name, 'request handler - namedHandler'); @@ -232,7 +232,7 @@ describe('fastify', () => { assert.strictEqual(span.name, 'middleware - runConnect'); assert.deepStrictEqual(span.attributes, { 'fastify.type': 'middleware', - 'plugin.name': 'fastify-express', + 'plugin.name': 'fastify -> @fastify/express', 'hook.name': 'onRequest', }); @@ -248,7 +248,7 @@ describe('fastify', () => { assert.strictEqual(span.name, 'middleware - enhanceRequest'); assert.deepStrictEqual(span.attributes, { 'fastify.type': 'middleware', - 'plugin.name': 'fastify-express', + 'plugin.name': 'fastify -> @fastify/express', 'hook.name': 'onRequest', }); @@ -338,12 +338,16 @@ describe('fastify', () => { // done was not yet called from the hook, so it should not end the span const preDoneSpans = getSpans().filter( - s => !s.attributes[AttributeNames.PLUGIN_NAME] + s => + !s.attributes[AttributeNames.PLUGIN_NAME] || + s.attributes[AttributeNames.PLUGIN_NAME] === 'fastify' ); assert.strictEqual(preDoneSpans.length, 0); hookDone!(); const postDoneSpans = getSpans().filter( - s => !s.attributes[AttributeNames.PLUGIN_NAME] + s => + !s.attributes[AttributeNames.PLUGIN_NAME] || + s.attributes[AttributeNames.PLUGIN_NAME] === 'fastify' ); assert.strictEqual(postDoneSpans.length, 1); }); @@ -367,7 +371,9 @@ describe('fastify', () => { await startServer(); await httpRequest.get(`http://localhost:${PORT}/test`); const spans = getSpans().filter( - s => !s.attributes[AttributeNames.PLUGIN_NAME] + s => + !s.attributes[AttributeNames.PLUGIN_NAME] || + s.attributes[AttributeNames.PLUGIN_NAME] === 'fastify' ); assert.strictEqual(spans.length, 1); });