Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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,8 +8,10 @@
import io.sentry.ILogger;
import io.sentry.IScopes;
import io.sentry.ISentryExecutorService;
import io.sentry.NoOpScopes;
import io.sentry.PerformanceCollectionData;
import io.sentry.ProfileChunk;
import io.sentry.Sentry;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.TransactionPerformanceCollector;
Expand Down Expand Up @@ -88,12 +90,14 @@ private void init() {
buildInfoProvider);
}

public synchronized void setScopes(final @NotNull IScopes scopes) {
this.scopes = scopes;
this.performanceCollector = scopes.getOptions().getTransactionPerformanceCollector();
}

public synchronized void start() {
if ((scopes == null || scopes != NoOpScopes.getInstance())
&& Sentry.getCurrentScopes() != NoOpScopes.getInstance()) {
this.scopes = Sentry.getCurrentScopes();
Comment thread
stefanosiano marked this conversation as resolved.
this.performanceCollector =
Sentry.getCurrentScopes().getOptions().getTransactionPerformanceCollector();
}

// Debug.startMethodTracingSampling() is only available since Lollipop, but Android Profiler
// causes crashes on api 21 -> https://github.com/getsentry/sentry-java/issues/3392
if (buildInfoProvider.getSdkInfoVersion() < Build.VERSION_CODES.LOLLIPOP_MR1) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.sentry.IScopes
import io.sentry.ISentryExecutorService
import io.sentry.MemoryCollectionData
import io.sentry.PerformanceCollectionData
import io.sentry.Sentry
import io.sentry.SentryLevel
import io.sentry.SentryNanotimeDate
import io.sentry.SentryTracer
Expand Down Expand Up @@ -80,7 +81,7 @@ class AndroidContinuousProfilerTest {
options.profilingTracesDirPath,
options.profilingTracesHz,
options.executorService
).also { it.setScopes(scopes) }
)
}
}

Expand Down Expand Up @@ -118,6 +119,8 @@ class AndroidContinuousProfilerTest {
// Profiler doesn't start if the folder doesn't exists.
// Usually it's generated when calling Sentry.init, but for tests we can create it manually.
File(fixture.options.profilingTracesDirPath!!).mkdirs()

Sentry.setCurrentScopes(fixture.scopes)
}

@AfterTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
<!-- <meta-data android:name="io.sentry.traces.sample-rate" android:value="0.8" /> -->

<!-- how to enable profiling when starting transactions -->
<meta-data android:name="io.sentry.traces.profiling.sample-rate" android:value="1.0" />
<!-- <meta-data android:name="io.sentry.traces.profiling.sample-rate" android:value="1.0" />-->

<!-- how to enable app start profiling -->
<meta-data android:name="io.sentry.traces.profiling.enable-app-start" android:value="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class MyApplication extends Application {

@Override
public void onCreate() {
Sentry.startProfiler();
strictMode();
super.onCreate();

Expand Down
16 changes: 16 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,10 @@ public final class io/sentry/HubAdapter : io/sentry/IHub {
public fun setTag (Ljava/lang/String;Ljava/lang/String;)V
public fun setTransaction (Ljava/lang/String;)V
public fun setUser (Lio/sentry/protocol/User;)V
public fun startProfiler ()V
public fun startSession ()V
public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public fun stopProfiler ()V
public fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public fun withIsolationScope (Lio/sentry/ScopeCallback;)V
public fun withScope (Lio/sentry/ScopeCallback;)V
Expand Down Expand Up @@ -677,8 +679,10 @@ public final class io/sentry/HubScopesWrapper : io/sentry/IHub {
public fun setTag (Ljava/lang/String;Ljava/lang/String;)V
public fun setTransaction (Ljava/lang/String;)V
public fun setUser (Lio/sentry/protocol/User;)V
public fun startProfiler ()V
public fun startSession ()V
public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public fun stopProfiler ()V
public fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public fun withIsolationScope (Lio/sentry/ScopeCallback;)V
public fun withScope (Lio/sentry/ScopeCallback;)V
Expand Down Expand Up @@ -926,11 +930,13 @@ public abstract interface class io/sentry/IScopes {
public abstract fun setTag (Ljava/lang/String;Ljava/lang/String;)V
public abstract fun setTransaction (Ljava/lang/String;)V
public abstract fun setUser (Lio/sentry/protocol/User;)V
public abstract fun startProfiler ()V
public abstract fun startSession ()V
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;
public fun startTransaction (Ljava/lang/String;Ljava/lang/String;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public abstract fun stopProfiler ()V
public abstract fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public abstract fun withIsolationScope (Lio/sentry/ScopeCallback;)V
public abstract fun withScope (Lio/sentry/ScopeCallback;)V
Expand Down Expand Up @@ -1477,8 +1483,10 @@ public final class io/sentry/NoOpHub : io/sentry/IHub {
public fun setTag (Ljava/lang/String;Ljava/lang/String;)V
public fun setTransaction (Ljava/lang/String;)V
public fun setUser (Lio/sentry/protocol/User;)V
public fun startProfiler ()V
public fun startSession ()V
public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public fun stopProfiler ()V
public fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public fun withIsolationScope (Lio/sentry/ScopeCallback;)V
public fun withScope (Lio/sentry/ScopeCallback;)V
Expand Down Expand Up @@ -1639,8 +1647,10 @@ public final class io/sentry/NoOpScopes : io/sentry/IScopes {
public fun setTag (Ljava/lang/String;Ljava/lang/String;)V
public fun setTransaction (Ljava/lang/String;)V
public fun setUser (Lio/sentry/protocol/User;)V
public fun startProfiler ()V
public fun startSession ()V
public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public fun stopProfiler ()V
public fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public fun withIsolationScope (Lio/sentry/ScopeCallback;)V
public fun withScope (Lio/sentry/ScopeCallback;)V
Expand Down Expand Up @@ -2310,9 +2320,11 @@ public final class io/sentry/Scopes : io/sentry/IScopes, io/sentry/metrics/Metri
public fun setTag (Ljava/lang/String;Ljava/lang/String;)V
public fun setTransaction (Ljava/lang/String;)V
public fun setUser (Lio/sentry/protocol/User;)V
public fun startProfiler ()V
public fun startSession ()V
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 stopProfiler ()V
public fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public fun withIsolationScope (Lio/sentry/ScopeCallback;)V
public fun withScope (Lio/sentry/ScopeCallback;)V
Expand Down Expand Up @@ -2378,8 +2390,10 @@ public final class io/sentry/ScopesAdapter : io/sentry/IScopes {
public fun setTag (Ljava/lang/String;Ljava/lang/String;)V
public fun setTransaction (Ljava/lang/String;)V
public fun setUser (Lio/sentry/protocol/User;)V
public fun startProfiler ()V
public fun startSession ()V
public fun startTransaction (Lio/sentry/TransactionContext;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public fun stopProfiler ()V
public fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public fun withIsolationScope (Lio/sentry/ScopeCallback;)V
public fun withScope (Lio/sentry/ScopeCallback;)V
Expand Down Expand Up @@ -2485,12 +2499,14 @@ public final class io/sentry/Sentry {
public static fun setTag (Ljava/lang/String;Ljava/lang/String;)V
public static fun setTransaction (Ljava/lang/String;)V
public static fun setUser (Lio/sentry/protocol/User;)V
public static fun startProfiler ()V
public static fun startSession ()V
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;
public static fun startTransaction (Ljava/lang/String;Ljava/lang/String;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public static fun startTransaction (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lio/sentry/TransactionOptions;)Lio/sentry/ITransaction;
public static fun stopProfiler ()V
public static fun traceHeaders ()Lio/sentry/SentryTraceHeader;
public static fun withIsolationScope (Lio/sentry/ScopeCallback;)V
public static fun withScope (Lio/sentry/ScopeCallback;)V
Expand Down
10 changes: 10 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,16 @@ public boolean isAncestorOf(final @Nullable IScopes otherScopes) {
return Sentry.startTransaction(transactionContext, transactionOptions);
}

@Override
public void startProfiler() {
Sentry.startProfiler();
}

@Override
public void stopProfiler() {
Sentry.stopProfiler();
}

@Override
public @NotNull SentryId captureProfileChunk(
final @NotNull ProfileChunk profilingContinuousData) {
Expand Down
10 changes: 10 additions & 0 deletions sentry/src/main/java/io/sentry/HubScopesWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@ public boolean isAncestorOf(final @Nullable IScopes otherScopes) {
return scopes.startTransaction(transactionContext, transactionOptions);
}

@Override
public void startProfiler() {
scopes.startProfiler();
}

@Override
public void stopProfiler() {
scopes.stopProfiler();
}

@Override
public @Nullable SentryTraceHeader traceHeaders() {
return scopes.traceHeaders();
Expand Down
2 changes: 0 additions & 2 deletions sentry/src/main/java/io/sentry/IContinuousProfiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public interface IContinuousProfiler {

void stop();

void setScopes(final @NotNull IScopes scopes);

/** Cancel the profiler and stops it. Used on SDK close. */
void close();

Expand Down
4 changes: 4 additions & 0 deletions sentry/src/main/java/io/sentry/IScopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,10 @@ ITransaction startTransaction(
final @NotNull TransactionContext transactionContext,
final @NotNull TransactionOptions transactionOptions);

void startProfiler();

void stopProfiler();

/**
* 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
3 changes: 0 additions & 3 deletions sentry/src/main/java/io/sentry/NoOpContinuousProfiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public void start() {}
@Override
public void stop() {}

@Override
public void setScopes(@NotNull IScopes scopes) {}

@Override
public boolean isRunning() {
return false;
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 @@ -247,6 +247,12 @@ public boolean isAncestorOf(@Nullable IScopes otherScopes) {
return NoOpTransaction.getInstance();
}

@Override
public void startProfiler() {}

@Override
public void stopProfiler() {}

@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 @@ -242,6 +242,12 @@ public boolean isAncestorOf(@Nullable IScopes otherScopes) {
return NoOpTransaction.getInstance();
}

@Override
public void startProfiler() {}

@Override
public void stopProfiler() {}

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

@Override
public void startProfiler() {
if (getOptions().isContinuousProfilingEnabled()) {
getOptions().getLogger().log(SentryLevel.DEBUG, "Started continuous Profiling.");
getOptions().getContinuousProfiler().start();
} else {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Continuous Profiling is not enabled. Set profilesSampleRate and profilesSampler to null to enable it.");
}
}

@Override
public void stopProfiler() {
if (getOptions().isContinuousProfilingEnabled()) {
getOptions().getLogger().log(SentryLevel.DEBUG, "Stopped continuous Profiling.");
getOptions().getContinuousProfiler().stop();
} else {
getOptions()
.getLogger()
.log(
SentryLevel.WARNING,
"Continuous Profiling is not enabled. Set profilesSampleRate and profilesSampler to null to enable it.");
}
}

@Deprecated
@SuppressWarnings("InlineMeSuggester")
@Override
Expand Down
10 changes: 10 additions & 0 deletions sentry/src/main/java/io/sentry/ScopesAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ public boolean isAncestorOf(final @Nullable IScopes otherScopes) {
return Sentry.startTransaction(transactionContext, transactionOptions);
}

@Override
public void startProfiler() {
Sentry.startProfiler();
}

@Override
public void stopProfiler() {
Sentry.stopProfiler();
}

@Deprecated
@Override
@SuppressWarnings("deprecation")
Expand Down
10 changes: 10 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,16 @@ public static void endSession() {
return getCurrentScopes().startTransaction(transactionContext, transactionOptions);
}

/** Starts the continuous profiler, if enabled. */
public static void startProfiler() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt of introducing a subclass (like a namespace) Sentry.profiling().start() akin to Sentry.metrics()? We're also gonna do that for replay soon

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to keep the SDKs aligned. Other SDKs, like cocoa, is doing SentrySDK.startProfiler

getCurrentScopes().startProfiler();
}

/** Starts the continuous profiler, if enabled. */
public static void stopProfiler() {
getCurrentScopes().stopProfiler();
}

/**
* 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
10 changes: 10 additions & 0 deletions sentry/src/test/java/io/sentry/HubAdapterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,14 @@ class HubAdapterTest {
HubAdapter.getInstance().reportFullyDisplayed()
verify(scopes).reportFullyDisplayed()
}

@Test fun `startProfiler calls Hub`() {
HubAdapter.getInstance().startProfiler()
verify(scopes).startProfiler()
}

@Test fun `stopProfiler calls Hub`() {
HubAdapter.getInstance().stopProfiler()
verify(scopes).stopProfiler()
}
}
6 changes: 6 additions & 0 deletions sentry/src/test/java/io/sentry/NoOpHubTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,10 @@ class NoOpHubTest {
sut.withScope(scopeCallback)
verify(scopeCallback).run(NoOpScope.getInstance())
}

@Test
fun `startProfiler doesnt throw`() = sut.startProfiler()

@Test
fun `stopProfiler doesnt throw`() = sut.stopProfiler()
}
10 changes: 10 additions & 0 deletions sentry/src/test/java/io/sentry/ScopesAdapterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,14 @@ class ScopesAdapterTest {
ScopesAdapter.getInstance().reportFullyDisplayed()
verify(scopes).reportFullyDisplayed()
}

@Test fun `startProfiler calls Scopes`() {
ScopesAdapter.getInstance().startProfiler()
verify(scopes).startProfiler()
}

@Test fun `stopProfiler calls Scopes`() {
ScopesAdapter.getInstance().stopProfiler()
verify(scopes).stopProfiler()
}
}
Loading