Skip to content

Commit 28f50d1

Browse files
RD-4242 - handle malformed trace id (#167)
* handle malformed trace id
1 parent 539e3d0 commit 28f50d1

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/lumigo_tracer/spans_container.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
_VERSION_PATH = os.path.join(os.path.dirname(__file__), "VERSION")
3232
MAX_LAMBDA_TIME = 15 * 60 * 1000
3333
FUNCTION_TYPE = "function"
34+
MALFORMED_TXID = "000000000000000000000000"
3435

3536

3637
class SpansContainer:
@@ -62,6 +63,10 @@ def __init__(
6263
self.region = region
6364
self.trace_root = trace_root
6465
self.trace_id_suffix = trace_id_suffix
66+
malformed_txid = False
67+
if transaction_id == MALFORMED_TXID:
68+
transaction_id = os.urandom(12).hex()
69+
malformed_txid = True
6570
self.transaction_id = transaction_id
6671
self.max_finish_time = max_finish_time
6772
self.base_msg = {
@@ -88,6 +93,7 @@ def __init__(
8893
"logGroupName": log_group_name,
8994
**(trigger_by or {}),
9095
},
96+
"isMalformedTransactionId": malformed_txid,
9197
EXECUTION_TAGS_KEY: [],
9298
},
9399
self.base_msg,
@@ -260,8 +266,13 @@ def _set_error_extra_data(self, event):
260266
)
261267

262268
def get_patched_root(self):
269+
"""
270+
We're changing the root in order to pass/share the transaction id. More info:
271+
https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids
272+
"""
273+
current_time = int(time.time())
263274
root = safe_split_get(self.trace_root, "-", 0)
264-
return f"Root={root}-0000{os.urandom(2).hex()}-{self.transaction_id}{self.trace_id_suffix}"
275+
return f"Root={root}-{hex(current_time)[2:]}-{self.transaction_id}{self.trace_id_suffix}"
265276

266277
@classmethod
267278
def get_span(cls) -> "SpansContainer":

src/test/unit/test_spans_container.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import copy
22
import inspect
33
import json
4+
from datetime import datetime
45

56
import pytest
67

78
from lumigo_tracer.wrappers.http.http_parser import HTTP_TYPE
8-
from lumigo_tracer.spans_container import SpansContainer, TimeoutMechanism, FUNCTION_TYPE
9+
from lumigo_tracer.spans_container import (
10+
SpansContainer,
11+
TimeoutMechanism,
12+
FUNCTION_TYPE,
13+
MALFORMED_TXID,
14+
)
915
from lumigo_tracer.lumigo_utils import Configuration, EXECUTION_TAGS_KEY
1016

1117

@@ -179,3 +185,32 @@ def test_get_span_by_id():
179185
container.add_span({"id": 3, "extra": "c"})
180186
assert SpansContainer.get_span().get_span_by_id(2)["extra"] == "b"
181187
assert SpansContainer.get_span().get_span_by_id(5) is None
188+
189+
190+
def test_get_patched_root(monkeypatch, context):
191+
monkeypatch.setenv(
192+
"_X_AMZN_TRACE_ID",
193+
"Root=1-5fd891b8-252f5de90a085ae04267aa4e;Parent=0a885f800de045d4;Sampled=0",
194+
)
195+
SpansContainer.create_span({}, context)
196+
result = SpansContainer.get_span().get_patched_root()
197+
root = result.split(";")[0].split("=")[1]
198+
one, current_time, txid = root.split("-")
199+
200+
result_time = datetime.fromtimestamp(int(current_time, 16))
201+
assert one == "1"
202+
assert (result_time - datetime.now()).total_seconds() < 5
203+
assert txid == "252f5de90a085ae04267aa4e"
204+
205+
206+
def test_malformed_txid(monkeypatch, context):
207+
monkeypatch.setenv(
208+
"_X_AMZN_TRACE_ID", f"Root=1-5fd891b8-{MALFORMED_TXID};Parent=0a885f800de045d4;Sampled=0"
209+
)
210+
SpansContainer.create_span({}, context)
211+
212+
assert SpansContainer.get_span().transaction_id != MALFORMED_TXID
213+
assert SpansContainer.get_span().function_span["isMalformedTransactionId"]
214+
result = SpansContainer.get_span().get_patched_root()
215+
output_trace_id = result.split(";")[0].split("=")[1].split("-")[2]
216+
assert output_trace_id == SpansContainer.get_span().transaction_id

0 commit comments

Comments
 (0)