Skip to content

Commit 53298b9

Browse files
authored
feat(node): Ensure koa spans have better data (#12108)
This ensures we have correct op, name & origin for all koa middleware spans. I also updated the E2E test to actually check this as well. I also noticed a problem in the instrumentation where the name is sometimes empty here, opened an upstream issue: open-telemetry/opentelemetry-js-contrib#2220 to look into this.
1 parent a61eec7 commit 53298b9

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

dev-packages/e2e-tests/test-applications/node-koa/tests/transactions.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ test('Sends an API route transaction', async ({ baseURL }) => {
6161
'koa.name': '',
6262
'koa.type': 'middleware',
6363
'otel.kind': 'INTERNAL',
64-
'sentry.origin': 'manual',
64+
'sentry.origin': 'auto.http.otel.koa',
65+
'sentry.op': 'middleware.koa',
6566
},
66-
origin: 'manual',
67-
description: 'middleware - ',
67+
op: 'middleware.koa',
68+
origin: 'auto.http.otel.koa',
69+
description: '< unknown >',
6870
parent_span_id: expect.any(String),
6971
span_id: expect.any(String),
7072
start_timestamp: expect.any(Number),
@@ -78,16 +80,18 @@ test('Sends an API route transaction', async ({ baseURL }) => {
7880
'koa.name': '/test-transaction',
7981
'koa.type': 'router',
8082
'otel.kind': 'INTERNAL',
81-
'sentry.origin': 'manual',
83+
'sentry.origin': 'auto.http.otel.koa',
84+
'sentry.op': 'router.koa',
8285
},
83-
description: 'router - /test-transaction',
86+
op: 'router.koa',
87+
description: '/test-transaction',
8488
parent_span_id: expect.any(String),
8589
span_id: expect.any(String),
8690
start_timestamp: expect.any(Number),
8791
status: 'ok',
8892
timestamp: expect.any(Number),
8993
trace_id: expect.any(String),
90-
origin: 'manual',
94+
origin: 'auto.http.otel.koa',
9195
},
9296
{
9397
data: {

packages/node/src/integrations/tracing/koa.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { isWrapped } from '@opentelemetry/core';
22
import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
33
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
44
import {
5+
SEMANTIC_ATTRIBUTE_SENTRY_OP,
6+
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
57
captureException,
68
defineIntegration,
79
getDefaultIsolationScope,
@@ -10,17 +12,40 @@ import {
1012
spanToJSON,
1113
} from '@sentry/core';
1214
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
13-
import type { IntegrationFn } from '@sentry/types';
15+
import type { IntegrationFn, Span } from '@sentry/types';
1416
import { consoleSandbox, logger } from '@sentry/utils';
1517
import { DEBUG_BUILD } from '../../debug-build';
1618

19+
function addKoaSpanAttributes(span: Span): void {
20+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.http.otel.koa');
21+
22+
const attributes = spanToJSON(span).data || {};
23+
24+
// this is one of: middleware, router
25+
const type = attributes['koa.type'];
26+
27+
if (type) {
28+
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, `${type}.koa`);
29+
}
30+
31+
// Also update the name
32+
const name = attributes['koa.name'];
33+
if (typeof name === 'string') {
34+
// Somehow, name is sometimes `''` for middleware spans
35+
// See: https://github.com/open-telemetry/opentelemetry-js-contrib/issues/2220
36+
span.updateName(name || '< unknown >');
37+
}
38+
}
39+
1740
const _koaIntegration = (() => {
1841
return {
1942
name: 'Koa',
2043
setupOnce() {
2144
addOpenTelemetryInstrumentation(
2245
new KoaInstrumentation({
2346
requestHook(span, info) {
47+
addKoaSpanAttributes(span);
48+
2449
if (getIsolationScope() === getDefaultIsolationScope()) {
2550
DEBUG_BUILD &&
2651
logger.warn('Isolation scope is default isolation scope - skipping setting transactionName');

0 commit comments

Comments
 (0)