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

Freeze sequence-valued span attributes #449

Merged
merged 4 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import threading
from contextlib import contextmanager
from types import TracebackType
from typing import Iterator, Optional, Sequence, Tuple, Type
from typing import Iterator, MutableSequence, Optional, Sequence, Tuple, Type

from opentelemetry import context as context_api
from opentelemetry import trace as trace_api
Expand Down Expand Up @@ -314,6 +314,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
# Freeze mutable sequences defensively
if isinstance(value, MutableSequence):
value = tuple(value)
elif not isinstance(value, (bool, str, int, float)):
logger.warning("invalid type for attribute value")
return
Expand Down
20 changes: 15 additions & 5 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,10 @@ def test_attributes(self):
root.set_attribute("attr-key", "attr-value2")

root.set_attribute("empty-list", [])
root.set_attribute("list-of-bools", [True, True, False])
root.set_attribute("list-of-numerics", [123, 314, 0])
list_of_bools = [True, True, False]
root.set_attribute("list-of-bools", list_of_bools)
list_of_numerics = [123, 314, 0]
root.set_attribute("list-of-numerics", list_of_numerics)

self.assertEqual(len(root.attributes), 10)
self.assertEqual(root.attributes["component"], "http")
Expand All @@ -426,12 +428,20 @@ 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)
c24t marked this conversation as resolved.
Show resolved Hide resolved
)
list_of_bools.append(False)
self.assertEqual(
root.attributes["list-of-numerics"], [123, 314, 0]
root.attributes["list-of-bools"], (True, True, False)
)
self.assertEqual(
root.attributes["list-of-numerics"], (123, 314, 0)
)
list_of_numerics.append(227)
self.assertEqual(
root.attributes["list-of-numerics"], (123, 314, 0)
)

attributes = {
Expand Down