A bytes-valued Resource attribute breaks every OTLP encoder - #5577
Open
dwin-gharibi wants to merge 2 commits into
Open
A bytes-valued Resource attribute breaks every OTLP encoder#5577dwin-gharibi wants to merge 2 commits into
dwin-gharibi wants to merge 2 commits into
Conversation
`bytes` is a valid attribute value type, and every OTLP encoder groups telemetry by using the Resource as a dict key. Assert that such a Resource stays hashable, usable as a dict key and serialisable, and that the span, metric and log export paths all survive it. These tests fail with "TypeError: Object of type bytes is not JSON serializable" against the current implementation.
`Resource.__hash__` and `to_json` serialise the attributes with `json.dumps`. `types.AnyValue` admits `bytes`, which `json.dumps` refuses, so hashing a Resource that carries any bytes attribute raises TypeError. Every OTLP encoder groups telemetry by using the Resource as a dictionary key, so the failure lands in the export path for all three signals. Under `BatchSpanProcessor` the exception is swallowed by the processor's broad exception handling, turning it into a permanent silent export failure rather than a visible crash. Give both call sites a `default` that renders bytes as hex. `__eq__` still distinguishes b"\x01" from the string "01", so the resulting hash collision is harmless, and `to_json` stays valid JSON.
Pull request dashboard statusWaiting on reviewers · refreshed 2026-08-23 17:11 UTC Review the latest changes. Status above doesn't look right?
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #5576.
Description
Resource.__hash__is implemented ashash(json.dumps(attributes, sort_keys=True)).types.AnyValuenow includesbytes, whichjson.dumpsrefuses, so hashing a Resource that carries any bytes attribute raisesTypeError.Every OTLP encoder uses the Resource as a dictionary key to group telemetry, so the failure lands squarely in the export path for all three signals.
Root cause
opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.py:256.__hash__derives its value by JSON-serialising the attributes, andto_jsonat line 258 has the same problem. Neither passes adefaulthandler, so any value outside JSON's native type set aborts the call.Approach
Add a
_json_defaulthandler used by both__hash__andto_jsonthat rendersbytes/bytearrayas hex and falls back tostrfor anything else unexpected.Rendering
b"\x01"and the string"01"identically means the two Resources hash alike, butResource.__eq__still distinguishes them, so dictionary lookups stay correct - a hash collision is a performance detail, not a correctness one.Deliberately minimal. Deriving a hash by serialising the entire attribute set on every call is wasteful for something used as a dictionary key in the export hot path, and a canonical-tuple hash would be a better long-term shape, but that is a larger change than this defect warrants.
Files changed
opentelemetry-sdk/src/opentelemetry/sdk/resources/__init__.pyopentelemetry-sdk/tests/resources/test_resource_bytes_attributes.py.changelog/5563.fixedTesting
A dedicated module covers the object contract - hashable, usable as a dict key,
to_jsonemits valid JSON, equal resources hash equally, differing bytes hash differently, and a bytes value stays distinct from its hex string - plus the full encode path for spans, metrics and logs, since the original failure only became visible at export.Eight of the nine fail before the change.
Result: 864 passed in
opentelemetry-sdk(855 baseline plus 9 new), 20 passed inopentelemetry-exporter-otlp-proto-common.Risk / compatibility
to_jsonoutput changes only for resources that previously could not be serialised at all. No existing behaviour is altered for resources whose attributes are JSON-native.