Skip to content

Commit c5ccd8a

Browse files
authored
Make user segment a top level property (#2257)
1 parent 743134a commit c5ccd8a

File tree

15 files changed

+143
-42
lines changed

15 files changed

+143
-42
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Features
6+
7+
- Make user segment a top level property ([#2257](https://github.com/getsentry/sentry-java/pull/2257))
8+
- Replace user `other` with `data` ([#2258](https://github.com/getsentry/sentry-java/pull/2258))
9+
310
## 6.5.0-beta.1
411

512
### Features

sentry-spring/src/main/java/io/sentry/spring/SentryUserFilter.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ private void apply(final @NotNull User existingUser, final @Nullable User userFr
6262
Optional.ofNullable(userFromProvider.getId()).ifPresent(existingUser::setId);
6363
Optional.ofNullable(userFromProvider.getIpAddress()).ifPresent(existingUser::setIpAddress);
6464
Optional.ofNullable(userFromProvider.getUsername()).ifPresent(existingUser::setUsername);
65-
if (userFromProvider.getOthers() != null && !userFromProvider.getOthers().isEmpty()) {
66-
Map<String, String> existingUserOthers = existingUser.getOthers();
67-
if (existingUserOthers == null) {
68-
existingUserOthers = new ConcurrentHashMap<>();
65+
if (userFromProvider.getData() != null && !userFromProvider.getData().isEmpty()) {
66+
Map<String, String> existingUserData = existingUser.getData();
67+
if (existingUserData == null) {
68+
existingUserData = new ConcurrentHashMap<>();
6969
}
70-
for (final Map.Entry<String, String> entry : userFromProvider.getOthers().entrySet()) {
71-
existingUserOthers.put(entry.getKey(), entry.getValue());
70+
for (final Map.Entry<String, String> entry : userFromProvider.getData().entrySet()) {
71+
existingUserData.put(entry.getKey(), entry.getValue());
7272
}
73-
existingUser.setOthers(existingUserOthers);
73+
existingUser.setData(existingUserData);
7474
}
7575
}
7676
}

sentry/api/sentry.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3017,17 +3017,21 @@ public final class io/sentry/protocol/TransactionNameSource : java/lang/Enum {
30173017
public final class io/sentry/protocol/User : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
30183018
public fun <init> ()V
30193019
public fun <init> (Lio/sentry/protocol/User;)V
3020+
public fun getData ()Ljava/util/Map;
30203021
public fun getEmail ()Ljava/lang/String;
30213022
public fun getId ()Ljava/lang/String;
30223023
public fun getIpAddress ()Ljava/lang/String;
30233024
public fun getOthers ()Ljava/util/Map;
3025+
public fun getSegment ()Ljava/lang/String;
30243026
public fun getUnknown ()Ljava/util/Map;
30253027
public fun getUsername ()Ljava/lang/String;
30263028
public fun serialize (Lio/sentry/JsonObjectWriter;Lio/sentry/ILogger;)V
3029+
public fun setData (Ljava/util/Map;)V
30273030
public fun setEmail (Ljava/lang/String;)V
30283031
public fun setId (Ljava/lang/String;)V
30293032
public fun setIpAddress (Ljava/lang/String;)V
30303033
public fun setOthers (Ljava/util/Map;)V
3034+
public fun setSegment (Ljava/lang/String;)V
30313035
public fun setUnknown (Ljava/util/Map;)V
30323036
public fun setUsername (Ljava/lang/String;)V
30333037
}
@@ -3039,10 +3043,12 @@ public final class io/sentry/protocol/User$Deserializer : io/sentry/JsonDeserial
30393043
}
30403044

30413045
public final class io/sentry/protocol/User$JsonKeys {
3046+
public static final field DATA Ljava/lang/String;
30423047
public static final field EMAIL Ljava/lang/String;
30433048
public static final field ID Ljava/lang/String;
30443049
public static final field IP_ADDRESS Ljava/lang/String;
30453050
public static final field OTHER Ljava/lang/String;
3051+
public static final field SEGMENT Ljava/lang/String;
30463052
public static final field USERNAME Ljava/lang/String;
30473053
public fun <init> ()V
30483054
}

sentry/src/main/java/io/sentry/Baggage.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,13 @@ public void setValuesFromTransaction(
292292
}
293293

294294
private static @Nullable String getSegment(final @NotNull User user) {
295-
final Map<String, String> others = user.getOthers();
296-
if (others != null) {
297-
return others.get("segment");
295+
if (user.getSegment() != null) {
296+
return user.getSegment();
297+
}
298+
299+
final Map<String, String> userData = user.getData();
300+
if (userData != null) {
301+
return userData.get("segment");
298302
} else {
299303
return null;
300304
}

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

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ public final class User implements JsonUnknown, JsonSerializable {
3131
/** Username of the user. */
3232
private @Nullable String username;
3333

34+
private @Nullable String segment;
35+
3436
/** Remote IP address of the user. */
3537
private @Nullable String ipAddress;
3638

3739
/**
3840
* Additional arbitrary fields, as stored in the database (and sometimes as sent by clients). All
3941
* data from `self.other` should end up here after store normalization.
4042
*/
41-
private @Nullable Map<String, @NotNull String> other;
43+
private @Nullable Map<String, @NotNull String> data;
4244

4345
/** unknown fields, only internal usage. */
4446
private @Nullable Map<String, @NotNull Object> unknown;
@@ -50,7 +52,8 @@ public User(final @NotNull User user) {
5052
this.username = user.username;
5153
this.id = user.id;
5254
this.ipAddress = user.ipAddress;
53-
this.other = CollectionUtils.newConcurrentHashMap(user.other);
55+
this.segment = user.segment;
56+
this.data = CollectionUtils.newConcurrentHashMap(user.data);
5457
this.unknown = CollectionUtils.newConcurrentHashMap(user.unknown);
5558
}
5659

@@ -108,6 +111,24 @@ public void setUsername(final @Nullable String username) {
108111
this.username = username;
109112
}
110113

114+
/**
115+
* Gets the segment of the user.
116+
*
117+
* @return the user segment.
118+
*/
119+
public @Nullable String getSegment() {
120+
return segment;
121+
}
122+
123+
/**
124+
* Sets the segment of the user.
125+
*
126+
* @param segment the segment.
127+
*/
128+
public void setSegment(final @Nullable String segment) {
129+
this.segment = segment;
130+
}
131+
111132
/**
112133
* Gets the IP address of the user.
113134
*
@@ -129,19 +150,43 @@ public void setIpAddress(final @Nullable String ipAddress) {
129150
/**
130151
* Gets other user related data.
131152
*
153+
* @deprecated use {{@link User#getData()}} instead
132154
* @return the other user data.
133155
*/
156+
@Deprecated
157+
@SuppressWarnings("InlineMeSuggester")
134158
public @Nullable Map<String, @NotNull String> getOthers() {
135-
return other;
159+
return getData();
136160
}
137161

138162
/**
139163
* Sets other user related data.
140164
*
165+
* @deprecated use {{@link User#setData(Map)}} instead
141166
* @param other the other user related data..
142167
*/
168+
@Deprecated
169+
@SuppressWarnings("InlineMeSuggester")
143170
public void setOthers(final @Nullable Map<String, @NotNull String> other) {
144-
this.other = CollectionUtils.newConcurrentHashMap(other);
171+
this.setData(other);
172+
}
173+
174+
/**
175+
* Gets additional arbitrary fields of the user.
176+
*
177+
* @return the other user data.
178+
*/
179+
public @Nullable Map<String, @NotNull String> getData() {
180+
return data;
181+
}
182+
183+
/**
184+
* Sets additional arbitrary fields of the user.
185+
*
186+
* @param data the other user related data..
187+
*/
188+
public void setData(final @Nullable Map<String, @NotNull String> data) {
189+
this.data = CollectionUtils.newConcurrentHashMap(data);
145190
}
146191

147192
// region json
@@ -161,8 +206,10 @@ public static final class JsonKeys {
161206
public static final String EMAIL = "email";
162207
public static final String ID = "id";
163208
public static final String USERNAME = "username";
209+
public static final String SEGMENT = "segment";
164210
public static final String IP_ADDRESS = "ip_address";
165211
public static final String OTHER = "other";
212+
public static final String DATA = "data";
166213
}
167214

168215
@Override
@@ -178,11 +225,14 @@ public void serialize(@NotNull JsonObjectWriter writer, @NotNull ILogger logger)
178225
if (username != null) {
179226
writer.name(JsonKeys.USERNAME).value(username);
180227
}
228+
if (segment != null) {
229+
writer.name(JsonKeys.SEGMENT).value(segment);
230+
}
181231
if (ipAddress != null) {
182232
writer.name(JsonKeys.IP_ADDRESS).value(ipAddress);
183233
}
184-
if (other != null) {
185-
writer.name(JsonKeys.OTHER).value(logger, other);
234+
if (data != null) {
235+
writer.name(JsonKeys.DATA).value(logger, data);
186236
}
187237
if (unknown != null) {
188238
for (String key : unknown.keySet()) {
@@ -214,14 +264,25 @@ public static final class Deserializer implements JsonDeserializer<User> {
214264
case JsonKeys.USERNAME:
215265
user.username = reader.nextStringOrNull();
216266
break;
267+
case JsonKeys.SEGMENT:
268+
user.segment = reader.nextStringOrNull();
269+
break;
217270
case JsonKeys.IP_ADDRESS:
218271
user.ipAddress = reader.nextStringOrNull();
219272
break;
220-
case JsonKeys.OTHER:
221-
user.other =
273+
case JsonKeys.DATA:
274+
user.data =
222275
CollectionUtils.newConcurrentHashMap(
223276
(Map<String, String>) reader.nextObjectOrNull());
224277
break;
278+
case JsonKeys.OTHER:
279+
// restore `other` from legacy JSON
280+
if (user.data == null || user.data.isEmpty()) {
281+
user.data =
282+
CollectionUtils.newConcurrentHashMap(
283+
(Map<String, String>) reader.nextObjectOrNull());
284+
}
285+
break;
225286
default:
226287
if (unknown == null) {
227288
unknown = new ConcurrentHashMap<>();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class ScopeTest {
3030
user.id = "123"
3131
user.ipAddress = "123.x"
3232
user.username = "userName"
33-
val others = mutableMapOf(Pair("others", "others"))
34-
user.others = others
33+
val data = mutableMapOf(Pair("data", "data"))
34+
user.data = data
3535

3636
scope.user = user
3737

sentry/src/test/java/io/sentry/protocol/UserSerializationTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ class UserSerializationTest {
4343
assertEquals(expectedJson, actualJson)
4444
}
4545

46+
@Test
47+
fun `deserialize legacy`() {
48+
val inputJson = sanitizedFile("json/user_legacy.json")
49+
val expectedJson = sanitizedFile("json/user.json")
50+
val actual = deserialize(inputJson)
51+
val actualJson = serialize(actual)
52+
assertEquals(expectedJson, actualJson)
53+
}
54+
4655
// Helper
4756

4857
private fun sanitizedFile(path: String): String {

sentry/src/test/java/io/sentry/protocol/UserTest.kt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class UserTest {
1717
assertNotNull(clone)
1818
assertNotSame(user, clone)
1919

20-
assertNotSame(user.others, clone.others)
20+
assertNotSame(user.data, clone.data)
2121

2222
assertNotSame(user.unknown, clone.unknown)
2323
}
@@ -31,7 +31,8 @@ class UserTest {
3131
assertEquals("123", clone.id)
3232
assertEquals("123.x", clone.ipAddress)
3333
assertEquals("userName", clone.username)
34-
assertEquals("others", clone.others!!["others"])
34+
assertEquals("userSegment", clone.segment)
35+
assertEquals("data", clone.data!!["data"])
3536
assertEquals("unknown", clone.unknown!!["unknown"])
3637
}
3738

@@ -44,36 +45,38 @@ class UserTest {
4445
user.id = "456"
4546
user.ipAddress = "456.x"
4647
user.username = "newUserName"
47-
user.others!!["others"] = "newOthers"
48-
user.others!!["anotherOne"] = "anotherOne"
48+
user.segment = "newUserSegment"
49+
user.data!!["data"] = "newOthers"
50+
user.data!!["anotherOne"] = "anotherOne"
4951
val newUnknown = mapOf(Pair("unknown", "newUnknown"), Pair("otherUnknown", "otherUnknown"))
5052
user.setUnknown(newUnknown)
5153

5254
assertEquals("[email protected]", clone.email)
5355
assertEquals("123", clone.id)
5456
assertEquals("123.x", clone.ipAddress)
5557
assertEquals("userName", clone.username)
56-
assertEquals("others", clone.others!!["others"])
57-
assertEquals(1, clone.others!!.size)
58+
assertEquals("userSegment", clone.segment)
59+
assertEquals("data", clone.data!!["data"])
60+
assertEquals(1, clone.data!!.size)
5861
assertEquals("unknown", clone.unknown!!["unknown"])
5962
assertEquals(1, clone.unknown!!.size)
6063
}
6164

6265
@Test
63-
fun `setting null others do not crash`() {
66+
fun `setting null data do not crash`() {
6467
val user = createUser()
65-
user.others = null
68+
user.data = null
6669

67-
assertNull(user.others)
70+
assertNull(user.data)
6871
}
6972

7073
@Test
71-
fun `when setOther receives immutable map as an argument, its still possible to add more others to the user`() {
74+
fun `when setOther receives immutable map as an argument, its still possible to add more data to the user`() {
7275
val user = User().apply {
73-
others = Collections.unmodifiableMap(mapOf("key1" to "value1"))
74-
others!!["key2"] = "value2"
76+
data = Collections.unmodifiableMap(mapOf("key1" to "value1"))
77+
data!!["key2"] = "value2"
7578
}
76-
assertNotNull(user.others) {
79+
assertNotNull(user.data) {
7780
assertEquals(mapOf("key1" to "value1", "key2" to "value2"), it)
7881
}
7982
}
@@ -84,8 +87,9 @@ class UserTest {
8487
id = "123"
8588
ipAddress = "123.x"
8689
username = "userName"
87-
val others = mutableMapOf(Pair("others", "others"))
88-
setOthers(others)
90+
segment = "userSegment"
91+
val data = mutableMapOf(Pair("data", "data"))
92+
setData(data)
8993
val unknown = mapOf(Pair("unknown", "unknown"))
9094
setUnknown(unknown)
9195
}

sentry/src/test/resources/json/sentry_base_event.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
"id": "efb2084b-1871-4b59-8897-b4bd9f196a01",
160160
"username": "60c05dff-7140-4d94-9a61-c9cdd9ca9b96",
161161
"ip_address": "51d22b77-f663-4dbe-8103-8b749d1d9a48",
162-
"other":
162+
"data":
163163
{
164164
"dc2813d0-0f66-4a3f-a995-71268f61a8fa": "991659ad-7c59-4dd3-bb89-0bd5c74014bd"
165165
}

sentry/src/test/resources/json/sentry_event.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@
306306
"id": "efb2084b-1871-4b59-8897-b4bd9f196a01",
307307
"username": "60c05dff-7140-4d94-9a61-c9cdd9ca9b96",
308308
"ip_address": "51d22b77-f663-4dbe-8103-8b749d1d9a48",
309-
"other":
309+
"data":
310310
{
311311
"dc2813d0-0f66-4a3f-a995-71268f61a8fa": "991659ad-7c59-4dd3-bb89-0bd5c74014bd"
312312
}

0 commit comments

Comments
 (0)