Skip to content

Commit 92a63ae

Browse files
committed
Set trace for captured error; set timestamp; refactor
1 parent 34ad29e commit 92a63ae

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/OtelSentrySpanProcessor.java

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import io.opentelemetry.sdk.trace.data.EventData;
1212
import io.opentelemetry.sdk.trace.data.ExceptionEventData;
1313
import io.sentry.Baggage;
14+
import io.sentry.DateUtils;
1415
import io.sentry.IScopes;
1516
import io.sentry.PropagationContext;
1617
import io.sentry.ScopesAdapter;
1718
import io.sentry.Sentry;
1819
import io.sentry.SentryDate;
20+
import io.sentry.SentryEvent;
1921
import io.sentry.SentryLevel;
2022
import io.sentry.SentryLongDate;
2123
import io.sentry.SentryTraceHeader;
@@ -149,29 +151,43 @@ public void onEnd(final @NotNull ReadableSpan spanBeingEnded) {
149151
new SentryLongDate(spanBeingEnded.toSpanData().getEndEpochNanos());
150152
sentrySpan.updateEndDate(finishDate);
151153

152-
final @NotNull IScopes spanScopes = sentrySpan.getScopes();
153-
if (spanScopes.getOptions().isCaptureOpenTelemetryEvents()) {
154-
final @NotNull List<EventData> events = spanBeingEnded.toSpanData().getEvents();
155-
for (EventData event : events) {
156-
if (event instanceof ExceptionEventData) {
157-
final @NotNull ExceptionEventData exceptionEvent = (ExceptionEventData) event;
158-
final @NotNull Throwable exception = exceptionEvent.getException();
159-
captureException(spanScopes, exception);
160-
}
154+
maybeCaptureSpanEventsAsExceptions(spanBeingEnded, sentrySpan);
155+
}
156+
}
157+
158+
private void maybeCaptureSpanEventsAsExceptions(
159+
final @NotNull ReadableSpan spanBeingEnded, final @NotNull IOtelSpanWrapper sentrySpan) {
160+
final @NotNull IScopes spanScopes = sentrySpan.getScopes();
161+
if (spanScopes.getOptions().isCaptureOpenTelemetryEvents()) {
162+
final @NotNull List<EventData> events = spanBeingEnded.toSpanData().getEvents();
163+
for (EventData event : events) {
164+
if (event instanceof ExceptionEventData) {
165+
final @NotNull ExceptionEventData exceptionEvent = (ExceptionEventData) event;
166+
captureException(spanScopes, exceptionEvent, sentrySpan);
161167
}
162168
}
163169
}
164170
}
165171

166-
private void captureException(final @NotNull IScopes scopes, final @NotNull Throwable throwable) {
172+
private void captureException(
173+
final @NotNull IScopes scopes,
174+
final @NotNull ExceptionEventData exceptionEvent,
175+
final @NotNull IOtelSpanWrapper sentrySpan) {
176+
final @NotNull Throwable exception = exceptionEvent.getException();
167177
final Mechanism mechanism = new Mechanism();
168-
mechanism.setType("OpenTelemetryInstrumentation");
178+
mechanism.setType("OpenTelemetrySpanEvent");
169179
mechanism.setHandled(true);
170-
// TODO [POTEL] thread might be wrong
180+
// This is potentially the wrong Thread as it's the current thread meaning the thread where
181+
// the span is being ended on. This may not match the thread where the exception occurred.
171182
final Throwable mechanismException =
172-
new ExceptionMechanismException(mechanism, throwable, Thread.currentThread());
173-
// TODO [POTEL] event timestamp should be taken from ExceptionEventData
174-
scopes.captureException(mechanismException);
183+
new ExceptionMechanismException(mechanism, exception, Thread.currentThread());
184+
185+
final SentryEvent event = new SentryEvent(mechanismException);
186+
event.setTimestamp(DateUtils.nanosToDate(exceptionEvent.getEpochNanos()));
187+
event.setLevel(SentryLevel.ERROR);
188+
event.getContexts().setTrace(sentrySpan.getSpanContext());
189+
190+
scopes.captureEvent(event);
175191
}
176192

177193
@Override

0 commit comments

Comments
 (0)