Skip to content

Commit

Permalink
refactor(instr-fastify): use exported strings for attributes (#2078)
Browse files Browse the repository at this point in the history
Refs: #2025
  • Loading branch information
david-luna authored Apr 3, 2024
1 parent 17a0bc1 commit 1c9aec1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions plugins/node/opentelemetry-instrumentation-fastify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <https://opentelemetry.io/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 [email protected]
: request.routerPath,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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');

Expand Down Expand Up @@ -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({
Expand All @@ -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');
};
Expand All @@ -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',
});
});
});
Expand Down

0 comments on commit 1c9aec1

Please sign in to comment.