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

Extract attributes from stdlib record after formatting the log #4446

Open
cruisehall opened this issue Feb 21, 2025 · 0 comments
Open

Extract attributes from stdlib record after formatting the log #4446

cruisehall opened this issue Feb 21, 2025 · 0 comments

Comments

@cruisehall
Copy link
Contributor

Is your feature request related to a problem?

Many 3rd party libraries (unfortunately) use a logging.Formatter to mutate the log record, for example:

As a result the LoggingHandler fails to capture valuable attributes as often as it is used in tandem with a Formatter that mutates log records as described above.

Describe the solution you'd like

I think opentelemetry.sdk._logs._internal.LoggingHandler._translate should extract the attributes from the log record (using self._get_attributes(record)) after the formatted body has been calculated using self.format(record).

    def _translate(self, record: logging.LogRecord) -> LogRecord:
        timestamp = int(record.created * 1e9)
        observered_timestamp = time_ns()
        span_context = get_current_span().get_span_context()
        # Instead of extracting attributes from 'record' here
        # attributes = self._get_attributes(record)
        severity_number = std_to_otel(record.levelno)
        if self.formatter:
            body = self.format(record)
        else:
           # ...
        # defer until the formatter has had a chance to mutate
        attributes = self._get_attributes(record)

Describe alternatives you've considered

I haven't thought of any alternatives, per se, but it's worth acknowledging that I don't think log formatters should be in the business of mutating the log record; python provides the "log factory" construct for this. Admittedly I'm asking the opentelemetry-python library to meet the Python ecosystem where it is.

Additional Context

No response

Would you like to implement a fix?

Yes

@cruisehall cruisehall changed the title Extract attributes from stdlib record _after_ formatting the log Extract attributes from stdlib record after formatting the log Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant