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 runtime validation in setAttribute #348

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6914ff3
Validate attribute value data types before adding to span
jakemalachowski Dec 27, 2019
59107ad
Change order of checks to remove an else condition
jakemalachowski Dec 27, 2019
8152fdb
Create separate sequence check method, add tests, fix linting issues
jakemalachowski Dec 29, 2019
1d972e8
Fix attribute value typing
jakemalachowski Dec 29, 2019
c535e0c
Apply linting changes
jakemalachowski Dec 29, 2019
59dd5f5
Use is not None, use optional type instead of union with None
jakemalachowski Dec 31, 2019
2a5df2b
Validate attribute value data types before adding to span
jakemalachowski Dec 27, 2019
bacb09b
Change order of checks to remove an else condition
jakemalachowski Dec 27, 2019
3e67a9e
Create separate sequence check method, add tests, fix linting issues
jakemalachowski Dec 29, 2019
ff61b9e
Fix attribute value typing
jakemalachowski Dec 29, 2019
4d74316
Apply linting changes
jakemalachowski Dec 29, 2019
8a7ec1c
Use is not None, use optional type instead of union with None
jakemalachowski Dec 31, 2019
f05956b
Merge branch 'ISSUE-347/attribue-value-type-enforcement' of github.co…
jakemalachowski Jan 3, 2020
e7e976a
Update opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
jakemalachowski Jan 3, 2020
75261d8
Clarify variable and method names, remove redundant check
jakemalachowski Jan 3, 2020
ca68796
Merge branch 'ISSUE-347/attribue-value-type-enforcement' of github.co…
jakemalachowski Jan 3, 2020
a9c554a
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
jakemalachowski Jan 3, 2020
1321134
Re-add explicit return None
jakemalachowski Jan 3, 2020
269b006
Commit lint changes
jakemalachowski Jan 7, 2020
47a5887
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
jakemalachowski Jan 7, 2020
669b3eb
Lint changes
jakemalachowski Jan 7, 2020
4a5812c
Lint changes
jakemalachowski Jan 7, 2020
6f1dd5d
Use number instead of int, float
jakemalachowski Jan 11, 2020
5225928
Revert AttributeValue typing change
jakemalachowski Jan 16, 2020
b83dc7b
Prevent duplicate isinstance checks, run linter
jakemalachowski Jan 17, 2020
d8e5946
Lint
jakemalachowski Jan 17, 2020
ad58890
Move length check inside validation method
jakemalachowski Jan 18, 2020
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
2 changes: 1 addition & 1 deletion opentelemetry-api/src/opentelemetry/util/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

import typing

AttributeValue = typing.Union[str, bool, float]
AttributeValue = typing.Union[str, bool, int, float, list]
Attributes = typing.Optional[typing.Dict[str, AttributeValue]]
21 changes: 21 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,27 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
if has_ended:
logger.warning("Setting attribute on ended span.")
return

if not isinstance(value, (int, float, bool, str, list)):
jakemalachowski marked this conversation as resolved.
Show resolved Hide resolved
logger.warning("Invalid data type for attribute value")
return
# If value is of type list, validate all items are of same,
# valid data type
if isinstance(value, list) and len(value) > 0:
first_element_data_type = type(value[0])
# If first value is numeric, validate all values are numeric
if first_element_data_type in (int, float):
if any(not isinstance(item, (int, float)) for item in value):
logger.warning("All data types of attribute value arrays must be identical")
return
# Else, validate all values are of same type
elif first_element_data_type in (bool, str):
if any(not isinstance(item, first_element_data_type) for item in value):
logger.warning("All data types of attribute value arrays must be identical")
return
else:
logger.warning("Invalid data type in value array")
jakemalachowski marked this conversation as resolved.
Show resolved Hide resolved
return
self.attributes[key] = value
Copy link
Member

Choose a reason for hiding this comment

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

There's a potential edge case where AttributeValues that are lists can be mutated afterward, resulting in invalid types that exporters will run into.

I've added a followup ticket on that here: #352

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a great catch. Would adding a copy of the list rather than the original list resolve this?

Copy link
Contributor

@ocelotl ocelotl Jan 3, 2020

Choose a reason for hiding this comment

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

We could store the copies of the sequence values in tuples. Now that you mention this, instead of accepting lists or tuples we should accept sequences.

Copy link
Member

Choose a reason for hiding this comment

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

yeah, I think that's a good solution. Would be good to make a separate PR for that.


def add_event(
Expand Down
21 changes: 20 additions & 1 deletion opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ def test_attributes(self):
root.set_attribute("attr-key", "attr-value1")
root.set_attribute("attr-key", "attr-value2")

self.assertEqual(len(root.attributes), 7)
root.set_attribute("empty-list", [])
root.set_attribute("list-of-bools", [True, True, False])
root.set_attribute("list-of-numerics", [123, 3.14, 0])

self.assertEqual(len(root.attributes), 10)
self.assertEqual(root.attributes["component"], "http")
self.assertEqual(root.attributes["http.method"], "GET")
self.assertEqual(
Expand All @@ -379,6 +383,9 @@ 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["list-of-bools"], [True, True, False])
self.assertEqual(root.attributes["list-of-numerics"], [123, 3.14, 0])

attributes = {
"attr-key": "val",
Expand All @@ -393,6 +400,18 @@ def test_attributes(self):
self.assertEqual(root.attributes["attr-key2"], "val2")
self.assertEqual(root.attributes["attr-in-both"], "span-attr")

def test_invalid_attribute_values(self):
class NonPrimitive:
pass

with self.tracer.start_as_current_span("root") as root:
root.set_attribute("non-primitive-data-type", NonPrimitive())
root.set_attribute("list-of-mixed-data-types-numeric-first", [123, False, "string"])
root.set_attribute("list-of-mixed-data-types-non-numeric-first", [False, 123, "string"])
root.set_attribute("list-with-non-primitive-data-type", [NonPrimitive(), 123])
jakemalachowski marked this conversation as resolved.
Show resolved Hide resolved

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

def test_sampling_attributes(self):
decision_attributes = {
"sampler-attr": "sample-val",
Expand Down