Skip to content

Commit

Permalink
chore(instr-express): use exported strings for attributes (#2031)
Browse files Browse the repository at this point in the history
* chore(instr-express): use exported strings for attributes

* use exported strings in tests
  • Loading branch information
JamieDanielson authored Mar 22, 2024
1 parent 9df30ea commit 6cd67c0
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 17 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.

8 changes: 4 additions & 4 deletions plugins/node/opentelemetry-instrumentation-express/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ const expressInstrumentation = new ExpressInstrumentation({

## Semantic Conventions

This package uses `@opentelemetry/semantic-conventions` version `1.0+`, which implements Semantic Convention [Version 1.7.0](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.7.0/semantic_conventions/README.md)
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: `HTTP_ROUTE` |
| Attribute | Short Description | Notes |
| ------------ | ---------------------------------- | -------------------------- |
| `http.route` | The matched route (path template). | Key: `SEMATTRS_HTTP_ROUTE` |

## Useful links

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"dependencies": {
"@opentelemetry/core": "^1.8.0",
"@opentelemetry/instrumentation": "^0.49.1",
"@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-express#readme"
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
isWrapped,
safeExecuteInTheMiddle,
} from '@opentelemetry/instrumentation';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
import {
ExpressLayer,
ExpressRouter,
Expand Down Expand Up @@ -202,7 +202,7 @@ export class ExpressInstrumentation extends InstrumentationBase<
.replace(/\/{2,}/g, '/');

const attributes: Attributes = {
[SemanticAttributes.HTTP_ROUTE]: route.length > 0 ? route : '/',
[SEMATTRS_HTTP_ROUTE]: route.length > 0 ? route : '/',
};
const metadata = getLayerMetadata(layer, layerPath);
const type = metadata.attributes[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
import * as assert from 'assert';
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
import { ExpressLayerType } from '../src/enums/ExpressLayerType';
Expand Down Expand Up @@ -145,7 +145,7 @@ describe('ExpressInstrumentation', () => {
.find(span => span.name.includes('request handler'));
assert.notStrictEqual(requestHandlerSpan, undefined);
assert.strictEqual(
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
'/mw'
);

Expand Down Expand Up @@ -196,7 +196,7 @@ describe('ExpressInstrumentation', () => {
.find(span => span.name.includes('request handler'));
assert.notStrictEqual(requestHandlerSpan, undefined);
assert.strictEqual(
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
'/'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import * as assert from 'assert';
import { AttributeNames } from '../src/enums/AttributeNames';
import { ExpressInstrumentation } from '../src';
import { createServer, httpRequest, serverWithMiddleware } from './utils';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
import * as testUtils from '@opentelemetry/contrib-test-utils';

const instrumentation = new ExpressInstrumentation();
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('ExpressInstrumentation', () => {
.find(span => span.name.includes('request handler'));
assert.notStrictEqual(requestHandlerSpan, undefined);
assert.strictEqual(
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
'/toto/:id'
);
assert.strictEqual(
Expand Down Expand Up @@ -481,7 +481,7 @@ describe('ExpressInstrumentation', () => {
.getFinishedSpans()
.find(span => span.name.includes('request handler'));
assert.strictEqual(
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
'/double-slashes/:id'
);
assert.strictEqual(rpcMetadata?.route, '/double-slashes/:id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import * as sinon from 'sinon';
import { ExpressInstrumentation } from '../src';
import { ExpressRequestInfo, SpanNameHook } from '../src/types';
import { ExpressLayerType } from '../src/enums/ExpressLayerType';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_METHOD } from '@opentelemetry/semantic-conventions';

const instrumentation = new ExpressInstrumentation();
instrumentation.enable();
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('ExpressInstrumentation hooks', () => {

it('should call requestHook when set in config', async () => {
const requestHook = sinon.spy((span: Span, info: ExpressRequestInfo) => {
span.setAttribute(SemanticAttributes.HTTP_METHOD, info.request.method);
span.setAttribute(SEMATTRS_HTTP_METHOD, info.request.method);

if (info.layerType) {
span.setAttribute('express.layer_type', info.layerType);
Expand Down

0 comments on commit 6cd67c0

Please sign in to comment.