Skip to content

Commit 1c9aec1

Browse files
authored
refactor(instr-fastify): use exported strings for attributes (#2078)
Refs: #2025
1 parent 17a0bc1 commit 1c9aec1

File tree

6 files changed

+29
-28
lines changed

6 files changed

+29
-28
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/node/opentelemetry-instrumentation-fastify/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ const fastifyInstrumentation = new FastifyInstrumentation({
6868
});
6969
```
7070

71+
## Semantic Conventions
72+
73+
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)
74+
75+
Attributes collected:
76+
77+
| Attribute | Short Description | Notes |
78+
| ------------ | ---------------------------------- | -------------------------- |
79+
| `http.route` | The matched route (path template). | Key: `SEMATTRS_HTTP_ROUTE` |
80+
7181
## Useful links
7282

7383
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>

plugins/node/opentelemetry-instrumentation-fastify/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"dependencies": {
6767
"@opentelemetry/core": "^1.8.0",
6868
"@opentelemetry/instrumentation": "^0.50.0",
69-
"@opentelemetry/semantic-conventions": "^1.0.0"
69+
"@opentelemetry/semantic-conventions": "^1.22.0"
7070
},
7171
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-fastify#readme"
7272
}

plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,14 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {
18-
context,
19-
SpanAttributes,
20-
SpanStatusCode,
21-
trace,
22-
} from '@opentelemetry/api';
17+
import { context, Attributes, SpanStatusCode, trace } from '@opentelemetry/api';
2318
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
2419
import {
2520
InstrumentationBase,
2621
InstrumentationNodeModuleDefinition,
2722
safeExecuteInTheMiddle,
2823
} from '@opentelemetry/instrumentation';
29-
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
24+
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
3025
import type {
3126
HookHandlerDoneFunction,
3227
FastifyInstance,
@@ -275,10 +270,10 @@ export class FastifyInstrumentation extends InstrumentationBase {
275270
handlerName || this.pluginName || ANONYMOUS_NAME
276271
}`;
277272

278-
const spanAttributes: SpanAttributes = {
273+
const spanAttributes: Attributes = {
279274
[AttributeNames.PLUGIN_NAME]: this.pluginName,
280275
[AttributeNames.FASTIFY_TYPE]: FastifyTypes.REQUEST_HANDLER,
281-
[SemanticAttributes.HTTP_ROUTE]: anyRequest.routeOptions
276+
[SEMATTRS_HTTP_ROUTE]: anyRequest.routeOptions
282277
? anyRequest.routeOptions.url // since [email protected]
283278
: request.routerPath,
284279
};

plugins/node/opentelemetry-instrumentation-fastify/test/fixtures/use-fastify.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// Use fastify from an ES module:
1818
// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-fastify.mjs
1919

20-
import { trace } from '@opentelemetry/api';
2120
import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';
2221

2322
import { FastifyInstrumentation } from '../../build/src/index.js';

plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
import * as assert from 'assert';
1818
import { context, SpanStatusCode } from '@opentelemetry/api';
19-
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
19+
import {
20+
SEMATTRS_HTTP_ROUTE,
21+
SEMATTRS_HTTP_METHOD,
22+
} from '@opentelemetry/semantic-conventions';
2023
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
2124
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
2225
import {
@@ -157,7 +160,7 @@ describe('fastify', () => {
157160
assert.deepStrictEqual(span.attributes, {
158161
'fastify.type': 'request_handler',
159162
'plugin.name': 'fastify -> @fastify/express',
160-
[SemanticAttributes.HTTP_ROUTE]: '/test',
163+
[SEMATTRS_HTTP_ROUTE]: '/test',
161164
});
162165
assert.strictEqual(
163166
span.name,
@@ -183,7 +186,7 @@ describe('fastify', () => {
183186
'fastify.type': 'request_handler',
184187
'fastify.name': 'namedHandler',
185188
'plugin.name': 'fastify -> @fastify/express',
186-
[SemanticAttributes.HTTP_ROUTE]: '/test',
189+
[SEMATTRS_HTTP_ROUTE]: '/test',
187190
});
188191
assert.strictEqual(span.name, 'request handler - namedHandler');
189192

@@ -474,10 +477,7 @@ describe('fastify', () => {
474477
describe('using requestHook in config', () => {
475478
it('calls requestHook provided function when set in config', async () => {
476479
const requestHook = (span: Span, info: FastifyRequestInfo) => {
477-
span.setAttribute(
478-
SemanticAttributes.HTTP_METHOD,
479-
info.request.method
480-
);
480+
span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method);
481481
};
482482

483483
instrumentation.setConfig({
@@ -498,17 +498,14 @@ describe('fastify', () => {
498498
assert.deepStrictEqual(span.attributes, {
499499
'fastify.type': 'request_handler',
500500
'plugin.name': 'fastify -> @fastify/express',
501-
[SemanticAttributes.HTTP_ROUTE]: '/test',
502-
[SemanticAttributes.HTTP_METHOD]: 'GET',
501+
[SEMATTRS_HTTP_ROUTE]: '/test',
502+
[SEMATTRS_HTTP_METHOD]: 'GET',
503503
});
504504
});
505505

506506
it('does not propagate an error from a requestHook that throws exception', async () => {
507507
const requestHook = (span: Span, info: FastifyRequestInfo) => {
508-
span.setAttribute(
509-
SemanticAttributes.HTTP_METHOD,
510-
info.request.method
511-
);
508+
span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method);
512509

513510
throw Error('error thrown in requestHook');
514511
};
@@ -531,8 +528,8 @@ describe('fastify', () => {
531528
assert.deepStrictEqual(span.attributes, {
532529
'fastify.type': 'request_handler',
533530
'plugin.name': 'fastify -> @fastify/express',
534-
[SemanticAttributes.HTTP_ROUTE]: '/test',
535-
[SemanticAttributes.HTTP_METHOD]: 'GET',
531+
[SEMATTRS_HTTP_ROUTE]: '/test',
532+
[SEMATTRS_HTTP_METHOD]: 'GET',
536533
});
537534
});
538535
});

0 commit comments

Comments
 (0)