Skip to content

Commit 3f10556

Browse files
authored
Isolate cached events with hashed dsn subfolder (#2038)
1 parent cfdd10b commit 3f10556

File tree

15 files changed

+146
-47
lines changed

15 files changed

+146
-47
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
- Include application permissions in Android events ([#2018](https://github.com/getsentry/sentry-java/pull/2018))
1414
- Automatically create transactions for UI events ([#1975](https://github.com/getsentry/sentry-java/pull/1975))
1515

16+
### Fixes
17+
18+
- Isolate cached events with hashed DSN subfolder ([#2038](https://github.com/getsentry/sentry-java/pull/2038))
19+
1620
### Changed
1721

1822
- Update sentry-native to 0.4.17 ([#2033](https://github.com/getsentry/sentry-java/pull/2033))

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,7 @@ private static void readDefaultOptionValues(
299299
private static void initializeCacheDirs(
300300
final @NotNull Context context, final @NotNull SentryAndroidOptions options) {
301301
final File cacheDir = new File(context.getCacheDir(), "sentry");
302-
final File profilingTracesDir = new File(cacheDir, "profiling_traces");
303302
options.setCacheDirPath(cacheDir.getAbsolutePath());
304-
options.setProfilingTracesDirPath(profilingTracesDir.getAbsolutePath());
305303
}
306304

307305
private static boolean isNdkAvailable(final @NotNull BuildInfoProvider buildInfoProvider) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private void init() {
7373
options.getLogger().log(SentryLevel.INFO, "Profiling is disabled in options.");
7474
return;
7575
}
76-
if (tracesFilesDirPath == null || tracesFilesDirPath.isEmpty()) {
76+
if (tracesFilesDirPath == null) {
7777
options
7878
.getLogger()
7979
.log(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final void register(final @NotNull IHub hub, final @NotNull SentryOptions
4040
// up by another integration (EnvelopeFileObserverIntegration).
4141
if (enabled && sentryNdkClass != null) {
4242
final String cachedDir = this.options.getCacheDirPath();
43-
if (cachedDir == null || cachedDir.isEmpty()) {
43+
if (cachedDir == null) {
4444
this.options.getLogger().log(SentryLevel.ERROR, "No cache dir path is defined in options.");
4545
disableNdkIntegration(this.options);
4646
return;

sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class AndroidTransactionProfilerTest {
128128
@Test
129129
fun `profiler evaluates profilingTracesDirPath options only on first transaction profiling`() {
130130
fixture.options.apply {
131-
profilingTracesDirPath = ""
131+
cacheDirPath = null
132132
}
133133

134134
// We create the profiler, and nothing goes wrong
@@ -174,7 +174,7 @@ class AndroidTransactionProfilerTest {
174174
@Test
175175
fun `profiler on tracesDirPath null`() {
176176
fixture.options.apply {
177-
profilingTracesDirPath = null
177+
cacheDirPath = null
178178
}
179179
val profiler = fixture.getSut(context)
180180
profiler.onTransactionStart(fixture.transaction1)
@@ -185,7 +185,7 @@ class AndroidTransactionProfilerTest {
185185
@Test
186186
fun `profiler on tracesDirPath empty`() {
187187
fixture.options.apply {
188-
profilingTracesDirPath = ""
188+
cacheDirPath = null
189189
}
190190
val profiler = fixture.getSut(context)
191191
profiler.onTransactionStart(fixture.transaction1)

sentry-samples/sentry-samples-android/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
android:name=".ThirdActivityFragment"
4646
android:exported="false" />
4747

48-
<activity
48+
<activity
4949
android:name=".GesturesActivity"
5050
android:exported="false" />
5151

sentry/api/sentry.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,6 @@ public class io/sentry/SentryOptions {
12381238
public fun setMaxTraceFileSize (J)V
12391239
public fun setPrintUncaughtStackTrace (Z)V
12401240
public fun setProfilingEnabled (Z)V
1241-
public fun setProfilingTracesDirPath (Ljava/lang/String;)V
12421241
public fun setProguardUuid (Ljava/lang/String;)V
12431242
public fun setProxy (Lio/sentry/SentryOptions$Proxy;)V
12441243
public fun setReadTimeoutMillis (I)V
@@ -2963,6 +2962,7 @@ public final class io/sentry/util/Platform {
29632962

29642963
public final class io/sentry/util/StringUtils {
29652964
public static fun byteCountToString (J)Ljava/lang/String;
2965+
public static fun calculateStringHash (Ljava/lang/String;Lio/sentry/ILogger;)Ljava/lang/String;
29662966
public static fun capitalize (Ljava/lang/String;)Ljava/lang/String;
29672967
public static fun getStringAfterDot (Ljava/lang/String;)Ljava/lang/String;
29682968
public static fun removeSurrounding (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;

sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface SendFireAndForgetFactory {
2424
SendFireAndForget create(@NotNull IHub hub, @NotNull SentryOptions options);
2525

2626
default boolean hasValidPath(final @Nullable String dirPath, final @NotNull ILogger logger) {
27-
if (dirPath == null || dirPath.isEmpty()) {
27+
if (dirPath == null) {
2828
logger.log(SentryLevel.INFO, "No cached dir path is defined in options.");
2929
return false;
3030
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,23 +220,23 @@ private static boolean initConfigurations(final @NotNull SentryOptions options)
220220
// eg release, distinctId, sentryClientName
221221

222222
// this should be after setting serializers
223-
if (options.getOutboxPath() != null) {
224-
final File outboxDir = new File(options.getOutboxPath());
223+
final String outboxPath = options.getOutboxPath();
224+
if (outboxPath != null) {
225+
final File outboxDir = new File(outboxPath);
225226
outboxDir.mkdirs();
226227
} else {
227228
logger.log(SentryLevel.INFO, "No outbox dir path is defined in options.");
228229
}
229230

230-
if (options.getCacheDirPath() != null && !options.getCacheDirPath().isEmpty()) {
231-
final File cacheDir = new File(options.getCacheDirPath());
231+
final String cacheDirPath = options.getCacheDirPath();
232+
if (cacheDirPath != null) {
233+
final File cacheDir = new File(cacheDirPath);
232234
cacheDir.mkdirs();
233235
options.setEnvelopeDiskCache(EnvelopeCache.create(options));
234236
}
235237

236238
final String profilingTracesDirPath = options.getProfilingTracesDirPath();
237-
if (options.isProfilingEnabled()
238-
&& profilingTracesDirPath != null
239-
&& !profilingTracesDirPath.isEmpty()) {
239+
if (options.isProfilingEnabled() && profilingTracesDirPath != null) {
240240

241241
final File profilingTracesDir = new File(profilingTracesDirPath);
242242
profilingTracesDir.mkdirs();

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.sentry.transport.NoOpEnvelopeCache;
1111
import io.sentry.transport.NoOpTransportGate;
1212
import io.sentry.util.Platform;
13+
import io.sentry.util.StringUtils;
1314
import java.io.File;
1415
import java.util.ArrayList;
1516
import java.util.HashMap;
@@ -55,6 +56,9 @@ public class SentryOptions {
5556
*/
5657
private @Nullable String dsn;
5758

59+
/** dsnHash is used as a subfolder of cacheDirPath to isolate events when rotating DSNs */
60+
private @Nullable String dsnHash;
61+
5862
/**
5963
* Controls how many seconds to wait before shutting down. Sentry SDKs send events from a
6064
* background queue and this queue is given a certain amount to drain pending events Default is
@@ -295,9 +299,6 @@ public class SentryOptions {
295299
/** Control if profiling is enabled or not for transactions */
296300
private boolean profilingEnabled = false;
297301

298-
/** The cache dir. path for caching profiling traces */
299-
private @Nullable String profilingTracesDirPath;
300-
301302
/** Max trace file size in bytes. */
302303
private long maxTraceFileSize = 5 * 1024 * 1024;
303304

@@ -384,8 +385,10 @@ public void addIntegration(@NotNull Integration integration) {
384385
*
385386
* @param dsn the DSN
386387
*/
387-
public void setDsn(@Nullable String dsn) {
388+
public void setDsn(final @Nullable String dsn) {
388389
this.dsn = dsn;
390+
391+
dsnHash = StringUtils.calculateStringHash(this.dsn, logger);
389392
}
390393

391394
/**
@@ -607,7 +610,11 @@ public void setBeforeBreadcrumb(@Nullable BeforeBreadcrumbCallback beforeBreadcr
607610
* @return the cache dir. path or null if not set
608611
*/
609612
public @Nullable String getCacheDirPath() {
610-
return cacheDirPath;
613+
if (cacheDirPath == null || cacheDirPath.isEmpty()) {
614+
return null;
615+
}
616+
617+
return dsnHash != null ? new File(cacheDirPath, dsnHash).getAbsolutePath() : cacheDirPath;
611618
}
612619

613620
/**
@@ -616,18 +623,19 @@ public void setBeforeBreadcrumb(@Nullable BeforeBreadcrumbCallback beforeBreadcr
616623
* @return the outbox path or null if not set
617624
*/
618625
public @Nullable String getOutboxPath() {
619-
if (cacheDirPath == null || cacheDirPath.isEmpty()) {
626+
final String cacheDirPath = getCacheDirPath();
627+
if (cacheDirPath == null) {
620628
return null;
621629
}
622-
return cacheDirPath + File.separator + "outbox";
630+
return new File(cacheDirPath, "outbox").getAbsolutePath();
623631
}
624632

625633
/**
626634
* Sets the cache dir. path
627635
*
628636
* @param cacheDirPath the cache dir. path
629637
*/
630-
public void setCacheDirPath(@Nullable String cacheDirPath) {
638+
public void setCacheDirPath(final @Nullable String cacheDirPath) {
631639
this.cacheDirPath = cacheDirPath;
632640
}
633641

@@ -1496,16 +1504,11 @@ public void setProfilingEnabled(boolean profilingEnabled) {
14961504
* @return the profiling traces dir. path or null if not set
14971505
*/
14981506
public @Nullable String getProfilingTracesDirPath() {
1499-
return profilingTracesDirPath;
1500-
}
1501-
1502-
/**
1503-
* Sets the profiling traces dir. path
1504-
*
1505-
* @param profilingTracesDirPath the profiling traces dir. path
1506-
*/
1507-
public void setProfilingTracesDirPath(@Nullable String profilingTracesDirPath) {
1508-
this.profilingTracesDirPath = profilingTracesDirPath;
1507+
final String cacheDirPath = getCacheDirPath();
1508+
if (cacheDirPath == null) {
1509+
return null;
1510+
}
1511+
return new File(cacheDirPath, "profiling_traces").getAbsolutePath();
15091512
}
15101513

15111514
/**

0 commit comments

Comments
 (0)