Skip to content

Commit

Permalink
improve attribute filtering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Xue committed May 12, 2020
1 parent 7dffbe7 commit 587297b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
47 changes: 19 additions & 28 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,33 +245,19 @@ def __init__(
self.status = None
self._lock = threading.Lock()

if attributes is None:
self._filter_attribute_values(attributes)
if attributes is None or len(attributes) == 0:
self.attributes = Span._empty_attributes
else:
self.attributes = BoundedDict(MAX_NUM_ATTRIBUTES)
for attr_key, attr_value in attributes.items():
error_message = self._check_attribute_value(attr_value)
if error_message:
logger.warning(error_message)
else:
self.attributes[attr_key] = attr_value
self.attributes = BoundedDict.from_map(MAX_NUM_ATTRIBUTES, attributes)

if events is None:
self.events = Span._empty_events
else:
self.events = BoundedList(MAX_NUM_EVENTS)
for event in events:
good_event = True
for attr_key, attr_value in list(event.attributes.items()):
error_message = self._check_attribute_value(attr_value)
if error_message:
logger.warning(error_message)
good_event = False
break
if isinstance(attr_value, MutableSequence):
attributes[attr_key] = tuple(attr_value)
if good_event:
self.events.append(event)
self._filter_attribute_values(event.attributes)
self.events.append(event)

if links is None:
self.links = Span._empty_links
Expand Down Expand Up @@ -428,6 +414,18 @@ def _check_attribute_value(value: types.AttributeValue) -> Optional[str]:
[valid_type.__name__ for valid_type in valid_types],
)

def _filter_attribute_values(self, attributes: types.Attributes):
if attributes:
for attr_key, attr_value in list(attributes.items()):
error_message = self._check_attribute_value(attr_value)
if error_message:
attributes.pop(attr_key)
logger.warning(error_message)
elif isinstance(attr_value, MutableSequence):
attributes[attr_key] = tuple(attr_value)
else:
attributes[attr_key] = attr_value

def _add_event(self, event: EventBase) -> None:
with self._lock:
if not self.is_recording_events():
Expand All @@ -447,16 +445,9 @@ def add_event(
attributes: types.Attributes = None,
timestamp: Optional[int] = None,
) -> None:
if attributes is None:
self._filter_attribute_values(attributes)
if attributes is None or len(attributes) == 0:
attributes = Span._empty_attributes
else:
for attr_key, attr_value in list(attributes.items()):
error_message = self._check_attribute_value(attr_value)
if error_message:
logger.warning(error_message)
return
if isinstance(attr_value, MutableSequence):
attributes[attr_key] = tuple(attr_value)
self._add_event(
Event(
name=name,
Expand Down
8 changes: 6 additions & 2 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,13 @@ def test_invalid_event_attributes(self):
root.add_event("event0", {"attr1": True, "attr2": ["hi", False]})
root.add_event("event0", {"attr1": dict()})
root.add_event("event0", {"attr1": [[True]]})
root.add_event("event0", {"attr1": [dict()]})
root.add_event("event0", {"attr1": [dict()], "attr2": [1, 2]})

self.assertEqual(len(root.events), 0)
self.assertEqual(len(root.events), 4)
self.assertEqual(root.events[0].attributes, {"attr1": True})
self.assertEqual(root.events[1].attributes, {})
self.assertEqual(root.events[2].attributes, {})
self.assertEqual(root.events[3].attributes, {"attr2": (1, 2)})

def test_links(self):
other_context1 = trace_api.SpanContext(
Expand Down

0 comments on commit 587297b

Please sign in to comment.