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(node): Ensure koa spans have better data #12108

Merged
merged 1 commit into from
May 21, 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
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
Loading