|
16 | 16 | from base64 import b64encode
|
17 | 17 | import inspect
|
18 | 18 | import traceback
|
| 19 | +from pathlib import Path |
19 | 20 |
|
20 | 21 | LUMIGO_DOMAINS_SCRUBBER_KEY = "LUMIGO_DOMAINS_SCRUBBER"
|
21 | 22 |
|
|
67 | 68 | NUMBER_OF_SPANS_IN_REPORT_OPTIMIZATION = 200
|
68 | 69 | DEFAULT_KEY_DEPTH = 4
|
69 | 70 | LUMIGO_TOKEN_KEY = "LUMIGO_TRACER_TOKEN"
|
| 71 | +LUMIGO_USE_TRACER_EXTENSION = "LUMIGO_USE_TRACER_EXTENSION" |
| 72 | +EXTENSION_DIR = "/tmp/lumigo-spans" |
70 | 73 | KILL_SWITCH = "LUMIGO_SWITCH_OFF"
|
71 | 74 | ERROR_SIZE_LIMIT_MULTIPLIER = 2
|
72 | 75 | CHINA_REGION = "cn-northwest-1"
|
|
81 | 84 | internal_error_already_logged = False
|
82 | 85 |
|
83 | 86 |
|
| 87 | +def should_use_tracer_extension() -> bool: |
| 88 | + return (os.environ.get(LUMIGO_USE_TRACER_EXTENSION) or "false").lower() == "true" |
| 89 | + |
| 90 | + |
84 | 91 | def get_region() -> str:
|
85 | 92 | return os.environ.get("AWS_REGION") or "UNKNOWN"
|
86 | 93 |
|
@@ -305,6 +312,10 @@ def report_json(region: Optional[str], msgs: List[dict], should_retry: bool = Tr
|
305 | 312 | except Exception as e:
|
306 | 313 | get_logger().exception("Failed to create request: A span was lost.", exc_info=e)
|
307 | 314 | 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 |
308 | 319 | if region == CHINA_REGION:
|
309 | 320 | return _publish_spans_to_kinesis(to_send, CHINA_REGION)
|
310 | 321 | host = None
|
@@ -337,6 +348,15 @@ def report_json(region: Optional[str], msgs: List[dict], should_retry: bool = Tr
|
337 | 348 | return duration
|
338 | 349 |
|
339 | 350 |
|
| 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 | + |
340 | 360 | def _publish_spans_to_kinesis(to_send: bytes, region: str) -> int:
|
341 | 361 | start_time = time.time()
|
342 | 362 | try:
|
|
0 commit comments