|
11 | 11 | import io.opentelemetry.sdk.trace.data.EventData; |
12 | 12 | import io.opentelemetry.sdk.trace.data.ExceptionEventData; |
13 | 13 | import io.sentry.Baggage; |
| 14 | +import io.sentry.DateUtils; |
14 | 15 | import io.sentry.IScopes; |
15 | 16 | import io.sentry.PropagationContext; |
16 | 17 | import io.sentry.ScopesAdapter; |
17 | 18 | import io.sentry.Sentry; |
18 | 19 | import io.sentry.SentryDate; |
| 20 | +import io.sentry.SentryEvent; |
19 | 21 | import io.sentry.SentryLevel; |
20 | 22 | import io.sentry.SentryLongDate; |
21 | 23 | import io.sentry.SentryTraceHeader; |
@@ -149,29 +151,43 @@ public void onEnd(final @NotNull ReadableSpan spanBeingEnded) { |
149 | 151 | new SentryLongDate(spanBeingEnded.toSpanData().getEndEpochNanos()); |
150 | 152 | sentrySpan.updateEndDate(finishDate); |
151 | 153 |
|
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); |
161 | 167 | } |
162 | 168 | } |
163 | 169 | } |
164 | 170 | } |
165 | 171 |
|
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(); |
167 | 177 | final Mechanism mechanism = new Mechanism(); |
168 | | - mechanism.setType("OpenTelemetryInstrumentation"); |
| 178 | + mechanism.setType("OpenTelemetrySpanEvent"); |
169 | 179 | 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. |
171 | 182 | 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); |
175 | 191 | } |
176 | 192 |
|
177 | 193 | @Override |
|
0 commit comments