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): Update scope transactionName when creating router span #11476

Merged
merged 2 commits into from
Apr 10, 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 @@ -75,6 +75,10 @@ router1.get('/test-exception', async ctx => {
throw new Error('This is an exception');
});

router1.get('/test-exception/:id', async ctx => {
throw new Error(`This is an exception with id ${ctx.params.id}`);
});

router1.get('/test-outgoing-fetch-external-allowed', async ctx => {
const fetchResponse = await fetch(`http://localhost:${port2}/external-allowed`);
const data = await fetchResponse.json();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,28 @@ test('Sends exception to Sentry', async ({ baseURL }) => {

test('Sends correct error event', async ({ baseURL }) => {
const errorEventPromise = waitForError('node-koa-app', event => {
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception';
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception with id 123';
});

try {
await axios.get(`${baseURL}/test-exception`);
await axios.get(`${baseURL}/test-exception/123`);
} catch {
// this results in an error, but we don't care - we want to check the error event
}

const errorEvent = await errorEventPromise;

expect(errorEvent.exception?.values).toHaveLength(1);
expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception');
expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception with id 123');

expect(errorEvent.request).toEqual({
method: 'GET',
cookies: {},
headers: expect.any(Object),
url: 'http://localhost:3030/test-exception',
url: 'http://localhost:3030/test-exception/123',
});

expect(errorEvent.transaction).toEqual('GET /test-exception');
expect(errorEvent.transaction).toEqual('GET /test-exception/:id');

expect(errorEvent.contexts?.trace).toEqual({
trace_id: expect.any(String),
Expand Down
29 changes: 27 additions & 2 deletions packages/node/src/integrations/tracing/koa.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
import { captureException, defineIntegration } from '@sentry/core';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import {
captureException,
defineIntegration,
getDefaultIsolationScope,
getIsolationScope,
spanToJSON,
} from '@sentry/core';
import type { IntegrationFn } from '@sentry/types';
import { logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../../debug-build';

const _koaIntegration = (() => {
return {
name: 'Koa',
setupOnce() {
registerInstrumentations({
instrumentations: [new KoaInstrumentation()],
instrumentations: [
new KoaInstrumentation({
requestHook(span, info) {
if (getIsolationScope() === getDefaultIsolationScope()) {
DEBUG_BUILD &&
logger.warn('Isolation scope is default isolation scope - skipping setting transactionName');
return;
}
const attributes = spanToJSON(span).data;
const route = attributes && attributes[SemanticAttributes.HTTP_ROUTE];
const method = info.context.request.method.toUpperCase() || 'GET';
if (route) {
getIsolationScope().setTransactionName(`${method} ${route}`);
}
},
}),
],
});
},
};
Expand Down
Loading