Skip to content

A bytes-valued Resource attribute breaks every OTLP encoder #5576

Description

@dwin-gharibi

Describe your environment

Labels: bug, sdk, exporter, data-loss
Affected packages: opentelemetry-sdk
Found on: main @ 0a5d76b6
Environment: CPython 3.12

What happened?

Resource.__hash__ is implemented as hash(json.dumps(attributes, sort_keys=True)). types.AnyValue now includes bytes, which json.dumps refuses, so hashing a Resource that carries any bytes attribute raises TypeError.

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.

Steps to Reproduce

from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.exporter.otlp.proto.common.trace_encoder import encode_spans

resource = Resource.create({"service.name": "svc", "build.id": b"\x01\x02\x03"})

exporter = InMemorySpanExporter()
provider = TracerProvider(resource=resource)
provider.add_span_processor(SimpleSpanProcessor(exporter))
with provider.get_tracer(__name__).start_as_current_span("span"):
    pass

encode_spans(exporter.get_finished_spans())

Expected Result

The Resource hashes, serialises and exports normally. bytes is a valid attribute value type and encodes to the OTLP bytes_value field.

Actual Result

TypeError: Object of type bytes is not JSON serializable

# the same failure from three independent directions
hash(resource)        TypeError: Object of type bytes is not JSON serializable
resource.to_json()    TypeError: Object of type bytes is not JSON serializable
{resource: 1}         TypeError: Object of type bytes is not JSON serializable

metrics encoder       TypeError: Object of type bytes is not JSON serializable
logs encoder          TypeError: Object of type bytes is not JSON serializable

# with BatchSpanProcessor the TypeError is swallowed by the processor
spans successfully exported: 0 out of 5

Additional context

Complete telemetry loss for any service that puts binary data in a resource attribute - a build fingerprint, a checksum, a compact instance identifier.

Severity comes from where it surfaces. BatchProcessor._export wraps the exporter call in a broad except Exception, so with the standard BatchSpanProcessor this is not a crash but a permanent, silent 100% export failure with only a log line per batch. Nothing else misbehaves, which makes it easy to misread as a collector problem.

Note the asymmetry that makes this surprising: bytes works correctly as a span attribute. Only Resource is affected.

Would you like to implement a fix?

Yes

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions