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

POC startSpan API #3724

Draft
wants to merge 1 commit into
base: 8.x.x
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 @@ -128,8 +128,10 @@ public OtelSpanWrapper(

final @NotNull ISpan childSpan =
scopes.getOptions().getSpanFactory().createSpan(scopes, spanOptions, spanContext, this);
// TODO [POTEL] spanOptions.isBindToScope with default true?
childSpan.makeCurrent();

if (spanOptions.isBindToScope()) {
childSpan.makeCurrent();
}
return childSpan;
}

Expand Down
12 changes: 10 additions & 2 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ public final class io/sentry/HubAdapter : io/sentry/IHub {
public fun setTransaction (Ljava/lang/String;)V
public fun setUser (Lio/sentry/protocol/User;)V
public fun startSession ()V
public fun startSpan (Lio/sentry/SpanContext;Lio/sentry/SpanOptions;)Lio/sentry/ISpan;
public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public fun withIsolationScope (Lio/sentry/ScopeCallback;)V
Expand Down Expand Up @@ -673,6 +674,7 @@ public final class io/sentry/HubScopesWrapper : io/sentry/IHub {
public fun setTransaction (Ljava/lang/String;)V
public fun setUser (Lio/sentry/protocol/User;)V
public fun startSession ()V
public fun startSpan (Lio/sentry/SpanContext;Lio/sentry/SpanOptions;)Lio/sentry/ISpan;
public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public fun withIsolationScope (Lio/sentry/ScopeCallback;)V
Expand Down Expand Up @@ -912,6 +914,7 @@ public abstract interface class io/sentry/IScopes {
public abstract fun setTransaction (Ljava/lang/String;)V
public abstract fun setUser (Lio/sentry/protocol/User;)V
public abstract fun startSession ()V
public abstract fun startSpan (Lio/sentry/SpanContext;Lio/sentry/SpanOptions;)Lio/sentry/ISpan;
public fun startTransaction (Lio/sentry/TransactionContext;)Lio/sentry/ITransaction;
public abstract fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public fun startTransaction (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ITransaction;
Expand Down Expand Up @@ -1450,6 +1453,7 @@ public final class io/sentry/NoOpHub : io/sentry/IHub {
public fun setTransaction (Ljava/lang/String;)V
public fun setUser (Lio/sentry/protocol/User;)V
public fun startSession ()V
public fun startSpan (Lio/sentry/SpanContext;Lio/sentry/SpanOptions;)Lio/sentry/ISpan;
public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public fun withIsolationScope (Lio/sentry/ScopeCallback;)V
Expand Down Expand Up @@ -1611,6 +1615,7 @@ public final class io/sentry/NoOpScopes : io/sentry/IScopes {
public fun setTransaction (Ljava/lang/String;)V
public fun setUser (Lio/sentry/protocol/User;)V
public fun startSession ()V
public fun startSpan (Lio/sentry/SpanContext;Lio/sentry/SpanOptions;)Lio/sentry/ISpan;
public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public fun withIsolationScope (Lio/sentry/ScopeCallback;)V
Expand Down Expand Up @@ -2207,6 +2212,7 @@ public final class io/sentry/Scopes : io/sentry/IScopes, io/sentry/metrics/Metri
public fun setTransaction (Ljava/lang/String;)V
public fun setUser (Lio/sentry/protocol/User;)V
public fun startSession ()V
public fun startSpan (Lio/sentry/SpanContext;Lio/sentry/SpanOptions;)Lio/sentry/ISpan;
public fun startSpanForMetric (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ISpan;
public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public fun traceHeaders ()Lio/sentry/SentryTraceHeader;
Expand Down Expand Up @@ -2274,6 +2280,7 @@ public final class io/sentry/ScopesAdapter : io/sentry/IScopes {
public fun setTransaction (Ljava/lang/String;)V
public fun setUser (Lio/sentry/protocol/User;)V
public fun startSession ()V
public fun startSpan (Lio/sentry/SpanContext;Lio/sentry/SpanOptions;)Lio/sentry/ISpan;
public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public fun withIsolationScope (Lio/sentry/ScopeCallback;)V
Expand Down Expand Up @@ -2381,6 +2388,7 @@ public final class io/sentry/Sentry {
public static fun setTransaction (Ljava/lang/String;)V
public static fun setUser (Lio/sentry/protocol/User;)V
public static fun startSession ()V
public static fun startSpan (Lio/sentry/SpanContext;Lio/sentry/SpanOptions;)Lio/sentry/ISpan;
public static fun startTransaction (Lio/sentry/TransactionContext;)Lio/sentry/ITransaction;
public static fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public static fun startTransaction (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ITransaction;
Expand Down Expand Up @@ -3552,9 +3560,11 @@ public class io/sentry/SpanOptions {
public fun <init> ()V
public fun getOrigin ()Ljava/lang/String;
public fun getStartTimestamp ()Lio/sentry/SentryDate;
public fun isBindToScope ()Z
public fun isIdle ()Z
public fun isTrimEnd ()Z
public fun isTrimStart ()Z
public fun setBindToScope (Z)V
public fun setIdle (Z)V
public fun setOrigin (Ljava/lang/String;)V
public fun setStartTimestamp (Lio/sentry/SentryDate;)V
Expand Down Expand Up @@ -3696,10 +3706,8 @@ public final class io/sentry/TransactionOptions : io/sentry/SpanOptions {
public fun getSpanFactory ()Lio/sentry/ISpanFactory;
public fun getTransactionFinishedCallback ()Lio/sentry/TransactionFinishedCallback;
public fun isAppStartTransaction ()Z
public fun isBindToScope ()Z
public fun isWaitForChildren ()Z
public fun setAppStartTransaction (Z)V
public fun setBindToScope (Z)V
public fun setCustomSamplingContext (Lio/sentry/CustomSamplingContext;)V
public fun setDeadlineTimeout (Ljava/lang/Long;)V
public fun setIdleTimeout (Ljava/lang/Long;)V
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/main/java/io/sentry/HubAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ public boolean isAncestorOf(final @Nullable IScopes otherScopes) {
return Sentry.startTransaction(transactionContext, transactionOptions);
}

@Override
public @NotNull ISpan startSpan(
@NotNull SpanContext spanContext, @NotNull SpanOptions spanOptions) {
return Sentry.startSpan(spanContext, spanOptions);
}

@Deprecated
@Override
public @Nullable SentryTraceHeader traceHeaders() {
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/main/java/io/sentry/HubScopesWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ public boolean isAncestorOf(final @Nullable IScopes otherScopes) {
return scopes.startTransaction(transactionContext, transactionOptions);
}

@Override
public @NotNull ISpan startSpan(
@NotNull SpanContext spanContext, @NotNull SpanOptions spanOptions) {
return scopes.startSpan(spanContext, spanOptions);
}

@Override
public @Nullable SentryTraceHeader traceHeaders() {
return scopes.traceHeaders();
Expand Down
10 changes: 10 additions & 0 deletions sentry/src/main/java/io/sentry/IScopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,16 @@ ITransaction startTransaction(
final @NotNull TransactionContext transactionContext,
final @NotNull TransactionOptions transactionOptions);

/**
* Creates a span and returns the instance.
*
* @param spanContext the span context
* @param spanOptions the span options
* @return created span
*/
@NotNull
ISpan startSpan(final @NotNull SpanContext spanContext, final @NotNull SpanOptions spanOptions);

/**
* Returns the "sentry-trace" header that allows tracing across services. Can also be used in
* &lt;meta&gt; HTML tags. Also see {@link IScopes#getBaggage()}.
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/main/java/io/sentry/NoOpHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ public boolean isAncestorOf(@Nullable IScopes otherScopes) {
return NoOpTransaction.getInstance();
}

@Override
public @NotNull ISpan startSpan(
@NotNull SpanContext spanContext, @NotNull SpanOptions spanOptions) {
return NoOpSpan.getInstance();
}

@Override
@Deprecated
@SuppressWarnings("InlineMeSuggester")
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/main/java/io/sentry/NoOpScopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,12 @@ public boolean isAncestorOf(@Nullable IScopes otherScopes) {
return NoOpTransaction.getInstance();
}

@Override
public @NotNull ISpan startSpan(
@NotNull SpanContext spanContext, @NotNull SpanOptions spanOptions) {
return NoOpSpan.getInstance();
}

@Override
@Deprecated
@SuppressWarnings("InlineMeSuggester")
Expand Down
89 changes: 89 additions & 0 deletions sentry/src/main/java/io/sentry/Scopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,95 @@ public void flush(long timeoutMillis) {
return createTransaction(transactionContext, transactionOptions);
}

@Override
public @NotNull ISpan startSpan(
@NotNull SpanContext spanContext, @NotNull SpanOptions spanOptions) {
final @Nullable ISpan parentSpan = getSpan();
if (parentSpan == null) {
return startTransaction(
new TransactionContext(spanContext), new TransactionOptions(spanOptions));
} else {
return parentSpan.startChild(spanContext, spanOptions);
}
}

// @Override
// public @NotNull ISpan startSpan(@NotNull SpanContext spanContext, @NotNull SpanOptions
// spanOptions) {
// Objects.requireNonNull(spanContext, "spanContext is required");
//
// spanContext.setOrigin(spanOptions.getOrigin());
//
// ISpan span;
// if (!isEnabled()) {
// getOptions()
// .getLogger()
// .log(
// SentryLevel.WARNING,
// "Instance is disabled and this 'startSpan' returns a no-op.");
// span = NoOpSpan.getInstance();
// } else if (SpanUtils.isIgnored(
// getOptions().getIgnoredSpanOrigins(), spanContext.getOrigin())) {
// getOptions()
// .getLogger()
// .log(
// SentryLevel.DEBUG,
// "Returning no-op for span origin %s as the SDK has been configured to ignore it",
// transactionContext.getOrigin());
// span = NoOpSpan.getInstance();
//
// } else if (!getOptions().getInstrumenter().equals(spanContext.getInstrumenter())) {
// getOptions()
// .getLogger()
// .log(
// SentryLevel.DEBUG,
// "Returning no-op for instrumenter %s as the SDK has been configured to use
// instrumenter %s",
// spanContext.getInstrumenter(),
// getOptions().getInstrumenter());
// span = NoOpSpan.getInstance();
// } else if (!getOptions().isTracingEnabled()) {
// getOptions()
// .getLogger()
// .log(
// SentryLevel.INFO, "Tracing is disabled and this 'startSpan' returns a no-op.");
// span = NoOpSpan.getInstance();
// } else {
// final SamplingContext samplingContext =
// new SamplingContext(spanContext, spanOptions.getCustomSamplingContext());
// final @NotNull TracesSampler tracesSampler = getOptions().getInternalTracesSampler();
// @NotNull TracesSamplingDecision samplingDecision = tracesSampler.sample(samplingContext);
// spanContext.setSamplingDecision(samplingDecision);
//
// final @Nullable ISpanFactory maybeSpanFactory = spanOptions.getSpanFactory();
// final @NotNull ISpanFactory spanFactory =
// maybeSpanFactory == null ? getOptions().getSpanFactory() : maybeSpanFactory;
//
// // TODO [POTEL] parent span?
// span =
// spanFactory.createSpan(this, spanOptions, spanContext, null);
//
// // The listener is called only if the transaction exists, as the transaction is needed to
// // stop it
// if (samplingDecision.getSampled() && samplingDecision.getProfileSampled()) {
// final ITransactionProfiler transactionProfiler = getOptions().getTransactionProfiler();
// // If the profiler is not running, we start and bind it here.
// if (!transactionProfiler.isRunning()) {
// transactionProfiler.start();
// transactionProfiler.bindTransaction(transaction);
// } else if (transactionOptions.isAppStartTransaction()) {
// // If the profiler is running and the current transaction is the app start, we bind
// it.
// transactionProfiler.bindTransaction(transaction);
// }
// }
// }
// if (spanOptions.isBindToScope()) {
// span.makeCurrent();
// }
// return span;
// }

private @NotNull ITransaction createTransaction(
final @NotNull TransactionContext transactionContext,
final @NotNull TransactionOptions transactionOptions) {
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/main/java/io/sentry/ScopesAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ public boolean isAncestorOf(final @Nullable IScopes otherScopes) {
return Sentry.startTransaction(transactionContext, transactionOptions);
}

@Override
public @NotNull ISpan startSpan(
@NotNull SpanContext spanContext, @NotNull SpanOptions spanOptions) {
return Sentry.startSpan(spanContext, spanOptions);
}

@Deprecated
@Override
@SuppressWarnings("deprecation")
Expand Down
12 changes: 12 additions & 0 deletions sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,18 @@ public static void endSession() {
return getCurrentScopes().startTransaction(transactionContext, transactionOptions);
}

/**
* Creates a Span and returns the instance.
*
* @param spanContext the span context
* @param spanOptions options for the span
* @return created span, may be a NoOp
*/
public static @NotNull ISpan startSpan(
final @NotNull SpanContext spanContext, final @NotNull SpanOptions spanOptions) {
return getCurrentScopes().startSpan(spanContext, spanOptions);
}

/**
* Returns the "sentry-trace" header that allows tracing across services. Can also be used in
* &lt;meta&gt; HTML tags. Also see {@link Sentry#getBaggage()}.
Expand Down
36 changes: 36 additions & 0 deletions sentry/src/main/java/io/sentry/SpanOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@
@Open
public class SpanOptions {

public SpanOptions() {}

/**
* copy constructor
*
* @param spanOptions options to copy
*/
SpanOptions(SpanOptions spanOptions) {
startTimestamp = spanOptions.startTimestamp;
trimStart = spanOptions.trimStart;
trimEnd = spanOptions.trimEnd;
isIdle = spanOptions.isIdle;
origin = spanOptions.origin;
}

/** The start timestamp of the transaction */
private @Nullable SentryDate startTimestamp = null;

Expand Down Expand Up @@ -53,6 +68,9 @@ public void setStartTimestamp(@Nullable SentryDate startTimestamp) {

protected @Nullable String origin = DEFAULT_ORIGIN;

/** Defines if transaction should be bound to scope */
private boolean bindToScope = false;

public boolean isTrimStart() {
return trimStart;
}
Expand Down Expand Up @@ -84,4 +102,22 @@ public void setIdle(boolean idle) {
public void setOrigin(final @Nullable String origin) {
this.origin = origin;
}

/**
* Checks if bindToScope is enabled
*
* @return true if enabled or false otherwise
*/
public boolean isBindToScope() {
return bindToScope;
}

/**
* Sets bindToScope to enabled or disabled
*
* @param bindToScope true if enabled or false otherwise
*/
public void setBindToScope(boolean bindToScope) {
this.bindToScope = bindToScope;
}
}
9 changes: 9 additions & 0 deletions sentry/src/main/java/io/sentry/TransactionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ public TransactionContext(
this.baggage = baggage;
}

@ApiStatus.Internal
TransactionContext(SpanContext spanContext) {
super(spanContext);
this.name = DEFAULT_TRANSACTION_NAME;
this.parentSamplingDecision = null;
this.transactionNameSource = DEFAULT_NAME_SOURCE;
this.baggage = null;
}

public @NotNull String getName() {
return name;
}
Expand Down
Loading
Loading