Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POTEL 36 - POC Capture OpenTelemetry Events #3564

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.data.EventData;
import io.opentelemetry.sdk.trace.internal.data.ExceptionEventData;
import io.sentry.Baggage;
import io.sentry.IScopes;
import io.sentry.PropagationContext;
Expand All @@ -19,7 +21,10 @@
import io.sentry.SentryTraceHeader;
import io.sentry.SpanId;
import io.sentry.TracesSamplingDecision;
import io.sentry.exception.ExceptionMechanismException;
import io.sentry.protocol.Mechanism;
import io.sentry.protocol.SentryId;
import java.util.List;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -135,9 +140,32 @@ public void onEnd(final @NotNull ReadableSpan spanBeingEnded) {
final @NotNull SentryDate finishDate =
new SentryLongDate(spanBeingEnded.toSpanData().getEndEpochNanos());
sentrySpan.updateEndDate(finishDate);

final @NotNull IScopes spanScopes = sentrySpan.getScopes();
if (spanScopes.getOptions().isCaptureOpenTelemetryEvents()) {
final @NotNull List<EventData> events = spanBeingEnded.toSpanData().getEvents();
for (EventData event : events) {
if (event instanceof ExceptionEventData) {
final @NotNull ExceptionEventData exceptionEvent = (ExceptionEventData) event;
final @NotNull Throwable exception = exceptionEvent.getException();
captureException(spanScopes, exception);
}
}
}
}
}

private void captureException(final @NotNull IScopes scopes, final @NotNull Throwable throwable) {
final Mechanism mechanism = new Mechanism();
mechanism.setType("OpenTelemetryInstrumentation");
mechanism.setHandled(true);
// TODO [POTEL] thread might be wrong
final Throwable mechanismException =
new ExceptionMechanismException(mechanism, throwable, Thread.currentThread());
// TODO [POTEL] event timestamp should be taken from ExceptionEventData
scopes.captureException(mechanismException);
}

@Override
public boolean isEndRequired() {
return true;
Expand Down
4 changes: 4 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,13 @@ public final class io/sentry/ExternalOptions {
public fun getTracePropagationTargets ()Ljava/util/List;
public fun getTracesSampleRate ()Ljava/lang/Double;
public fun getTracingOrigins ()Ljava/util/List;
public fun isCaptureOpenTelemetryEvents ()Ljava/lang/Boolean;
public fun isEnableBackpressureHandling ()Ljava/lang/Boolean;
public fun isEnablePrettySerializationOutput ()Ljava/lang/Boolean;
public fun isEnabled ()Ljava/lang/Boolean;
public fun isSendDefaultPii ()Ljava/lang/Boolean;
public fun isSendModules ()Ljava/lang/Boolean;
public fun setCaptureOpenTelemetryEvents (Ljava/lang/Boolean;)V
public fun setCron (Lio/sentry/SentryOptions$Cron;)V
public fun setDebug (Ljava/lang/Boolean;)V
public fun setDist (Ljava/lang/String;)V
Expand Down Expand Up @@ -2757,6 +2759,7 @@ public class io/sentry/SentryOptions {
public fun isAttachServerName ()Z
public fun isAttachStacktrace ()Z
public fun isAttachThreads ()Z
public fun isCaptureOpenTelemetryEvents ()Z
public fun isDebug ()Z
public fun isEnableAppStartProfiling ()Z
public fun isEnableAutoSessionTracking ()Z
Expand Down Expand Up @@ -2794,6 +2797,7 @@ public class io/sentry/SentryOptions {
public fun setBeforeSend (Lio/sentry/SentryOptions$BeforeSendCallback;)V
public fun setBeforeSendTransaction (Lio/sentry/SentryOptions$BeforeSendTransactionCallback;)V
public fun setCacheDirPath (Ljava/lang/String;)V
public fun setCaptureOpenTelemetryEvents (Z)V
public fun setConnectionStatusProvider (Lio/sentry/IConnectionStatusProvider;)V
public fun setConnectionTimeoutMillis (I)V
public fun setCron (Lio/sentry/SentryOptions$Cron;)V
Expand Down
14 changes: 14 additions & 0 deletions sentry/src/main/java/io/sentry/ExternalOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public final class ExternalOptions {
private @Nullable Boolean sendModules;
private @Nullable Boolean sendDefaultPii;
private @Nullable Boolean enableBackpressureHandling;
private @Nullable Boolean captureOpenTelemetryEvents;

private @Nullable SentryOptions.Cron cron;

Expand Down Expand Up @@ -139,6 +140,9 @@ public final class ExternalOptions {
options.setEnableBackpressureHandling(
propertiesProvider.getBooleanProperty("enable-backpressure-handling"));

options.setCaptureOpenTelemetryEvents(
propertiesProvider.getBooleanProperty("capture-opentelemetry-events"));

for (final String ignoredExceptionType :
propertiesProvider.getList("ignored-exceptions-for-type")) {
try {
Expand Down Expand Up @@ -460,4 +464,14 @@ public void setEnableBackpressureHandling(final @Nullable Boolean enableBackpres
public void setCron(final @Nullable SentryOptions.Cron cron) {
this.cron = cron;
}

@ApiStatus.Experimental
public void setCaptureOpenTelemetryEvents(final @Nullable Boolean captureOpenTelemetryEvents) {
this.captureOpenTelemetryEvents = captureOpenTelemetryEvents;
}

@ApiStatus.Experimental
public @Nullable Boolean isCaptureOpenTelemetryEvents() {
return captureOpenTelemetryEvents;
}
}
15 changes: 15 additions & 0 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,8 @@ public class SentryOptions {

private @NotNull ScopeType defaultScopeType = ScopeType.ISOLATION;

@ApiStatus.Experimental private boolean captureOpenTelemetryEvents = false;

/**
* Adds an event processor
*
Expand Down Expand Up @@ -2440,6 +2442,16 @@ public void setDefaultScopeType(final @NotNull ScopeType scopeType) {
return defaultScopeType;
}

@ApiStatus.Experimental
public void setCaptureOpenTelemetryEvents(final boolean captureOpenTelemetryEvents) {
this.captureOpenTelemetryEvents = captureOpenTelemetryEvents;
}

@ApiStatus.Experimental
public boolean isCaptureOpenTelemetryEvents() {
return captureOpenTelemetryEvents;
}

/** The BeforeSend callback */
public interface BeforeSendCallback {

Expand Down Expand Up @@ -2694,6 +2706,9 @@ public void merge(final @NotNull ExternalOptions options) {
if (options.isSendDefaultPii() != null) {
setSendDefaultPii(options.isSendDefaultPii());
}
if (options.isCaptureOpenTelemetryEvents() != null) {
setCaptureOpenTelemetryEvents(options.isCaptureOpenTelemetryEvents());
}

if (options.getCron() != null) {
if (getCron() == null) {
Expand Down
Loading