Skip to content
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

bug: promslog panics if a user sets a log attribute with a key of source #745

Open
tjhop opened this issue Jan 10, 2025 · 1 comment
Open

Comments

@tjhop
Copy link
Contributor

tjhop commented Jan 10, 2025

This issue is for tracking purposes, @SuperQ and I are discussing things further in slack.


In our slog implementation, we always instruct slog to emit source code info of the calling log statement:
https://github.com/prometheus/common/blob/main/promslog/slog.go#L183

We also do some default formatting of the resulting source code position info, both in the default implementation and the legacy go-kit styled implementation:
https://github.com/prometheus/common/blob/main/promslog/slog.go#L77-L79
https://github.com/prometheus/common/blob/main/promslog/slog.go#L54-L63

As mentioned in slog's docs, the AddSource field adds a key-value pair onto the logger keyed by SourceKey, which is a constant value of source:
https://pkg.go.dev/log/slog#pkg-constants

We key on that SourceKey in our ReplaceAttr funcs that we use for formatting log lines, and when an attribute is found with a key equal to SourceKey, it extracts the any from the value and casts it to an *slog.Source. We do not check the error from the type cast, as slog always adds source info as an attribute with a key of SourceKey and value of slog.Source.

This implementation is naive, as users may add their own attributes with a key of source, causing promslog to panic:
SuperQ/chrony_exporter#107

Putting aside the usefulness/validity of duplicate keys in a structured log message, this is quite the landmine for users to run into, and promslog should handle it more gracefully.

@SuperQ
Copy link
Member

SuperQ commented Jan 10, 2025

We could do something similar to what we do for labels in Prometheus. We can have a set of reserved keys and rename duplicate keys to logged_KEY. So if someone logs source, it would become logged_source.

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

No branches or pull requests

2 participants