logging: exception.message is the exception's first argument, not its message

Open Beginner friendly
#5,353 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python

Research direction

Start with LoggingHandler._get_attributes in opentelemetry/instrumentation/logging/handler.py and inspect the same expression in opentelemetry/instrumentation/structlog/__init__.py and the deprecated SDK handler at opentelemetry/sdk/_logs/_internal/__init__.py. Check the tests for exception attributes and compare log output with Span.record_exception for the examples in the issue. Done means all three handlers export str(exception) consistently, including exceptions with multiple or no arguments.

Written by the indexing model from the issue text.

Description

(Posted by Claude on my behalf)

Describe your environment

OS: Linux
Python version: 3.13
Package version: opentelemetry-instrumentation-logging 0.66b2, opentelemetry-sdk 1.45.2 (the same code is on main)

What happened?

LoggingHandler sets exception.message to str(value.args[0]). For any exception whose message is not its first argument, the exported attribute is wrong, and it disagrees with what Span.record_exception exports for the same exception, which uses str(exception).

Exception str(exc) log exception.message span exception.message
OSError(2, "No such file or directory") [Errno 2] No such file or directory 2 [Errno 2] No such file or directory
ValueError("bad value", 42) ('bad value', 42) bad value ('bad value', 42)
ValueError() (empty) attribute absent (empty)

OSError and its subclasses are the common case: every file and socket error exports its errno as the message.

Steps to Reproduce
import logging

from opentelemetry.instrumentation.logging.handler import LoggingHandler
from opentelemetry.sdk._logs import LoggerProvider
from opentelemetry.sdk._logs.export import InMemoryLogRecordExporter, SimpleLogRecordProcessor

exporter = InMemoryLogRecordExporter()
provider = LoggerProvider()
provider.add_log_record_processor(SimpleLogRecordProcessor(exporter))
log = logging.getLogger("demo")
log.addHandler(LoggingHandler(logger_provider=provider))

try:
    raise OSError(2, "No such file or directory")
except OSError:
    log.exception("failed")

print(exporter.get_finished_logs()[-1].log_record.attributes["exception.message"])
Expected Result

[Errno 2] No such file or directory, i.e. str(exc), as Span.record_exception exports (EXCEPTION_MESSAGE: str(exception) in opentelemetry/sdk/trace/__init__.py).

Actual Result

2

Additional context

The same expression is in three places:

  • opentelemetry/instrumentation/logging/handler.py, LoggingHandler._get_attributes
  • opentelemetry/instrumentation/structlog/__init__.py, StructlogProcessor._get_attributes
  • opentelemetry/sdk/_logs/_internal/__init__.py, the SDK's deprecated LoggingHandler (in opentelemetry-python)
Would you like to implement a fix?

No

Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
3d 12h
Merged PRs (30d)
51

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from open-telemetry/opentelemetry-python-contrib

All issues in open-telemetry/opentelemetry-python-contrib

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.