Skip to content

fix(exporter/otlp): support non-standard types as attribute values and log body#5239

Open
grvmishra788 wants to merge 8 commits into
open-telemetry:mainfrom
grvmishra788:worktree-issue-5210
Open

fix(exporter/otlp): support non-standard types as attribute values and log body#5239
grvmishra788 wants to merge 8 commits into
open-telemetry:mainfrom
grvmishra788:worktree-issue-5210

Conversation

@grvmishra788
Copy link
Copy Markdown
Contributor

@grvmishra788 grvmishra788 commented May 21, 2026

Description

Passing a pathlib.Path (or any other type outside the OTLP AnyValue spec) as a span/log attribute value or log body caused the OTLP exporter to crash:

Exception: Invalid type <class 'pathlib.PosixPath'> of value /path/to/something

Both the proto-common and json-common OTLP encoders (_encode_value()) had no fallback for unrecognised types. This PR adds a str() best-effort fallback before raising, so non-standard types are encoded as their string representation instead of crashing the exporter. If str() itself raises, the original exception propagates unchanged.

The fix mirrors the existing _clean_extended_attribute_value() behaviour in the SDK and is applied consistently across both encoder implementations.

Fixes #5210.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • New tests added to both encoder test suites.
  • Existing None-handling tests (allow_null=True/False) and list-with-None error paths all pass unchanged.

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@grvmishra788 grvmishra788 requested a review from a team as a code owner May 21, 2026 02:22
grvmishra788 and others added 3 commits May 21, 2026 02:27
…c-methods

- Use str(Path(...)) as expected value so tests pass on Windows (backslash separator)
- Add pylint disable for too-many-public-methods on TestCommonEncoder (21 methods)
]
)
)
# Third-party instrumentation can inject arbitrary types that cannot be exhaustively
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instrumentation should not do this, and ideally has type checking or something to ensure that it isn't doing this.. I would log an error here too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added _logger.error() before the str() fallback in both json-common and proto-common

@grvmishra788 grvmishra788 requested a review from DylanRussell May 21, 2026 21:03
Copy link
Copy Markdown
Contributor

@herin049 herin049 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I don't think this added behavior should be something that lives in the exporter at all. The OTel Python internals should always be operating with only the types outlined in the spec. If we do decide we want to support automatic coercion of attribute types, it should be performed at a much higher level (e.g. at .set_attribute(...)). There is another discussion entirely about whether we do want to support this behavior.

@herin049 herin049 moved this to Reviewed PRs that need fixes in Python PR digest May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

Feature Request: Support for pathlib.Path objects in OTLP Exporter attributes

3 participants