|
| 1 | +package io.sentry.opentelemetry; |
| 2 | + |
| 3 | +import io.opentelemetry.api.trace.Span; |
| 4 | +import io.opentelemetry.api.trace.SpanId; |
| 5 | +import io.opentelemetry.api.trace.TraceId; |
| 6 | +import io.sentry.EventProcessor; |
| 7 | +import io.sentry.Hint; |
| 8 | +import io.sentry.HubAdapter; |
| 9 | +import io.sentry.ISpan; |
| 10 | +import io.sentry.Instrumenter; |
| 11 | +import io.sentry.SentryEvent; |
| 12 | +import io.sentry.SentrySpanStorage; |
| 13 | +import io.sentry.SpanContext; |
| 14 | +import io.sentry.protocol.SentryId; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | +import org.jetbrains.annotations.Nullable; |
| 17 | + |
| 18 | +public final class OpenTelemetryLinkErrorEventProcessor implements EventProcessor { |
| 19 | + |
| 20 | + private final @NotNull SentrySpanStorage spanStorage = SentrySpanStorage.getInstance(); |
| 21 | + |
| 22 | + @Override |
| 23 | + public @Nullable SentryEvent process(final @NotNull SentryEvent event, final @NotNull Hint hint) { |
| 24 | + if (Instrumenter.OTEL.equals(HubAdapter.getInstance().getOptions().getInstrumenter())) { |
| 25 | + @NotNull final Span otelSpan = Span.current(); |
| 26 | + @NotNull final String traceId = otelSpan.getSpanContext().getTraceId(); |
| 27 | + @NotNull final String spanId = otelSpan.getSpanContext().getSpanId(); |
| 28 | + |
| 29 | + if (TraceId.isValid(traceId) && SpanId.isValid(spanId)) { |
| 30 | + final @Nullable ISpan sentrySpan = spanStorage.get(spanId); |
| 31 | + if (sentrySpan != null) { |
| 32 | + final @NotNull SpanContext sentrySpanSpanContext = sentrySpan.getSpanContext(); |
| 33 | + final @NotNull String operation = sentrySpanSpanContext.getOperation(); |
| 34 | + final @Nullable io.sentry.SpanId parentSpanId = sentrySpanSpanContext.getParentSpanId(); |
| 35 | + final @NotNull SpanContext spanContext = |
| 36 | + new SpanContext( |
| 37 | + new SentryId(traceId), |
| 38 | + new io.sentry.SpanId(spanId), |
| 39 | + operation, |
| 40 | + parentSpanId, |
| 41 | + null); |
| 42 | + |
| 43 | + event.getContexts().setTrace(spanContext); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return event; |
| 49 | + } |
| 50 | +} |
0 commit comments