Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy uuid generation for SentryId and SpanId #3770

Merged
merged 18 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Use `<meta-data android:name="io.sentry.force-init" android:value="true" />` to ensure Sentry Android auto init is not easily overwritten
- Attach request body for `application/x-www-form-urlencoded` requests in Spring ([#3731](https://github.com/getsentry/sentry-java/pull/3731))
- Previously request body was only attached for `application/json` requests
- Lazy uuid generation for SentryId and SpanId ([#3770](https://github.com/getsentry/sentry-java/pull/3770))
- Support `graphql-java` v22 via a new module `sentry-graphql-22` ([#3740](https://github.com/getsentry/sentry-java/pull/3740))
- If you are using `graphql-java` v21 or earlier, you can use the `sentry-graphql` module
- For `graphql-java` v22 and newer please use the `sentry-graphql-22` module
Expand Down
1 change: 1 addition & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -6374,6 +6374,7 @@ public final class io/sentry/util/SpanUtils {
}

public final class io/sentry/util/StringUtils {
public static final field PROPER_NIL_UUID Ljava/lang/String;
public static fun byteCountToString (J)Ljava/lang/String;
public static fun calculateStringHash (Ljava/lang/String;Lio/sentry/ILogger;)Ljava/lang/String;
public static fun camelCase (Ljava/lang/String;)Ljava/lang/String;
Expand Down
31 changes: 18 additions & 13 deletions sentry/src/main/java/io/sentry/SpanId.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
package io.sentry;

import io.sentry.util.Objects;
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(new UUID(0, 0));
public static final SpanId EMPTY_ID = new SpanId(PROPER_NIL_UUID);

private final @NotNull String value;
private final @NotNull LazyEvaluator<String> lazyValue;

public SpanId(final @NotNull String value) {
this.value = Objects.requireNonNull(value, "value is required");
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
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

@Override
public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger logger)
throws IOException {
writer.value(value);
writer.value(lazyValue.getValue());
}

// JsonElementDeserializer
Expand Down
32 changes: 18 additions & 14 deletions sentry/src/main/java/io/sentry/protocol/SentryId.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,58 @@
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;
import org.jetbrains.annotations.NotNull;
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<UUID> 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("-", "");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This toString is called from multiple places. since uuid wont be changes. it is good to cache that. but also this toString will do unnecessary calculations.
Please consider holding toString value instead of uuid similar to SpanId or also cache the toString result.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is more related to #3772
But if toString is called right after creating SentryId, it wont help much of delaying this UUID cost.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adinauer -> Please see comment from @kozaxinan

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we should measure impact of this PR. I'm afraid this doesn't have the desired effect due to toString(), hashCode and equals being called early. Caching the value for toString() might have a bigger impact, thanks for the suggestion.
Changing how we use IDs (i.e. only generate them when sending) will be a bigger change and I'm not entirely certain it's possible. Offering a configurable ID generation mechanism might be the better approach. We'll discuss internally and update once we know more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kozaxinan Good point, I'll rework SentryId to cache the resulting String instead of the UUID object.

As mentioned by @adinauer, measuring the performance impact of this change has yet to be done.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice this will allow more improvement. Will this be ported to version 7?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kozaxinan we're planning to explore multiple options to improve the SDK. We can check which of those make sense to port to v7 when we know which ones we'll keep.

}

@Override
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) {
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public final class StringUtils {

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

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

private StringUtils() {}
Expand Down
Loading