diff --git a/package-lock.json b/package-lock.json index b86482f96e..c30bea59b2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37582,7 +37582,7 @@ "dependencies": { "@opentelemetry/core": "^1.8.0", "@opentelemetry/instrumentation": "^0.50.0", - "@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.50.0", "@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 70b5a4bd0d..0f527fc875 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" } diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts index dacc257e97..223ed28854 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -14,19 +14,14 @@ * limitations under the License. */ -import { - context, - SpanAttributes, - SpanStatusCode, - trace, -} from '@opentelemetry/api'; +import { context, Attributes, SpanStatusCode, trace } from '@opentelemetry/api'; import { getRPCMetadata, RPCType } from '@opentelemetry/core'; import { InstrumentationBase, 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 +270,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..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 { 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 +160,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 +186,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'); @@ -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( - SemanticAttributes.HTTP_METHOD, - info.request.method - ); + span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method); }; instrumentation.setConfig({ @@ -498,17 +498,14 @@ 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, - info.request.method - ); + span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method); throw Error('error thrown in requestHook'); }; @@ -531,8 +528,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', }); }); });