Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

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

### Fixes

Expand Down
2 changes: 2 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -2829,6 +2829,7 @@ public final class io/sentry/SentryAttributeType : java/lang/Enum {
public static final field INTEGER Lio/sentry/SentryAttributeType;
public static final field STRING Lio/sentry/SentryAttributeType;
public fun apiName ()Ljava/lang/String;
public static fun inferFrom (Ljava/lang/Object;)Lio/sentry/SentryAttributeType;
public static fun valueOf (Ljava/lang/String;)Lio/sentry/SentryAttributeType;
public static fun values ()[Lio/sentry/SentryAttributeType;
}
Expand Down Expand Up @@ -3338,6 +3339,7 @@ public final class io/sentry/SentryLogEvent$JsonKeys {
public final class io/sentry/SentryLogEventAttributeValue : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
public fun <init> (Lio/sentry/SentryAttributeType;Ljava/lang/Object;)V
public fun <init> (Ljava/lang/String;Ljava/lang/Object;)V
public static fun fromAttribute (Lio/sentry/SentryAttribute;)Lio/sentry/SentryLogEventAttributeValue;
public fun getType ()Ljava/lang/String;
public fun getUnknown ()Ljava/util/Map;
public fun getValue ()Ljava/lang/Object;
Expand Down
14 changes: 14 additions & 0 deletions sentry/src/main/java/io/sentry/SentryAttributeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Locale;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public enum SentryAttributeType {
STRING,
Expand All @@ -12,4 +13,17 @@ public enum SentryAttributeType {
public @NotNull String apiName() {
return name().toLowerCase(Locale.ROOT);
}

public static @NotNull SentryAttributeType inferFrom(final @Nullable Object value) {
if (value instanceof Boolean) {
return BOOLEAN;
}
if (value instanceof Integer) {
return INTEGER;
}
Comment thread
alexander-alderman-webb marked this conversation as resolved.
if (value instanceof Number) {
return DOUBLE;
}
return STRING;
}
}
15 changes: 15 additions & 0 deletions sentry/src/main/java/io/sentry/SentryLogEventAttributeValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ public SentryLogEventAttributeValue(
this(type.apiName(), value);
}

/**
* Creates a {@link SentryLogEventAttributeValue} from a {@link SentryAttribute}, inferring the
* type if not explicitly set.
*
* @param attribute the attribute
* @return the attribute value
*/
public static @NotNull SentryLogEventAttributeValue fromAttribute(
final @NotNull SentryAttribute attribute) {
final @Nullable Object value = attribute.getValue();
final @NotNull SentryAttributeType type =
attribute.getType() == null ? SentryAttributeType.inferFrom(value) : attribute.getType();
return new SentryLogEventAttributeValue(type, value);
}

public @NotNull String getType() {
return type;
}
Expand Down
29 changes: 11 additions & 18 deletions sentry/src/main/java/io/sentry/logger/LoggerApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.sentry.util.Platform;
import io.sentry.util.TracingUtils;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -163,6 +164,14 @@ private void captureLog(
final @NotNull String message,
final @Nullable Object... args) {
final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes = new HashMap<>();

final @NotNull Map<String, SentryAttribute> scopeAttributes =
scopes.getCombinedScopeView().getAttributes();
for (SentryAttribute scopeAttribute : scopeAttributes.values()) {
attributes.put(
scopeAttribute.getName(), SentryLogEventAttributeValue.fromAttribute(scopeAttribute));
}

final @NotNull String origin = params.getOrigin();
if (!"manual".equalsIgnoreCase(origin)) {
attributes.put(
Expand All @@ -173,17 +182,14 @@ private void captureLog(

if (incomingAttributes != null) {
for (SentryAttribute attribute : incomingAttributes.getAttributes().values()) {
final @Nullable Object value = attribute.getValue();
final @NotNull SentryAttributeType type =
attribute.getType() == null ? getType(value) : attribute.getType();
attributes.put(attribute.getName(), new SentryLogEventAttributeValue(type, value));
attributes.put(attribute.getName(), SentryLogEventAttributeValue.fromAttribute(attribute));
}
}

if (args != null) {
int i = 0;
for (Object arg : args) {
final @NotNull SentryAttributeType type = getType(arg);
final @NotNull SentryAttributeType type = SentryAttributeType.inferFrom(arg);
attributes.put(
"sentry.message.parameter." + i, new SentryLogEventAttributeValue(type, arg));
i++;
Expand Down Expand Up @@ -292,17 +298,4 @@ private void setUser(final @NotNull HashMap<String, SentryLogEventAttributeValue
}
}
}

private @NotNull SentryAttributeType getType(final @Nullable Object arg) {
if (arg instanceof Boolean) {
return SentryAttributeType.BOOLEAN;
}
if (arg instanceof Integer) {
return SentryAttributeType.INTEGER;
}
if (arg instanceof Number) {
return SentryAttributeType.DOUBLE;
}
return SentryAttributeType.STRING;
}
}
27 changes: 10 additions & 17 deletions sentry/src/main/java/io/sentry/metrics/MetricsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.sentry.util.Platform;
import io.sentry.util.TracingUtils;
import java.util.HashMap;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -167,6 +168,14 @@ private void captureMetrics(
private @NotNull HashMap<String, SentryLogEventAttributeValue> createAttributes(
final @NotNull SentryMetricsParameters params) {
final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes = new HashMap<>();

final @NotNull Map<String, SentryAttribute> scopeAttributes =
scopes.getCombinedScopeView().getAttributes();
for (SentryAttribute scopeAttribute : scopeAttributes.values()) {
attributes.put(
scopeAttribute.getName(), SentryLogEventAttributeValue.fromAttribute(scopeAttribute));
}

final @NotNull String origin = params.getOrigin();
if (!"manual".equalsIgnoreCase(origin)) {
attributes.put(
Expand All @@ -177,10 +186,7 @@ private void captureMetrics(

if (incomingAttributes != null) {
for (SentryAttribute attribute : incomingAttributes.getAttributes().values()) {
final @Nullable Object value = attribute.getValue();
final @NotNull SentryAttributeType type =
attribute.getType() == null ? getType(value) : attribute.getType();
attributes.put(attribute.getName(), new SentryLogEventAttributeValue(type, value));
attributes.put(attribute.getName(), SentryLogEventAttributeValue.fromAttribute(attribute));
}
}

Expand Down Expand Up @@ -279,17 +285,4 @@ private void setUser(final @NotNull HashMap<String, SentryLogEventAttributeValue
}
}
}

private @NotNull SentryAttributeType getType(final @Nullable Object arg) {
if (arg instanceof Boolean) {
return SentryAttributeType.BOOLEAN;
}
if (arg instanceof Integer) {
return SentryAttributeType.INTEGER;
}
if (arg instanceof Number) {
return SentryAttributeType.DOUBLE;
}
return SentryAttributeType.STRING;
}
}
Loading