From 149d59cd3f6bfb65a8992b4444d014cfa2ed3dbe Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Tue, 22 Oct 2024 16:17:49 +0200 Subject: [PATCH] [WIP] use SentryUUID instead of UUID.random().toString --- .../java/io/sentry/android/core/AndroidProfiler.java | 3 ++- .../main/java/io/sentry/android/core/Installation.java | 3 ++- .../internal/util/SentryFrameMetricsCollector.java | 3 ++- sentry/src/main/java/io/sentry/ProfilingTraceData.java | 2 +- sentry/src/main/java/io/sentry/SpanId.java | 10 ++-------- .../src/main/java/io/sentry/cache/EnvelopeCache.java | 3 ++- sentry/src/main/java/io/sentry/protocol/SentryId.java | 5 +++-- 7 files changed, 14 insertions(+), 15 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java index 379123c2c8..03d0501926 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java @@ -13,6 +13,7 @@ import io.sentry.MemoryCollectionData; import io.sentry.PerformanceCollectionData; import io.sentry.SentryLevel; +import io.sentry.SentryUUID; import io.sentry.android.core.internal.util.SentryFrameMetricsCollector; import io.sentry.profilemeasurements.ProfileMeasurement; import io.sentry.profilemeasurements.ProfileMeasurementValue; @@ -137,7 +138,7 @@ public AndroidProfiler( if (buildInfoProvider.getSdkInfoVersion() < Build.VERSION_CODES.LOLLIPOP) return null; // We create a file with a uuid name, so no need to check if it already exists - traceFile = new File(traceFilesDir, UUID.randomUUID() + ".trace"); + traceFile = new File(traceFilesDir, SentryUUID.generateSentryId() + ".trace"); measurementsMap.clear(); screenFrameRateMeasurements.clear(); diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java b/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java index 4c9b5ddbc2..08e9e34822 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/Installation.java @@ -2,6 +2,7 @@ import android.content.Context; import io.sentry.ISentryLifecycleToken; +import io.sentry.SentryUUID; import io.sentry.util.AutoClosableReentrantLock; import java.io.File; import java.io.FileOutputStream; @@ -65,7 +66,7 @@ public static String id(final @NotNull Context context) throws RuntimeException static @NotNull String writeInstallationFile(final @NotNull File installation) throws IOException { try (final OutputStream out = new FileOutputStream(installation)) { - final String id = UUID.randomUUID().toString(); + final String id = SentryUUID.generateSentryId(); out.write(id.getBytes(UTF_8)); out.flush(); return id; diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/internal/util/SentryFrameMetricsCollector.java b/sentry-android-core/src/main/java/io/sentry/android/core/internal/util/SentryFrameMetricsCollector.java index 25ff5da2bd..3a76cad40f 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/internal/util/SentryFrameMetricsCollector.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/internal/util/SentryFrameMetricsCollector.java @@ -16,6 +16,7 @@ import io.sentry.ILogger; import io.sentry.SentryLevel; import io.sentry.SentryOptions; +import io.sentry.SentryUUID; import io.sentry.android.core.BuildInfoProvider; import io.sentry.android.core.ContextUtils; import io.sentry.util.Objects; @@ -262,7 +263,7 @@ public void onActivityDestroyed(@NotNull Activity activity) {} if (!isAvailable) { return null; } - final String uid = UUID.randomUUID().toString(); + final String uid = SentryUUID.generateSentryId(); listenerMap.put(uid, listener); trackCurrentWindow(); return uid; diff --git a/sentry/src/main/java/io/sentry/ProfilingTraceData.java b/sentry/src/main/java/io/sentry/ProfilingTraceData.java index 17332b5931..2bcdb55500 100644 --- a/sentry/src/main/java/io/sentry/ProfilingTraceData.java +++ b/sentry/src/main/java/io/sentry/ProfilingTraceData.java @@ -154,7 +154,7 @@ public ProfilingTraceData( // Stacktrace context this.transactionId = transactionId; this.traceId = traceId; - this.profileId = UUID.randomUUID().toString(); + this.profileId = SentryUUID.generateSentryId(); this.environment = environment != null ? environment : DEFAULT_ENVIRONMENT; this.truncationReason = truncationReason; if (!isTruncationReasonValid()) { diff --git a/sentry/src/main/java/io/sentry/SpanId.java b/sentry/src/main/java/io/sentry/SpanId.java index 7857919e02..7eeb23a571 100644 --- a/sentry/src/main/java/io/sentry/SpanId.java +++ b/sentry/src/main/java/io/sentry/SpanId.java @@ -3,14 +3,12 @@ import static io.sentry.util.StringUtils.PROPER_NIL_UUID; import io.sentry.util.LazyEvaluator; -import io.sentry.util.StringUtils; import java.io.IOException; import java.util.Objects; -import java.util.UUID; import org.jetbrains.annotations.NotNull; public final class SpanId implements JsonSerializable { - public static final SpanId EMPTY_ID = new SpanId(PROPER_NIL_UUID); + public static final SpanId EMPTY_ID = new SpanId(PROPER_NIL_UUID.replace("-", "")); private final @NotNull LazyEvaluator lazyValue; @@ -21,11 +19,7 @@ public SpanId(final @NotNull String value) { public SpanId() { this.lazyValue = - new LazyEvaluator<>( - () -> - StringUtils.normalizeUUID(UUID.randomUUID().toString()) - .replace("-", "") - .substring(0, 16)); + new LazyEvaluator<>(SentryUUID::generateSpanId); } @Override diff --git a/sentry/src/main/java/io/sentry/cache/EnvelopeCache.java b/sentry/src/main/java/io/sentry/cache/EnvelopeCache.java index 0255d05a18..142776fa51 100644 --- a/sentry/src/main/java/io/sentry/cache/EnvelopeCache.java +++ b/sentry/src/main/java/io/sentry/cache/EnvelopeCache.java @@ -16,6 +16,7 @@ import io.sentry.SentryItemType; import io.sentry.SentryLevel; import io.sentry.SentryOptions; +import io.sentry.SentryUUID; import io.sentry.Session; import io.sentry.UncaughtExceptionHandlerIntegration; import io.sentry.hints.AbnormalExit; @@ -368,7 +369,7 @@ public void discard(final @NotNull SentryEnvelope envelope) { if (fileNameMap.containsKey(envelope)) { fileName = fileNameMap.get(envelope); } else { - fileName = UUID.randomUUID() + SUFFIX_ENVELOPE_FILE; + fileName = SentryUUID.generateSentryId() + SUFFIX_ENVELOPE_FILE; fileNameMap.put(envelope, fileName); } diff --git a/sentry/src/main/java/io/sentry/protocol/SentryId.java b/sentry/src/main/java/io/sentry/protocol/SentryId.java index bf1bde0c4d..bb304978ee 100644 --- a/sentry/src/main/java/io/sentry/protocol/SentryId.java +++ b/sentry/src/main/java/io/sentry/protocol/SentryId.java @@ -5,6 +5,7 @@ import io.sentry.JsonSerializable; import io.sentry.ObjectReader; import io.sentry.ObjectWriter; +import io.sentry.SentryUUID; import io.sentry.util.LazyEvaluator; import io.sentry.util.StringUtils; import java.io.IOException; @@ -14,7 +15,7 @@ public final class SentryId implements JsonSerializable { - public static final SentryId EMPTY_ID = new SentryId(new UUID(0, 0)); + public static final SentryId EMPTY_ID = new SentryId(StringUtils.PROPER_NIL_UUID); private final @NotNull LazyEvaluator lazyStringValue; @@ -26,7 +27,7 @@ public SentryId(@Nullable UUID uuid) { if (uuid != null) { this.lazyStringValue = new LazyEvaluator<>(() -> normalize(uuid.toString())); } else { - this.lazyStringValue = new LazyEvaluator<>(() -> normalize(UUID.randomUUID().toString())); + this.lazyStringValue = new LazyEvaluator<>(SentryUUID::generateSentryId); } }