Skip to content

Commit 2297432

Browse files
authored
RD-5479 (#199)
* RD-5479 create extensions api in tracer
1 parent 0ab634a commit 2297432

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/lumigo_tracer/lumigo_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from base64 import b64encode
1717
import inspect
1818
import traceback
19+
from pathlib import Path
1920

2021
LUMIGO_DOMAINS_SCRUBBER_KEY = "LUMIGO_DOMAINS_SCRUBBER"
2122

@@ -67,6 +68,8 @@
6768
NUMBER_OF_SPANS_IN_REPORT_OPTIMIZATION = 200
6869
DEFAULT_KEY_DEPTH = 4
6970
LUMIGO_TOKEN_KEY = "LUMIGO_TRACER_TOKEN"
71+
LUMIGO_USE_TRACER_EXTENSION = "LUMIGO_USE_TRACER_EXTENSION"
72+
EXTENSION_DIR = "/tmp/lumigo-spans"
7073
KILL_SWITCH = "LUMIGO_SWITCH_OFF"
7174
ERROR_SIZE_LIMIT_MULTIPLIER = 2
7275
CHINA_REGION = "cn-northwest-1"
@@ -81,6 +84,10 @@
8184
internal_error_already_logged = False
8285

8386

87+
def should_use_tracer_extension() -> bool:
88+
return (os.environ.get(LUMIGO_USE_TRACER_EXTENSION) or "false").lower() == "true"
89+
90+
8491
def get_region() -> str:
8592
return os.environ.get("AWS_REGION") or "UNKNOWN"
8693

@@ -305,6 +312,10 @@ def report_json(region: Optional[str], msgs: List[dict], should_retry: bool = Tr
305312
except Exception as e:
306313
get_logger().exception("Failed to create request: A span was lost.", exc_info=e)
307314
return 0
315+
if should_use_tracer_extension():
316+
with lumigo_safe_execute("report json file: writing spans to file"):
317+
write_spans_to_file(to_send)
318+
return 0
308319
if region == CHINA_REGION:
309320
return _publish_spans_to_kinesis(to_send, CHINA_REGION)
310321
host = None
@@ -337,6 +348,15 @@ def report_json(region: Optional[str], msgs: List[dict], should_retry: bool = Tr
337348
return duration
338349

339350

351+
def write_spans_to_file(to_send: bytes) -> None:
352+
file_name = f"{hashlib.md5(to_send).hexdigest()}_single"
353+
Path(EXTENSION_DIR).mkdir(parents=True, exist_ok=True)
354+
file_path = os.path.join(EXTENSION_DIR, file_name)
355+
get_logger().info(f"writing spans to file {file_path}")
356+
with open(file_path, "wb") as spans_file:
357+
spans_file.write(to_send)
358+
359+
340360
def _publish_spans_to_kinesis(to_send: bytes, region: str) -> int:
341361
start_time = time.time()
342362
try:

src/test/conftest.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import builtins
22
import logging
3+
import os
4+
import shutil
35
from types import SimpleNamespace
46

5-
from lumigo_tracer import lumigo_utils
6-
from lumigo_tracer.spans_container import SpansContainer
77
import mock
88
import pytest
99

10+
from lumigo_tracer import lumigo_utils
1011
from lumigo_tracer.lumigo_utils import Configuration, get_omitting_regex, get_logger, get_edge_host
12+
from lumigo_tracer.spans_container import SpansContainer
1113
from lumigo_tracer.wrappers.http.http_data_classes import HttpState
1214

1315

@@ -83,3 +85,10 @@ def capture_all_logs(caplog):
8385
@pytest.fixture
8486
def context():
8587
return SimpleNamespace(aws_request_id="1234", get_remaining_time_in_millis=lambda: 1000 * 2)
88+
89+
90+
@pytest.fixture(autouse=True)
91+
def extension_clean():
92+
yield
93+
if os.path.exists("/tmp/lumigo-spans"):
94+
shutil.rmtree("/tmp/lumigo-spans")

src/test/unit/test_lumigo_utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import importlib.util
22
import inspect
3+
import hashlib
34
import logging
45
from collections import OrderedDict
56
from decimal import Decimal
@@ -442,6 +443,26 @@ def test_get_edge_host(arg, host, monkeypatch):
442443
assert get_edge_host("region") == host
443444

444445

446+
def test_report_json_extension(monkeypatch, reporter_mock):
447+
monkeypatch.setattr(Configuration, "should_report", True)
448+
monkeypatch.setenv("LUMIGO_USE_TRACER_EXTENSION", "TRUE")
449+
single = [{"a": "b"}]
450+
to_send = _create_request_body(single, True).encode()
451+
md5str = str(hashlib.md5(to_send).hexdigest())
452+
file_name = f"{md5str}_single"
453+
file_path = f"/tmp/lumigo-spans/{file_name}"
454+
asserting_extension(file_path, single)
455+
# test that same file doesnt cause error
456+
asserting_extension(file_path, single)
457+
458+
459+
def asserting_extension(file_path, single):
460+
duration = report_json(None, [{"a": "b"}])
461+
span_from_file = json.load(open(file_path, "r"))
462+
assert duration == 0
463+
assert span_from_file == single
464+
465+
445466
@pytest.mark.parametrize(
446467
"errors, final_log", [(ValueError, "ERROR"), ([ValueError, Mock()], "INFO")]
447468
)

0 commit comments

Comments
 (0)