Skip to content

Commit 9f5a867

Browse files
refactor(instr-restify): use exported strings for attributes (#2098)
* refactor(instr-restify): use exported strings for attributes * chore(instr-restify): fix lint issues --------- Co-authored-by: Marc Pichler <[email protected]>
1 parent 5f42c51 commit 9f5a867

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
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-restify/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ const restifyInstrumentation = new RestifyInstrumentation({
6262
});
6363
```
6464

65+
## Semantic Conventions
66+
67+
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)
68+
69+
Attributes collected:
70+
71+
| Attribute | Short Description |
72+
| ------------ | ---------------------------------- |
73+
| `http.route` | The matched route (path template). |
74+
6575
## Useful links
6676

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

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

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

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type * as restify from 'restify';
2020
import * as api from '@opentelemetry/api';
2121
import type { Server } from 'restify';
2222
import { LayerType } from './types';
23-
import * as AttributeNames from './enums/AttributeNames';
23+
import { AttributeNames } from './enums/AttributeNames';
2424
import { VERSION } from './version';
2525
import * as constants from './constants';
2626
import {
@@ -30,7 +30,7 @@ import {
3030
isWrapped,
3131
safeExecuteInTheMiddle,
3232
} from '@opentelemetry/instrumentation';
33-
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
33+
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
3434
import { isPromise, isAsyncFunction } from './utils';
3535
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
3636
import type { RestifyInstrumentationConfig } from './types';
@@ -185,11 +185,11 @@ export class RestifyInstrumentation extends InstrumentationBase<any> {
185185
? `request handler - ${route}`
186186
: `middleware - ${fnName || 'anonymous'}`;
187187
const attributes = {
188-
[AttributeNames.AttributeNames.NAME]: fnName,
189-
[AttributeNames.AttributeNames.VERSION]: this._moduleVersion || 'n/a',
190-
[AttributeNames.AttributeNames.TYPE]: metadata.type,
191-
[AttributeNames.AttributeNames.METHOD]: metadata.methodName,
192-
[SemanticAttributes.HTTP_ROUTE]: route,
188+
[AttributeNames.NAME]: fnName,
189+
[AttributeNames.VERSION]: this._moduleVersion || 'n/a',
190+
[AttributeNames.TYPE]: metadata.type,
191+
[AttributeNames.METHOD]: metadata.methodName,
192+
[SEMATTRS_HTTP_ROUTE]: route,
193193
};
194194
const span = this.tracer.startSpan(
195195
spanName,

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import { context, trace, Span } from '@opentelemetry/api';
18-
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
18+
import { SEMATTRS_HTTP_METHOD } from '@opentelemetry/semantic-conventions';
1919
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
2020
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
2121
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
@@ -493,10 +493,7 @@ describe('Restify Instrumentation', () => {
493493
describe('using requestHook in config', () => {
494494
it('calls requestHook provided function when set in config', async () => {
495495
const requestHook = (span: Span, info: RestifyRequestInfo) => {
496-
span.setAttribute(
497-
SemanticAttributes.HTTP_METHOD,
498-
info.request.method
499-
);
496+
span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method);
500497
span.setAttribute('restify.layer', info.layerType);
501498
};
502499

@@ -518,10 +515,7 @@ describe('Restify Instrumentation', () => {
518515
// span from get
519516
const span = memoryExporter.getFinishedSpans()[2];
520517
assert.notStrictEqual(span, undefined);
521-
assert.strictEqual(
522-
span.attributes[SemanticAttributes.HTTP_METHOD],
523-
'GET'
524-
);
518+
assert.strictEqual(span.attributes[SEMATTRS_HTTP_METHOD], 'GET');
525519
assert.strictEqual(
526520
span.attributes['restify.layer'],
527521
'request_handler'
@@ -533,10 +527,7 @@ describe('Restify Instrumentation', () => {
533527

534528
it('does not propagate an error from a requestHook that throws exception', async () => {
535529
const requestHook = (span: Span, info: RestifyRequestInfo) => {
536-
span.setAttribute(
537-
SemanticAttributes.HTTP_METHOD,
538-
info.request.method
539-
);
530+
span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method);
540531

541532
throw Error('error thrown in requestHook');
542533
};
@@ -559,10 +550,7 @@ describe('Restify Instrumentation', () => {
559550
// span from get
560551
const span = memoryExporter.getFinishedSpans()[2];
561552
assert.notStrictEqual(span, undefined);
562-
assert.strictEqual(
563-
span.attributes[SemanticAttributes.HTTP_METHOD],
564-
'GET'
565-
);
553+
assert.strictEqual(span.attributes[SEMATTRS_HTTP_METHOD], 'GET');
566554
}
567555
}
568556
);

0 commit comments

Comments
 (0)