Skip to content

Commit

Permalink
refactor(instr-restify): use exported strings for attributes (#2098)
Browse files Browse the repository at this point in the history
* refactor(instr-restify): use exported strings for attributes

* chore(instr-restify): fix lint issues

---------

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
david-luna and pichlermarc authored Apr 17, 2024
1 parent 5f42c51 commit 9f5a867
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 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-restify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ const restifyInstrumentation = new RestifyInstrumentation({
});
```

## 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 |
| ------------ | ---------------------------------- |
| `http.route` | The matched route (path template). |

## 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 @@ -63,7 +63,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-restify#readme"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import type * as restify from 'restify';
import * as api from '@opentelemetry/api';
import type { Server } from 'restify';
import { LayerType } from './types';
import * as AttributeNames from './enums/AttributeNames';
import { AttributeNames } from './enums/AttributeNames';
import { VERSION } from './version';
import * as constants from './constants';
import {
Expand All @@ -30,7 +30,7 @@ import {
isWrapped,
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
import { isPromise, isAsyncFunction } from './utils';
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
import type { RestifyInstrumentationConfig } from './types';
Expand Down Expand Up @@ -185,11 +185,11 @@ export class RestifyInstrumentation extends InstrumentationBase<any> {
? `request handler - ${route}`
: `middleware - ${fnName || 'anonymous'}`;
const attributes = {
[AttributeNames.AttributeNames.NAME]: fnName,
[AttributeNames.AttributeNames.VERSION]: this._moduleVersion || 'n/a',
[AttributeNames.AttributeNames.TYPE]: metadata.type,
[AttributeNames.AttributeNames.METHOD]: metadata.methodName,
[SemanticAttributes.HTTP_ROUTE]: route,
[AttributeNames.NAME]: fnName,
[AttributeNames.VERSION]: this._moduleVersion || 'n/a',
[AttributeNames.TYPE]: metadata.type,
[AttributeNames.METHOD]: metadata.methodName,
[SEMATTRS_HTTP_ROUTE]: route,
};
const span = this.tracer.startSpan(
spanName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { context, trace, Span } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_METHOD } from '@opentelemetry/semantic-conventions';
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
Expand Down Expand Up @@ -493,10 +493,7 @@ describe('Restify Instrumentation', () => {
describe('using requestHook in config', () => {
it('calls requestHook provided function when set in config', async () => {
const requestHook = (span: Span, info: RestifyRequestInfo) => {
span.setAttribute(
SemanticAttributes.HTTP_METHOD,
info.request.method
);
span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method);
span.setAttribute('restify.layer', info.layerType);
};

Expand All @@ -518,10 +515,7 @@ describe('Restify Instrumentation', () => {
// span from get
const span = memoryExporter.getFinishedSpans()[2];
assert.notStrictEqual(span, undefined);
assert.strictEqual(
span.attributes[SemanticAttributes.HTTP_METHOD],
'GET'
);
assert.strictEqual(span.attributes[SEMATTRS_HTTP_METHOD], 'GET');
assert.strictEqual(
span.attributes['restify.layer'],
'request_handler'
Expand All @@ -533,10 +527,7 @@ describe('Restify Instrumentation', () => {

it('does not propagate an error from a requestHook that throws exception', async () => {
const requestHook = (span: Span, info: RestifyRequestInfo) => {
span.setAttribute(
SemanticAttributes.HTTP_METHOD,
info.request.method
);
span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method);

throw Error('error thrown in requestHook');
};
Expand All @@ -559,10 +550,7 @@ describe('Restify Instrumentation', () => {
// span from get
const span = memoryExporter.getFinishedSpans()[2];
assert.notStrictEqual(span, undefined);
assert.strictEqual(
span.attributes[SemanticAttributes.HTTP_METHOD],
'GET'
);
assert.strictEqual(span.attributes[SEMATTRS_HTTP_METHOD], 'GET');
}
}
);
Expand Down

0 comments on commit 9f5a867

Please sign in to comment.