From f364f3eb2d75d09c78b91f9d587a932c89c56bfb Mon Sep 17 00:00:00 2001 From: David Luna Date: Wed, 3 Apr 2024 16:53:35 +0200 Subject: [PATCH 1/3] refactor(instrumentation-fastify): use exported strings for attributes --- package-lock.json | 4 ++-- .../README.md | 10 ++++++++++ .../package.json | 2 +- .../src/instrumentation.ts | 8 ++++---- .../test/fixtures/use-fastify.mjs | 1 - .../test/instrumentation.test.ts | 18 +++++++++--------- 6 files changed, 26 insertions(+), 17 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4c2130b743..bb78eaedbd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37582,7 +37582,7 @@ "dependencies": { "@opentelemetry/core": "^1.8.0", "@opentelemetry/instrumentation": "^0.49.1", - "@opentelemetry/semantic-conventions": "^1.0.0" + "@opentelemetry/semantic-conventions": "^1.22.0" }, "devDependencies": { "@fastify/express": "^2.0.2", @@ -46026,7 +46026,7 @@ "@opentelemetry/instrumentation-http": "^0.49.1", "@opentelemetry/sdk-trace-base": "^1.8.0", "@opentelemetry/sdk-trace-node": "^1.8.0", - "@opentelemetry/semantic-conventions": "^1.0.0", + "@opentelemetry/semantic-conventions": "^1.22.0", "@types/express": "4.17.18", "@types/mocha": "7.0.2", "@types/node": "18.15.3", diff --git a/plugins/node/opentelemetry-instrumentation-fastify/README.md b/plugins/node/opentelemetry-instrumentation-fastify/README.md index a4a30e62cd..9f230b7b5b 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/README.md +++ b/plugins/node/opentelemetry-instrumentation-fastify/README.md @@ -68,6 +68,16 @@ const fastifyInstrumentation = new FastifyInstrumentation({ }); ``` +## Semantic Conventions + +This package uses `@opentelemetry/semantic-conventions` version `1.22+`, which implements Semantic Convention [Version 1.7.0](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/semantic_conventions/README.md) + +Attributes collected: + +| Attribute | Short Description | Notes | +| ------------ | ---------------------------------- | -------------------------- | +| `http.route` | The matched route (path template). | Key: `SEMATTRS_HTTP_ROUTE` | + ## Useful links - For more information on OpenTelemetry, visit: diff --git a/plugins/node/opentelemetry-instrumentation-fastify/package.json b/plugins/node/opentelemetry-instrumentation-fastify/package.json index 5efd419db0..051d78e496 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/package.json +++ b/plugins/node/opentelemetry-instrumentation-fastify/package.json @@ -66,7 +66,7 @@ "dependencies": { "@opentelemetry/core": "^1.8.0", "@opentelemetry/instrumentation": "^0.49.1", - "@opentelemetry/semantic-conventions": "^1.0.0" + "@opentelemetry/semantic-conventions": "^1.22.0" }, "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 dacc257e97..f439d21a77 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -16,7 +16,7 @@ import { context, - SpanAttributes, + Attributes, SpanStatusCode, trace, } from '@opentelemetry/api'; @@ -26,7 +26,7 @@ import { InstrumentationNodeModuleDefinition, safeExecuteInTheMiddle, } from '@opentelemetry/instrumentation'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions'; import type { HookHandlerDoneFunction, FastifyInstance, @@ -275,10 +275,10 @@ export class FastifyInstrumentation extends InstrumentationBase { handlerName || this.pluginName || ANONYMOUS_NAME }`; - const spanAttributes: SpanAttributes = { + const spanAttributes: Attributes = { [AttributeNames.PLUGIN_NAME]: this.pluginName, [AttributeNames.FASTIFY_TYPE]: FastifyTypes.REQUEST_HANDLER, - [SemanticAttributes.HTTP_ROUTE]: anyRequest.routeOptions + [SEMATTRS_HTTP_ROUTE]: anyRequest.routeOptions ? anyRequest.routeOptions.url // since fastify@4.10.0 : request.routerPath, }; diff --git a/plugins/node/opentelemetry-instrumentation-fastify/test/fixtures/use-fastify.mjs b/plugins/node/opentelemetry-instrumentation-fastify/test/fixtures/use-fastify.mjs index a0d81bcc38..4b234dcb7b 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/test/fixtures/use-fastify.mjs +++ b/plugins/node/opentelemetry-instrumentation-fastify/test/fixtures/use-fastify.mjs @@ -17,7 +17,6 @@ // 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'; diff --git a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts index 3c0f541b06..5be800fef9 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts @@ -16,7 +16,7 @@ import * as assert from 'assert'; import { context, SpanStatusCode } from '@opentelemetry/api'; -import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; +import { SEMATTRS_HTTP_ROUTE, SEMATTRS_HTTP_METHOD} from '@opentelemetry/semantic-conventions'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; import { @@ -157,7 +157,7 @@ describe('fastify', () => { assert.deepStrictEqual(span.attributes, { 'fastify.type': 'request_handler', 'plugin.name': 'fastify -> @fastify/express', - [SemanticAttributes.HTTP_ROUTE]: '/test', + [SEMATTRS_HTTP_ROUTE]: '/test', }); assert.strictEqual( span.name, @@ -183,7 +183,7 @@ describe('fastify', () => { 'fastify.type': 'request_handler', 'fastify.name': 'namedHandler', 'plugin.name': 'fastify -> @fastify/express', - [SemanticAttributes.HTTP_ROUTE]: '/test', + [SEMATTRS_HTTP_ROUTE]: '/test', }); assert.strictEqual(span.name, 'request handler - namedHandler'); @@ -475,7 +475,7 @@ describe('fastify', () => { it('calls requestHook provided function when set in config', async () => { const requestHook = (span: Span, info: FastifyRequestInfo) => { span.setAttribute( - SemanticAttributes.HTTP_METHOD, + SEMATTRS_HTTP_METHOD, info.request.method ); }; @@ -498,15 +498,15 @@ describe('fastify', () => { assert.deepStrictEqual(span.attributes, { 'fastify.type': 'request_handler', 'plugin.name': 'fastify -> @fastify/express', - [SemanticAttributes.HTTP_ROUTE]: '/test', - [SemanticAttributes.HTTP_METHOD]: 'GET', + [SEMATTRS_HTTP_ROUTE]: '/test', + [SEMATTRS_HTTP_METHOD]: 'GET', }); }); it('does not propagate an error from a requestHook that throws exception', async () => { const requestHook = (span: Span, info: FastifyRequestInfo) => { span.setAttribute( - SemanticAttributes.HTTP_METHOD, + SEMATTRS_HTTP_METHOD, info.request.method ); @@ -531,8 +531,8 @@ describe('fastify', () => { assert.deepStrictEqual(span.attributes, { 'fastify.type': 'request_handler', 'plugin.name': 'fastify -> @fastify/express', - [SemanticAttributes.HTTP_ROUTE]: '/test', - [SemanticAttributes.HTTP_METHOD]: 'GET', + [SEMATTRS_HTTP_ROUTE]: '/test', + [SEMATTRS_HTTP_METHOD]: 'GET', }); }); }); From 030c4b92727774a9fa52050e561a932243ddfac5 Mon Sep 17 00:00:00 2001 From: David Luna Date: Wed, 3 Apr 2024 17:09:39 +0200 Subject: [PATCH 2/3] chore(instrumentation-fastify): fix lint issues --- .../src/instrumentation.ts | 7 +------ .../test/instrumentation.test.ts | 15 ++++++--------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts index f439d21a77..223ed28854 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -14,12 +14,7 @@ * limitations under the License. */ -import { - context, - Attributes, - SpanStatusCode, - trace, -} from '@opentelemetry/api'; +import { context, Attributes, SpanStatusCode, trace } from '@opentelemetry/api'; import { getRPCMetadata, RPCType } from '@opentelemetry/core'; import { InstrumentationBase, diff --git a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts index 5be800fef9..73fb586da1 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts @@ -16,7 +16,10 @@ import * as assert from 'assert'; import { context, SpanStatusCode } from '@opentelemetry/api'; -import { SEMATTRS_HTTP_ROUTE, SEMATTRS_HTTP_METHOD} from '@opentelemetry/semantic-conventions'; +import { + SEMATTRS_HTTP_ROUTE, + SEMATTRS_HTTP_METHOD, +} from '@opentelemetry/semantic-conventions'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; import { @@ -474,10 +477,7 @@ describe('fastify', () => { describe('using requestHook in config', () => { it('calls requestHook provided function when set in config', async () => { const requestHook = (span: Span, info: FastifyRequestInfo) => { - span.setAttribute( - SEMATTRS_HTTP_METHOD, - info.request.method - ); + span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method); }; instrumentation.setConfig({ @@ -505,10 +505,7 @@ describe('fastify', () => { it('does not propagate an error from a requestHook that throws exception', async () => { const requestHook = (span: Span, info: FastifyRequestInfo) => { - span.setAttribute( - SEMATTRS_HTTP_METHOD, - info.request.method - ); + span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method); throw Error('error thrown in requestHook'); }; From b08bee6e5cea3c5e557f591d7cb35a8e58ffae1f Mon Sep 17 00:00:00 2001 From: David Luna Date: Wed, 3 Apr 2024 17:19:22 +0200 Subject: [PATCH 3/3] chore(instrumentation-fastify): update package.json --- 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 e22ed32332..64fe425979 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/package.json +++ b/plugins/node/opentelemetry-instrumentation-fastify/package.json @@ -66,7 +66,7 @@ "dependencies": { "@opentelemetry/core": "^1.8.0", "@opentelemetry/instrumentation": "^0.50.0", - "@opentelemetry/semantic-conventions": "^1.0.0" + "@opentelemetry/semantic-conventions": "^1.22.0" }, "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-fastify#readme" }