|
1 | 1 | import copy
|
2 | 2 | import inspect
|
3 | 3 | import json
|
| 4 | +from datetime import datetime |
4 | 5 |
|
5 | 6 | import pytest
|
6 | 7 |
|
7 | 8 | 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 | +) |
9 | 15 | from lumigo_tracer.lumigo_utils import Configuration, EXECUTION_TAGS_KEY
|
10 | 16 |
|
11 | 17 |
|
@@ -179,3 +185,32 @@ def test_get_span_by_id():
|
179 | 185 | container.add_span({"id": 3, "extra": "c"})
|
180 | 186 | assert SpansContainer.get_span().get_span_by_id(2)["extra"] == "b"
|
181 | 187 | 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