Skip to content

Extract attributes from stdlib record after formatting the log #4446

Description

@cruisehall

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

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions