Skip to content
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

Add conversion from sequence to immutable tuple #401

Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
if error_message is not None:
logger.warning("%s in attribute value sequence", error_message)
return
elif not isinstance(value, str):
# convert value : sequence into immutable tuple lest client changes it.
value = tuple(value)
elif not isinstance(value, (bool, str, Number, Sequence)):
logger.warning("invalid type for attribute value")
return
Expand All @@ -261,6 +264,7 @@ def _check_attribute_value_sequence(sequence: Sequence) -> Optional[str]:
for element in sequence:
if not isinstance(element, first_element_type):
return "different type"

return None

def add_event(
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,12 @@ def test_attributes(self):
self.assertEqual(root.attributes["http.status_text"], "OK")
self.assertEqual(root.attributes["misc.pi"], 3.14)
self.assertEqual(root.attributes["attr-key"], "attr-value2")
self.assertEqual(root.attributes["empty-list"], [])
self.assertEqual(root.attributes["empty-list"], ())
self.assertEqual(
root.attributes["list-of-bools"], [True, True, False]
root.attributes["list-of-bools"], (True, True, False)
)
self.assertEqual(
root.attributes["list-of-numerics"], [123, 3.14, 0]
root.attributes["list-of-numerics"], (123, 3.14, 0)
)

attributes = {
Expand Down