-
Notifications
You must be signed in to change notification settings - Fork 980
Self-referential attribute value crashes the instrumented application with RecursionError #5565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dwin-gharibi
wants to merge
4
commits into
open-telemetry:main
Choose a base branch
from
dwin-gharibi:fix/attribute-value-recursion-guard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3d83de1
test(api): cover cyclic and deeply nested attribute values
dwin-gharibi b0c34d7
test(sdk): assert telemetry APIs survive cyclic attribute values
dwin-gharibi 7734eaf
fix(api): cap attribute value nesting depth
dwin-gharibi 928417b
Merge branch 'main' into fix/attribute-value-recursion-guard
DylanRussell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `opentelemetry-api`: replace an attribute value nested more than 100 levels deep, including a self-referential one, with `None` instead of raising `RecursionError` into the caller |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Telemetry calls must never raise into the instrumented application. | ||
|
|
||
| `AnyValue` allows arbitrarily nested Sequence/Mapping attribute values, so a | ||
| user handing the SDK a self-referential structure must not blow the stack. | ||
| """ | ||
|
|
||
| import unittest | ||
|
|
||
| from opentelemetry.sdk._logs import LoggerProvider | ||
| from opentelemetry.sdk.metrics import MeterProvider | ||
| from opentelemetry.sdk.metrics.export import InMemoryMetricReader | ||
| 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, | ||
| ) | ||
|
|
||
|
|
||
| def _cyclic_list(): | ||
| value = [1, 2] | ||
| value.append(value) | ||
| return value | ||
|
|
||
|
|
||
| def _cyclic_dict(): | ||
| value = {"a": 1} | ||
| value["self"] = value | ||
| return value | ||
|
|
||
|
|
||
| class TestAttributeNestingDepthDoesNotRaise(unittest.TestCase): | ||
| def setUp(self): | ||
| self.exporter = InMemorySpanExporter() | ||
| self.tracer_provider = TracerProvider(shutdown_on_exit=False) | ||
| self.tracer_provider.add_span_processor(SimpleSpanProcessor(self.exporter)) | ||
| self.tracer = self.tracer_provider.get_tracer(__name__) | ||
|
|
||
| def test_set_attribute_with_cyclic_sequence(self): | ||
| with self.tracer.start_as_current_span("span") as span: | ||
| span.set_attribute("cyclic", _cyclic_list()) | ||
| self.assertIsNone(self.exporter.get_finished_spans()[0].attributes["cyclic"]) | ||
|
|
||
| def test_set_attribute_with_cyclic_mapping(self): | ||
| with self.tracer.start_as_current_span("span") as span: | ||
| span.set_attribute("cyclic", _cyclic_dict()) | ||
| self.assertIsNone(self.exporter.get_finished_spans()[0].attributes["cyclic"]) | ||
|
|
||
| def test_set_attributes_keeps_sibling_attributes(self): | ||
| """One unusable value must not discard the attributes beside it.""" | ||
| with self.tracer.start_as_current_span("span") as span: | ||
| span.set_attributes({"good": "kept", "cyclic": _cyclic_list()}) | ||
| attributes = self.exporter.get_finished_spans()[0].attributes | ||
| self.assertEqual(attributes["good"], "kept") | ||
| self.assertIsNone(attributes["cyclic"]) | ||
|
|
||
| def test_add_event_with_cyclic_value(self): | ||
| with self.tracer.start_as_current_span("span") as span: | ||
| span.add_event("event", {"cyclic": _cyclic_list()}) | ||
| event = self.exporter.get_finished_spans()[0].events[0] | ||
| self.assertIsNone(event.attributes["cyclic"]) | ||
|
|
||
| def test_resource_create_with_cyclic_value(self): | ||
| resource = Resource.create({"cyclic": _cyclic_list()}) | ||
| self.assertIsNone(resource.attributes["cyclic"]) | ||
|
|
||
| def test_counter_add_with_cyclic_value(self): | ||
| reader = InMemoryMetricReader() | ||
| meter_provider = MeterProvider(metric_readers=[reader]) | ||
| meter_provider.get_meter(__name__).create_counter("counter").add(1, {"cyclic": _cyclic_list()}) | ||
| metrics_data = reader.get_metrics_data() | ||
| data_point = metrics_data.resource_metrics[0].scope_metrics[0].metrics[0].data.data_points[0] | ||
| self.assertIsNone(dict(data_point.attributes)["cyclic"]) | ||
| meter_provider.shutdown() | ||
|
|
||
| def test_logger_emit_with_cyclic_value(self): | ||
| logger_provider = LoggerProvider(shutdown_on_exit=False) | ||
| # Must not raise; the record is dropped at the processor, not here. | ||
| logger_provider.get_logger(__name__).emit(body="body", attributes={"cyclic": _cyclic_list()}) | ||
|
|
||
| def tearDown(self): | ||
| self.tracer_provider.shutdown() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
100 seems too deep. Maybe we should cap this at like 20 ? @xrmx WDYT ?