-
-
Notifications
You must be signed in to change notification settings - Fork 451
Improve Log Attributes API #4416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feat/logs-attributes-in-api
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.sentry; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public final class SentryAttribute { | ||
|
||
private final @NotNull String name; | ||
private final @Nullable SentryAttributeType type; | ||
private final @Nullable Object value; | ||
|
||
private SentryAttribute( | ||
final @NotNull String name, | ||
final @Nullable SentryAttributeType type, | ||
final @Nullable Object value) { | ||
this.name = name; | ||
this.type = type; | ||
this.value = value; | ||
} | ||
|
||
public @NotNull String getName() { | ||
return name; | ||
} | ||
|
||
public @Nullable SentryAttributeType getType() { | ||
return type; | ||
} | ||
|
||
public @Nullable Object getValue() { | ||
return value; | ||
} | ||
|
||
public static @NotNull SentryAttribute named( | ||
final @NotNull String name, final @Nullable Object value) { | ||
return new SentryAttribute(name, null, value); | ||
} | ||
|
||
public static @NotNull SentryAttribute booleanAttribute( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. L: how about |
||
final @NotNull String name, final @Nullable Boolean value) { | ||
return new SentryAttribute(name, SentryAttributeType.BOOLEAN, value); | ||
} | ||
|
||
public static @NotNull SentryAttribute integerAttribute( | ||
final @NotNull String name, final @Nullable Integer value) { | ||
return new SentryAttribute(name, SentryAttributeType.INTEGER, value); | ||
} | ||
|
||
public static @NotNull SentryAttribute doubleAttribute( | ||
final @NotNull String name, final @Nullable Double value) { | ||
return new SentryAttribute(name, SentryAttributeType.DOUBLE, value); | ||
} | ||
|
||
public static @NotNull SentryAttribute stringAttribute( | ||
final @NotNull String name, final @Nullable String value) { | ||
return new SentryAttribute(name, SentryAttributeType.STRING, value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.sentry; | ||
|
||
import java.util.Locale; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public enum SentryAttributeType { | ||
STRING, | ||
BOOLEAN, | ||
INTEGER, | ||
DOUBLE; | ||
|
||
public @NotNull String apiName() { | ||
return name().toLowerCase(Locale.ROOT); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.sentry; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public final class SentryAttributes { | ||
|
||
private final @NotNull List<SentryAttribute> attributes; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we change this to Map<String, SentryAttribute>? |
||
|
||
private SentryAttributes(final @NotNull List<SentryAttribute> attributes) { | ||
this.attributes = attributes; | ||
} | ||
|
||
public void add(final @Nullable SentryAttribute attribute) { | ||
if (attribute == null) { | ||
return; | ||
} | ||
attributes.add(attribute); | ||
} | ||
|
||
public @NotNull List<SentryAttribute> getAttributes() { | ||
return attributes; | ||
} | ||
|
||
public static @NotNull SentryAttributes of(SentryAttribute... attributes) { | ||
return new SentryAttributes(Arrays.asList(attributes)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where we use this class, we rely on |
||
} | ||
|
||
public static @NotNull SentryAttributes fromMap(final @Nullable Map<String, Object> attributes) { | ||
if (attributes == null) { | ||
return new SentryAttributes(new ArrayList<>()); | ||
} | ||
SentryAttributes sentryAttributes = new SentryAttributes(new ArrayList<>(attributes.size())); | ||
for (Map.Entry<String, Object> attribute : attributes.entrySet()) { | ||
sentryAttributes.add(SentryAttribute.named(attribute.getKey(), attribute.getValue())); | ||
} | ||
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above, we could filter for nulls here. |
||
|
||
return sentryAttributes; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
|
||
import io.sentry.SentryDate; | ||
import io.sentry.SentryLogLevel; | ||
import java.util.Map; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
@@ -31,15 +30,8 @@ void log( | |
@Nullable Object... args); | ||
|
||
void log( | ||
@Nullable Map<String, Object> attributes, | ||
@NotNull SentryLogLevel level, | ||
@Nullable String message, | ||
@Nullable Object... args); | ||
|
||
void log( | ||
@Nullable Map<String, Object> attributes, | ||
@NotNull SentryLogLevel level, | ||
@Nullable SentryDate timestamp, | ||
@NotNull LogParams params, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. L: should it be named |
||
@Nullable String message, | ||
@Nullable Object... args); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.sentry.logger; | ||
|
||
import io.sentry.SentryAttributes; | ||
import io.sentry.SentryDate; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public final class LogParams { | ||
|
||
private @Nullable SentryDate timestamp; | ||
private @Nullable SentryAttributes attributes; | ||
|
||
public @Nullable SentryDate getTimestamp() { | ||
return timestamp; | ||
} | ||
|
||
public void setTimestamp(final @Nullable SentryDate timestamp) { | ||
this.timestamp = timestamp; | ||
} | ||
|
||
public @Nullable SentryAttributes getAttributes() { | ||
return attributes; | ||
} | ||
|
||
public void setAttributes(final @Nullable SentryAttributes attributes) { | ||
this.attributes = attributes; | ||
} | ||
|
||
public static @NotNull LogParams create( | ||
final @Nullable SentryDate timestamp, final @Nullable SentryAttributes attributes) { | ||
final @NotNull LogParams params = new LogParams(); | ||
|
||
params.setTimestamp(timestamp); | ||
params.setAttributes(attributes); | ||
|
||
return params; | ||
} | ||
Comment on lines
+29
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this not directly the constructor? |
||
|
||
public static @NotNull LogParams create(final @Nullable SentryAttributes attributes) { | ||
return create(null, attributes); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We call this in
sentry/src/main/java/io/sentry/SentryAttributes.java
when building attributes from a Map.As we take in a generic Map, the key for an entry could be
null
.We should check for nulls either here (making the
name
param nullable and e.g. not constructing the attribute if it is) or insentry/src/main/java/io/sentry/SentryAttributes.java
when iterating the entry set.