You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
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 bySourceKey
, which is a constant value ofsource
:https://pkg.go.dev/log/slog#pkg-constants
We key on that
SourceKey
in ourReplaceAttr
funcs that we use for formatting log lines, and when an attribute is found with a key equal toSourceKey
, it extracts theany
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 ofSourceKey
and value ofslog.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.
The text was updated successfully, but these errors were encountered: