Repository navigation
Conversation
Bound attribute nesting at 20 levels and replace cyclic or deeper values with None. Clean top-level attributes independently so invalid values do not discard valid siblings in spans, resources, and metric measurements. Add API, SDK, resource, and measurement regression tests. Fixes open-telemetry#5564 Assisted-by: Kimi
Assisted-by: Kimi
Pull request dashboard statusWaiting on the author · refreshed 2026-12-02 18:08 UTC Respond to 2 review items (e.g. link a commit, explain why not, ask a follow-up): Status above doesn't look right?
|
DylanRussell
left a comment
There was a problem hiding this comment.
Thanks for picking this up! The cycle detection and top-level isolation look good overall — a few items to address:
- The
@overloadstubs on_clean_attribute_value(opentelemetry-api/src/opentelemetry/attributes/__init__.pylines 36–47) are now unused (since_set_itemscalls_clean_attributes) and no longer accurate: passing a cyclic or overly nestedMappingnow returnsNone, notMapping[str, types.AnyValue]. Those overloads can be removed. - See inline comments below (in particular,
pyrightcurrently fails on the new API test file).
|
|
||
| def _cyclic_list(): | ||
| value = [1, 2] | ||
| value.append(value) |
There was a problem hiding this comment.
pyright (tox -e typecheck) currently fails with 7 errors on this file (unlike test_attributes.py, this file doesn't opt out of type checking):
- Line 11:
value = [1, 2]; value.append(value)—list[int]not assignable toint(reportArgumentType) - Line 29:
value = {"value": 1}; value["self"] = value—dict[str, int]not assignable toint(reportArgumentType) - Line 37:
cleaned = cleaned[0]—self.assertIsInstance(cleaned, tuple)doesn't narrowAnyValuein pyright (reportIndexIssue/reportArgumentType/reportOptionalSubscript)
Annotating value: list[Any] = [1, 2] / value: dict[str, Any] = {"value": 1} and narrowing cleaned (e.g. assert isinstance(cleaned, tuple)) should clear these.
| The recursively cleaned AnyValue. | ||
| """ | ||
| try: | ||
| return _clean_attribute_value_impl(value, max_value_length, 0, set()) |
There was a problem hiding this comment.
This allocates a new set() and makes an extra _clean_attribute_value_impl call on every attribute value, including primitives (str, int, float, bool, None), which make up the vast majority of attributes. Fast-pathing primitives here (or lazily allocating ancestors only when encountering (Sequence, Mapping)) avoids that overhead on the hot path.
| depth: int, | ||
| ancestors: set[int], | ||
| ) -> types.AnyValue: | ||
| if depth > _MAX_NESTING_DEPTH: |
There was a problem hiding this comment.
Minor nit: checking depth > _MAX_NESTING_DEPTH at entry allows 21 nested empty containers (e.g. [[...[]...]], where the 21st container is at depth == 20 and never recurses), whereas 21 nested non-empty containers are rejected.
| @@ -0,0 +1 @@ | |||
| Prevent cyclic or excessively nested attribute values from raising `RecursionError`. | |||
There was a problem hiding this comment.
Nit: consider prefixing with the affected packages to match existing changelog entries:
| Prevent cyclic or excessively nested attribute values from raising `RecursionError`. | |
| `opentelemetry-api`, `opentelemetry-sdk`: prevent cyclic or excessively nested attribute values from raising `RecursionError`. |
Remove obsolete overloads, fast-path scalar attributes, allocate ancestor tracking lazily, and reject excessive empty containers consistently. Fix test typing and name the affected packages in the changelog. Assisted-by: Kimi
Replaces #5976. Uses OpenTelemetry's
Assisted-byAI disclosure convention while retainingcsw7777as the sole human commit author.Summary
Fixes #5564.
_clean_attribute_valueoverloads and apply the depth limit consistently to empty and non-empty containers.Noneinstead of leakingRecursionErrorinto application code.Measurementattributes.BoundedAttributes, and metric measurements.This follows the 20-level approach previously reviewed in #5565 and #5757. In particular, it adds the metrics regression coverage requested in #5757 and reapplies the fix onto current
main.Compatibility
Only values that previously caused
RecursionErrorare affected. Valid values at the supported depth remain intact, and valid sibling attributes are preserved. A warning is emitted when a value is replaced withNone.Validation
pyrighton the changed API source and test file: 0 errors, 0 warnings.ruff formatandruff checkon every changed Python file: passed.pylinton every changed Python file: passed, 10.00/10.The focused suites were:
python -m pytest opentelemetry-api/tests/attributes \ opentelemetry-sdk/tests/test_attribute_nesting_depth.py \ opentelemetry-sdk/tests/metrics/test_metrics.py -q -o addopts=""🤖 Generated with Kimi