Skip to content

Commit c549bcf

Browse files
committed
Merge branch 'feat/better-attributes-api' into feat/attributes-flattened
2 parents 42bdc88 + 43f9c85 commit c549bcf

File tree

7 files changed

+117
-41
lines changed

7 files changed

+117
-41
lines changed

sentry/api/sentry.api

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,7 +2686,7 @@ public final class io/sentry/SentryAttributeType : java/lang/Enum {
26862686
public final class io/sentry/SentryAttributes {
26872687
public fun add (Lio/sentry/SentryAttribute;)V
26882688
public static fun fromMap (Ljava/util/Map;)Lio/sentry/SentryAttributes;
2689-
public fun getAttributes ()Ljava/util/List;
2689+
public fun getAttributes ()Ljava/util/Map;
26902690
public static fun of ([Lio/sentry/SentryAttribute;)Lio/sentry/SentryAttributes;
26912691
}
26922692

@@ -4744,7 +4744,7 @@ public abstract interface class io/sentry/logger/ILoggerApi {
47444744
public abstract fun fatal (Ljava/lang/String;[Ljava/lang/Object;)V
47454745
public abstract fun info (Ljava/lang/String;[Ljava/lang/Object;)V
47464746
public abstract fun log (Lio/sentry/SentryLogLevel;Lio/sentry/SentryDate;Ljava/lang/String;[Ljava/lang/Object;)V
4747-
public abstract fun log (Lio/sentry/SentryLogLevel;Lio/sentry/logger/LogParams;Ljava/lang/String;[Ljava/lang/Object;)V
4747+
public abstract fun log (Lio/sentry/SentryLogLevel;Lio/sentry/logger/SentryLogParameters;Ljava/lang/String;[Ljava/lang/Object;)V
47484748
public abstract fun log (Lio/sentry/SentryLogLevel;Ljava/lang/String;[Ljava/lang/Object;)V
47494749
public abstract fun trace (Ljava/lang/String;[Ljava/lang/Object;)V
47504750
public abstract fun warn (Ljava/lang/String;[Ljava/lang/Object;)V
@@ -4755,24 +4755,14 @@ public abstract interface class io/sentry/logger/ILoggerBatchProcessor {
47554755
public abstract fun close (Z)V
47564756
}
47574757

4758-
public final class io/sentry/logger/LogParams {
4759-
public fun <init> ()V
4760-
public static fun create (Lio/sentry/SentryAttributes;)Lio/sentry/logger/LogParams;
4761-
public static fun create (Lio/sentry/SentryDate;Lio/sentry/SentryAttributes;)Lio/sentry/logger/LogParams;
4762-
public fun getAttributes ()Lio/sentry/SentryAttributes;
4763-
public fun getTimestamp ()Lio/sentry/SentryDate;
4764-
public fun setAttributes (Lio/sentry/SentryAttributes;)V
4765-
public fun setTimestamp (Lio/sentry/SentryDate;)V
4766-
}
4767-
47684758
public final class io/sentry/logger/LoggerApi : io/sentry/logger/ILoggerApi {
47694759
public fun <init> (Lio/sentry/Scopes;)V
47704760
public fun debug (Ljava/lang/String;[Ljava/lang/Object;)V
47714761
public fun error (Ljava/lang/String;[Ljava/lang/Object;)V
47724762
public fun fatal (Ljava/lang/String;[Ljava/lang/Object;)V
47734763
public fun info (Ljava/lang/String;[Ljava/lang/Object;)V
47744764
public fun log (Lio/sentry/SentryLogLevel;Lio/sentry/SentryDate;Ljava/lang/String;[Ljava/lang/Object;)V
4775-
public fun log (Lio/sentry/SentryLogLevel;Lio/sentry/logger/LogParams;Ljava/lang/String;[Ljava/lang/Object;)V
4765+
public fun log (Lio/sentry/SentryLogLevel;Lio/sentry/logger/SentryLogParameters;Ljava/lang/String;[Ljava/lang/Object;)V
47764766
public fun log (Lio/sentry/SentryLogLevel;Ljava/lang/String;[Ljava/lang/Object;)V
47774767
public fun trace (Ljava/lang/String;[Ljava/lang/Object;)V
47784768
public fun warn (Ljava/lang/String;[Ljava/lang/Object;)V
@@ -4793,7 +4783,7 @@ public final class io/sentry/logger/NoOpLoggerApi : io/sentry/logger/ILoggerApi
47934783
public static fun getInstance ()Lio/sentry/logger/NoOpLoggerApi;
47944784
public fun info (Ljava/lang/String;[Ljava/lang/Object;)V
47954785
public fun log (Lio/sentry/SentryLogLevel;Lio/sentry/SentryDate;Ljava/lang/String;[Ljava/lang/Object;)V
4796-
public fun log (Lio/sentry/SentryLogLevel;Lio/sentry/logger/LogParams;Ljava/lang/String;[Ljava/lang/Object;)V
4786+
public fun log (Lio/sentry/SentryLogLevel;Lio/sentry/logger/SentryLogParameters;Ljava/lang/String;[Ljava/lang/Object;)V
47974787
public fun log (Lio/sentry/SentryLogLevel;Ljava/lang/String;[Ljava/lang/Object;)V
47984788
public fun trace (Ljava/lang/String;[Ljava/lang/Object;)V
47994789
public fun warn (Ljava/lang/String;[Ljava/lang/Object;)V
@@ -4805,6 +4795,16 @@ public final class io/sentry/logger/NoOpLoggerBatchProcessor : io/sentry/logger/
48054795
public static fun getInstance ()Lio/sentry/logger/NoOpLoggerBatchProcessor;
48064796
}
48074797

4798+
public final class io/sentry/logger/SentryLogParameters {
4799+
public fun <init> ()V
4800+
public static fun create (Lio/sentry/SentryAttributes;)Lio/sentry/logger/SentryLogParameters;
4801+
public static fun create (Lio/sentry/SentryDate;Lio/sentry/SentryAttributes;)Lio/sentry/logger/SentryLogParameters;
4802+
public fun getAttributes ()Lio/sentry/SentryAttributes;
4803+
public fun getTimestamp ()Lio/sentry/SentryDate;
4804+
public fun setAttributes (Lio/sentry/SentryAttributes;)V
4805+
public fun setTimestamp (Lio/sentry/SentryDate;)V
4806+
}
4807+
48084808
public final class io/sentry/opentelemetry/OpenTelemetryUtil {
48094809
public fun <init> ()V
48104810
public static fun applyIgnoredSpanOrigins (Lio/sentry/SentryOptions;)V

sentry/src/main/java/io/sentry/SentryAttributes.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,52 @@
11
package io.sentry;
22

3-
import java.util.ArrayList;
4-
import java.util.Arrays;
5-
import java.util.List;
63
import java.util.Map;
4+
import java.util.concurrent.ConcurrentHashMap;
75
import org.jetbrains.annotations.NotNull;
86
import org.jetbrains.annotations.Nullable;
97

108
public final class SentryAttributes {
119

12-
private final @NotNull List<SentryAttribute> attributes;
10+
private final @NotNull Map<String, SentryAttribute> attributes;
1311

14-
private SentryAttributes(final @NotNull List<SentryAttribute> attributes) {
12+
private SentryAttributes(final @NotNull Map<String, SentryAttribute> attributes) {
1513
this.attributes = attributes;
1614
}
1715

1816
public void add(final @Nullable SentryAttribute attribute) {
1917
if (attribute == null) {
2018
return;
2119
}
22-
attributes.add(attribute);
20+
attributes.put(attribute.getName(), attribute);
2321
}
2422

25-
public @NotNull List<SentryAttribute> getAttributes() {
23+
public @NotNull Map<String, SentryAttribute> getAttributes() {
2624
return attributes;
2725
}
2826

29-
public static @NotNull SentryAttributes of(SentryAttribute... attributes) {
30-
return new SentryAttributes(Arrays.asList(attributes));
27+
public static @NotNull SentryAttributes of(final @Nullable SentryAttribute... attributes) {
28+
if (attributes == null) {
29+
return new SentryAttributes(new ConcurrentHashMap<>());
30+
}
31+
final @NotNull SentryAttributes sentryAttributes =
32+
new SentryAttributes(new ConcurrentHashMap<>(attributes.length));
33+
for (SentryAttribute attribute : attributes) {
34+
sentryAttributes.add(attribute);
35+
}
36+
return sentryAttributes;
3137
}
3238

3339
public static @NotNull SentryAttributes fromMap(final @Nullable Map<String, Object> attributes) {
3440
if (attributes == null) {
35-
return new SentryAttributes(new ArrayList<>());
41+
return new SentryAttributes(new ConcurrentHashMap<>());
3642
}
37-
SentryAttributes sentryAttributes = new SentryAttributes(new ArrayList<>(attributes.size()));
43+
final @NotNull SentryAttributes sentryAttributes =
44+
new SentryAttributes(new ConcurrentHashMap<>(attributes.size()));
3845
for (Map.Entry<String, Object> attribute : attributes.entrySet()) {
39-
sentryAttributes.add(SentryAttribute.named(attribute.getKey(), attribute.getValue()));
46+
final @Nullable String key = attribute.getKey();
47+
if (key != null) {
48+
sentryAttributes.add(SentryAttribute.named(key, attribute.getValue()));
49+
}
4050
}
4151

4252
return sentryAttributes;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void log(
3131

3232
void log(
3333
@NotNull SentryLogLevel level,
34-
@NotNull LogParams params,
34+
@NotNull SentryLogParameters params,
3535
@Nullable String message,
3636
@Nullable Object... args);
3737
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void log(
7070
final @NotNull SentryLogLevel level,
7171
final @Nullable String message,
7272
final @Nullable Object... args) {
73-
captureLog(level, LogParams.create(null, null), message, args);
73+
captureLog(level, SentryLogParameters.create(null, null), message, args);
7474
}
7575

7676
@Override
@@ -79,13 +79,13 @@ public void log(
7979
final @Nullable SentryDate timestamp,
8080
final @Nullable String message,
8181
final @Nullable Object... args) {
82-
captureLog(level, LogParams.create(timestamp, null), message, args);
82+
captureLog(level, SentryLogParameters.create(timestamp, null), message, args);
8383
}
8484

8585
@Override
8686
public void log(
8787
final @NotNull SentryLogLevel level,
88-
final @NotNull LogParams params,
88+
final @NotNull SentryLogParameters params,
8989
final @Nullable String message,
9090
final @Nullable Object... args) {
9191
captureLog(level, params, message, args);
@@ -94,7 +94,7 @@ public void log(
9494
@SuppressWarnings("AnnotateFormatMethod")
9595
private void captureLog(
9696
final @NotNull SentryLogLevel level,
97-
final @NotNull LogParams params,
97+
final @NotNull SentryLogParameters params,
9898
final @Nullable String message,
9999
final @Nullable Object... args) {
100100
final @NotNull SentryOptions options = scopes.getOptions();
@@ -168,7 +168,7 @@ private void captureLog(
168168
final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes = new HashMap<>();
169169

170170
if (incomingAttributes != null) {
171-
for (SentryAttribute attribute : incomingAttributes.getAttributes()) {
171+
for (SentryAttribute attribute : incomingAttributes.getAttributes().values()) {
172172
final @Nullable Object value = attribute.getValue();
173173
final @NotNull SentryAttributeType type =
174174
attribute.getType() == null ? getType(value) : attribute.getType();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void log(
6565
@Override
6666
public void log(
6767
@NotNull SentryLogLevel level,
68-
@NotNull LogParams params,
68+
@NotNull SentryLogParameters params,
6969
@Nullable String message,
7070
@Nullable Object... args) {
7171
// do nothing

sentry/src/main/java/io/sentry/logger/LogParams.java renamed to sentry/src/main/java/io/sentry/logger/SentryLogParameters.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Nullable;
77

8-
public final class LogParams {
8+
public final class SentryLogParameters {
99

1010
private @Nullable SentryDate timestamp;
1111
private @Nullable SentryAttributes attributes;
@@ -26,17 +26,17 @@ public void setAttributes(final @Nullable SentryAttributes attributes) {
2626
this.attributes = attributes;
2727
}
2828

29-
public static @NotNull LogParams create(
29+
public static @NotNull SentryLogParameters create(
3030
final @Nullable SentryDate timestamp, final @Nullable SentryAttributes attributes) {
31-
final @NotNull LogParams params = new LogParams();
31+
final @NotNull SentryLogParameters params = new SentryLogParameters();
3232

3333
params.setTimestamp(timestamp);
3434
params.setAttributes(attributes);
3535

3636
return params;
3737
}
3838

39-
public static @NotNull LogParams create(final @Nullable SentryAttributes attributes) {
39+
public static @NotNull SentryLogParameters create(final @Nullable SentryAttributes attributes) {
4040
return create(null, attributes);
4141
}
4242
}

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

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import io.sentry.clientreport.DiscardReason
77
import io.sentry.clientreport.DiscardedEvent
88
import io.sentry.hints.SessionEndHint
99
import io.sentry.hints.SessionStartHint
10+
import io.sentry.logger.SentryLogParameters
1011
import io.sentry.protocol.Feedback
1112
import io.sentry.protocol.SentryId
1213
import io.sentry.protocol.SentryTransaction
@@ -2648,12 +2649,12 @@ class ScopesTest {
26482649
}
26492650

26502651
@Test
2651-
fun `creating log with attributes works`() {
2652+
fun `creating log with attributes from map works`() {
26522653
val (sut, mockClient) = getEnabledScopes {
26532654
it.logs.isEnabled = true
26542655
}
26552656

2656-
sut.logger().log(mapOf("attrname1" to "attrval1"), SentryLogLevel.WARN, "log message")
2657+
sut.logger().log(SentryLogLevel.WARN, SentryLogParameters.create(SentryAttributes.fromMap(mapOf("attrname1" to "attrval1"))), "log message")
26572658

26582659
verify(mockClient).captureLog(
26592660
check {
@@ -2669,13 +2670,78 @@ class ScopesTest {
26692670
)
26702671
}
26712672

2673+
@Test
2674+
fun `creating log with attributes works`() {
2675+
val (sut, mockClient) = getEnabledScopes {
2676+
it.logs.isEnabled = true
2677+
}
2678+
2679+
sut.logger().log(
2680+
SentryLogLevel.WARN,
2681+
SentryLogParameters.create(
2682+
SentryAttributes.of(
2683+
SentryAttribute.stringAttribute("strattr", "strval"),
2684+
SentryAttribute.booleanAttribute("boolattr", true),
2685+
SentryAttribute.integerAttribute("intattr", 17),
2686+
SentryAttribute.doubleAttribute("doubleattr", 3.8),
2687+
SentryAttribute.named("namedstrattr", "namedstrval"),
2688+
SentryAttribute.named("namedboolattr", false),
2689+
SentryAttribute.named("namedintattr", 18),
2690+
SentryAttribute.named("nameddoubleattr", 4.9)
2691+
)
2692+
),
2693+
"log message"
2694+
)
2695+
2696+
verify(mockClient).captureLog(
2697+
check {
2698+
assertEquals("log message", it.body)
2699+
assertEquals(SentryLogLevel.WARN, it.level)
2700+
assertEquals(13, it.severityNumber)
2701+
2702+
val strattr = it.attributes?.get("strattr")!!
2703+
assertEquals("strval", strattr.value)
2704+
assertEquals("string", strattr.type)
2705+
2706+
val boolattr = it.attributes?.get("boolattr")!!
2707+
assertEquals(true, boolattr.value)
2708+
assertEquals("boolean", boolattr.type)
2709+
2710+
val intattr = it.attributes?.get("intattr")!!
2711+
assertEquals(17, intattr.value)
2712+
assertEquals("integer", intattr.type)
2713+
2714+
val doubleattr = it.attributes?.get("doubleattr")!!
2715+
assertEquals(3.8, doubleattr.value)
2716+
assertEquals("double", doubleattr.type)
2717+
2718+
val namedstrattr = it.attributes?.get("namedstrattr")!!
2719+
assertEquals("namedstrval", namedstrattr.value)
2720+
assertEquals("string", namedstrattr.type)
2721+
2722+
val namedboolattr = it.attributes?.get("namedboolattr")!!
2723+
assertEquals(false, namedboolattr.value)
2724+
assertEquals("boolean", namedboolattr.type)
2725+
2726+
val namedintattr = it.attributes?.get("namedintattr")!!
2727+
assertEquals(18, namedintattr.value)
2728+
assertEquals("integer", namedintattr.type)
2729+
2730+
val nameddoubleattr = it.attributes?.get("nameddoubleattr")!!
2731+
assertEquals(4.9, nameddoubleattr.value)
2732+
assertEquals("double", nameddoubleattr.type)
2733+
},
2734+
anyOrNull()
2735+
)
2736+
}
2737+
26722738
@Test
26732739
fun `creating log with attributes and timestamp works`() {
26742740
val (sut, mockClient) = getEnabledScopes {
26752741
it.logs.isEnabled = true
26762742
}
26772743

2678-
sut.logger().log(mapOf("attrname1" to "attrval1"), SentryLogLevel.WARN, SentryLongDate(123), "log message")
2744+
sut.logger().log(SentryLogLevel.WARN, SentryLogParameters.create(SentryLongDate(123), SentryAttributes.of(SentryAttribute.named("attrname1", "attrval1"))), "log message")
26792745

26802746
verify(mockClient).captureLog(
26812747
check {
@@ -2698,7 +2764,7 @@ class ScopesTest {
26982764
it.logs.isEnabled = true
26992765
}
27002766

2701-
sut.logger().log(mapOf("attrname1" to "attrval1"), SentryLogLevel.WARN, SentryLongDate(123), "log %s %d %b %.0f", "message", 1, true, 3.2)
2767+
sut.logger().log(SentryLogLevel.WARN, SentryLogParameters.create(SentryLongDate(123), SentryAttributes.of(SentryAttribute.named("attrname1", "attrval1"))), "log %s %d %b %.0f", "message", 1, true, 3.2)
27022768

27032769
verify(mockClient).captureLog(
27042770
check {

0 commit comments

Comments
 (0)