Skip to content

Commit 70090c1

Browse files
MrFabiopichlermarc
andauthored
feat(koa): Use exported strings for attributes (#2033)
* feat(koa): Use exported strings for attributes * feat(koa): Updated package-lock.json #2025 --------- Co-authored-by: Marc Pichler <[email protected]>
1 parent 995b364 commit 70090c1

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
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-koa/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"dependencies": {
6868
"@opentelemetry/core": "^1.8.0",
6969
"@opentelemetry/instrumentation": "^0.49.1",
70-
"@opentelemetry/semantic-conventions": "^1.0.0",
70+
"@opentelemetry/semantic-conventions": "^1.22.0",
7171
"@types/koa": "2.14.0",
7272
"@types/koa__router": "12.0.3"
7373
},

plugins/node/opentelemetry-instrumentation-koa/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { KoaContext, KoaLayerType, KoaInstrumentationConfig } from './types';
1717
import { KoaMiddleware } from './internal-types';
1818
import { AttributeNames } from './enums/AttributeNames';
1919
import { Attributes } from '@opentelemetry/api';
20-
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
20+
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
2121

2222
export const getMiddlewareMetadata = (
2323
context: KoaContext,
@@ -33,7 +33,7 @@ export const getMiddlewareMetadata = (
3333
attributes: {
3434
[AttributeNames.KOA_NAME]: layerPath?.toString(),
3535
[AttributeNames.KOA_TYPE]: KoaLayerType.ROUTER,
36-
[SemanticAttributes.HTTP_ROUTE]: layerPath?.toString(),
36+
[SEMATTRS_HTTP_ROUTE]: layerPath?.toString(),
3737
},
3838
name: context._matchedRouteName || `router - ${layerPath}`,
3939
};

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import {
2323
InMemorySpanExporter,
2424
SimpleSpanProcessor,
2525
} from '@opentelemetry/sdk-trace-base';
26-
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
26+
import {
27+
SEMATTRS_EXCEPTION_MESSAGE,
28+
SEMATTRS_HTTP_METHOD,
29+
SEMATTRS_HTTP_ROUTE,
30+
} from '@opentelemetry/semantic-conventions';
2731

2832
import { KoaInstrumentation } from '../src';
2933
const plugin = new KoaInstrumentation();
@@ -175,7 +179,7 @@ describe('Koa Instrumentation', () => {
175179
);
176180

177181
assert.strictEqual(
178-
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
182+
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
179183
'/post/:id'
180184
);
181185

@@ -226,7 +230,7 @@ describe('Koa Instrumentation', () => {
226230
);
227231

228232
assert.strictEqual(
229-
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
233+
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
230234
'/^\\/post/'
231235
);
232236

@@ -273,7 +277,7 @@ describe('Koa Instrumentation', () => {
273277
);
274278

275279
assert.strictEqual(
276-
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
280+
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
277281
'/post/:id'
278282
);
279283

@@ -322,7 +326,7 @@ describe('Koa Instrumentation', () => {
322326
);
323327

324328
assert.strictEqual(
325-
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
329+
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
326330
'/:first/post/:id'
327331
);
328332

@@ -369,7 +373,7 @@ describe('Koa Instrumentation', () => {
369373
);
370374

371375
assert.strictEqual(
372-
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
376+
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
373377
'/:first/post/:id'
374378
);
375379

@@ -570,7 +574,7 @@ describe('Koa Instrumentation', () => {
570574
assert.ok(exceptionEvent, 'There should be an exception event recorded');
571575
assert.deepStrictEqual(exceptionEvent.name, 'exception');
572576
assert.deepStrictEqual(
573-
exceptionEvent.attributes![SemanticAttributes.EXCEPTION_MESSAGE],
577+
exceptionEvent.attributes![SEMATTRS_EXCEPTION_MESSAGE],
574578
'I failed!'
575579
);
576580
});
@@ -591,10 +595,7 @@ describe('Koa Instrumentation', () => {
591595
);
592596

593597
const requestHook = sinon.spy((span: Span, info: KoaRequestInfo) => {
594-
span.setAttribute(
595-
SemanticAttributes.HTTP_METHOD,
596-
info.context.request.method
597-
);
598+
span.setAttribute(SEMATTRS_HTTP_METHOD, info.context.request.method);
598599

599600
throw Error('error thrown in requestHook');
600601
});

0 commit comments

Comments
 (0)