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
Many 3rd party libraries (unfortunately) use a logging.Formatter to mutate the log record, for example:
celery.app.log.TaskFormatter -- adds task_id & task_name to the record.__dict__ in the TaskFormatter.format() method
uvicorn.logging.AccessFormatter -- adds client_addr, request_line and status_code to the __dict__ of the record (via a shallow copy)
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)
ifself.formatter:
body=self.format(record)
else:
# ...# defer until the formatter has had a chance to mutateattributes=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
The text was updated successfully, but these errors were encountered:
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
Is your feature request related to a problem?
Many 3rd party libraries (unfortunately) use a
logging.Formatter
to mutate the logrecord
, for example:task_id
&task_name
to therecord.__dict__
in theTaskFormatter.format()
methodclient_addr
,request_line
andstatus_code
to the__dict__
of therecord
(via a shallow copy)As a result the
LoggingHandler
fails to capture valuable attributes as often as it is used in tandem with aFormatter
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 logrecord
(usingself._get_attributes(record)
) after the formattedbody
has been calculated usingself.format(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
The text was updated successfully, but these errors were encountered: