Skip to content

Commit 42bdc88

Browse files
committed
Attribute.flattened
1 parent ffb15c2 commit 42bdc88

4 files changed

Lines changed: 64 additions & 8 deletions

File tree

sentry/api/sentry.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,7 @@ public final class io/sentry/JsonObjectWriter : io/sentry/ObjectWriter {
12501250
}
12511251

12521252
public final class io/sentry/JsonReflectionObjectSerializer {
1253+
public fun <init> (I)V
12531254
public fun serialize (Ljava/lang/Object;Lio/sentry/ILogger;)Ljava/lang/Object;
12541255
public fun serializeObject (Ljava/lang/Object;Lio/sentry/ILogger;)Ljava/util/Map;
12551256
}
@@ -2661,6 +2662,9 @@ public final class io/sentry/SentryAppStartProfilingOptions$JsonKeys {
26612662
public final class io/sentry/SentryAttribute {
26622663
public static fun booleanAttribute (Ljava/lang/String;Ljava/lang/Boolean;)Lio/sentry/SentryAttribute;
26632664
public static fun doubleAttribute (Ljava/lang/String;Ljava/lang/Double;)Lio/sentry/SentryAttribute;
2665+
public static fun flattened (Ljava/lang/String;Ljava/lang/Object;)Lio/sentry/SentryAttribute;
2666+
public static fun flattened (Ljava/lang/String;Ljava/lang/Object;I)Lio/sentry/SentryAttribute;
2667+
public fun getFlattenDepth ()I
26642668
public fun getName ()Ljava/lang/String;
26652669
public fun getType ()Lio/sentry/SentryAttributeType;
26662670
public fun getValue ()Ljava/lang/Object;

sentry/src/main/java/io/sentry/JsonReflectionObjectSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public final class JsonReflectionObjectSerializer {
3333
private final Set<Object> visiting = new HashSet<>();
3434
private final int maxDepth;
3535

36-
JsonReflectionObjectSerializer(int maxDepth) {
36+
public JsonReflectionObjectSerializer(int maxDepth) {
3737
this.maxDepth = maxDepth;
3838
}
3939

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ public final class SentryAttribute {
88
private final @NotNull String name;
99
private final @Nullable SentryAttributeType type;
1010
private final @Nullable Object value;
11+
private final int flattenDepth;
1112

1213
private SentryAttribute(
1314
final @NotNull String name,
1415
final @Nullable SentryAttributeType type,
15-
final @Nullable Object value) {
16+
final @Nullable Object value,
17+
final int flattenDepth) {
1618
this.name = name;
1719
this.type = type;
1820
this.value = value;
21+
this.flattenDepth = flattenDepth;
1922
}
2023

2124
public @NotNull String getName() {
@@ -30,28 +33,42 @@ private SentryAttribute(
3033
return value;
3134
}
3235

36+
public int getFlattenDepth() {
37+
return flattenDepth;
38+
}
39+
3340
public static @NotNull SentryAttribute named(
3441
final @NotNull String name, final @Nullable Object value) {
35-
return new SentryAttribute(name, null, value);
42+
return new SentryAttribute(name, null, value, 0);
43+
}
44+
45+
public static @NotNull SentryAttribute flattened(
46+
final @NotNull String name, final @Nullable Object value) {
47+
return new SentryAttribute(name, null, value, 1);
48+
}
49+
50+
public static @NotNull SentryAttribute flattened(
51+
final @NotNull String name, final @Nullable Object value, final int flattenDepth) {
52+
return new SentryAttribute(name, null, value, flattenDepth);
3653
}
3754

3855
public static @NotNull SentryAttribute booleanAttribute(
3956
final @NotNull String name, final @Nullable Boolean value) {
40-
return new SentryAttribute(name, SentryAttributeType.BOOLEAN, value);
57+
return new SentryAttribute(name, SentryAttributeType.BOOLEAN, value, 0);
4158
}
4259

4360
public static @NotNull SentryAttribute integerAttribute(
4461
final @NotNull String name, final @Nullable Integer value) {
45-
return new SentryAttribute(name, SentryAttributeType.INTEGER, value);
62+
return new SentryAttribute(name, SentryAttributeType.INTEGER, value, 0);
4663
}
4764

4865
public static @NotNull SentryAttribute doubleAttribute(
4966
final @NotNull String name, final @Nullable Double value) {
50-
return new SentryAttribute(name, SentryAttributeType.DOUBLE, value);
67+
return new SentryAttribute(name, SentryAttributeType.DOUBLE, value, 0);
5168
}
5269

5370
public static @NotNull SentryAttribute stringAttribute(
5471
final @NotNull String name, final @Nullable String value) {
55-
return new SentryAttribute(name, SentryAttributeType.STRING, value);
72+
return new SentryAttribute(name, SentryAttributeType.STRING, value, 0);
5673
}
5774
}

sentry/src/main/java/io/sentry/logger/LoggerApi.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.sentry.HostnameCache;
44
import io.sentry.IScope;
55
import io.sentry.ISpan;
6+
import io.sentry.JsonReflectionObjectSerializer;
67
import io.sentry.PropagationContext;
78
import io.sentry.Scopes;
89
import io.sentry.SentryAttribute;
@@ -20,6 +21,7 @@
2021
import io.sentry.util.Platform;
2122
import io.sentry.util.TracingUtils;
2223
import java.util.HashMap;
24+
import java.util.Map;
2325
import org.jetbrains.annotations.ApiStatus;
2426
import org.jetbrains.annotations.NotNull;
2527
import org.jetbrains.annotations.Nullable;
@@ -170,7 +172,9 @@ private void captureLog(
170172
final @Nullable Object value = attribute.getValue();
171173
final @NotNull SentryAttributeType type =
172174
attribute.getType() == null ? getType(value) : attribute.getType();
173-
attributes.put(attribute.getName(), new SentryLogEventAttributeValue(type, value));
175+
final @Nullable Object convertedValue =
176+
maybeConvertValue(attribute.getName(), value, attribute.getFlattenDepth(), attributes);
177+
attributes.put(attribute.getName(), new SentryLogEventAttributeValue(type, convertedValue));
174178
}
175179
}
176180

@@ -216,6 +220,37 @@ private void captureLog(
216220
return attributes;
217221
}
218222

223+
private @Nullable Object maybeConvertValue(
224+
final @Nullable Object name,
225+
final @Nullable Object value,
226+
final int flattenDepth,
227+
final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes) {
228+
if (value == null) {
229+
return null;
230+
}
231+
232+
if (flattenDepth > 0) {
233+
JsonReflectionObjectSerializer serializer = new JsonReflectionObjectSerializer(1);
234+
try {
235+
Map<String, Object> stringObjectMap =
236+
serializer.serializeObject(value, scopes.getOptions().getLogger());
237+
for (final @NotNull Map.Entry<String, Object> entry : stringObjectMap.entrySet()) {
238+
attributes.put(
239+
name + "." + entry.getKey(),
240+
new SentryLogEventAttributeValue(getType(entry.getValue()), entry.getValue()));
241+
}
242+
return value;
243+
} catch (Exception e) {
244+
scopes
245+
.getOptions()
246+
.getLogger()
247+
.log(SentryLevel.DEBUG, "Unable to flatten log attribute value", e);
248+
}
249+
}
250+
251+
return value;
252+
}
253+
219254
private void setServerName(
220255
final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes) {
221256
final @NotNull SentryOptions options = scopes.getOptions();

0 commit comments

Comments
 (0)