Skip to content

Commit

Permalink
Forward Sentry API to Sentry through OTel
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer committed May 22, 2024
1 parent b3919bd commit 36ed84a
Show file tree
Hide file tree
Showing 23 changed files with 364 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public final class io/sentry/opentelemetry/OtelContextScopesStorage : io/sentry/

public final class io/sentry/opentelemetry/OtelSpanFactory : io/sentry/ISpanFactory {
public fun <init> ()V
public fun createSpan (Ljava/lang/String;Lio/sentry/IScopes;Lio/sentry/SpanOptions;Lio/sentry/ISpan;)Lio/sentry/ISpan;
public fun createSpan (Ljava/lang/String;Ljava/lang/String;Lio/sentry/IScopes;Lio/sentry/SpanOptions;Lio/sentry/ISpan;)Lio/sentry/ISpan;
public fun createTransaction (Lio/sentry/TransactionContext;Lio/sentry/IScopes;Lio/sentry/TransactionOptions;Lio/sentry/TransactionPerformanceCollector;)Lio/sentry/ITransaction;
public fun retrieveCurrentSpan (Lio/sentry/IScopes;)Lio/sentry/ISpan;
}

public final class io/sentry/opentelemetry/OtelSpanWrapper : io/sentry/ISpan {
public fun <init> (Lio/opentelemetry/api/trace/Span;Lio/sentry/IScopes;)V
public fun <init> (Lio/opentelemetry/sdk/trace/ReadWriteSpan;Lio/sentry/IScopes;Lio/sentry/SentryDate;Lio/opentelemetry/api/trace/Span;)V
public fun finish ()V
public fun finish (Lio/sentry/SpanStatus;)V
public fun finish (Lio/sentry/SpanStatus;Lio/sentry/SentryDate;)V
Expand All @@ -36,17 +36,18 @@ public final class io/sentry/opentelemetry/OtelSpanWrapper : io/sentry/ISpan {
public fun getFinishDate ()Lio/sentry/SentryDate;
public fun getLocalMetricsAggregator ()Lio/sentry/metrics/LocalMetricsAggregator;
public fun getMeasurements ()Ljava/util/Map;
public fun getName ()Ljava/lang/String;
public fun getNameSource ()Lio/sentry/protocol/TransactionNameSource;
public fun getOperation ()Ljava/lang/String;
public fun getSamplingDecision ()Lio/sentry/TracesSamplingDecision;
public fun getScopes ()Lio/sentry/IScopes;
public fun getSpanContext ()Lio/sentry/SpanContext;
public fun getStartDate ()Lio/sentry/SentryDate;
public fun getStatus ()Lio/sentry/SpanStatus;
public fun getTag (Ljava/lang/String;)Ljava/lang/String;
public fun getTags ()Ljava/util/Map;
public fun getThrowable ()Ljava/lang/Throwable;
public fun getTraceId ()Lio/sentry/protocol/SentryId;
public fun getTransactionName ()Ljava/lang/String;
public fun getTransactionNameSource ()Lio/sentry/protocol/TransactionNameSource;
public fun isFinished ()Z
public fun isNoOp ()Z
public fun isProfileSampled ()Ljava/lang/Boolean;
Expand All @@ -57,12 +58,12 @@ public final class io/sentry/opentelemetry/OtelSpanWrapper : io/sentry/ISpan {
public fun setDescription (Ljava/lang/String;)V
public fun setMeasurement (Ljava/lang/String;Ljava/lang/Number;)V
public fun setMeasurement (Ljava/lang/String;Ljava/lang/Number;Lio/sentry/MeasurementUnit;)V
public fun setName (Ljava/lang/String;)V
public fun setName (Ljava/lang/String;Lio/sentry/protocol/TransactionNameSource;)V
public fun setOperation (Ljava/lang/String;)V
public fun setStatus (Lio/sentry/SpanStatus;)V
public fun setTag (Ljava/lang/String;Ljava/lang/String;)V
public fun setThrowable (Ljava/lang/Throwable;)V
public fun setTransactionName (Ljava/lang/String;)V
public fun setTransactionName (Ljava/lang/String;Lio/sentry/protocol/TransactionNameSource;)V
public fun startChild (Ljava/lang/String;)Lio/sentry/ISpan;
public fun startChild (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ISpan;
public fun startChild (Ljava/lang/String;Ljava/lang/String;Lio/sentry/SentryDate;Lio/sentry/Instrumenter;)Lio/sentry/ISpan;
Expand All @@ -75,7 +76,7 @@ public final class io/sentry/opentelemetry/OtelSpanWrapper : io/sentry/ISpan {
}

public final class io/sentry/opentelemetry/OtelTransactionSpanForwarder : io/sentry/ITransaction {
public fun <init> (Lio/sentry/ISpan;)V
public fun <init> (Lio/sentry/opentelemetry/OtelSpanWrapper;)V
public fun finish ()V
public fun finish (Lio/sentry/SpanStatus;)V
public fun finish (Lio/sentry/SpanStatus;Lio/sentry/SentryDate;)V
Expand All @@ -89,7 +90,6 @@ public final class io/sentry/opentelemetry/OtelTransactionSpanForwarder : io/sen
public fun getLatestActiveSpan ()Lio/sentry/ISpan;
public fun getLocalMetricsAggregator ()Lio/sentry/metrics/LocalMetricsAggregator;
public fun getName ()Ljava/lang/String;
public fun getNameSource ()Lio/sentry/protocol/TransactionNameSource;
public fun getOperation ()Ljava/lang/String;
public fun getSamplingDecision ()Lio/sentry/TracesSamplingDecision;
public fun getSpanContext ()Lio/sentry/SpanContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.sentry.IScopes;
import io.sentry.ISpan;
import io.sentry.ISpanFactory;
import io.sentry.ITransaction;
import io.sentry.NoOpSpan;
import io.sentry.NoOpTransaction;
import io.sentry.SentryDate;
import io.sentry.SpanOptions;
import io.sentry.TransactionContext;
import io.sentry.TransactionOptions;
import io.sentry.TransactionPerformanceCollector;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -27,13 +32,33 @@ public final class OtelSpanFactory implements ISpanFactory {
@NotNull IScopes scopes,
@NotNull TransactionOptions transactionOptions,
@Nullable TransactionPerformanceCollector transactionPerformanceCollector) {
final @NotNull ISpan span = createSpan(context.getName(), scopes, transactionOptions, null);
final @Nullable OtelSpanWrapper span =
createSpanInternal(
context.getName(), context.getDescription(), scopes, transactionOptions, null);
if (span == null) {
return NoOpTransaction.getInstance();
}
return new OtelTransactionSpanForwarder(span);
}

@Override
public @NotNull ISpan createSpan(
final @NotNull String name,
final @Nullable String description,
final @NotNull IScopes scopes,
final @NotNull SpanOptions spanOptions,
final @Nullable ISpan parentSpan) {
final @Nullable OtelSpanWrapper span =
createSpanInternal(name, description, scopes, spanOptions, parentSpan);
if (span == null) {
return NoOpSpan.getInstance();
}
return span;
}

private @Nullable OtelSpanWrapper createSpanInternal(
final @NotNull String name,
final @Nullable String description,
final @NotNull IScopes scopes,
final @NotNull SpanOptions spanOptions,
final @Nullable ISpan parentSpan) {
Expand All @@ -46,15 +71,28 @@ public final class OtelSpanFactory implements ISpanFactory {
// spanBuilder.setParent()
}
}
// TODO [POTEL] start timestamp
final @NotNull Span span = spanBuilder.startSpan();
return new OtelSpanWrapper(span, scopes);

final @Nullable SentryDate startTimestampFromOptions = spanOptions.getStartTimestamp();
final @NotNull SentryDate startTimestamp =
startTimestampFromOptions == null
? scopes.getOptions().getDateProvider().now()
: startTimestampFromOptions;
spanBuilder.setStartTimestamp(startTimestamp.nanoTimestamp(), TimeUnit.NANOSECONDS);

final @NotNull Span otelSpan = spanBuilder.startSpan();
final @Nullable OtelSpanWrapper sentrySpan = storage.getSentrySpan(otelSpan.getSpanContext());
if (sentrySpan != null && description != null) {
sentrySpan.setDescription(description);
}
return sentrySpan;
}

@Override
public @Nullable ISpan retrieveCurrentSpan(IScopes scopes) {
// TODO [POTEL] should we use Context.fromContextOrNull and read span from there?
final @NotNull Span span = Span.current();
final @Nullable Span span = Span.fromContextOrNull(Context.current());
if (span == null) {
return null;
}
return storage.getSentrySpan(span.getSpanContext());
}

Expand Down
Loading

0 comments on commit 36ed84a

Please sign in to comment.