Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(koa): Use exported strings for attributes #2033

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading