Skip to content

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

@tjhop

Description

@tjhop

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions