getsentry / team-webplatform-meta

0 stars 0 forks source link

Generate errors from OpenTelemetry data #31

Open AbhiPrasad opened 1 year ago

AbhiPrasad commented 1 year ago

In OpenTelemetry, spans can have exception events.

Let's create Sentry errors from these exception events and send them to Sentry! We can link them to the parent transaction via trace context.

Span span = myTracer.startSpan(/*...*/);
try {
  // Code that does the actual work which the Span represents
} catch (Throwable e) {
  span.recordException(e, Attributes.of("exception.escaped", true));
  throw e;
} finally {
  span.end();
}

The advantage of doing this is that we'll get this working out of the box for users who already have errors instrumented with OpenTelemetry.

### OpenTelemetry Exceptions
- [ ] https://github.com/getsentry/develop/pull/837
- [ ] https://github.com/getsentry/sentry-javascript/pull/7165
- [ ] https://github.com/getsentry/develop/pull/838
- [ ] Python
- [ ] Ruby
- [ ] Java
- [ ] Go
- [ ] Update product to indicate to users that the error event comes from an otel SDK
AbhiPrasad commented 1 year ago

Here's a basic JavaScript example to make this work.

otelSpan.events.forEach(event => {
  if (event.name === 'exception') {
    const attributes = event.attributes;
    if (attributes) {
      const message = attributes[SemanticAttributes.EXCEPTION_MESSAGE] as string;

      const syntheticError = new Error(message);
      syntheticError.stack = attributes[SemanticAttributes.EXCEPTION_STACKTRACE] as string;
      syntheticError.name = attributes[SemanticAttributes.EXCEPTION_TYPE] as string;

      hub.captureException(syntheticError, {
        captureContext: {
          contexts: {
            trace: {
              trace_id: otelSpan.spanContext().traceId,
              span_id: otelSpan.spanContext().spanId,
              parent_span_id: otelSpan.parentSpanId,
            },
          },
        },
      });
    }
  }
});
smeubank commented 1 year ago

@AbhiPrasad anyway this could be done server side?

AbhiPrasad commented 1 year ago

We don't attach this information in any way on spans (the sentry schema for spans has no concept for events), so currently no.