Skip to content

Commit ac12613

Browse files
lbloderadinauerstefanosianogetsentry-bot
authored
SessionID as string (#3834)
* lazy uuid generation for SentryId and SpanId * add changelog * add tests for lazy init, rework SentryId to cache string result * fix changelog * Update sentry/src/main/java/io/sentry/protocol/SentryId.java Co-authored-by: Stefano <[email protected]> * Update sentry/src/main/java/io/sentry/protocol/SentryId.java Co-authored-by: Stefano <[email protected]> * Update sentry/src/main/java/io/sentry/protocol/SentryId.java Co-authored-by: Stefano <[email protected]> * Update sentry/src/main/java/io/sentry/protocol/SentryId.java Co-authored-by: Stefano <[email protected]> * Update sentry/src/main/java/io/sentry/protocol/SentryId.java Co-authored-by: Stefano <[email protected]> * Format code * Add object comparison to SpanFrameMetricsCollector only check for time difference of .equals if objects are not the same * add proposed SentryUUID class * [WIP] use SentryUUID instead of UUID.random().toString * Format code * split SentryUUID internals into separate classes for easier attribution * add changelog * replace uuid with string for sessionId, use SentryUUID for generating sessionId * add changelog * Format code * fix tests * fix SpanId.EMPTY_ID * fix tests * Format code * move changelog entry to breaking changes section, add more detail * test normalized is never called in no-arg constructor, only called once when invoking with string * add log if invalid session id is being deserialized * fix test * Format code * use new Random Generator * fix Sentry Empty ID, replace dashes if 36 char uuid String is passed to SentryId constructor * fix Tests for SpanId, add tests for SentryUUID and UUIDStringUtils * allow 36 and 32 character session ids --------- Co-authored-by: Alexander Dinauer <[email protected]> Co-authored-by: Stefano <[email protected]> Co-authored-by: Sentry Github Bot <[email protected]>
1 parent 741807a commit ac12613

16 files changed

+74
-72
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
### Breaking Changes
66

7+
- Use String instead of UUID for SessionId ([#3834](https://github.com/getsentry/sentry-java/pull/3834))
8+
- The `Session` constructor now takes a `String` instead of a `UUID` for the `sessionId` parameter.
9+
- `Session.getSessionId()` now returns a `String` instead of a `UUID`.
710
- The Android minSdk level for all Android modules is now 21 ([#3852](https://github.com/getsentry/sentry-java/pull/3852))
811
- The minSdk level for sentry-android-ndk changed from 19 to 21 ([#3851](https://github.com/getsentry/sentry-java/pull/3851))
912

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class LifecycleWatcherTest {
195195
DateUtils.getCurrentDateTime(),
196196
0,
197197
"abc",
198-
UUID.fromString("3c1ffc32-f68f-4af2-a1ee-dd72f4d62d17"),
198+
"3c1ffc32-f68f-4af2-a1ee-dd72f4d62d17",
199199
true,
200200
0,
201201
10.0,
@@ -222,7 +222,7 @@ class LifecycleWatcherTest {
222222
DateUtils.getDateTime(-1),
223223
0,
224224
"abc",
225-
UUID.fromString("3c1ffc32-f68f-4af2-a1ee-dd72f4d62d17"),
225+
"3c1ffc32-f68f-4af2-a1ee-dd72f4d62d17",
226226
true,
227227
0,
228228
10.0,
@@ -263,7 +263,7 @@ class LifecycleWatcherTest {
263263
DateUtils.getCurrentDateTime(),
264264
0,
265265
"abc",
266-
UUID.fromString("3c1ffc32-f68f-4af2-a1ee-dd72f4d62d17"),
266+
"3c1ffc32-f68f-4af2-a1ee-dd72f4d62d17",
267267
true,
268268
0,
269269
10.0,

sentry/api/sentry.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3277,7 +3277,7 @@ public final class io/sentry/SentryWrapper {
32773277
}
32783278

32793279
public final class io/sentry/Session : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
3280-
public fun <init> (Lio/sentry/Session$State;Ljava/util/Date;Ljava/util/Date;ILjava/lang/String;Ljava/util/UUID;Ljava/lang/Boolean;Ljava/lang/Long;Ljava/lang/Double;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
3280+
public fun <init> (Lio/sentry/Session$State;Ljava/util/Date;Ljava/util/Date;ILjava/lang/String;Ljava/lang/String;Ljava/lang/Boolean;Ljava/lang/Long;Ljava/lang/Double;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
32813281
public fun <init> (Ljava/lang/String;Lio/sentry/protocol/User;Ljava/lang/String;Ljava/lang/String;)V
32823282
public fun clone ()Lio/sentry/Session;
32833283
public synthetic fun clone ()Ljava/lang/Object;
@@ -3292,7 +3292,7 @@ public final class io/sentry/Session : io/sentry/JsonSerializable, io/sentry/Jso
32923292
public fun getIpAddress ()Ljava/lang/String;
32933293
public fun getRelease ()Ljava/lang/String;
32943294
public fun getSequence ()Ljava/lang/Long;
3295-
public fun getSessionId ()Ljava/util/UUID;
3295+
public fun getSessionId ()Ljava/lang/String;
32963296
public fun getStarted ()Ljava/util/Date;
32973297
public fun getStatus ()Lio/sentry/Session$State;
32983298
public fun getTimestamp ()Ljava/util/Date;

sentry/src/main/java/io/sentry/Session.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.Date;
99
import java.util.Locale;
1010
import java.util.Map;
11-
import java.util.UUID;
1211
import java.util.concurrent.ConcurrentHashMap;
1312
import java.util.concurrent.atomic.AtomicInteger;
1413
import org.jetbrains.annotations.ApiStatus;
@@ -38,7 +37,7 @@ public enum State {
3837
private final @Nullable String distinctId;
3938

4039
/** the SessionId, sid */
41-
private final @Nullable UUID sessionId;
40+
private final @Nullable String sessionId;
4241

4342
/** The session init flag */
4443
private @Nullable Boolean init;
@@ -79,7 +78,7 @@ public Session(
7978
final @Nullable Date timestamp,
8079
final int errorCount,
8180
final @Nullable String distinctId,
82-
final @Nullable UUID sessionId,
81+
final @Nullable String sessionId,
8382
final @Nullable Boolean init,
8483
final @Nullable Long sequence,
8584
final @Nullable Double duration,
@@ -115,7 +114,7 @@ public Session(
115114
DateUtils.getCurrentDateTime(),
116115
0,
117116
distinctId,
118-
UUID.randomUUID(),
117+
SentryUUID.generateSentryId(),
119118
true,
120119
null,
121120
null,
@@ -142,7 +141,7 @@ public boolean isTerminated() {
142141
return distinctId;
143142
}
144143

145-
public @Nullable UUID getSessionId() {
144+
public @Nullable String getSessionId() {
146145
return sessionId;
147146
}
148147

@@ -366,7 +365,7 @@ public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger
366365
throws IOException {
367366
writer.beginObject();
368367
if (sessionId != null) {
369-
writer.name(JsonKeys.SID).value(sessionId.toString());
368+
writer.name(JsonKeys.SID).value(sessionId);
370369
}
371370
if (distinctId != null) {
372371
writer.name(JsonKeys.DID).value(distinctId);
@@ -435,7 +434,7 @@ public static final class Deserializer implements JsonDeserializer<Session> {
435434
Date timestamp = null;
436435
Integer errorCount = null; // @NotNull
437436
String distinctId = null;
438-
UUID sessionId = null;
437+
String sessionId = null;
439438
Boolean init = null;
440439
State status = null; // @NotNull
441440
Long sequence = null;
@@ -451,12 +450,11 @@ public static final class Deserializer implements JsonDeserializer<Session> {
451450
final String nextName = reader.nextName();
452451
switch (nextName) {
453452
case JsonKeys.SID:
454-
String sidString = null;
455-
try {
456-
sidString = reader.nextStringOrNull();
457-
sessionId = UUID.fromString(sidString);
458-
} catch (IllegalArgumentException e) {
459-
logger.log(SentryLevel.ERROR, "%s sid is not valid.", sidString);
453+
String sid = reader.nextStringOrNull();
454+
if (sid != null && (sid.length() == 36 || sid.length() == 32)) {
455+
sessionId = sid;
456+
} else {
457+
logger.log(SentryLevel.ERROR, "%s sid is not valid.", sid);
460458
}
461459
break;
462460
case JsonKeys.DID:

sentry/src/main/java/io/sentry/cache/CacheStrategy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.Arrays;
2929
import java.util.Iterator;
3030
import java.util.List;
31-
import java.util.UUID;
3231
import org.jetbrains.annotations.NotNull;
3332
import org.jetbrains.annotations.Nullable;
3433

@@ -242,7 +241,7 @@ private boolean isValidSession(final @NotNull Session session) {
242241
return false;
243242
}
244243

245-
final UUID sessionId = session.getSessionId();
244+
final String sessionId = session.getSessionId();
246245

247246
return sessionId != null;
248247
}

sentry/src/test/java/io/sentry/CombinedScopeViewTest.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import org.mockito.kotlin.same
1616
import org.mockito.kotlin.verify
1717
import org.mockito.kotlin.whenever
1818
import java.lang.RuntimeException
19-
import java.util.UUID
2019
import kotlin.test.Test
2120
import kotlin.test.assertEquals
2221
import kotlin.test.assertNotNull
@@ -933,17 +932,17 @@ class CombinedScopeViewTest {
933932
@Test
934933
fun `retrieves last event id from global scope`() {
935934
val combined = fixture.getSut()
936-
fixture.scope.lastEventId = SentryId(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2dc"))
937-
fixture.isolationScope.lastEventId = SentryId(UUID.fromString("d81d4e2e-bcf2-11e6-869b-7df92533d2dd"))
938-
fixture.globalScope.lastEventId = SentryId(UUID.fromString("e81d4e2e-bcf2-11e6-869b-7df92533d2de"))
935+
fixture.scope.lastEventId = SentryId("c81d4e2e-bcf2-11e6-869b-7df92533d2dc")
936+
fixture.isolationScope.lastEventId = SentryId("d81d4e2e-bcf2-11e6-869b-7df92533d2dd")
937+
fixture.globalScope.lastEventId = SentryId("e81d4e2e-bcf2-11e6-869b-7df92533d2de")
939938

940939
assertEquals("e81d4e2ebcf211e6869b7df92533d2de", combined.lastEventId.toString())
941940
}
942941

943942
@Test
944943
fun `sets last event id on all scopes`() {
945944
val combined = fixture.getSut()
946-
combined.lastEventId = SentryId(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db"))
945+
combined.lastEventId = SentryId("c81d4e2e-bcf2-11e6-869b-7df92533d2db")
947946

948947
assertEquals("c81d4e2ebcf211e6869b7df92533d2db", fixture.scope.lastEventId.toString())
949948
assertEquals("c81d4e2ebcf211e6869b7df92533d2db", fixture.isolationScope.lastEventId.toString())
@@ -953,9 +952,9 @@ class CombinedScopeViewTest {
953952
@Test
954953
fun `retrieves propagation context from default scope`() {
955954
val combined = fixture.getSut()
956-
fixture.scope.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2dc")) }
957-
fixture.isolationScope.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("d81d4e2e-bcf2-11e6-869b-7df92533d2dd")) }
958-
fixture.globalScope.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("e81d4e2e-bcf2-11e6-869b-7df92533d2de")) }
955+
fixture.scope.propagationContext = PropagationContext().also { it.traceId = SentryId("c81d4e2e-bcf2-11e6-869b-7df92533d2dc") }
956+
fixture.isolationScope.propagationContext = PropagationContext().also { it.traceId = SentryId("d81d4e2e-bcf2-11e6-869b-7df92533d2dd") }
957+
fixture.globalScope.propagationContext = PropagationContext().also { it.traceId = SentryId("e81d4e2e-bcf2-11e6-869b-7df92533d2de") }
959958

960959
assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType)
961960
assertEquals("d81d4e2ebcf211e6869b7df92533d2dd", combined.propagationContext.traceId.toString())
@@ -965,7 +964,7 @@ class CombinedScopeViewTest {
965964
fun `sets propagation context on default scope`() {
966965
val combined = fixture.getSut()
967966

968-
combined.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db")) }
967+
combined.propagationContext = PropagationContext().also { it.traceId = SentryId("c81d4e2e-bcf2-11e6-869b-7df92533d2db") }
969968

970969
assertEquals(ScopeType.ISOLATION, fixture.options.defaultScopeType)
971970
assertNotEquals("c81d4e2ebcf211e6869b7df92533d2db", fixture.scope.propagationContext.traceId.toString())
@@ -976,9 +975,9 @@ class CombinedScopeViewTest {
976975
@Test
977976
fun `withPropagationContext uses default scope`() {
978977
val combined = fixture.getSut()
979-
fixture.scope.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2dc")) }
980-
fixture.isolationScope.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("d81d4e2e-bcf2-11e6-869b-7df92533d2dd")) }
981-
fixture.globalScope.propagationContext = PropagationContext().also { it.traceId = SentryId(UUID.fromString("e81d4e2e-bcf2-11e6-869b-7df92533d2de")) }
978+
fixture.scope.propagationContext = PropagationContext().also { it.traceId = SentryId("c81d4e2e-bcf2-11e6-869b-7df92533d2dc") }
979+
fixture.isolationScope.propagationContext = PropagationContext().also { it.traceId = SentryId("d81d4e2e-bcf2-11e6-869b-7df92533d2dd") }
980+
fixture.globalScope.propagationContext = PropagationContext().also { it.traceId = SentryId("e81d4e2e-bcf2-11e6-869b-7df92533d2de") }
982981

983982
var capturedPropagationContext: PropagationContext? = null
984983
combined.withPropagationContext { propagationContext ->

sentry/src/test/java/io/sentry/JsonObjectSerializerTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import java.util.Calendar
1010
import java.util.Currency
1111
import java.util.Locale
1212
import java.util.TimeZone
13-
import java.util.UUID
1413
import java.util.concurrent.atomic.AtomicBoolean
1514
import java.util.concurrent.atomic.AtomicIntegerArray
1615

@@ -240,7 +239,7 @@ internal class JsonObjectSerializerTest {
240239

241240
@Test
242241
fun `serializing UUID`() {
243-
fixture.getSUT().serialize(fixture.writer, fixture.logger, UUID.fromString("828900a5-15dc-413f-8c17-6ef04d74e074"))
242+
fixture.getSUT().serialize(fixture.writer, fixture.logger, "828900a5-15dc-413f-8c17-6ef04d74e074")
244243
verify(fixture.writer).value("828900a5-15dc-413f-8c17-6ef04d74e074")
245244
}
246245

sentry/src/test/java/io/sentry/JsonReflectionObjectSerializerTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import java.net.URI
77
import java.util.Calendar
88
import java.util.Currency
99
import java.util.Locale
10-
import java.util.UUID
1110
import java.util.concurrent.atomic.AtomicBoolean
1211
import java.util.concurrent.atomic.AtomicIntegerArray
1312
import kotlin.test.assertEquals
@@ -318,7 +317,7 @@ class JsonReflectionObjectSerializerTest {
318317

319318
@Test
320319
fun `UUID is serialized`() {
321-
val actual = fixture.getSut().serialize(UUID.fromString("828900a5-15dc-413f-8c17-6ef04d74e074"), fixture.logger)
320+
val actual = fixture.getSut().serialize("828900a5-15dc-413f-8c17-6ef04d74e074", fixture.logger)
322321
assertEquals("828900a5-15dc-413f-8c17-6ef04d74e074", actual)
323322
}
324323

sentry/src/test/java/io/sentry/JsonSerializerTest.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,17 @@ class JsonSerializerTest {
371371
assertSessionData(expectedSession)
372372
}
373373

374+
@Test
375+
fun `session deserializes 32 character id`() {
376+
val sessionId = "c81d4e2ebcf211e6869b7df92533d2db"
377+
val session = createSessionMockData("c81d4e2ebcf211e6869b7df92533d2db")
378+
val jsonSession = serializeToString(session)
379+
// reversing, so we can assert values and not a json string
380+
val expectedSession = fixture.serializer.deserialize(StringReader(jsonSession), Session::class.java)
381+
382+
assertSessionData(expectedSession, "c81d4e2ebcf211e6869b7df92533d2db")
383+
}
384+
374385
@Test
375386
fun `When deserializing an Envelope, all the values should be set to the SentryEnvelope object`() {
376387
val jsonEnvelope = FileFromResources.invoke("envelope_session.txt")
@@ -1248,9 +1259,9 @@ class JsonSerializerTest {
12481259
assertEquals(replayRecording, deserializedRecording)
12491260
}
12501261

1251-
private fun assertSessionData(expectedSession: Session?) {
1262+
private fun assertSessionData(expectedSession: Session?, expectedSessionId: String = "c81d4e2e-bcf2-11e6-869b-7df92533d2db") {
12521263
assertNotNull(expectedSession)
1253-
assertEquals(UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db"), expectedSession.sessionId)
1264+
assertEquals(expectedSessionId, expectedSession.sessionId)
12541265
assertEquals("123", expectedSession.distinctId)
12551266
assertTrue(expectedSession.init!!)
12561267
assertEquals("2020-02-07T14:16:00.000Z", DateUtils.getTimestamp(expectedSession.started!!))
@@ -1280,14 +1291,14 @@ class JsonSerializerTest {
12801291
private fun generateEmptySentryEvent(date: Date = Date()): SentryEvent =
12811292
SentryEvent(date)
12821293

1283-
private fun createSessionMockData(): Session =
1294+
private fun createSessionMockData(sessionId: String = "c81d4e2e-bcf2-11e6-869b-7df92533d2db"): Session =
12841295
Session(
12851296
Session.State.Ok,
12861297
DateUtils.getDateTime("2020-02-07T14:16:00.000Z"),
12871298
DateUtils.getDateTime("2020-02-07T14:16:00.000Z"),
12881299
2,
12891300
"123",
1290-
UUID.fromString("c81d4e2e-bcf2-11e6-869b-7df92533d2db"),
1301+
sessionId,
12911302
true,
12921303
123456.toLong(),
12931304
6000.toDouble(),

sentry/src/test/java/io/sentry/OutboxSenderTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import java.io.FileNotFoundException
2020
import java.nio.file.Files
2121
import java.nio.file.Paths
2222
import java.util.Date
23-
import java.util.UUID
2423
import kotlin.test.Test
2524
import kotlin.test.assertEquals
2625
import kotlin.test.assertFalse
@@ -74,7 +73,7 @@ class OutboxSenderTest {
7473
@Test
7574
fun `when parser is EnvelopeReader and serializer returns SentryEvent, event captured, file is deleted `() {
7675
fixture.envelopeReader = EnvelopeReader(JsonSerializer(fixture.options))
77-
val expected = SentryEvent(SentryId(UUID.fromString("9ec79c33-ec99-42ab-8353-589fcb2e04dc")), Date())
76+
val expected = SentryEvent(SentryId("9ec79c33-ec99-42ab-8353-589fcb2e04dc"), Date())
7877
whenever(fixture.serializer.deserialize(any(), eq(SentryEvent::class.java))).thenReturn(expected)
7978
val sut = fixture.getSut()
8079
val path = getTempEnvelope()

0 commit comments

Comments
 (0)