Skip to content

Don't stringify all attribute values before encoding via json.Marshal #6

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

Merged
merged 4 commits into from
Oct 1, 2024

Conversation

addrummond
Copy link
Contributor

@addrummond addrummond commented Sep 26, 2024

Hey samber. Thanks for this library – it has been very useful.

This is an attempt to fix this issue.

In the current implementation, a call such as

type Foo struct {
  Bar string
  Amp string
}
slog.Info("Log message", "foo", Foo{Bar: "bar", Amp: "amp"})

will send JSON like

{..., "foo": "{Bar:bar Amp:amp}"}

where the Foo value has been stringified prior to JSON serialization. Similarly, integers and floats will appear as strings in the JSON sent to datadog.

This PR passes through types which have a reasonable JSON representation without converting them to strings first. The slog.Info call above now sends JSON like

{..., "foo": {"Bar": "bar", "Amp": "amp"}}

I've attempted to add tests which illustrate the fix and which break if the 'bug fix' commit is reverted. The test for the Handler is not ideal as it tests a private function, but I think it's a reasonable alternative to adding some quite complex mocking.

@samber
Copy link
Owner

samber commented Oct 1, 2024

Ok! Thanks for this contrib and your clear explanations. 👍

@samber samber merged commit 6471c8e into samber:main Oct 1, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom JSON marshaller
2 participants