Skip to content

Commit 858da6c

Browse files
adinauerclaude
andcommitted
feat: Support collections and arrays in log attribute type inference
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3791872 commit 858da6c

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
lines changed

sentry/api/sentry.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,6 +2813,7 @@ public final class io/sentry/SentryAppStartProfilingOptions$JsonKeys {
28132813
}
28142814

28152815
public final class io/sentry/SentryAttribute {
2816+
public static fun arrayAttribute (Ljava/lang/String;Ljava/util/Collection;)Lio/sentry/SentryAttribute;
28162817
public static fun booleanAttribute (Ljava/lang/String;Ljava/lang/Boolean;)Lio/sentry/SentryAttribute;
28172818
public static fun doubleAttribute (Ljava/lang/String;Ljava/lang/Double;)Lio/sentry/SentryAttribute;
28182819
public fun getName ()Ljava/lang/String;
@@ -2824,6 +2825,7 @@ public final class io/sentry/SentryAttribute {
28242825
}
28252826

28262827
public final class io/sentry/SentryAttributeType : java/lang/Enum {
2828+
public static final field ARRAY Lio/sentry/SentryAttributeType;
28272829
public static final field BOOLEAN Lio/sentry/SentryAttributeType;
28282830
public static final field DOUBLE Lio/sentry/SentryAttributeType;
28292831
public static final field INTEGER Lio/sentry/SentryAttributeType;

sentry/src/main/java/io/sentry/SentryAttribute.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.sentry;
22

3+
import java.util.Collection;
34
import org.jetbrains.annotations.NotNull;
45
import org.jetbrains.annotations.Nullable;
56

@@ -54,4 +55,9 @@ private SentryAttribute(
5455
final @NotNull String name, final @Nullable String value) {
5556
return new SentryAttribute(name, SentryAttributeType.STRING, value);
5657
}
58+
59+
public static @NotNull SentryAttribute arrayAttribute(
60+
final @NotNull String name, final @Nullable Collection<?> value) {
61+
return new SentryAttribute(name, SentryAttributeType.ARRAY, value);
62+
}
5763
}

sentry/src/main/java/io/sentry/SentryAttributeType.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.sentry;
22

33
import java.math.BigInteger;
4+
import java.util.Collection;
45
import java.util.Locale;
56
import java.util.concurrent.atomic.AtomicInteger;
67
import java.util.concurrent.atomic.AtomicLong;
@@ -11,7 +12,8 @@ public enum SentryAttributeType {
1112
STRING,
1213
BOOLEAN,
1314
INTEGER,
14-
DOUBLE;
15+
DOUBLE,
16+
ARRAY;
1517

1618
public @NotNull String apiName() {
1719
return name().toLowerCase(Locale.ROOT);
@@ -33,6 +35,9 @@ public enum SentryAttributeType {
3335
if (value instanceof Number) {
3436
return DOUBLE;
3537
}
38+
if (value instanceof Collection || (value != null && value.getClass().isArray())) {
39+
return ARRAY;
40+
}
3641
return STRING;
3742
}
3843
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,39 @@ class SentryAttributeTypeTest {
7777
fun `inferFrom returns STRING for null`() {
7878
assertEquals(SentryAttributeType.STRING, SentryAttributeType.inferFrom(null))
7979
}
80+
81+
@Test
82+
fun `inferFrom returns ARRAY for List of Strings`() {
83+
assertEquals(SentryAttributeType.ARRAY, SentryAttributeType.inferFrom(listOf("a", "b")))
84+
}
85+
86+
@Test
87+
fun `inferFrom returns ARRAY for List of Integers`() {
88+
assertEquals(SentryAttributeType.ARRAY, SentryAttributeType.inferFrom(listOf(1, 2, 3)))
89+
}
90+
91+
@Test
92+
fun `inferFrom returns ARRAY for Set of Booleans`() {
93+
assertEquals(SentryAttributeType.ARRAY, SentryAttributeType.inferFrom(setOf(true, false)))
94+
}
95+
96+
@Test
97+
fun `inferFrom returns ARRAY for String array`() {
98+
assertEquals(SentryAttributeType.ARRAY, SentryAttributeType.inferFrom(arrayOf("a", "b")))
99+
}
100+
101+
@Test
102+
fun `inferFrom returns ARRAY for int array`() {
103+
assertEquals(SentryAttributeType.ARRAY, SentryAttributeType.inferFrom(intArrayOf(1, 2)))
104+
}
105+
106+
@Test
107+
fun `inferFrom returns ARRAY for empty list`() {
108+
assertEquals(SentryAttributeType.ARRAY, SentryAttributeType.inferFrom(emptyList<String>()))
109+
}
110+
111+
@Test
112+
fun `inferFrom returns ARRAY for mixed-type list`() {
113+
assertEquals(SentryAttributeType.ARRAY, SentryAttributeType.inferFrom(listOf("a", 1, true)))
114+
}
80115
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class SentryLogsSerializationTest {
3838
"sentry.sdk.name" to
3939
SentryLogEventAttributeValue("string", "sentry.java.spring-boot.jakarta"),
4040
"sentry.environment" to SentryLogEventAttributeValue("string", "production"),
41+
"custom.array" to SentryLogEventAttributeValue("array", listOf("a", "b")),
4142
"sentry.sdk.version" to SentryLogEventAttributeValue("string", "8.11.1"),
4243
"sentry.trace.parent_span_id" to
4344
SentryLogEventAttributeValue("string", "f28b86350e534671"),

sentry/src/test/resources/json/sentry_logs.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
"type": "string",
2121
"value": "production"
2222
},
23+
"custom.array":
24+
{
25+
"type": "array",
26+
"value": ["a", "b"]
27+
},
2328
"sentry.sdk.version":
2429
{
2530
"type": "string",

0 commit comments

Comments
 (0)