Skip to content

Commit 49c47b6

Browse files
committed
Add wrapper for trace_is and span_id.
1 parent 953b45e commit 49c47b6

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

tests/test_otel.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@
101101
_headers = {}
102102

103103

104+
def decode_id(span, value):
105+
if value in ["trace_id", "span_id"]:
106+
return hexlify(getattr(span, value)).decode("utf-8")
107+
return value
108+
109+
104110
def span_attr(span, attr, atype):
105111
for value in (atrb.value for atrb in span.attributes if atrb.key == attr):
106112
return getattr(value, atype)
@@ -442,10 +448,7 @@ def test_variables(
442448
):
443449
if http_ver == 0:
444450
pytest.skip("no headers support")
445-
value = value[idx]
446-
if type(value) is str and value in ["trace_id", "span_id"]:
447-
value = hexlify(getattr(span, value)).decode("utf-8")
448-
assert headers.get(name) == value
451+
assert headers.get(name) == decode_id(span, value[idx])
449452

450453
@pytest.mark.xfail(reason="otel variables are present when trace is off")
451454
@pytest.mark.parametrize("idx", [10, 11], ids=["no context", "context"])
@@ -505,13 +508,6 @@ def test_trace_context(
505508
if http_ver == 0:
506509
pytest.skip("no headers support")
507510
value = value[idx - 2]
508-
if type(value) is str:
509-
value = "-".join(
510-
(
511-
hexlify(getattr(span, val)).decode("utf-8")
512-
if val in ["trace_id", "span_id"]
513-
else val
514-
)
515-
for val in value.split("-")
516-
)
511+
if value is not None:
512+
value = "-".join(decode_id(span, val) for val in value.split("-"))
517513
assert headers.get(name) == value

0 commit comments

Comments
 (0)