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(otel): Convert exception otel events to sentry errors #7165

Merged
merged 1 commit into from
Feb 14, 2023
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
49 changes: 48 additions & 1 deletion packages/opentelemetry-node/src/spanprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Context } from '@opentelemetry/api';
import { trace } from '@opentelemetry/api';
import type { Span as OtelSpan, SpanProcessor as OtelSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
import { Transaction } from '@sentry/tracing';
import type { DynamicSamplingContext, Span as SentrySpan, TraceparentData, TransactionContext } from '@sentry/types';
import { logger } from '@sentry/utils';
import { isString, logger } from '@sentry/utils';

import { SENTRY_DYNAMIC_SAMPLING_CONTEXT_KEY, SENTRY_TRACE_PARENT_CONTEXT_KEY } from './constants';
import { isSentryRequestSpan } from './utils/is-sentry-request';
Expand Down Expand Up @@ -93,6 +94,12 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
* @inheritDoc
*/
public onEnd(otelSpan: OtelSpan): void {
const hub = getCurrentHub();
if (!hub) {
__DEBUG_BUILD__ && logger.error('SentrySpanProcessor has triggered onEnd before a hub has been setup.');
return;
}

const otelSpanId = otelSpan.spanContext().spanId;
const sentrySpan = SENTRY_SPAN_PROCESSOR_MAP.get(otelSpanId);

Expand All @@ -112,6 +119,46 @@ export class SentrySpanProcessor implements OtelSpanProcessor {
return;
}

otelSpan.events.forEach(event => {
if (event.name !== 'exception') {
return;
}

const attributes = event.attributes;
if (!attributes) {
return;
}

const message = attributes[SemanticAttributes.EXCEPTION_MESSAGE];
const syntheticError = new Error(message as string | undefined);

const stack = attributes[SemanticAttributes.EXCEPTION_STACKTRACE];
if (isString(stack)) {
syntheticError.stack = stack;
}

const type = attributes[SemanticAttributes.EXCEPTION_TYPE];
if (isString(type)) {
syntheticError.name = type;
}

hub.captureException(syntheticError, {
captureContext: {
contexts: {
otel: {
attributes: otelSpan.attributes,
resource: otelSpan.resource.attributes,
},
trace: {
trace_id: otelSpan.spanContext().traceId,
span_id: otelSpan.spanContext().spanId,
parent_span_id: otelSpan.parentSpanId,
},
},
},
});
});

if (sentrySpan instanceof Transaction) {
updateTransactionWithOtelData(sentrySpan, otelSpan);
} else {
Expand Down
40 changes: 40 additions & 0 deletions packages/opentelemetry-node/test/spanprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,46 @@ describe('SentrySpanProcessor', () => {
trace_id: otelSpan.spanContext().traceId,
});
});

it('generates Sentry errors from opentelemetry span exception events', () => {
let sentryEvent: any;
let otelSpan: any;

client = new NodeClient({
...DEFAULT_NODE_CLIENT_OPTIONS,
beforeSend: event => {
sentryEvent = event;
return null;
},
});
hub = new Hub(client);
makeMain(hub);

const tracer = provider.getTracer('default');

tracer.startActiveSpan('GET /users', parentOtelSpan => {
tracer.startActiveSpan('SELECT * FROM users;', child => {
child.recordException(new Error('this is an otel error!'));
otelSpan = child as OtelSpan;
child.end();
});

parentOtelSpan.end();
});

expect(sentryEvent).toBeDefined();
expect(sentryEvent.exception).toBeDefined();
expect(sentryEvent.exception.values[0]).toEqual({
mechanism: expect.any(Object),
type: 'Error',
value: 'this is an otel error!',
});
expect(sentryEvent.contexts.trace).toEqual({
parent_span_id: otelSpan.parentSpanId,
span_id: otelSpan.spanContext().spanId,
trace_id: otelSpan.spanContext().traceId,
});
});
});

// OTEL expects a custom date format
Expand Down