Skip to content

Commit 061649a

Browse files
authored
Merge d535d44 into de80a23
2 parents de80a23 + d535d44 commit 061649a

File tree

6 files changed

+53
-35
lines changed

6 files changed

+53
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Features
66

77
- Add scope-level attributes API ([#5118](https://github.com/getsentry/sentry-java/pull/5118))
8+
- Automatically include scope attributes in logs and metrics ([#5120](https://github.com/getsentry/sentry-java/pull/5120))
89

910
### Fixes
1011

sentry/api/sentry.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,6 +2829,7 @@ public final class io/sentry/SentryAttributeType : java/lang/Enum {
28292829
public static final field INTEGER Lio/sentry/SentryAttributeType;
28302830
public static final field STRING Lio/sentry/SentryAttributeType;
28312831
public fun apiName ()Ljava/lang/String;
2832+
public static fun inferFrom (Ljava/lang/Object;)Lio/sentry/SentryAttributeType;
28322833
public static fun valueOf (Ljava/lang/String;)Lio/sentry/SentryAttributeType;
28332834
public static fun values ()[Lio/sentry/SentryAttributeType;
28342835
}
@@ -3338,6 +3339,7 @@ public final class io/sentry/SentryLogEvent$JsonKeys {
33383339
public final class io/sentry/SentryLogEventAttributeValue : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
33393340
public fun <init> (Lio/sentry/SentryAttributeType;Ljava/lang/Object;)V
33403341
public fun <init> (Ljava/lang/String;Ljava/lang/Object;)V
3342+
public static fun fromAttribute (Lio/sentry/SentryAttribute;)Lio/sentry/SentryLogEventAttributeValue;
33413343
public fun getType ()Ljava/lang/String;
33423344
public fun getUnknown ()Ljava/util/Map;
33433345
public fun getValue ()Ljava/lang/Object;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Locale;
44
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
56

67
public enum SentryAttributeType {
78
STRING,
@@ -12,4 +13,17 @@ public enum SentryAttributeType {
1213
public @NotNull String apiName() {
1314
return name().toLowerCase(Locale.ROOT);
1415
}
16+
17+
public static @NotNull SentryAttributeType inferFrom(final @Nullable Object value) {
18+
if (value instanceof Boolean) {
19+
return BOOLEAN;
20+
}
21+
if (value instanceof Integer) {
22+
return INTEGER;
23+
}
24+
if (value instanceof Number) {
25+
return DOUBLE;
26+
}
27+
return STRING;
28+
}
1529
}

sentry/src/main/java/io/sentry/SentryLogEventAttributeValue.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ public SentryLogEventAttributeValue(
2727
this(type.apiName(), value);
2828
}
2929

30+
/**
31+
* Creates a {@link SentryLogEventAttributeValue} from a {@link SentryAttribute}, inferring the
32+
* type if not explicitly set.
33+
*
34+
* @param attribute the attribute
35+
* @return the attribute value
36+
*/
37+
public static @NotNull SentryLogEventAttributeValue fromAttribute(
38+
final @NotNull SentryAttribute attribute) {
39+
final @Nullable Object value = attribute.getValue();
40+
final @NotNull SentryAttributeType type =
41+
attribute.getType() == null ? SentryAttributeType.inferFrom(value) : attribute.getType();
42+
return new SentryLogEventAttributeValue(type, value);
43+
}
44+
3045
public @NotNull String getType() {
3146
return type;
3247
}

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import io.sentry.util.Platform;
2222
import io.sentry.util.TracingUtils;
2323
import java.util.HashMap;
24+
import java.util.Map;
2425
import org.jetbrains.annotations.NotNull;
2526
import org.jetbrains.annotations.Nullable;
2627

@@ -163,6 +164,14 @@ private void captureLog(
163164
final @NotNull String message,
164165
final @Nullable Object... args) {
165166
final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes = new HashMap<>();
167+
168+
final @NotNull Map<String, SentryAttribute> scopeAttributes =
169+
scopes.getCombinedScopeView().getAttributes();
170+
for (SentryAttribute scopeAttribute : scopeAttributes.values()) {
171+
attributes.put(
172+
scopeAttribute.getName(), SentryLogEventAttributeValue.fromAttribute(scopeAttribute));
173+
}
174+
166175
final @NotNull String origin = params.getOrigin();
167176
if (!"manual".equalsIgnoreCase(origin)) {
168177
attributes.put(
@@ -173,17 +182,14 @@ private void captureLog(
173182

174183
if (incomingAttributes != null) {
175184
for (SentryAttribute attribute : incomingAttributes.getAttributes().values()) {
176-
final @Nullable Object value = attribute.getValue();
177-
final @NotNull SentryAttributeType type =
178-
attribute.getType() == null ? getType(value) : attribute.getType();
179-
attributes.put(attribute.getName(), new SentryLogEventAttributeValue(type, value));
185+
attributes.put(attribute.getName(), SentryLogEventAttributeValue.fromAttribute(attribute));
180186
}
181187
}
182188

183189
if (args != null) {
184190
int i = 0;
185191
for (Object arg : args) {
186-
final @NotNull SentryAttributeType type = getType(arg);
192+
final @NotNull SentryAttributeType type = SentryAttributeType.inferFrom(arg);
187193
attributes.put(
188194
"sentry.message.parameter." + i, new SentryLogEventAttributeValue(type, arg));
189195
i++;
@@ -292,17 +298,4 @@ private void setUser(final @NotNull HashMap<String, SentryLogEventAttributeValue
292298
}
293299
}
294300
}
295-
296-
private @NotNull SentryAttributeType getType(final @Nullable Object arg) {
297-
if (arg instanceof Boolean) {
298-
return SentryAttributeType.BOOLEAN;
299-
}
300-
if (arg instanceof Integer) {
301-
return SentryAttributeType.INTEGER;
302-
}
303-
if (arg instanceof Number) {
304-
return SentryAttributeType.DOUBLE;
305-
}
306-
return SentryAttributeType.STRING;
307-
}
308301
}

sentry/src/main/java/io/sentry/metrics/MetricsApi.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.sentry.util.Platform;
2121
import io.sentry.util.TracingUtils;
2222
import java.util.HashMap;
23+
import java.util.Map;
2324
import org.jetbrains.annotations.NotNull;
2425
import org.jetbrains.annotations.Nullable;
2526

@@ -167,6 +168,14 @@ private void captureMetrics(
167168
private @NotNull HashMap<String, SentryLogEventAttributeValue> createAttributes(
168169
final @NotNull SentryMetricsParameters params) {
169170
final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes = new HashMap<>();
171+
172+
final @NotNull Map<String, SentryAttribute> scopeAttributes =
173+
scopes.getCombinedScopeView().getAttributes();
174+
for (SentryAttribute scopeAttribute : scopeAttributes.values()) {
175+
attributes.put(
176+
scopeAttribute.getName(), SentryLogEventAttributeValue.fromAttribute(scopeAttribute));
177+
}
178+
170179
final @NotNull String origin = params.getOrigin();
171180
if (!"manual".equalsIgnoreCase(origin)) {
172181
attributes.put(
@@ -177,10 +186,7 @@ private void captureMetrics(
177186

178187
if (incomingAttributes != null) {
179188
for (SentryAttribute attribute : incomingAttributes.getAttributes().values()) {
180-
final @Nullable Object value = attribute.getValue();
181-
final @NotNull SentryAttributeType type =
182-
attribute.getType() == null ? getType(value) : attribute.getType();
183-
attributes.put(attribute.getName(), new SentryLogEventAttributeValue(type, value));
189+
attributes.put(attribute.getName(), SentryLogEventAttributeValue.fromAttribute(attribute));
184190
}
185191
}
186192

@@ -279,17 +285,4 @@ private void setUser(final @NotNull HashMap<String, SentryLogEventAttributeValue
279285
}
280286
}
281287
}
282-
283-
private @NotNull SentryAttributeType getType(final @Nullable Object arg) {
284-
if (arg instanceof Boolean) {
285-
return SentryAttributeType.BOOLEAN;
286-
}
287-
if (arg instanceof Integer) {
288-
return SentryAttributeType.INTEGER;
289-
}
290-
if (arg instanceof Number) {
291-
return SentryAttributeType.DOUBLE;
292-
}
293-
return SentryAttributeType.STRING;
294-
}
295288
}

0 commit comments

Comments
 (0)