Adding support for array attributes to Zipkin exporter#1285
Conversation
exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py
Outdated
Show resolved
Hide resolved
exporter/opentelemetry-exporter-zipkin/src/opentelemetry/exporter/zipkin/__init__.py
Outdated
Show resolved
Hide resolved
| if isinstance(element, (int, bool, float)): | ||
| tag_value_element = str(element) | ||
| elif isinstance(element, str): | ||
| tag_value_element = element |
There was a problem hiding this comment.
nit L354 to 357 repeated here, could be refactored. One complication is sequence attributes are allowed to have None.
Could also combine the two conditions
| if isinstance(element, (int, bool, float)): | |
| tag_value_element = str(element) | |
| elif isinstance(element, str): | |
| tag_value_element = element | |
| if isinstance(element, (int, bool, float, str)): | |
| tag_value_element = str(element) |
There was a problem hiding this comment.
Simplified the formatting as recommended but didn't refactor the duplication as it's small.
I also changed the logic to silently skip invalid sequence elements versus completely failing so that we're passing along as much data s possible.
| running_string_length += ( | ||
| 2 # accounts for ', ' connector | ||
| ) | ||
| if running_string_length > self.max_tag_value_length: |
There was a problem hiding this comment.
Is max_tag_value_length supposed to be encoded byte length or string length?
There was a problem hiding this comment.
We're outputting as ASCII with json.dumps() so one and the same.
There was a problem hiding this comment.
I'm not sure actually so I checked the python docs:
If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped.
So I think if the escape sequences are long, you could get longer length. I think this is OK tho 😄 Maybe just update max_tag_value_length docstring to say its string length if not specified?
exporter/opentelemetry-exporter-zipkin/tests/test_zipkin_exporter.py
Outdated
Show resolved
Hide resolved
…thub.com/robwknox/opentelemetry-python into 1110_zipkin_exporter_support_array_attrs
…elements versus bailing completely out
aabmass
left a comment
There was a problem hiding this comment.
As an aside, I believe I've identified a bug in that bool objects are supposed to be serialized to
trueorfalseto adhere to JSON spec, but we appear to be retaining python formatting ofTrueandFalse. Will file a separate bug report.
👍
Nice and thanks for addressing comments! LGTM
ocelotl
left a comment
There was a problem hiding this comment.
LGTM, just leaving some suggestions.
| value = self._extract_tag_value_string_from_sequence( | ||
| attribute_value | ||
| ) | ||
| if not value: |
There was a problem hiding this comment.
| if not value: | |
| if value is None: |
| ) | ||
| self.assertEqual( | ||
| tags["list5"], | ||
| '["True","True","True","True","True","True","True","True","True","True","True","True","True","True","True","True","True","True"]', |
There was a problem hiding this comment.
| '["True","True","True","True","True","True","True","True","True","True","True","True","True","True","True","True","True","True"]', | |
| partial_dump([str(True)] * 25) |
| tags = json.loads(kwargs["data"])[0]["tags"] | ||
| self.assertEqual(len(tags["k1"]), 128) | ||
| self.assertEqual(len(tags["k2"]), 50) | ||
|
|
There was a problem hiding this comment.
To avoid these long testing sequences, may I suggest:
from functools import partial
from json import dumps
...
partial_dumps = partial(dumps, separators=(",", ":"))| self.assertEqual(len(tags["string2"]), 50) | ||
| self.assertEqual( | ||
| tags["list1"], | ||
| '["a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a"]', |
There was a problem hiding this comment.
| '["a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a","a"]', | |
| partial_dumps(["a"] * 25), |
| ) | ||
| self.assertEqual( | ||
| tags["range1"], | ||
| '["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"]', |
There was a problem hiding this comment.
| '["0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"]', | |
| partial_dumps([str(i) for i in range(25)]), |
Description
Adds support for basic sequence (list, tuple, range) attribute values in the Zipkin exporter.
Since the value is serialized to a JSON list string, specific logic is needed to ensure max_tag_value_length is honored at the element boundary.
As an aside, I believe I've identified a bug in that bool objects are supposed to be serialized to
trueorfalseto adhere to JSON spec, but we appear to be retaining python formatting ofTrueandFalse. Will file a separate bug report.Fixes #1110
Type of change
How Has This Been Tested?
tox environments:
Checklist: