@@ -440,6 +440,7 @@ def force_flush(self, timeout_millis: int = 30000) -> bool:
440440 "exc_text" ,
441441 "filename" ,
442442 "funcName" ,
443+ "getMessage" ,
443444 "message" ,
444445 "levelname" ,
445446 "levelno" ,
@@ -505,51 +506,26 @@ def _translate(self, record: logging.LogRecord) -> LogRecord:
505506 observered_timestamp = time_ns ()
506507 span_context = get_current_span ().get_span_context ()
507508 attributes = self ._get_attributes (record )
508- # This comment is taken from GanyedeNil's PR #3343, I have redacted it
509- # slightly for clarity:
510- # According to the definition of the Body field type in the
511- # OTel 1.22.0 Logs Data Model article, the Body field should be of
512- # type 'any' and should not use the str method to directly translate
513- # the msg. This is because str only converts non-text types into a
514- # human-readable form, rather than a standard format, which leads to
515- # the need for additional operations when collected through a log
516- # collector.
517- # Considering that he Body field should be of type 'any' and should not
518- # use the str method but record.msg is also a string type, then the
519- # difference is just the self.args formatting?
520- # The primary consideration depends on the ultimate purpose of the log.
521- # Converting the default log directly into a string is acceptable as it
522- # will be required to be presented in a more readable format. However,
523- # this approach might not be as "standard" when hoping to aggregate
524- # logs and perform subsequent data analysis. In the context of log
525- # extraction, it would be more appropriate for the msg to be
526- # converted into JSON format or remain unchanged, as it will eventually
527- # be transformed into JSON. If the final output JSON data contains a
528- # structure that appears similar to JSON but is not, it may confuse
529- # users. This is particularly true for operation and maintenance
530- # personnel who need to deal with log data in various languages.
531- # Where is the JSON converting occur? and what about when the msg
532- # represents something else but JSON, the expected behavior change?
533- # For the ConsoleLogExporter, it performs the to_json operation in
534- # opentelemetry.sdk._logs._internal.export.ConsoleLogExporter.__init__,
535- # so it can handle any type of input without problems. As for the
536- # OTLPLogExporter, it also handles any type of input encoding in
537- # _encode_log located in
538- # opentelemetry.exporter.otlp.proto.common._internal._log_encoder.
539- # Therefore, no extra operation is needed to support this change.
540- # The only thing to consider is the users who have already been using
541- # this SDK. If they upgrade the SDK after this change, they will need
542- # to readjust their logging collection rules to adapt to the latest
543- # output format. Therefore, this change is considered a breaking
544- # change and needs to be upgraded at an appropriate time.
545509 severity_number = std_to_otel (record .levelno )
546510 if self .formatter :
547511 body = self .format (record )
548512 else :
549- if isinstance (record .msg , str ) and record .args :
550- body = record .msg % record .args
551- else :
513+ # `record.getMessage()` uses `record.msg` as a template to format
514+ # `record.args` into. There is a special case in `record.getMessage()`
515+ # where it will only attempt formatting if args are provided,
516+ # otherwise, it just stringifies `record.msg`.
517+ #
518+ # Since the OTLP body field has a type of 'any' and the logging module
519+ # is sometimes used in such a way that objects incorrectly end up
520+ # set as record.msg, in those cases we would like to bypass
521+ # `record.getMessage()` completely and set the body to the object
522+ # itself instead of its string representation.
523+ # For more background, see: https://github.com/open-telemetry/opentelemetry-python/pull/4216
524+ if not record .args and not isinstance (record .msg , str ):
525+ # no args are provided so it's *mostly* safe to use the message template as the body
552526 body = record .msg
527+ else :
528+ body = record .getMessage ()
553529
554530 # related to https://github.com/open-telemetry/opentelemetry-python/issues/3548
555531 # Severity Text = WARN as defined in https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#displaying-severity.
0 commit comments