Skip to content

Commit 2601edf

Browse files
authored
Merge debd140 into 8c90f98
2 parents 8c90f98 + debd140 commit 2601edf

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- Use `<meta-data android:name="io.sentry.force-init" android:value="true" />` to ensure Sentry Android auto init is not easily overwritten
2424
- Attach request body for `application/x-www-form-urlencoded` requests in Spring ([#3731](https://github.com/getsentry/sentry-java/pull/3731))
2525
- Previously request body was only attached for `application/json` requests
26+
- Lazy uuid generation for SentryId and SpanId ([#3770](https://github.com/getsentry/sentry-java/pull/3770))
2627
- Support `graphql-java` v22 via a new module `sentry-graphql-22` ([#3740](https://github.com/getsentry/sentry-java/pull/3740))
2728
- If you are using `graphql-java` v21 or earlier, you can use the `sentry-graphql` module
2829
- For `graphql-java` v22 and newer please use the `sentry-graphql-22` module

sentry/api/sentry.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6374,6 +6374,7 @@ public final class io/sentry/util/SpanUtils {
63746374
}
63756375

63766376
public final class io/sentry/util/StringUtils {
6377+
public static final field PROPER_NIL_UUID Ljava/lang/String;
63776378
public static fun byteCountToString (J)Ljava/lang/String;
63786379
public static fun calculateStringHash (Ljava/lang/String;Lio/sentry/ILogger;)Ljava/lang/String;
63796380
public static fun camelCase (Ljava/lang/String;)Ljava/lang/String;

sentry/src/main/java/io/sentry/SpanId.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,57 @@
11
package io.sentry;
22

3-
import io.sentry.util.Objects;
3+
import static io.sentry.util.StringUtils.PROPER_NIL_UUID;
4+
5+
import io.sentry.util.LazyEvaluator;
46
import io.sentry.util.StringUtils;
57
import java.io.IOException;
8+
import java.util.Objects;
69
import java.util.UUID;
710
import org.jetbrains.annotations.NotNull;
811

912
public final class SpanId implements JsonSerializable {
10-
public static final SpanId EMPTY_ID = new SpanId(new UUID(0, 0));
13+
public static final SpanId EMPTY_ID = new SpanId(PROPER_NIL_UUID);
1114

12-
private final @NotNull String value;
15+
private final @NotNull LazyEvaluator<String> lazyValue;
1316

1417
public SpanId(final @NotNull String value) {
15-
this.value = Objects.requireNonNull(value, "value is required");
18+
Objects.requireNonNull(value, "value is required");
19+
this.lazyValue = new LazyEvaluator<>(() -> value);
1620
}
1721

1822
public SpanId() {
19-
this(UUID.randomUUID());
20-
}
21-
22-
private SpanId(final @NotNull UUID uuid) {
23-
this(StringUtils.normalizeUUID(uuid.toString()).replace("-", "").substring(0, 16));
23+
this.lazyValue =
24+
new LazyEvaluator<>(
25+
() ->
26+
StringUtils.normalizeUUID(UUID.randomUUID().toString())
27+
.replace("-", "")
28+
.substring(0, 16));
2429
}
2530

2631
@Override
2732
public boolean equals(Object o) {
2833
if (this == o) return true;
2934
if (o == null || getClass() != o.getClass()) return false;
3035
SpanId spanId = (SpanId) o;
31-
return value.equals(spanId.value);
36+
return lazyValue.getValue().equals(spanId.lazyValue.getValue());
3237
}
3338

3439
@Override
3540
public int hashCode() {
36-
return value.hashCode();
41+
return lazyValue.getValue().hashCode();
3742
}
3843

3944
@Override
4045
public String toString() {
41-
return this.value;
46+
return lazyValue.getValue();
4247
}
4348

4449
// JsonElementSerializer
4550

4651
@Override
4752
public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger logger)
4853
throws IOException {
49-
writer.value(value);
54+
writer.value(lazyValue.getValue());
5055
}
5156

5257
// JsonElementDeserializer

sentry/src/main/java/io/sentry/protocol/SentryId.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,58 @@
55
import io.sentry.JsonSerializable;
66
import io.sentry.ObjectReader;
77
import io.sentry.ObjectWriter;
8+
import io.sentry.util.LazyEvaluator;
89
import io.sentry.util.StringUtils;
910
import java.io.IOException;
1011
import java.util.UUID;
1112
import org.jetbrains.annotations.NotNull;
1213
import org.jetbrains.annotations.Nullable;
1314

1415
public final class SentryId implements JsonSerializable {
15-
private final @NotNull UUID uuid;
1616

1717
public static final SentryId EMPTY_ID = new SentryId(new UUID(0, 0));
1818

19+
private final @NotNull LazyEvaluator<UUID> lazyValue;
20+
1921
public SentryId() {
2022
this((UUID) null);
2123
}
2224

2325
public SentryId(@Nullable UUID uuid) {
24-
if (uuid == null) {
25-
uuid = UUID.randomUUID();
26+
if (uuid != null) {
27+
this.lazyValue = new LazyEvaluator<>(() -> uuid);
28+
} else {
29+
this.lazyValue = new LazyEvaluator<>(UUID::randomUUID);
2630
}
27-
this.uuid = uuid;
2831
}
2932

3033
public SentryId(final @NotNull String sentryIdString) {
31-
this.uuid = fromStringSentryId(StringUtils.normalizeUUID(sentryIdString));
34+
if (sentryIdString.length() != 32 && sentryIdString.length() != 36) {
35+
throw new IllegalArgumentException(
36+
"String representation of SentryId has either 32 (UUID no dashes) "
37+
+ "or 36 characters long (completed UUID). Received: "
38+
+ sentryIdString);
39+
}
40+
this.lazyValue =
41+
new LazyEvaluator<>(() -> fromStringSentryId(StringUtils.normalizeUUID(sentryIdString)));
3242
}
3343

3444
@Override
3545
public String toString() {
36-
return StringUtils.normalizeUUID(uuid.toString()).replace("-", "");
46+
return StringUtils.normalizeUUID(lazyValue.getValue().toString()).replace("-", "");
3747
}
3848

3949
@Override
4050
public boolean equals(final @Nullable Object o) {
4151
if (this == o) return true;
4252
if (o == null || getClass() != o.getClass()) return false;
4353
SentryId sentryId = (SentryId) o;
44-
return uuid.compareTo(sentryId.uuid) == 0;
54+
return lazyValue.getValue().compareTo(sentryId.lazyValue.getValue()) == 0;
4555
}
4656

4757
@Override
4858
public int hashCode() {
49-
return uuid.hashCode();
59+
return lazyValue.getValue().hashCode();
5060
}
5161

5262
private @NotNull UUID fromStringSentryId(@NotNull String sentryIdString) {
@@ -60,12 +70,6 @@ public int hashCode() {
6070
.insert(23, "-")
6171
.toString();
6272
}
63-
if (sentryIdString.length() != 36) {
64-
throw new IllegalArgumentException(
65-
"String representation of SentryId has either 32 (UUID no dashes) "
66-
+ "or 36 characters long (completed UUID). Received: "
67-
+ sentryIdString);
68-
}
6973

7074
return UUID.fromString(sentryIdString);
7175
}

sentry/src/main/java/io/sentry/util/StringUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public final class StringUtils {
2020

2121
private static final Charset UTF_8 = Charset.forName("UTF-8");
2222

23+
public static final String PROPER_NIL_UUID = "00000000-0000-0000-0000-000000000000";
2324
private static final String CORRUPTED_NIL_UUID = "0000-0000";
24-
private static final String PROPER_NIL_UUID = "00000000-0000-0000-0000-000000000000";
2525
private static final @NotNull Pattern PATTERN_WORD_SNAKE_CASE = Pattern.compile("[\\W_]+");
2626

2727
private StringUtils() {}

0 commit comments

Comments
 (0)