From 22793d3cccfec33e11d0f436091487a139c2b4dd Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 1 Nov 2023 15:54:08 -0700 Subject: [PATCH 01/10] fix(fastify): fix span attributes and avoid FSTDEP017 FastifyDeprecation warning for 404 request For a 404 `request.routeOptions.url` is undefined. Since fastify@4.10.0 when routeOptions was added, we shouldn't fallback to the deprecated request.routerPath. This also corrects the assumption that the handler name is "bound ..." in all cases. E.g. for a 404 it is Fastify's core "basic404" internal function. Fixes: https://github.com/open-telemetry/opentelemetry-js-contrib/issues/1757 --- .../package.json | 2 +- .../src/instrumentation.ts | 16 ++++++++++------ .../test/instrumentation.test.ts | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-fastify/package.json b/plugins/node/opentelemetry-instrumentation-fastify/package.json index 9d110cccc1..7e8357db94 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/package.json +++ b/plugins/node/opentelemetry-instrumentation-fastify/package.json @@ -55,7 +55,7 @@ "@types/express": "4.17.18", "@types/mocha": "7.0.2", "@types/node": "18.15.3", - "fastify": "4.18.0", + "fastify": "4.24.3", "mocha": "7.2.0", "nyc": "15.1.0", "rimraf": "5.0.5", diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts index ba96a020dd..484aef8226 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -96,8 +96,9 @@ export class FastifyInstrumentation extends InstrumentationBase { const anyRequest = request as any; const rpcMetadata = getRPCMetadata(context.active()); - const routeName = - anyRequest.routeOptions?.config?.url || request.routerPath; + const routeName = anyRequest.routeOptions + ? anyRequest.routeOptions.url + : request.routerPath; if (routeName && rpcMetadata?.type === RPCType.HTTP) { rpcMetadata.route = routeName; } @@ -265,9 +266,11 @@ export class FastifyInstrumentation extends InstrumentationBase { const anyRequest = request as any; const handler = - anyRequest.routeOptions?.handler || anyRequest.context?.handler || {}; + anyRequest.routeOptions?.handler || anyRequest.context?.handler; - const handlerName = handler?.name.substr(6); + const handlerName = handler?.name.startsWith('bound ') + ? handler.name.substr(6) + : handler?.name; const spanName = `${FastifyNames.REQUEST_HANDLER} - ${ handlerName || this.pluginName || ANONYMOUS_NAME }`; @@ -275,8 +278,9 @@ export class FastifyInstrumentation extends InstrumentationBase { const spanAttributes: SpanAttributes = { [AttributeNames.PLUGIN_NAME]: this.pluginName, [AttributeNames.FASTIFY_TYPE]: FastifyTypes.REQUEST_HANDLER, - [SemanticAttributes.HTTP_ROUTE]: - anyRequest.routeOptions?.config?.url || request.routerPath, + [SemanticAttributes.HTTP_ROUTE]: anyRequest.routeOptions + ? anyRequest.routeOptions.url + : request.routerPath, }; if (handlerName) { spanAttributes[AttributeNames.FASTIFY_NAME] = handlerName; diff --git a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts index 90a881a321..7b7fa63a65 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts @@ -183,6 +183,23 @@ describe('fastify', () => { assert.strictEqual(span.parentSpanId, baseSpan.spanContext().spanId); }); + it('should generate span for 404 request', async () => { + await startServer(); + await httpRequest.get(`http://localhost:${PORT}/no-such-route`); + + const spans = memoryExporter.getFinishedSpans(); + assert.strictEqual(spans.length, 5); + const span = spans[2]; + assert.deepStrictEqual(span.attributes, { + 'fastify.name': 'basic404', + 'fastify.type': 'request_handler', + 'plugin.name': 'fastify -> @fastify/express', + }); + assert.strictEqual(span.name, 'request handler - basic404'); + const baseSpan = spans[1]; + assert.strictEqual(span.parentSpanId, baseSpan.spanContext().spanId); + }); + describe('when subsystem is registered', () => { beforeEach(async () => { httpInstrumentation.enable(); From 94553e423c0e7817c09e02383d50589518b1278a Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 1 Nov 2023 16:20:32 -0700 Subject: [PATCH 02/10] need to limit to fastify 4.18.0, because later versions require a newer typescript than 4.4.4 --- plugins/node/opentelemetry-instrumentation-fastify/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-fastify/package.json b/plugins/node/opentelemetry-instrumentation-fastify/package.json index 7e8357db94..9d110cccc1 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/package.json +++ b/plugins/node/opentelemetry-instrumentation-fastify/package.json @@ -55,7 +55,7 @@ "@types/express": "4.17.18", "@types/mocha": "7.0.2", "@types/node": "18.15.3", - "fastify": "4.24.3", + "fastify": "4.18.0", "mocha": "7.2.0", "nyc": "15.1.0", "rimraf": "5.0.5", From 03a26acf256f737a723db94c468ef47fbb14a486 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Mon, 6 Nov 2023 10:58:03 -0800 Subject: [PATCH 03/10] hopefully helpful comment on when routeOptions.url became available --- .../src/instrumentation.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts index efe9adbc74..dacc257e97 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -97,7 +97,7 @@ export class FastifyInstrumentation extends InstrumentationBase { const rpcMetadata = getRPCMetadata(context.active()); const routeName = anyRequest.routeOptions - ? anyRequest.routeOptions.url + ? anyRequest.routeOptions.url // since fastify@4.10.0 : request.routerPath; if (routeName && rpcMetadata?.type === RPCType.HTTP) { rpcMetadata.route = routeName; @@ -279,7 +279,7 @@ export class FastifyInstrumentation extends InstrumentationBase { [AttributeNames.PLUGIN_NAME]: this.pluginName, [AttributeNames.FASTIFY_TYPE]: FastifyTypes.REQUEST_HANDLER, [SemanticAttributes.HTTP_ROUTE]: anyRequest.routeOptions - ? anyRequest.routeOptions.url + ? anyRequest.routeOptions.url // since fastify@4.10.0 : request.routerPath, }; if (handlerName) { From 839838f625321a6fcbee976be6db167504f851f1 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 7 Nov 2023 12:17:22 -0800 Subject: [PATCH 04/10] add some more versions to fastify test-all-versions tests --- .../opentelemetry-instrumentation-fastify/.tav.yml | 6 +++++- .../package.json | 2 ++ .../test/instrumentation.test.ts | 12 +++++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-fastify/.tav.yml b/plugins/node/opentelemetry-instrumentation-fastify/.tav.yml index d20da709a9..48410d0903 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/.tav.yml +++ b/plugins/node/opentelemetry-instrumentation-fastify/.tav.yml @@ -1,5 +1,9 @@ "fastify": - - versions: "4.23.2" + # Sanity check the first 4.x release, instead of all releases, plus recent + # releases. + - versions: "4.0.0 || >=4.24.3 <5" commands: npm run test + +# Fastify versions after 4.18.0 require a typescript greater than 4.4.4. "typescript": - versions: "4.7.4" diff --git a/plugins/node/opentelemetry-instrumentation-fastify/package.json b/plugins/node/opentelemetry-instrumentation-fastify/package.json index 9d110cccc1..8ec920e463 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/package.json +++ b/plugins/node/opentelemetry-instrumentation-fastify/package.json @@ -49,6 +49,7 @@ "@fastify/express": "^2.0.2", "@opentelemetry/api": "^1.3.0", "@opentelemetry/context-async-hooks": "^1.8.0", + "@opentelemetry/contrib-test-utils": "^0.34.2", "@opentelemetry/instrumentation-http": "0.44.0", "@opentelemetry/sdk-trace-base": "^1.8.0", "@opentelemetry/sdk-trace-node": "^1.8.0", @@ -59,6 +60,7 @@ "mocha": "7.2.0", "nyc": "15.1.0", "rimraf": "5.0.5", + "semver": "^7.5.4", "test-all-versions": "5.0.1", "ts-mocha": "10.0.0", "typescript": "4.4.4" diff --git a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts index d12cecd6c3..d58ae2aea7 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts @@ -25,6 +25,8 @@ import { SimpleSpanProcessor, } from '@opentelemetry/sdk-trace-base'; import { Span } from '@opentelemetry/api'; +import { getPackageVersion } from '@opentelemetry/contrib-test-utils'; +import * as semver from 'semver'; import * as http from 'http'; import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; import { AttributeNames, FastifyInstrumentation } from '../src'; @@ -32,6 +34,8 @@ import { FastifyRequestInfo } from '../src/types'; const URL = require('url').URL; +const fastifyVersion = getPackageVersion('fastify'); + const httpRequest = { get: (options: http.ClientRequestArgs | string) => { return new Promise((resolve, reject) => { @@ -441,7 +445,13 @@ describe('fastify', () => { await startServer(); }); - it('preClose is not instrumented', async () => { + it('preClose is not instrumented', async function () { + // 'preClose' was added in fastify@4.16.0 + if (semver.lt(fastifyVersion, '4.16.0')) { + this.skip(); + return; + } + app.addHook('preClose', () => { assertRootContextActive(); }); From 18832b4b46ed1ea4fc7d4bde78031a2f3b03ee2b Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 7 Nov 2023 13:42:28 -0800 Subject: [PATCH 05/10] fix compile --- .../package.json | 1 + .../test/instrumentation.test.ts | 13 ++++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-fastify/package.json b/plugins/node/opentelemetry-instrumentation-fastify/package.json index 8ec920e463..db7b99ab80 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/package.json +++ b/plugins/node/opentelemetry-instrumentation-fastify/package.json @@ -56,6 +56,7 @@ "@types/express": "4.17.18", "@types/mocha": "7.0.2", "@types/node": "18.15.3", + "@types/semver": "7.5.5", "fastify": "4.18.0", "mocha": "7.2.0", "nyc": "15.1.0", diff --git a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts index d58ae2aea7..8da39fcb31 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts @@ -449,14 +449,13 @@ describe('fastify', () => { // 'preClose' was added in fastify@4.16.0 if (semver.lt(fastifyVersion, '4.16.0')) { this.skip(); - return; - } - - app.addHook('preClose', () => { - assertRootContextActive(); - }); + } else { + app.addHook('preClose', () => { + assertRootContextActive(); + }); - await startServer(); + await startServer(); + } }); it('onClose is not instrumented', async () => { From 4dffa45ed269f1d8f438574a2225796a905c7e0d Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 7 Nov 2023 16:09:36 -0800 Subject: [PATCH 06/10] poke to re-run tests to see if some failures are spurious --- .../test/instrumentation.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts index 8da39fcb31..f5aba05288 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts @@ -446,7 +446,7 @@ describe('fastify', () => { }); it('preClose is not instrumented', async function () { - // 'preClose' was added in fastify@4.16.0 + // 'preClose' was added in fastify@4.16.0. if (semver.lt(fastifyVersion, '4.16.0')) { this.skip(); } else { From b0b74e0ca29548a3eccc1806d8fefb3dcb34826a Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Thu, 16 Nov 2023 14:06:23 -0800 Subject: [PATCH 07/10] sync package-lock file. However this is also undoing part of recent #1723 changes. --- package-lock.json | 50 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4464f67380..4df7deafd0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -34363,7 +34363,7 @@ "license": "Apache-2.0", "dependencies": { "@opentelemetry/sdk-metrics": "^1.8.0", - "systeminformation": "^5.21.17" + "systeminformation": "^5.0.0" }, "devDependencies": { "@opentelemetry/api": "^1.3.0", @@ -34429,7 +34429,7 @@ "@opentelemetry/contrib-test-utils": "^0.34.3", "@types/mocha": "^9.1.1", "@types/node": "18.6.5", - "@types/sinon": "^10.0.20", + "@types/sinon": "^10.0.11", "expect": "29.2.0", "mocha": "7.2.0", "nyc": "15.1.0", @@ -34706,7 +34706,7 @@ "@opentelemetry/sdk-trace-node": "^1.8.0", "@types/mocha": "7.0.2", "@types/node": "18.6.5", - "@types/sinon": "^10.0.20", + "@types/sinon": "^10.0.11", "mocha": "7.2.0", "nyc": "15.1.0", "rimraf": "5.0.5", @@ -35363,7 +35363,7 @@ "express": "^4.17.1" }, "devDependencies": { - "@types/express": "^4.17.21", + "@types/express": "^4.17.13", "ts-node": "^10.6.0", "typescript": "4.4.4" }, @@ -35660,16 +35660,19 @@ "@fastify/express": "^2.0.2", "@opentelemetry/api": "^1.3.0", "@opentelemetry/context-async-hooks": "^1.8.0", + "@opentelemetry/contrib-test-utils": "^0.34.2", "@opentelemetry/instrumentation-http": "^0.45.1", "@opentelemetry/sdk-trace-base": "^1.8.0", "@opentelemetry/sdk-trace-node": "^1.8.0", "@types/express": "4.17.18", "@types/mocha": "7.0.2", "@types/node": "18.15.3", + "@types/semver": "7.5.5", "fastify": "4.18.0", "mocha": "7.2.0", "nyc": "15.1.0", "rimraf": "5.0.5", + "semver": "^7.5.4", "test-all-versions": "5.0.1", "ts-mocha": "10.0.0", "typescript": "4.4.4" @@ -35693,6 +35696,12 @@ "integrity": "sha512-p6ua9zBxz5otCmbpb5D3U4B5Nanw6Pk3PPyX05xnxbB/fRv71N7CPmORg7uAD5P70T0xmx1pzAx/FUfa5X+3cw==", "dev": true }, + "plugins/node/opentelemetry-instrumentation-fastify/node_modules/@types/semver": { + "version": "7.5.5", + "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.5.5.tgz", + "integrity": "sha512-+d+WYC1BxJ6yVOgUgzK8gWvp5qF8ssV5r4nsDcZWKRWcDQLQ619tvWAxJQYGgBrO1MnLJC7a5GtiYsAoQ47dJg==", + "dev": true + }, "plugins/node/opentelemetry-instrumentation-generic-pool": { "name": "@opentelemetry/instrumentation-generic-pool", "version": "0.32.4", @@ -35915,7 +35924,7 @@ "version": "0.23.0", "license": "Apache-2.0", "dependencies": { - "@koa/router": "^12.0.1", + "@koa/router": "^12.0.0", "@opentelemetry/api": "^1.0.0", "@opentelemetry/exporter-jaeger": "^1.0.0", "@opentelemetry/exporter-zipkin": "^1.0.0", @@ -35928,7 +35937,7 @@ "koa": "^2.13.0" }, "devDependencies": { - "@types/koa": "^2.13.11", + "@types/koa": "^2.13.5", "cross-env": "^7.0.0", "ts-node": "^10.6.0", "typescript": "4.4.4" @@ -36263,7 +36272,7 @@ "mysql": "^2.18.1" }, "devDependencies": { - "@types/mysql": "^2.15.24", + "@types/mysql": "^2.15.21", "cross-env": "^7.0.0", "ts-node": "^10.6.0", "typescript": "4.4.4" @@ -36666,7 +36675,7 @@ "redis": "^3.1.1" }, "devDependencies": { - "@types/express": "^4.17.21", + "@types/express": "^4.17.14", "cross-env": "^7.0.0", "ts-node": "^10.6.0", "typescript": "4.4.4" @@ -43991,7 +44000,7 @@ "nyc": "15.1.0", "rimraf": "5.0.5", "sinon": "15.2.0", - "systeminformation": "^5.21.17", + "systeminformation": "^5.0.0", "ts-mocha": "10.0.0", "typescript": "4.4.4" } @@ -44547,6 +44556,7 @@ "@fastify/express": "^2.0.2", "@opentelemetry/api": "^1.3.0", "@opentelemetry/context-async-hooks": "^1.8.0", + "@opentelemetry/contrib-test-utils": "^0.34.2", "@opentelemetry/core": "^1.8.0", "@opentelemetry/instrumentation": "^0.45.1", "@opentelemetry/instrumentation-http": "^0.45.1", @@ -44556,10 +44566,12 @@ "@types/express": "4.17.18", "@types/mocha": "7.0.2", "@types/node": "18.15.3", + "@types/semver": "7.5.5", "fastify": "4.18.0", "mocha": "7.2.0", "nyc": "15.1.0", "rimraf": "5.0.5", + "semver": "^7.5.4", "test-all-versions": "5.0.1", "ts-mocha": "10.0.0", "typescript": "4.4.4" @@ -44576,6 +44588,12 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-18.15.3.tgz", "integrity": "sha512-p6ua9zBxz5otCmbpb5D3U4B5Nanw6Pk3PPyX05xnxbB/fRv71N7CPmORg7uAD5P70T0xmx1pzAx/FUfa5X+3cw==", "dev": true + }, + "@types/semver": { + "version": "7.5.5", + "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.5.5.tgz", + "integrity": "sha512-+d+WYC1BxJ6yVOgUgzK8gWvp5qF8ssV5r4nsDcZWKRWcDQLQ619tvWAxJQYGgBrO1MnLJC7a5GtiYsAoQ47dJg==", + "dev": true } } }, @@ -44603,7 +44621,7 @@ "@opentelemetry/semantic-conventions": "^1.0.0", "@types/mocha": "7.0.2", "@types/node": "18.6.5", - "@types/sinon": "^10.0.20", + "@types/sinon": "^10.0.11", "mocha": "7.2.0", "nyc": "15.1.0", "rimraf": "5.0.5", @@ -45921,7 +45939,7 @@ "@opentelemetry/contrib-test-utils": "^0.34.3", "@types/mocha": "^9.1.1", "@types/node": "18.6.5", - "@types/sinon": "^10.0.20", + "@types/sinon": "^10.0.11", "expect": "29.2.0", "mocha": "7.2.0", "nyc": "15.1.0", @@ -52171,7 +52189,7 @@ "@opentelemetry/sdk-trace-base": "^1.0.0", "@opentelemetry/sdk-trace-node": "^1.0.0", "@opentelemetry/semantic-conventions": "^1.0.0", - "@types/express": "^4.17.21", + "@types/express": "^4.17.13", "axios": "^0.21.1", "cross-env": "^7.0.3", "express": "^4.17.1", @@ -55822,7 +55840,7 @@ "koa-example": { "version": "file:plugins/node/opentelemetry-instrumentation-koa/examples", "requires": { - "@koa/router": "^12.0.1", + "@koa/router": "^12.0.0", "@opentelemetry/api": "^1.0.0", "@opentelemetry/exporter-jaeger": "^1.0.0", "@opentelemetry/exporter-zipkin": "^1.0.0", @@ -55831,7 +55849,7 @@ "@opentelemetry/instrumentation-koa": "^0.31.0", "@opentelemetry/sdk-trace-base": "^1.0.0", "@opentelemetry/sdk-trace-node": "^1.0.0", - "@types/koa": "^2.13.11", + "@types/koa": "^2.13.5", "axios": "^0.21.1", "cross-env": "^7.0.0", "koa": "^2.13.0", @@ -58683,7 +58701,7 @@ "@opentelemetry/instrumentation-mysql": "^0.31.0", "@opentelemetry/sdk-trace-base": "^1.0.0", "@opentelemetry/sdk-trace-node": "^1.0.0", - "@types/mysql": "^2.15.24", + "@types/mysql": "^2.15.21", "cross-env": "^7.0.0", "mysql": "^2.18.1", "ts-node": "^10.6.0", @@ -61917,7 +61935,7 @@ "@opentelemetry/instrumentation-redis": "^0.32.0", "@opentelemetry/sdk-trace-base": "^1.0.0", "@opentelemetry/sdk-trace-node": "^1.0.0", - "@types/express": "^4.17.21", + "@types/express": "^4.17.14", "axios": "^0.21.1", "cross-env": "^7.0.0", "express": "^4.17.1", From d8ff26ea203489b267130f799600977c8cc1a979 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Thu, 16 Nov 2023 16:53:28 -0800 Subject: [PATCH 08/10] add an ESM test --- .../test/fixtures/use-fastify.mjs | 54 +++++++++++++++++++ .../test/instrumentation.test.ts | 32 ++++++++++- 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 plugins/node/opentelemetry-instrumentation-fastify/test/fixtures/use-fastify.mjs diff --git a/plugins/node/opentelemetry-instrumentation-fastify/test/fixtures/use-fastify.mjs b/plugins/node/opentelemetry-instrumentation-fastify/test/fixtures/use-fastify.mjs new file mode 100644 index 0000000000..a0d81bcc38 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-fastify/test/fixtures/use-fastify.mjs @@ -0,0 +1,54 @@ +/* + * Copyright The OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Use fastify from an ES module: +// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-fastify.mjs + +import { trace } from '@opentelemetry/api'; +import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils'; + +import { FastifyInstrumentation } from '../../build/src/index.js'; + +const sdk = createTestNodeSdk({ + serviceName: 'use-fastify', + instrumentations: [ + new FastifyInstrumentation() + ] +}) +sdk.start(); + +import Fastify from 'fastify'; +import http from 'http'; + +// Start a fastify server. +const app = Fastify(); +app.get('/a-route', function aRoute(_request, reply) { + reply.send({ hello: 'world' }); +}) +const addr = await app.listen({ port: 0 }); + +// Make a single request to it. +await new Promise(resolve => { + http.get(addr + '/a-route', (res) => { + res.resume(); + res.on('end', () => { + resolve(); + }); + }) +}) + +await app.close(); +await sdk.shutdown(); diff --git a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts index f5aba05288..3c0f541b06 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts @@ -25,7 +25,11 @@ import { SimpleSpanProcessor, } from '@opentelemetry/sdk-trace-base'; import { Span } from '@opentelemetry/api'; -import { getPackageVersion } from '@opentelemetry/contrib-test-utils'; +import { + getPackageVersion, + runTestFixture, + TestCollector, +} from '@opentelemetry/contrib-test-utils'; import * as semver from 'semver'; import * as http from 'http'; import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; @@ -533,4 +537,30 @@ describe('fastify', () => { }); }); }); + + it('should work with ESM usage', async () => { + await runTestFixture({ + cwd: __dirname, + argv: ['fixtures/use-fastify.mjs'], + env: { + NODE_OPTIONS: + '--experimental-loader=@opentelemetry/instrumentation/hook.mjs', + NODE_NO_WARNINGS: '1', + }, + checkResult: (err, stdout, stderr) => { + assert.ifError(err); + }, + checkCollector: (collector: TestCollector) => { + const spans = collector.sortedSpans; + assert.strictEqual(spans.length, 1); + assert.strictEqual(spans[0].name, 'request handler - aRoute'); + assert.strictEqual( + spans[0].attributes.filter(a => a.key === 'plugin.name')[0]?.value + ?.stringValue, + 'fastify', + 'attribute plugin.name' + ); + }, + }); + }); }); From bda679b044431b35b4e9626dc8ace9f5bff66895 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 21 Nov 2023 13:40:33 -0800 Subject: [PATCH 09/10] bump to latest contrib-test-utils ver --- package-lock.json | 4 ++-- .../node/opentelemetry-instrumentation-fastify/package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9353f74644..7dc33283c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35645,7 +35645,7 @@ "@fastify/express": "^2.0.2", "@opentelemetry/api": "^1.3.0", "@opentelemetry/context-async-hooks": "^1.8.0", - "@opentelemetry/contrib-test-utils": "^0.34.2", + "@opentelemetry/contrib-test-utils": "^0.34.3", "@opentelemetry/instrumentation-http": "^0.45.1", "@opentelemetry/sdk-trace-base": "^1.8.0", "@opentelemetry/sdk-trace-node": "^1.8.0", @@ -44540,7 +44540,7 @@ "@fastify/express": "^2.0.2", "@opentelemetry/api": "^1.3.0", "@opentelemetry/context-async-hooks": "^1.8.0", - "@opentelemetry/contrib-test-utils": "^0.34.2", + "@opentelemetry/contrib-test-utils": "^0.34.3", "@opentelemetry/core": "^1.8.0", "@opentelemetry/instrumentation": "^0.45.1", "@opentelemetry/instrumentation-http": "^0.45.1", diff --git a/plugins/node/opentelemetry-instrumentation-fastify/package.json b/plugins/node/opentelemetry-instrumentation-fastify/package.json index ec6c548e00..b2819efe19 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/package.json +++ b/plugins/node/opentelemetry-instrumentation-fastify/package.json @@ -46,7 +46,7 @@ "@fastify/express": "^2.0.2", "@opentelemetry/api": "^1.3.0", "@opentelemetry/context-async-hooks": "^1.8.0", - "@opentelemetry/contrib-test-utils": "^0.34.2", + "@opentelemetry/contrib-test-utils": "^0.34.3", "@opentelemetry/instrumentation-http": "^0.45.1", "@opentelemetry/sdk-trace-base": "^1.8.0", "@opentelemetry/sdk-trace-node": "^1.8.0", From 9ed84de7b9d32580fd20d540f8f54a68dd5edefe Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Thu, 23 Nov 2023 13:54:15 -0800 Subject: [PATCH 10/10] update to latest contrib-test-utils release, sync package-lock --- package-lock.json | 4 ++-- .../node/opentelemetry-instrumentation-fastify/package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 006b991348..0d85574b87 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35666,7 +35666,7 @@ "@fastify/express": "^2.0.2", "@opentelemetry/api": "^1.3.0", "@opentelemetry/context-async-hooks": "^1.8.0", - "@opentelemetry/contrib-test-utils": "^0.34.3", + "@opentelemetry/contrib-test-utils": "^0.35.0", "@opentelemetry/instrumentation-http": "^0.45.1", "@opentelemetry/sdk-trace-base": "^1.8.0", "@opentelemetry/sdk-trace-node": "^1.8.0", @@ -44561,7 +44561,7 @@ "@fastify/express": "^2.0.2", "@opentelemetry/api": "^1.3.0", "@opentelemetry/context-async-hooks": "^1.8.0", - "@opentelemetry/contrib-test-utils": "^0.34.3", + "@opentelemetry/contrib-test-utils": "^0.35.0", "@opentelemetry/core": "^1.8.0", "@opentelemetry/instrumentation": "^0.45.1", "@opentelemetry/instrumentation-http": "^0.45.1", diff --git a/plugins/node/opentelemetry-instrumentation-fastify/package.json b/plugins/node/opentelemetry-instrumentation-fastify/package.json index b2819efe19..a0c12a1418 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/package.json +++ b/plugins/node/opentelemetry-instrumentation-fastify/package.json @@ -46,7 +46,7 @@ "@fastify/express": "^2.0.2", "@opentelemetry/api": "^1.3.0", "@opentelemetry/context-async-hooks": "^1.8.0", - "@opentelemetry/contrib-test-utils": "^0.34.3", + "@opentelemetry/contrib-test-utils": "^0.35.0", "@opentelemetry/instrumentation-http": "^0.45.1", "@opentelemetry/sdk-trace-base": "^1.8.0", "@opentelemetry/sdk-trace-node": "^1.8.0",