Skip to content

Commit 6521389

Browse files
AbhiPrasadstephanie-anderson
authored andcommitted
feat(otel): Add spec for converting otel exceptions to sentry errors (#837)
1 parent f79ea33 commit 6521389

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

develop-docs/sdk/performance/opentelemetry.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ class SentrySpanProcessor implements SpanProcessor {
113113
return;
114114
}
115115

116+
generateSentryErrorsFromOtelSpan(otelSpan);
117+
116118
if (sentrySpan instanceof Transaction) {
117119
updateTransactionWithOtelData(sentrySpan, otelSpan);
118120
finishTransactionWithContextFromOtelData(sentrySpan, otelSpan);
@@ -361,6 +363,38 @@ if (instrumenter === 'otel') {
361363

362364
You are also free to do both, or an alternative method, the only requirement is that we don't double instrument.
363365

366+
### Step 7: Define `generateSentryErrorsFromOtelSpan`
367+
368+
In OpenTelemetry, spans can have [exception events](https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/exceptions/). These have a stacktrace, message, and type. We want to convert these to Sentry errors and attach them to the trace.
369+
370+
```ts
371+
function generateSentryErrorsFromOtelSpan(otelSpan) {
372+
otelSpan.events.forEach(event => {
373+
// Only convert exception events to Sentry errors.
374+
if (event.name !=== 'exception') {
375+
return;
376+
}
377+
378+
const attributes = event.attributes;
379+
380+
const message = attributes[SemanticAttributes.EXCEPTION_MESSAGE];
381+
const syntheticError = new Error(message);
382+
syntheticError.stack = attributes[SemanticAttributes.EXCEPTION_STACKTRACE];
383+
syntheticError.name = attributes[SemanticAttributes.EXCEPTION_TYPE];
384+
385+
Sentry.captureException(syntheticError, {
386+
contexts: {
387+
trace: {
388+
trace_id: otelSpan.spanContext().traceId,
389+
span_id: otelSpan.spanContext().spanId,
390+
parent_span_id: otelSpan.parentSpanId,
391+
},
392+
},
393+
});
394+
});
395+
}
396+
```
397+
364398
## Span Protocol
365399

366400
Below describe the transformations between an OpenTelemetry span and a Sentry Span. Related: [the interface for a Sentry Span](https://develop.sentry.dev/sdk/event-payloads/span/), [the Relay spec for a Sentry Span](https://github.com/getsentry/relay/blob/master/relay-general/src/protocol/span.rs) and the spec for an [OpenTelemetry span](https://github.com/open-telemetry/opentelemetry-proto/blob/724e427879e3d2bae2edc0218fff06e37b9eb46e/opentelemetry/proto/trace/v1/trace.proto#L80-L256).

0 commit comments

Comments
 (0)