Repository navigation
fix: encode bytes, mapping attributes and dropped counts per OTLP/JSON - #2827
tonyandrewmeyer merged 2 commits into
Conversation
tonyandrewmeyer
left a comment
There was a problem hiding this comment.
Thanks for this, and for splitting it out from the span kind fix! I ran the encoder before and after this change into a local OpenTelemetry Collector (0.162.0, OTLP/HTTP with the debug exporter). Before the change, a mapping attribute came out as Map({"":null}) and b'hi' arrived as the wrong bytes. After it, both decode correctly, and the dropped count shows up. So with the Collector, the old mapping encoding meant silently lost data rather than a rejected request.
I don't think the proto round-trip test is needed. The expected values in the tests match what the Collector decoded, and I'd rather not add a test dependency for it.
Once #2824 is merged, the rebase should leave the import in test_otlp_json.py without the # type: ignore (#2824 dropped it). Also a small one: the encoder doesn't send links at all at the moment, so I'd drop "and links" from the description, since it'll end up in the commit message.
- Encode bytes attribute values as base64 rather than hex. Only trace and span IDs are hex in OTLP/JSON, so receivers decoded the hex string as base64 and got corrupted bytes. - Encode mapping attribute values as a list of key/value pairs. The old shape was rejected by strict protobuf JSON parsers, which fails the whole export request. - Use the lowerCamelCase droppedAttributesCount key, and include the attributes the SDK dropped due to span limits in the count.
1532e0c to
643b068
Compare
|
Thanks for testing it against a real Collector! Good to know the old mapping encoding was losing data silently, not just getting rejected.
|
canonical#2827) Fixes canonical#2826. Changes to `ops_tracing._otlp_json`, tested with `opentelemetry-sdk` 1.45.0: - **Bytes attributes** are now base64-encoded. In OTLP/JSON only `traceId` and `spanId` are hex. Before this change, `b'hi'` was sent as `"6869"` and decoded by receivers as `b'\xeb\xce\xbd'`. The `# FIXME: not reached!` comment was out of date, because the SDK does keep `bytes` values, so I removed it. - **Mapping attributes** are now encoded as `{"key": ..., "value": ...}` pairs. Before, a single mapping attribute made a strict protobuf JSON parser reject the whole export request. - **Dropped attribute count** now uses the lowerCamelCase key `droppedAttributesCount`, as OTLP/JSON requires. The count now also includes attributes the SDK dropped due to span limits (`dropped_attributes` on spans and events), not only the ones the encoder couldn't convert. `Resource` doesn't track dropped attributes, so it falls back to 0. ## Testing - New unit tests in `tracing/test/test_otlp_json.py` cover bytes, mappings, and the dropped count from span and event limits and from values the encoder can't convert. 4 of them failed before the fix. - The tracing unit tests pass, and `ruff check --preview`, `ruff format --preview --check`, `codespell` and `pyright` are clean. - I also checked by hand that the output parses with `google.protobuf.json_format.Parse` into `ExportTraceServiceRequest` in strict mode (unknown fields not ignored). The checks were: - a CLIENT span with bytes, nested mapping and array attributes - an event with a bytes attribute - a span over its attribute limit Everything round-tripped to the original values and counts. - Checked during review against an OpenTelemetry Collector (0.162.0): bytes, mappings and the dropped count all decode correctly. (cherry picked from commit 4a7b1c3)
The bytes, mapping and mixed-type list tests from the canonical#2827 backport need an opentelemetry-sdk that keeps those attribute values. The repository root's lock on this branch has opentelemetry-sdk 1.34, which converts or drops them before the encoder sees them, so `tox -e coverage-tracing` failed. They still run under `tracing/`, which has 1.45. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nch (#2964) This cherry-picks fixes from main that were merged between the last backport round (#2789) and 21 November: * #2738: a non-leader touching its own application databag gets the "is not leader" `RelationDataAccessError` on every access path, rather than a bare `ModelError: permission denied`. * #2780: an `_Abort(0)` from the charm's `__init__` (for example, `load_config(errors='blocked')`) is treated as success in `ops.testing`, as it is in production. * #2787: `Context.trace_data` is no longer empty after the first `Context` with opentelemetry-sdk 1.45. * #2824: ops-tracing sends the correct OTLP span kind for spans that aren't INTERNAL. * #2847 and #2867: a `user-id` or `group-id` of 0 is kept in Pebble services and exec checks. * #2857: the consistency checker accepts tagged Juju versions (like `3.6-beta1`) and action parameters with a list of types. * #2877 and #2876: an error connecting the exec websockets cleans up properly. * #2875: errors reading the Pebble response are wrapped in `ConnectionError`, so `Container.can_connect()` returns `False` rather than raising when Pebble drops the connection. * #2827: OTLP/JSON encoding of bytes and mapping attributes, and of dropped attribute counts. * #2870: exec output is decoded incrementally, so a multi-byte character split across messages no longer raises. * #2872: combining layers in Harness and Scenario keeps the check level, and the original `override`, as Pebble does. * #2871: `pull()` works with paths that have non-ASCII characters or leading or trailing whitespace. * #2855 (partly): `State(opened_ports=[ops.Port(...)])` works, and Scenario ports compare equal to an equal `ops.Port`. * #2917: `get_notices(types=...)` filters correctly in Harness and `ops.testing`. * #2901: `push()` escapes backslashes in the filename. It also includes #2090, which was merged in October 2025 but never backported: an action declared with the shorthand (no params or description, so `null` in `actions.yaml`) no longer crashes the consistency checker. There were some adjustments, which are described in the individual commit messages: * #2780: on this branch, a non-zero `_Abort` is raised as is rather than wrapped in `UncaughtCharmError`, so the new tests expect that. The runtime's `_Abort(0)` handling also covers constructing the charm here, so it now only swallows one once the `Ops` object exists (otherwise a leaked `OPERATOR_DISPATCH` ended in an `AttributeError`). This branch also doesn't have the framework close from #2541 when constructing the charm fails, so `Ops` closes the framework itself after swallowing an `_Abort(0)`. * #2857: `JujuVersion` doesn't accept a bare major version like `'3'`, but the checker on this branch always has (and the tests use them), so I kept accepting those. * #2855: I only ported the `ops.Port` and equality parts. The duplicate-entity checks could start failing existing tests, which seems wrong for a maintenance release. The other `State` fixes in #2855 (comparing unit IDs with `is not`, the shared default `BindAddress`, the `deferred()` error message, and the `get_relation` docstring) aren't included either. `State` only replaced `opened_ports` when the converted list compared unequal, which the new equality defeats, so it now always stores the converted ports. * #2875: before Python 3.10, `socket.timeout` isn't a subclass of `TimeoutError`, so it's checked for as well. Otherwise a timeout waiting for Pebble's response became a plain `ConnectionError` on 3.8 and 3.9, and `wait_change()` stopped retrying it. * #2901: I left out the new `ValueError` for a path containing CR, LF, or NUL. It isn't an `OSError` or a `pebble.Error`, so `Container.push_path()` would stop collecting it and give up on the remaining files. Those paths still get Pebble's `APIError`, as before. `push()` only takes a `str` here, so the real Pebble tests (this one and the #2871 one) pass `str(path)`. * There's an extra commit that skips three of the #2827 tests when opentelemetry-sdk drops bytes, mapping, and mixed-type list values before the encoder sees them. The root lock on this branch has 1.34, which does, so `tox -e coverage-tracing` failed. They still run under `tracing/`. #2738 can turn a charm's passing unit tests red, because Harness and Scenario now raise for something production already did, so the 2.23.x release notes should call it out. Something like: > In Harness and `ops.testing`, a non-leader unit can no longer read its own application databag with `in`, `len()` or iteration, or `update()` it, matching what happens in production ([#2738](#2738)). Unit tests that did this will now raise `RelationDataAccessError`. I left out #2748 (it's built on the hookcmds-based error handling, so would need rewriting), #2851 (`push_path` skipping symlinks is a behaviour change), and #2878 (`CheckInfo.level` changes from `None` to `UNSET`). Fixes merged after 21 November can go in the next round. I've run `tox -e lint`, `static`, `unit`, `py3.8-unit`, `coverage-tracing`, and `pebble` locally, as well as lint, static, and unit in `tracing/`. --------- Co-authored-by: James Garner <james.garner@canonical.com> Co-authored-by: Gangavarapu Vivek <gangavarapu.0104@gmail.com> Co-authored-by: Suyash Bhattarai <133975972+ItsM0rty@users.noreply.github.com> Co-authored-by: Ben Hoyt <ben.hoyt@canonical.com> Co-authored-by: nancy <singhnancy2004@gmail.com> Co-authored-by: Matt Patchava <matt.patchava@canonical.com> Co-authored-by: Indian-boult <64060864+Indian-boult@users.noreply.github.com> Co-authored-by: Dima Tisnek <dima.tisnek@canonical.com>
Fixes #2826.
Changes to
ops_tracing._otlp_json, tested withopentelemetry-sdk1.45.0:traceIdandspanIdare hex. Before this change,b'hi'was sent as"6869"and decoded by receivers asb'\xeb\xce\xbd'. The# FIXME: not reached!comment was out of date, because the SDK does keepbytesvalues, so I removed it.{"key": ..., "value": ...}pairs. Before, a single mapping attribute made a strict protobuf JSON parser reject the whole export request.droppedAttributesCount, as OTLP/JSON requires. The count now also includes attributes the SDK dropped due to span limits (dropped_attributeson spans and events), not only the ones the encoder couldn't convert.Resourcedoesn't track dropped attributes, so it falls back to 0.Testing
New unit tests in
tracing/test/test_otlp_json.pycover bytes, mappings, and the dropped count from span and event limits and from values the encoder can't convert. 4 of them failed before the fix.The tracing unit tests pass, and
ruff check --preview,ruff format --preview --check,codespellandpyrightare clean.I also checked by hand that the output parses with
google.protobuf.json_format.ParseintoExportTraceServiceRequestin strict mode (unknown fields not ignored). The checks were:Everything round-tripped to the original values and counts.
Checked during review against an OpenTelemetry Collector (0.162.0): bytes, mappings and the dropped count all decode correctly.