Skip to content

Commit

Permalink
feat(koa): Use exported strings for attributes (#2033)
Browse files Browse the repository at this point in the history
* feat(koa): Use exported strings for attributes

* feat(koa): Updated package-lock.json #2025

---------

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
MrFabio and pichlermarc committed Apr 2, 2024
1 parent 995b364 commit 70090c1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 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.

Expand Up @@ -67,7 +67,7 @@
"dependencies": {
"@opentelemetry/core": "^1.8.0",
"@opentelemetry/instrumentation": "^0.49.1",
"@opentelemetry/semantic-conventions": "^1.0.0",
"@opentelemetry/semantic-conventions": "^1.22.0",
"@types/koa": "2.14.0",
"@types/koa__router": "12.0.3"
},
Expand Down
4 changes: 2 additions & 2 deletions plugins/node/opentelemetry-instrumentation-koa/src/utils.ts
Expand Up @@ -17,7 +17,7 @@ import { KoaContext, KoaLayerType, KoaInstrumentationConfig } from './types';
import { KoaMiddleware } from './internal-types';
import { AttributeNames } from './enums/AttributeNames';
import { Attributes } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';

export const getMiddlewareMetadata = (
context: KoaContext,
Expand All @@ -33,7 +33,7 @@ export const getMiddlewareMetadata = (
attributes: {
[AttributeNames.KOA_NAME]: layerPath?.toString(),
[AttributeNames.KOA_TYPE]: KoaLayerType.ROUTER,
[SemanticAttributes.HTTP_ROUTE]: layerPath?.toString(),
[SEMATTRS_HTTP_ROUTE]: layerPath?.toString(),
},
name: context._matchedRouteName || `router - ${layerPath}`,
};
Expand Down
23 changes: 12 additions & 11 deletions plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts
Expand Up @@ -23,7 +23,11 @@ import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
SEMATTRS_EXCEPTION_MESSAGE,
SEMATTRS_HTTP_METHOD,
SEMATTRS_HTTP_ROUTE,
} from '@opentelemetry/semantic-conventions';

import { KoaInstrumentation } from '../src';
const plugin = new KoaInstrumentation();
Expand Down Expand Up @@ -175,7 +179,7 @@ describe('Koa Instrumentation', () => {
);

assert.strictEqual(
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
'/post/:id'
);

Expand Down Expand Up @@ -226,7 +230,7 @@ describe('Koa Instrumentation', () => {
);

assert.strictEqual(
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
'/^\\/post/'
);

Expand Down Expand Up @@ -273,7 +277,7 @@ describe('Koa Instrumentation', () => {
);

assert.strictEqual(
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
'/post/:id'
);

Expand Down Expand Up @@ -322,7 +326,7 @@ describe('Koa Instrumentation', () => {
);

assert.strictEqual(
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
'/:first/post/:id'
);

Expand Down Expand Up @@ -369,7 +373,7 @@ describe('Koa Instrumentation', () => {
);

assert.strictEqual(
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
requestHandlerSpan?.attributes[SEMATTRS_HTTP_ROUTE],
'/:first/post/:id'
);

Expand Down Expand Up @@ -570,7 +574,7 @@ describe('Koa Instrumentation', () => {
assert.ok(exceptionEvent, 'There should be an exception event recorded');
assert.deepStrictEqual(exceptionEvent.name, 'exception');
assert.deepStrictEqual(
exceptionEvent.attributes![SemanticAttributes.EXCEPTION_MESSAGE],
exceptionEvent.attributes![SEMATTRS_EXCEPTION_MESSAGE],
'I failed!'
);
});
Expand All @@ -591,10 +595,7 @@ describe('Koa Instrumentation', () => {
);

const requestHook = sinon.spy((span: Span, info: KoaRequestInfo) => {
span.setAttribute(
SemanticAttributes.HTTP_METHOD,
info.context.request.method
);
span.setAttribute(SEMATTRS_HTTP_METHOD, info.context.request.method);

throw Error('error thrown in requestHook');
});
Expand Down

0 comments on commit 70090c1

Please sign in to comment.