Skip to content

Commit

Permalink
Delete trace attrib if set to invalid value (open-telemetry#998)
Browse files Browse the repository at this point in the history
  • Loading branch information
LetzNico committed Oct 6, 2020
1 parent b565d6b commit 622abd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,11 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
return
with self._lock:
self.attributes[key] = value
else:
try:
self.attributes.pop(key)
except KeyError:
pass

def _add_event(self, event: EventBase) -> None:
with self._lock:
Expand Down
10 changes: 10 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,16 @@ def test_invalid_attribute_values(self):

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

def test_replace_attribute_by_none(self):
"""An attribute with a valid value should disappear if
its value becomes invalid
"""
with self.tracer.start_as_current_span("root") as root:
root.set_attribute("valid-attribute-value", 50)
self.assertEqual(len(root.attributes), 1)
root.set_attribute("valid-attribute-value", None)
self.assertEqual(len(root.attributes), 0)

def test_byte_type_attribute_value(self):
with self.tracer.start_as_current_span("root") as root:
with self.assertLogs(level=WARNING):
Expand Down

0 comments on commit 622abd9

Please sign in to comment.