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

Improve attributes validation #460

Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import random
import threading
from contextlib import contextmanager
from numbers import Number
from types import TracebackType
from typing import Iterator, Optional, Sequence, Tuple, Type

Expand Down Expand Up @@ -233,12 +232,16 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
logger.warning("Setting attribute on ended span.")
return

if not key:
logger.warning("invalid key (empty or null)")
return

if isinstance(value, Sequence):
error_message = self._check_attribute_value_sequence(value)
if error_message is not None:
logger.warning("%s in attribute value sequence", error_message)
return
elif not isinstance(value, (bool, str, Number, Sequence)):
elif not isinstance(value, (bool, str, int, float)):
logger.warning("invalid type for attribute value")
return

Expand All @@ -254,10 +257,7 @@ def _check_attribute_value_sequence(sequence: Sequence) -> Optional[str]:

first_element_type = type(sequence[0])

if issubclass(first_element_type, Number):
first_element_type = Number

if first_element_type not in (bool, str, Number):
if first_element_type not in (bool, str, int, float):
return "invalid type"

for element in sequence:
Expand Down
19 changes: 16 additions & 3 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def test_attributes(self):

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

self.assertEqual(len(root.attributes), 10)
self.assertEqual(root.attributes["component"], "http")
Expand All @@ -409,7 +409,7 @@ def test_attributes(self):
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, 314, 0]
)

attributes = {
Expand Down Expand Up @@ -440,6 +440,9 @@ def test_invalid_attribute_values(self):
"list-with-non-primitive-data-type", [dict(), 123]
)

root.set_attribute("", 123)
root.set_attribute(None, 123)

self.assertEqual(len(root.attributes), 0)

def test_check_sequence_helper(self):
Expand All @@ -458,8 +461,18 @@ def test_check_sequence_helper(self):
),
"different type",
)
self.assertEqual(
trace.Span._check_attribute_value_sequence([1, 2, 3.4, 5]),
"different type",
)
self.assertIsNone(
trace.Span._check_attribute_value_sequence([1, 2, 3, 5])
)
self.assertIsNone(
trace.Span._check_attribute_value_sequence([1.2, 2.3, 3.4, 4.5])
)
self.assertIsNone(
trace.Span._check_attribute_value_sequence([1, 2, 3.4, 5])
trace.Span._check_attribute_value_sequence([True, False])
)
self.assertIsNone(
trace.Span._check_attribute_value_sequence(["ss", "dw", "fw"])
Expand Down