Skip to content

Commit 110ae5e

Browse files
authored
Add instrumenter option (#2349)
1 parent 425ede7 commit 110ae5e

File tree

14 files changed

+193
-24
lines changed

14 files changed

+193
-24
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.sentry.IHub;
1616
import io.sentry.ISpan;
1717
import io.sentry.ITransaction;
18+
import io.sentry.Instrumenter;
1819
import io.sentry.Integration;
1920
import io.sentry.Scope;
2021
import io.sentry.SentryLevel;
@@ -197,7 +198,10 @@ private void startTracing(final @NotNull Activity activity) {
197198
// start specific span for app start
198199
appStartSpan =
199200
transaction.startChild(
200-
getAppStartOp(coldStart), getAppStartDesc(coldStart), appStartTime);
201+
getAppStartOp(coldStart),
202+
getAppStartDesc(coldStart),
203+
appStartTime,
204+
Instrumenter.SENTRY);
201205
}
202206

203207
// lets bind to the scope so other integrations can pick it up

sentry/api/sentry.api

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ public abstract interface class io/sentry/ISpan {
502502
public abstract fun setThrowable (Ljava/lang/Throwable;)V
503503
public abstract fun startChild (Ljava/lang/String;)Lio/sentry/ISpan;
504504
public abstract fun startChild (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ISpan;
505-
public abstract fun startChild (Ljava/lang/String;Ljava/lang/String;Ljava/util/Date;)Lio/sentry/ISpan;
505+
public abstract fun startChild (Ljava/lang/String;Ljava/lang/String;Ljava/util/Date;Lio/sentry/Instrumenter;)Lio/sentry/ISpan;
506506
public abstract fun toBaggageHeader (Ljava/util/List;)Lio/sentry/BaggageHeader;
507507
public abstract fun toSentryTrace ()Lio/sentry/SentryTraceHeader;
508508
public abstract fun traceContext ()Lio/sentry/TraceContext;
@@ -531,6 +531,13 @@ public abstract interface class io/sentry/ITransportFactory {
531531
public abstract fun create (Lio/sentry/SentryOptions;Lio/sentry/RequestDetails;)Lio/sentry/transport/ITransport;
532532
}
533533

534+
public final class io/sentry/Instrumenter : java/lang/Enum {
535+
public static final field OTEL Lio/sentry/Instrumenter;
536+
public static final field SENTRY Lio/sentry/Instrumenter;
537+
public static fun valueOf (Ljava/lang/String;)Lio/sentry/Instrumenter;
538+
public static fun values ()[Lio/sentry/Instrumenter;
539+
}
540+
534541
public abstract interface class io/sentry/Integration {
535542
public abstract fun register (Lio/sentry/IHub;Lio/sentry/SentryOptions;)V
536543
}
@@ -740,7 +747,7 @@ public final class io/sentry/NoOpSpan : io/sentry/ISpan {
740747
public fun setThrowable (Ljava/lang/Throwable;)V
741748
public fun startChild (Ljava/lang/String;)Lio/sentry/ISpan;
742749
public fun startChild (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ISpan;
743-
public fun startChild (Ljava/lang/String;Ljava/lang/String;Ljava/util/Date;)Lio/sentry/ISpan;
750+
public fun startChild (Ljava/lang/String;Ljava/lang/String;Ljava/util/Date;Lio/sentry/Instrumenter;)Lio/sentry/ISpan;
744751
public fun toBaggageHeader (Ljava/util/List;)Lio/sentry/BaggageHeader;
745752
public fun toSentryTrace ()Lio/sentry/SentryTraceHeader;
746753
public fun traceContext ()Lio/sentry/TraceContext;
@@ -779,7 +786,7 @@ public final class io/sentry/NoOpTransaction : io/sentry/ITransaction {
779786
public fun setThrowable (Ljava/lang/Throwable;)V
780787
public fun startChild (Ljava/lang/String;)Lio/sentry/ISpan;
781788
public fun startChild (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ISpan;
782-
public fun startChild (Ljava/lang/String;Ljava/lang/String;Ljava/util/Date;)Lio/sentry/ISpan;
789+
public fun startChild (Ljava/lang/String;Ljava/lang/String;Ljava/util/Date;Lio/sentry/Instrumenter;)Lio/sentry/ISpan;
783790
public fun toBaggageHeader (Ljava/util/List;)Lio/sentry/BaggageHeader;
784791
public fun toSentryTrace ()Lio/sentry/SentryTraceHeader;
785792
public fun traceContext ()Lio/sentry/TraceContext;
@@ -1369,6 +1376,7 @@ public class io/sentry/SentryOptions {
13691376
public fun getIgnoredExceptionsForType ()Ljava/util/Set;
13701377
public fun getInAppExcludes ()Ljava/util/List;
13711378
public fun getInAppIncludes ()Ljava/util/List;
1379+
public fun getInstrumenter ()Lio/sentry/Instrumenter;
13721380
public fun getIntegrations ()Ljava/util/List;
13731381
public fun getLogger ()Lio/sentry/ILogger;
13741382
public fun getMaxAttachmentSize ()J
@@ -1449,6 +1457,7 @@ public class io/sentry/SentryOptions {
14491457
public fun setFlushTimeoutMillis (J)V
14501458
public fun setHostnameVerifier (Ljavax/net/ssl/HostnameVerifier;)V
14511459
public fun setIdleTimeout (Ljava/lang/Long;)V
1460+
public fun setInstrumenter (Lio/sentry/Instrumenter;)V
14521461
public fun setLogger (Lio/sentry/ILogger;)V
14531462
public fun setMaxAttachmentSize (J)V
14541463
public fun setMaxBreadcrumbs (I)V
@@ -1578,7 +1587,7 @@ public final class io/sentry/SentryTracer : io/sentry/ITransaction {
15781587
public fun setThrowable (Ljava/lang/Throwable;)V
15791588
public fun startChild (Ljava/lang/String;)Lio/sentry/ISpan;
15801589
public fun startChild (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ISpan;
1581-
public fun startChild (Ljava/lang/String;Ljava/lang/String;Ljava/util/Date;)Lio/sentry/ISpan;
1590+
public fun startChild (Ljava/lang/String;Ljava/lang/String;Ljava/util/Date;Lio/sentry/Instrumenter;)Lio/sentry/ISpan;
15821591
public fun toBaggageHeader (Ljava/util/List;)Lio/sentry/BaggageHeader;
15831592
public fun toSentryTrace ()Lio/sentry/SentryTraceHeader;
15841593
public fun traceContext ()Lio/sentry/TraceContext;
@@ -1684,7 +1693,7 @@ public final class io/sentry/Span : io/sentry/ISpan {
16841693
public fun setThrowable (Ljava/lang/Throwable;)V
16851694
public fun startChild (Ljava/lang/String;)Lio/sentry/ISpan;
16861695
public fun startChild (Ljava/lang/String;Ljava/lang/String;)Lio/sentry/ISpan;
1687-
public fun startChild (Ljava/lang/String;Ljava/lang/String;Ljava/util/Date;)Lio/sentry/ISpan;
1696+
public fun startChild (Ljava/lang/String;Ljava/lang/String;Ljava/util/Date;Lio/sentry/Instrumenter;)Lio/sentry/ISpan;
16881697
public fun toBaggageHeader (Ljava/util/List;)Lio/sentry/BaggageHeader;
16891698
public fun toSentryTrace ()Lio/sentry/SentryTraceHeader;
16901699
public fun traceContext ()Lio/sentry/TraceContext;
@@ -1848,10 +1857,12 @@ public final class io/sentry/TransactionContext : io/sentry/SpanContext {
18481857
public static fun fromSentryTrace (Ljava/lang/String;Lio/sentry/protocol/TransactionNameSource;Ljava/lang/String;Lio/sentry/SentryTraceHeader;Lio/sentry/Baggage;)Lio/sentry/TransactionContext;
18491858
public static fun fromSentryTrace (Ljava/lang/String;Ljava/lang/String;Lio/sentry/SentryTraceHeader;)Lio/sentry/TransactionContext;
18501859
public fun getBaggage ()Lio/sentry/Baggage;
1860+
public fun getInstrumenter ()Lio/sentry/Instrumenter;
18511861
public fun getName ()Ljava/lang/String;
18521862
public fun getParentSampled ()Ljava/lang/Boolean;
18531863
public fun getParentSamplingDecision ()Lio/sentry/TracesSamplingDecision;
18541864
public fun getTransactionNameSource ()Lio/sentry/protocol/TransactionNameSource;
1865+
public fun setInstrumenter (Lio/sentry/Instrumenter;)V
18551866
public fun setParentSampled (Ljava/lang/Boolean;)V
18561867
public fun setParentSampled (Ljava/lang/Boolean;Ljava/lang/Boolean;)V
18571868
}

sentry/src/main/java/io/sentry/Hub.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,15 @@ public void flush(long timeoutMillis) {
699699
SentryLevel.WARNING,
700700
"Instance is disabled and this 'startTransaction' returns a no-op.");
701701
transaction = NoOpTransaction.getInstance();
702+
} else if (!options.getInstrumenter().equals(transactionContext.getInstrumenter())) {
703+
options
704+
.getLogger()
705+
.log(
706+
SentryLevel.DEBUG,
707+
"Returning no-op for instrumenter %s as the SDK has been configured to use instrumenter %s",
708+
transactionContext.getInstrumenter(),
709+
options.getInstrumenter());
710+
transaction = NoOpTransaction.getInstance();
702711
} else if (!options.isTracingEnabled()) {
703712
options
704713
.getLogger()

sentry/src/main/java/io/sentry/ISpan.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ public interface ISpan {
2020
@ApiStatus.Internal
2121
@NotNull
2222
ISpan startChild(
23-
@NotNull String operation, @Nullable String description, @Nullable Date timestamp);
23+
@NotNull String operation,
24+
@Nullable String description,
25+
@Nullable Date timestamp,
26+
@NotNull Instrumenter instrumenter);
2427

2528
/**
2629
* Starts a child Span.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.sentry;
2+
3+
/**
4+
* Which framework is responsible for instrumenting. This includes starting and stopping of
5+
* transactions and spans.
6+
*/
7+
public enum Instrumenter {
8+
SENTRY,
9+
10+
/** OpenTelemetry */
11+
OTEL
12+
}

sentry/src/main/java/io/sentry/NoOpSpan.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public static NoOpSpan getInstance() {
2323

2424
@Override
2525
public @NotNull ISpan startChild(
26-
@NotNull String operation, @Nullable String description, @Nullable Date timestamp) {
26+
@NotNull String operation,
27+
@Nullable String description,
28+
@Nullable Date timestamp,
29+
@NotNull Instrumenter instrumenter) {
2730
return NoOpSpan.getInstance();
2831
}
2932

sentry/src/main/java/io/sentry/NoOpTransaction.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public void setName(@NotNull String name, @NotNull TransactionNameSource transac
4343

4444
@Override
4545
public @NotNull ISpan startChild(
46-
@NotNull String operation, @Nullable String description, @Nullable Date timestamp) {
46+
@NotNull String operation,
47+
@Nullable String description,
48+
@Nullable Date timestamp,
49+
@NotNull Instrumenter instrumenter) {
4750
return NoOpSpan.getInstance();
4851
}
4952

sentry/src/main/java/io/sentry/SentryOptions.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ public class SentryOptions {
361361
/** Modules (dependencies, packages) that will be send along with each event. */
362362
private @NotNull IModulesLoader modulesLoader = NoOpModulesLoader.getInstance();
363363

364+
/** Which framework is responsible for instrumenting. */
365+
private @NotNull Instrumenter instrumenter = Instrumenter.SENTRY;
366+
364367
/**
365368
* Adds an event processor
366369
*
@@ -1741,6 +1744,28 @@ public void setSendClientReports(boolean sendClientReports) {
17411744
}
17421745
}
17431746

1747+
/**
1748+
* Sets the instrumenter used for performance instrumentation.
1749+
*
1750+
* <p>If you set this to something other than {{@link Instrumenter#SENTRY}} Sentry will not create
1751+
* any transactions automatically nor will it create transactions if you call
1752+
* startTransaction(...), nor will it create child spans if you call startChild(...)
1753+
*
1754+
* @param instrumenter - the instrumenter to use
1755+
*/
1756+
public void setInstrumenter(final @NotNull Instrumenter instrumenter) {
1757+
this.instrumenter = instrumenter;
1758+
}
1759+
1760+
/**
1761+
* Returns the instrumenter used for performance instrumentation
1762+
*
1763+
* @return the configured instrumenter
1764+
*/
1765+
public @NotNull Instrumenter getInstrumenter() {
1766+
return instrumenter;
1767+
}
1768+
17441769
/**
17451770
* Returns a ClientReportRecorder or a NoOp if sending of client reports has been disabled.
17461771
*

sentry/src/main/java/io/sentry/SentryTracer.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public final class SentryTracer implements ITransaction {
7777
private final @NotNull Baggage baggage;
7878
private @NotNull TransactionNameSource transactionNameSource;
7979
private final @NotNull Map<String, MeasurementValue> measurements;
80+
private final @NotNull Instrumenter instrumenter;
8081

8182
public SentryTracer(final @NotNull TransactionContext context, final @NotNull IHub hub) {
8283
this(context, hub, null);
@@ -110,6 +111,7 @@ public SentryTracer(
110111
this.measurements = new ConcurrentHashMap<>();
111112
this.root = new Span(context, this, hub, startTimestamp);
112113
this.name = context.getName();
114+
this.instrumenter = context.getInstrumenter();
113115
this.hub = hub;
114116
this.waitForChildren = waitForChildren;
115117
this.idleTimeout = idleTimeout;
@@ -195,8 +197,9 @@ ISpan startChild(
195197
final @NotNull SpanId parentSpanId,
196198
final @NotNull String operation,
197199
final @Nullable String description,
198-
final @Nullable Date timestamp) {
199-
return createChild(parentSpanId, operation, description, timestamp);
200+
final @Nullable Date timestamp,
201+
final @NotNull Instrumenter instrumenter) {
202+
return createChild(parentSpanId, operation, description, timestamp, instrumenter);
200203
}
201204

202205
/**
@@ -207,19 +210,24 @@ ISpan startChild(
207210
*/
208211
@NotNull
209212
private ISpan createChild(final @NotNull SpanId parentSpanId, final @NotNull String operation) {
210-
return createChild(parentSpanId, operation, null, null);
213+
return createChild(parentSpanId, operation, null, null, Instrumenter.SENTRY);
211214
}
212215

213216
@NotNull
214217
private ISpan createChild(
215218
final @NotNull SpanId parentSpanId,
216219
final @NotNull String operation,
217220
final @Nullable String description,
218-
@Nullable Date timestamp) {
221+
@Nullable Date timestamp,
222+
final @NotNull Instrumenter instrumenter) {
219223
if (root.isFinished()) {
220224
return NoOpSpan.getInstance();
221225
}
222226

227+
if (!this.instrumenter.equals(instrumenter)) {
228+
return NoOpSpan.getInstance();
229+
}
230+
223231
Objects.requireNonNull(parentSpanId, "parentSpanId is required");
224232
Objects.requireNonNull(operation, "operation is required");
225233
cancelTimer();
@@ -256,26 +264,34 @@ private ISpan createChild(
256264

257265
@Override
258266
public @NotNull ISpan startChild(
259-
final @NotNull String operation, @Nullable String description, @Nullable Date timestamp) {
260-
return createChild(operation, description, timestamp);
267+
final @NotNull String operation,
268+
@Nullable String description,
269+
@Nullable Date timestamp,
270+
@NotNull Instrumenter instrumenter) {
271+
return createChild(operation, description, timestamp, instrumenter);
261272
}
262273

263274
@Override
264275
public @NotNull ISpan startChild(
265276
final @NotNull String operation, final @Nullable String description) {
266-
return createChild(operation, description, null);
277+
return createChild(operation, description, null, Instrumenter.SENTRY);
267278
}
268279

269280
private @NotNull ISpan createChild(
270281
final @NotNull String operation,
271282
final @Nullable String description,
272-
@Nullable Date timestamp) {
283+
@Nullable Date timestamp,
284+
final @NotNull Instrumenter instrumenter) {
273285
if (root.isFinished()) {
274286
return NoOpSpan.getInstance();
275287
}
276288

289+
if (!this.instrumenter.equals(instrumenter)) {
290+
return NoOpSpan.getInstance();
291+
}
292+
277293
if (children.size() < hub.getOptions().getMaxSpans()) {
278-
return root.startChild(operation, description, timestamp);
294+
return root.startChild(operation, description, timestamp, instrumenter);
279295
} else {
280296
hub.getOptions()
281297
.getLogger()

sentry/src/main/java/io/sentry/Span.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,14 @@ public Span(
117117
public @NotNull ISpan startChild(
118118
final @NotNull String operation,
119119
final @Nullable String description,
120-
final @Nullable Date timestamp) {
120+
final @Nullable Date timestamp,
121+
final @NotNull Instrumenter instrumenter) {
121122
if (finished.get()) {
122123
return NoOpSpan.getInstance();
123124
}
124125

125-
return transaction.startChild(context.getSpanId(), operation, description, timestamp);
126+
return transaction.startChild(
127+
context.getSpanId(), operation, description, timestamp, instrumenter);
126128
}
127129

128130
@Override

0 commit comments

Comments
 (0)