in elasticapm/utils/disttracing.py [0:0]
def add_tracestate(self, key, val) -> None:
"""
Add key/value pair to the tracestate.
We do most of the validation for valid characters here. We have to make
sure none of the reserved separators for tracestate are used in our
key/value pairs, and we also need to check that all characters are
within the valid range. Checking here means we never have to re-check
a pair once set, which saves time in the _set_tracestate() function.
"""
key = str(key)
val = str(val)
for bad in (":", ";", ",", "="):
if bad in key or bad in val:
logger.debug("New tracestate key/val pair contains invalid character '{}', ignoring.".format(bad))
return
for c in itertools.chain(key, val):
# Tracestate spec only allows for characters between ASCII 0x20 and 0x7E
if ord(c) < 0x20 or ord(c) > 0x7E:
logger.debug("Modifications to TraceState would introduce invalid character '{}', ignoring.".format(c))
return
oldval = self.tracestate_dict.pop(key, None)
self.tracestate_dict[key] = val
try:
self.tracestate = self._set_tracestate()
except TraceStateFormatException:
if oldval is not None:
self.tracestate_dict[key] = oldval
else:
self.tracestate_dict.pop(key)