Skip to content

Commit

Permalink
feat(node): Ensure koa spans have better data (#12108)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mydea committed May 21, 2024
1 parent a61eec7 commit 53298b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ test('Sends an API route transaction', async ({ baseURL }) => {
'koa.name': '',
'koa.type': 'middleware',
'otel.kind': 'INTERNAL',
'sentry.origin': 'manual',
'sentry.origin': 'auto.http.otel.koa',
'sentry.op': 'middleware.koa',
},
origin: 'manual',
description: 'middleware - ',
op: 'middleware.koa',
origin: 'auto.http.otel.koa',
description: '< unknown >',
parent_span_id: expect.any(String),
span_id: expect.any(String),
start_timestamp: expect.any(Number),
Expand All @@ -78,16 +80,18 @@ test('Sends an API route transaction', async ({ baseURL }) => {
'koa.name': '/test-transaction',
'koa.type': 'router',
'otel.kind': 'INTERNAL',
'sentry.origin': 'manual',
'sentry.origin': 'auto.http.otel.koa',
'sentry.op': 'router.koa',
},
description: 'router - /test-transaction',
op: 'router.koa',
description: '/test-transaction',
parent_span_id: expect.any(String),
span_id: expect.any(String),
start_timestamp: expect.any(Number),
status: 'ok',
timestamp: expect.any(Number),
trace_id: expect.any(String),
origin: 'manual',
origin: 'auto.http.otel.koa',
},
{
data: {
Expand Down
27 changes: 26 additions & 1 deletion packages/node/src/integrations/tracing/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { isWrapped } from '@opentelemetry/core';
import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
captureException,
defineIntegration,
getDefaultIsolationScope,
Expand All @@ -10,17 +12,40 @@ import {
spanToJSON,
} from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn } from '@sentry/types';
import type { IntegrationFn, Span } from '@sentry/types';
import { consoleSandbox, logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../../debug-build';

function addKoaSpanAttributes(span: Span): void {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.http.otel.koa');

const attributes = spanToJSON(span).data || {};

// this is one of: middleware, router
const type = attributes['koa.type'];

if (type) {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, `${type}.koa`);
}

// Also update the name
const name = attributes['koa.name'];
if (typeof name === 'string') {
// Somehow, name is sometimes `''` for middleware spans
// See: https://github.com/open-telemetry/opentelemetry-js-contrib/issues/2220
span.updateName(name || '< unknown >');
}
}

const _koaIntegration = (() => {
return {
name: 'Koa',
setupOnce() {
addOpenTelemetryInstrumentation(
new KoaInstrumentation({
requestHook(span, info) {
addKoaSpanAttributes(span);

if (getIsolationScope() === getDefaultIsolationScope()) {
DEBUG_BUILD &&
logger.warn('Isolation scope is default isolation scope - skipping setting transactionName');
Expand Down

0 comments on commit 53298b9

Please sign in to comment.