Skip to content

Ensure that log attributes of Object type are stringified #4409

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

Open
wants to merge 1 commit into
base: feat/logs-attributes-in-api
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ public final class SentryLogEventAttributeValue implements JsonUnknown, JsonSeri

public SentryLogEventAttributeValue(final @NotNull String type, final @Nullable Object value) {
this.type = type;
this.value = value;
if (value != null && type.equals("string")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we don't have a dedicated object type? I think this looks a bit ugly from an API standpoint to specify a string type and then pass an object in 😬

I think our UI supports building trees based on the dot-convention, so how I'd image it looking in pseudocode:

// we get this as an input
"point" to SentryLogEventAttributeValue("object", Point(20,30))

// we now know it's an object type so we should dismember it via reflection
val parentKey = "point"
for (field in getDeclaredFields) {
  // do this recursively for nested objects
  serialize(parentKey + "." + field.name to SentryLogEventAttributeValue(field.type, field.value))
}

Do you think that would be feasible? The object type is only on the SDK level but does not get passed to our backend. Kind of like our JsonObjectSerializer does via reflection.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering how objects were actually serialized as JSON :)

So, for what it's worth this SentryLogEventAttributeValue is not really meant to be used directly by users, I think it should be marked ApiStatus.Internal.
We expect the user to call log(attributes, ...) where attributes is a Map<String, Object>.
So we could have an object type that we use internally, also I think we should really use an enum for the type and not a string.

Anyway, I think yours is a cool idea, I just don't know if that's what a user would expect to see there, or if they would rather see value.toString() as the attribute value. In the Java world I would say people would expect value.toString(), what do you think?
Let me also check internally what we expect from an end product prespective because this could apply to many other platforms.

this.value = value.toString();
} else {
this.value = value;
}
}

public @NotNull String getType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ class SentryLogsSerializationTest {
"sentry.sdk.name" to SentryLogEventAttributeValue("string", "sentry.java.spring-boot.jakarta"),
"sentry.environment" to SentryLogEventAttributeValue("string", "production"),
"sentry.sdk.version" to SentryLogEventAttributeValue("string", "8.11.1"),
"sentry.trace.parent_span_id" to SentryLogEventAttributeValue("string", "f28b86350e534671")
"sentry.trace.parent_span_id" to SentryLogEventAttributeValue("string", "f28b86350e534671"),
"custom.boolean" to SentryLogEventAttributeValue("boolean", true),
"custom.double" to SentryLogEventAttributeValue("double", 11.12.toDouble()),
"custom.point" to SentryLogEventAttributeValue("string", Point(20, 30)),
"custom.integer" to SentryLogEventAttributeValue("integer", 10)
)
it.severityNumber = 10
}
Expand Down Expand Up @@ -75,4 +79,12 @@ class SentryLogsSerializationTest {
val reader = JsonObjectReader(StringReader(json))
return SentryLogEvents.Deserializer().deserialize(reader, fixture.logger)
}

companion object {
data class Point(val x: Int, val y: Int) {
override fun toString(): String {
return "Point{x:$x,y:$y}-Hello"
}
}
}
}
18 changes: 18 additions & 0 deletions sentry/src/test/resources/json/sentry_logs.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@
{
"type": "string",
"value": "f28b86350e534671"
},
"custom.boolean":
{
"type": "boolean",
"value": true
},
"custom.double": {
"type": "double",
"value": 11.12
},
"custom.point": {
"type": "string",
"value": "Point{x:20,y:30}-Hello"
},
"custom.integer":
{
"type": "integer",
"value": 10
}
}
}
Expand Down
Loading