Skip to content

Commit 55df61c

Browse files
committed
allow setting profiling-traces-dir-path independently from cache dir using external options
1 parent fba1881 commit 55df61c

File tree

6 files changed

+68
-5
lines changed

6 files changed

+68
-5
lines changed

sentry/api/sentry.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ public final class io/sentry/ExternalOptions {
491491
public fun getPrintUncaughtStackTrace ()Ljava/lang/Boolean;
492492
public fun getProfileSessionSampleRate ()Ljava/lang/Double;
493493
public fun getProfilesSampleRate ()Ljava/lang/Double;
494+
public fun getProfilingTracesDirPath ()Ljava/lang/String;
494495
public fun getProguardUuid ()Ljava/lang/String;
495496
public fun getProxy ()Lio/sentry/SentryOptions$Proxy;
496497
public fun getRelease ()Ljava/lang/String;
@@ -533,6 +534,7 @@ public final class io/sentry/ExternalOptions {
533534
public fun setPrintUncaughtStackTrace (Ljava/lang/Boolean;)V
534535
public fun setProfileSessionSampleRate (Ljava/lang/Double;)V
535536
public fun setProfilesSampleRate (Ljava/lang/Double;)V
537+
public fun setProfilingTracesDirPath (Ljava/lang/String;)V
536538
public fun setProguardUuid (Ljava/lang/String;)V
537539
public fun setProxy (Lio/sentry/SentryOptions$Proxy;)V
538540
public fun setRelease (Ljava/lang/String;)V
@@ -3479,6 +3481,7 @@ public class io/sentry/SentryOptions {
34793481
public fun setProfileSessionSampleRate (Ljava/lang/Double;)V
34803482
public fun setProfilesSampleRate (Ljava/lang/Double;)V
34813483
public fun setProfilesSampler (Lio/sentry/SentryOptions$ProfilesSamplerCallback;)V
3484+
public fun setProfilingTracesDirPath (Ljava/lang/String;)V
34823485
public fun setProfilingTracesHz (I)V
34833486
public fun setProguardUuid (Ljava/lang/String;)V
34843487
public fun setProxy (Lio/sentry/SentryOptions$Proxy;)V

sentry/src/main/java/io/sentry/ExternalOptions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public final class ExternalOptions {
5757
private @Nullable Boolean captureOpenTelemetryEvents;
5858

5959
private @Nullable Double profileSessionSampleRate;
60+
private @Nullable String profilingTracesDirPath;
6061

6162
private @Nullable SentryOptions.Cron cron;
6263

@@ -207,6 +208,8 @@ public final class ExternalOptions {
207208
options.setProfileSessionSampleRate(
208209
propertiesProvider.getDoubleProperty("profile-session-sample-rate"));
209210

211+
options.setProfilingTracesDirPath(propertiesProvider.getProperty("profiling-traces-dir-path"));
212+
210213
return options;
211214
}
212215

@@ -543,4 +546,12 @@ public void setEnableLogs(final @Nullable Boolean enableLogs) {
543546
public void setProfileSessionSampleRate(@Nullable Double profileSessionSampleRate) {
544547
this.profileSessionSampleRate = profileSessionSampleRate;
545548
}
549+
550+
public @Nullable String getProfilingTracesDirPath() {
551+
return profilingTracesDirPath;
552+
}
553+
554+
public void setProfilingTracesDirPath(@Nullable String profilingTracesDirPath) {
555+
this.profilingTracesDirPath = profilingTracesDirPath;
556+
}
546557
}

sentry/src/main/java/io/sentry/Sentry.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,16 @@ private static void initConfigurations(final @NotNull SentryOptions options) {
666666
options.getBackpressureMonitor().start();
667667
}
668668

669-
// TODO: make this configurable
670-
if (options.isContinuousProfilingEnabled() && profilingTracesDirPath != null) {
669+
if (options.isContinuousProfilingEnabled()
670+
&& profilingTracesDirPath != null
671+
&& options.getContinuousProfiler() == NoOpContinuousProfiler.getInstance()) {
671672
final IContinuousProfiler continuousProfiler =
672673
ProfilingServiceLoader.loadContinuousProfiler(
673-
new SystemOutLogger(), profilingTracesDirPath, 10, options.getExecutorService());
674+
new SystemOutLogger(),
675+
profilingTracesDirPath,
676+
options.getProfilingTracesHz(),
677+
options.getExecutorService());
678+
674679
options.setContinuousProfiler(continuousProfiler);
675680
}
676681

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,8 @@ public class SentryOptions {
578578

579579
private @NotNull ISocketTagger socketTagger = NoOpSocketTagger.getInstance();
580580

581+
private @Nullable String profilingTracesDirPath;
582+
581583
/**
582584
* Adds an event processor
583585
*
@@ -2026,14 +2028,25 @@ public void setStartProfilerOnAppStart(final boolean startProfilerOnAppStart) {
20262028
* @return the profiling traces dir. path or null if not set
20272029
*/
20282030
public @Nullable String getProfilingTracesDirPath() {
2031+
if (profilingTracesDirPath != null && !profilingTracesDirPath.isEmpty()) {
2032+
return dsnHash != null
2033+
? new File(profilingTracesDirPath, dsnHash).getAbsolutePath()
2034+
: profilingTracesDirPath;
2035+
}
2036+
20292037
final String cacheDirPath = getCacheDirPath();
2038+
20302039
if (cacheDirPath == null) {
2031-
// TODO: Should we add ExternalOptions to let users define the tracesDirPath?
2032-
return new File(".", "profiling_traces").getAbsolutePath();
2040+
return null;
20332041
}
2042+
20342043
return new File(cacheDirPath, "profiling_traces").getAbsolutePath();
20352044
}
20362045

2046+
public void setProfilingTracesDirPath(final @Nullable String profilingTracesDirPath) {
2047+
this.profilingTracesDirPath = profilingTracesDirPath;
2048+
}
2049+
20372050
/**
20382051
* Returns a list of origins to which `sentry-trace` header should be sent in HTTP integrations.
20392052
*
@@ -3223,6 +3236,10 @@ public void merge(final @NotNull ExternalOptions options) {
32233236
if (options.getProfileSessionSampleRate() != null) {
32243237
setProfileSessionSampleRate(options.getProfileSessionSampleRate());
32253238
}
3239+
3240+
if (options.getProfilingTracesDirPath() != null) {
3241+
setProfilingTracesDirPath(options.getProfilingTracesDirPath());
3242+
}
32263243
}
32273244

32283245
private @NotNull SdkVersion createSdkVersion() {

sentry/src/test/java/io/sentry/ExternalOptionsTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,13 @@ class ExternalOptionsTest {
383383
}
384384
}
385385

386+
@Test
387+
fun `creates options with profilingTracesDirPath set to profile_traces`() {
388+
withPropertiesFile("profiling-traces-dir-path=profile_traces") { options ->
389+
assertTrue(options.profilingTracesDirPath == "profile_traces")
390+
}
391+
}
392+
386393
private fun withPropertiesFile(
387394
textLines: List<String> = emptyList(),
388395
logger: ILogger = mock(),

sentry/src/test/java/io/sentry/SentryOptionsTest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,9 @@ class SentryOptionsTest {
408408
externalOptions.isGlobalHubMode = true
409409
externalOptions.isEnableLogs = true
410410
externalOptions.profileSessionSampleRate = 0.8
411+
externalOptions.profilingTracesDirPath = "/profiling-traces"
411412

413+
val hash = StringUtils.calculateStringHash(externalOptions.dsn, mock())
412414
val options = SentryOptions()
413415

414416
options.merge(externalOptions)
@@ -463,6 +465,7 @@ class SentryOptionsTest {
463465
assertTrue(options.isGlobalHubMode!!)
464466
assertTrue(options.logs.isEnabled!!)
465467
assertEquals(0.8, options.profileSessionSampleRate)
468+
assertEquals("/profiling-traces${File.separator}${hash}", options.profilingTracesDirPath)
466469
}
467470

468471
@Test
@@ -534,6 +537,23 @@ class SentryOptionsTest {
534537
)
535538
}
536539

540+
@Test
541+
fun `when cacheDirPath and profilingTracesDirPath are set, profilingTracesDirPath takes precedence`() {
542+
val dsn = "http://key@localhost/proj"
543+
val hash = StringUtils.calculateStringHash(dsn, mock())
544+
val options =
545+
SentryOptions().apply {
546+
setDsn(dsn)
547+
cacheDirPath = "${File.separator}test"
548+
profilingTracesDirPath = "${File.separator}test-profiles"
549+
}
550+
551+
assertEquals(
552+
"${File.separator}test-profiles${File.separator}${hash}",
553+
options.profilingTracesDirPath,
554+
)
555+
}
556+
537557
@Test
538558
fun `getCacheDirPathWithoutDsn does not contain dsn hash`() {
539559
val dsn = "http://key@localhost/proj"

0 commit comments

Comments
 (0)