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 Hapi spans have better data #12140

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 @@ -56,6 +56,30 @@ test('Sends successful transaction', async ({ baseURL }) => {
},
}),
);

const spans = transactionEvent.spans || [];

expect(spans).toEqual([
{
data: {
'hapi.type': 'router',
'http.method': 'GET',
'http.route': '/test-success',
'otel.kind': 'INTERNAL',
'sentry.op': 'router.hapi',
'sentry.origin': 'auto.http.otel.hapi',
},
description: 'GET /test-success',
op: 'router.hapi',
origin: 'auto.http.otel.hapi',
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),
},
]);
});

test('Sends parameterized transactions to Sentry', async ({ baseURL }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ describe('hapi auto-instrumentation', () => {
'http.route': '/',
'http.method': 'GET',
'hapi.type': 'router',
'sentry.origin': 'manual',
'sentry.op': 'http',
'sentry.origin': 'auto.http.otel.hapi',
'sentry.op': 'router.hapi',
}),
description: 'GET /',
op: 'http',
op: 'router.hapi',
origin: 'auto.http.otel.hapi',
status: 'ok',
}),
]),
Expand Down
33 changes: 32 additions & 1 deletion packages/node/src/integrations/tracing/hapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import { isWrapped } from '@opentelemetry/core';
import { HapiInstrumentation } from '@opentelemetry/instrumentation-hapi';
import {
SDK_VERSION,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SPAN_STATUS_ERROR,
captureException,
defineIntegration,
getActiveSpan,
getClient,
getDefaultIsolationScope,
getIsolationScope,
getRootSpan,
isEnabled,
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';
import type { Boom, RequestEvent, ResponseObject, Server } from './types';
Expand Down Expand Up @@ -95,6 +99,16 @@ export const hapiErrorPlugin = {
export async function setupHapiErrorHandler(server: Server): Promise<void> {
await server.register(hapiErrorPlugin);

// Sadly, middleware spans do not go through `requestHook`, so we handle those here
// We register this hook in this method, because if we register it in the integration `setup`,
// it would always run even for users that are not even using hapi
const client = getClient();
if (client) {
client.on('spanStart', span => {
addHapiSpanAttributes(span);
});
}

// eslint-disable-next-line @typescript-eslint/unbound-method
if (!isWrapped(server.register) && isEnabled()) {
consoleSandbox(() => {
Expand All @@ -105,3 +119,20 @@ export async function setupHapiErrorHandler(server: Server): Promise<void> {
});
}
}

function addHapiSpanAttributes(span: Span): void {
const attributes = spanToJSON(span).data || {};

// this is one of: router, plugin, server.ext
const type = attributes['hapi.type'];

// If this is already set, or we have no Hapi span, no need to process again...
if (attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] || !type) {
return;
}

span.setAttributes({
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.otel.hapi',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: `${type}.hapi`,
});
}
Loading