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 2 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
3 changes: 1 addition & 2 deletions opentelemetry-api/src/opentelemetry/util/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
float,
Sequence[str],
Sequence[bool],
Sequence[int],
Sequence[float],
Sequence[Union[int, float]],
]
Attributes = Optional[Dict[str, AttributeValue]]
16 changes: 10 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,12 +257,13 @@ 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"

# int and float are both numeric types, allow mixed arrays of those
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and Sequence[Union[int, float]] above: do we really want to allow mixed-type array attributes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. #348 added a specific test for that. The specification is not that clear, it talks about numeric as a primitive type [1]. Having a second thought, float and int are treated different in the exporters, so I think it would make sense not to avoid mixed arrays of those. If you agree I could update this.

[1] https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/api-tracing.md#set-attributes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want this to block the PR, up to you whether to update this in this PR or another one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the PR to avoid that kind of mixed attributes.

if first_element_type in (int, float):
first_element_type = (int, float)

for element in sequence:
if not isinstance(element, first_element_type):
return "different type"
Expand Down
3 changes: 3 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
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 Down