From 6ac38d4fcf9359cef3c04dae34651c64c347eb21 Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Tue, 10 Sep 2024 14:50:00 +0200 Subject: [PATCH 1/5] use LazyEvaluator for SpanId creation --- sentry/src/main/java/io/sentry/SpanId.java | 24 ++++++++++------------ 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/sentry/src/main/java/io/sentry/SpanId.java b/sentry/src/main/java/io/sentry/SpanId.java index 70608fb7cb..1a34b634e2 100644 --- a/sentry/src/main/java/io/sentry/SpanId.java +++ b/sentry/src/main/java/io/sentry/SpanId.java @@ -1,26 +1,24 @@ package io.sentry; -import io.sentry.util.Objects; +import io.sentry.util.LazyEvaluator; import io.sentry.util.StringUtils; import java.io.IOException; import java.util.UUID; import org.jetbrains.annotations.NotNull; public final class SpanId implements JsonSerializable { - public static final SpanId EMPTY_ID = new SpanId(new UUID(0, 0)); + public static final SpanId EMPTY_ID = new SpanId("00000000-0000-0000-0000-000000000000"); - private final @NotNull String value; + private final @NotNull LazyEvaluator lazyValue; public SpanId(final @NotNull String value) { - this.value = Objects.requireNonNull(value, "value is required"); + this.lazyValue = new LazyEvaluator<>(() -> value); } public SpanId() { - this(UUID.randomUUID()); - } - - private SpanId(final @NotNull UUID uuid) { - this(StringUtils.normalizeUUID(uuid.toString()).replace("-", "").substring(0, 16)); + this.lazyValue = new LazyEvaluator<>(() -> + StringUtils.normalizeUUID(UUID.randomUUID().toString()).replace("-", "").substring(0, 16) + ); } @Override @@ -28,17 +26,17 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SpanId spanId = (SpanId) o; - return value.equals(spanId.value); + return lazyValue.getValue().equals(spanId.lazyValue.getValue()); } @Override public int hashCode() { - return value.hashCode(); + return lazyValue.getValue().hashCode(); } @Override public String toString() { - return this.value; + return lazyValue.getValue(); } // JsonElementSerializer @@ -46,7 +44,7 @@ public String toString() { @Override public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger logger) throws IOException { - writer.value(value); + writer.value(lazyValue.getValue()); } // JsonElementDeserializer From f84b78fc2a3dd1d0621e26d9592886c89c165852 Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Tue, 10 Sep 2024 12:54:14 +0000 Subject: [PATCH 2/5] Format code --- sentry/src/main/java/io/sentry/SpanId.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sentry/src/main/java/io/sentry/SpanId.java b/sentry/src/main/java/io/sentry/SpanId.java index 1a34b634e2..e07c643fb0 100644 --- a/sentry/src/main/java/io/sentry/SpanId.java +++ b/sentry/src/main/java/io/sentry/SpanId.java @@ -16,9 +16,12 @@ public SpanId(final @NotNull String value) { } public SpanId() { - this.lazyValue = new LazyEvaluator<>(() -> - StringUtils.normalizeUUID(UUID.randomUUID().toString()).replace("-", "").substring(0, 16) - ); + this.lazyValue = + new LazyEvaluator<>( + () -> + StringUtils.normalizeUUID(UUID.randomUUID().toString()) + .replace("-", "") + .substring(0, 16)); } @Override From a9c051bfbe1742b5c2c1296327422986e6e4bc44 Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Tue, 1 Oct 2024 16:53:46 +0200 Subject: [PATCH 3/5] make SentryId generate UUID lazily --- .../java/io/sentry/protocol/SentryId.java | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/sentry/src/main/java/io/sentry/protocol/SentryId.java b/sentry/src/main/java/io/sentry/protocol/SentryId.java index 109655fdf2..bf59c1592c 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.util.LazyEvaluator; import io.sentry.util.StringUtils; import java.io.IOException; import java.util.UUID; @@ -12,28 +13,37 @@ import org.jetbrains.annotations.Nullable; public final class SentryId implements JsonSerializable { - private final @NotNull UUID uuid; public static final SentryId EMPTY_ID = new SentryId(new UUID(0, 0)); + private final @NotNull LazyEvaluator lazyValue; + public SentryId() { this((UUID) null); } public SentryId(@Nullable UUID uuid) { - if (uuid == null) { - uuid = UUID.randomUUID(); + if (uuid != null) { + this.lazyValue = new LazyEvaluator<>(() -> uuid); + } else { + this.lazyValue = new LazyEvaluator<>(UUID::randomUUID); } - this.uuid = uuid; } public SentryId(final @NotNull String sentryIdString) { - this.uuid = fromStringSentryId(StringUtils.normalizeUUID(sentryIdString)); + if (sentryIdString.length() != 32 && sentryIdString.length() != 36) { + throw new IllegalArgumentException( + "String representation of SentryId has either 32 (UUID no dashes) " + + "or 36 characters long (completed UUID). Received: " + + sentryIdString); + } + this.lazyValue = + new LazyEvaluator<>(() -> fromStringSentryId(StringUtils.normalizeUUID(sentryIdString))); } @Override public String toString() { - return StringUtils.normalizeUUID(uuid.toString()).replace("-", ""); + return StringUtils.normalizeUUID(lazyValue.getValue().toString()).replace("-", ""); } @Override @@ -41,12 +51,12 @@ public boolean equals(final @Nullable Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SentryId sentryId = (SentryId) o; - return uuid.compareTo(sentryId.uuid) == 0; + return lazyValue.getValue().compareTo(sentryId.lazyValue.getValue()) == 0; } @Override public int hashCode() { - return uuid.hashCode(); + return lazyValue.getValue().hashCode(); } private @NotNull UUID fromStringSentryId(@NotNull String sentryIdString) { @@ -60,12 +70,6 @@ public int hashCode() { .insert(23, "-") .toString(); } - if (sentryIdString.length() != 36) { - throw new IllegalArgumentException( - "String representation of SentryId has either 32 (UUID no dashes) " - + "or 36 characters long (completed UUID). Received: " - + sentryIdString); - } return UUID.fromString(sentryIdString); } From 59068b8113fee1849aece589b218ba9f04f44af1 Mon Sep 17 00:00:00 2001 From: Lukas Bloder Date: Tue, 1 Oct 2024 16:54:01 +0200 Subject: [PATCH 4/5] Update sentry/src/main/java/io/sentry/SpanId.java Co-authored-by: Alexander Dinauer --- sentry/src/main/java/io/sentry/SpanId.java | 1 + 1 file changed, 1 insertion(+) diff --git a/sentry/src/main/java/io/sentry/SpanId.java b/sentry/src/main/java/io/sentry/SpanId.java index e07c643fb0..8c68d4c963 100644 --- a/sentry/src/main/java/io/sentry/SpanId.java +++ b/sentry/src/main/java/io/sentry/SpanId.java @@ -12,6 +12,7 @@ public final class SpanId implements JsonSerializable { private final @NotNull LazyEvaluator lazyValue; public SpanId(final @NotNull String value) { + Objects.requireNonNull(value, "value is required") this.lazyValue = new LazyEvaluator<>(() -> value); } From d552d04176c7c77824a680b0f44fce83d1572469 Mon Sep 17 00:00:00 2001 From: Sentry Github Bot Date: Tue, 1 Oct 2024 14:57:53 +0000 Subject: [PATCH 5/5] Format code --- sentry/src/main/java/io/sentry/protocol/SentryId.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sentry/src/main/java/io/sentry/protocol/SentryId.java b/sentry/src/main/java/io/sentry/protocol/SentryId.java index bf59c1592c..ad2bc45ef6 100644 --- a/sentry/src/main/java/io/sentry/protocol/SentryId.java +++ b/sentry/src/main/java/io/sentry/protocol/SentryId.java @@ -33,9 +33,9 @@ public SentryId(@Nullable UUID uuid) { public SentryId(final @NotNull String sentryIdString) { if (sentryIdString.length() != 32 && sentryIdString.length() != 36) { throw new IllegalArgumentException( - "String representation of SentryId has either 32 (UUID no dashes) " - + "or 36 characters long (completed UUID). Received: " - + sentryIdString); + "String representation of SentryId has either 32 (UUID no dashes) " + + "or 36 characters long (completed UUID). Received: " + + sentryIdString); } this.lazyValue = new LazyEvaluator<>(() -> fromStringSentryId(StringUtils.normalizeUUID(sentryIdString)));