Skip to content

Commit e92bff1

Browse files
authored
Rd 2 6508 when send only if error true tracer should always create a file (#232)
* fix: stop file for when send_only_if_error = TRUE
1 parent 1d344fc commit e92bff1

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

src/lumigo_tracer/lumigo_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ def get_span_file_name(span_type: str):
385385

386386

387387
def write_extension_file(data: List[Dict], span_type: str):
388+
Path(get_extension_dir()).mkdir(parents=True, exist_ok=True)
388389
to_send = aws_dump(data).encode()
389390
file_path = get_span_file_name(span_type)
390391
with open(file_path, "wb") as span_file:
@@ -396,7 +397,6 @@ def write_spans_to_files(
396397
spans: List[Dict], max_spans=MAX_NUMBER_OF_SPANS, is_start_span=True
397398
) -> None:
398399
to_send = spans[:max_spans]
399-
Path(get_extension_dir()).mkdir(parents=True, exist_ok=True)
400400
if is_start_span:
401401
get_logger().info("Creating start span file")
402402
write_extension_file(to_send, "span")

src/lumigo_tracer/spans_container.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
is_provision_concurrency_initialization,
2626
get_stacktrace,
2727
write_extension_file,
28+
should_use_tracer_extension,
2829
)
2930
from lumigo_tracer import lumigo_utils
3031
from lumigo_tracer.parsing_utils import parse_trace_id, safe_split_get, recursive_json_join
@@ -278,7 +279,8 @@ def end(self, ret_val=None, event: Optional[dict] = None, context=None) -> Optio
278279
get_logger().debug(
279280
"No Spans were sent, `Configuration.send_only_if_error` is on and no span has error"
280281
)
281-
# write_extension_file([{}], "stop")
282+
if should_use_tracer_extension():
283+
write_extension_file([{}], "stop")
282284
return reported_rtt
283285

284286
def _set_error_extra_data(self, event):

src/test/unit/test_spans_container.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import copy
2+
import os
3+
24
import mock
35
import inspect
46
import json
@@ -62,20 +64,33 @@ def test_spans_container_end_function_got_none_return_value(monkeypatch):
6264
def test_spans_container_end_function_not_send_spans_on_send_only_on_errors_mode(
6365
monkeypatch, dummy_span, tmpdir
6466
):
67+
monkeypatch.setenv("LUMIGO_USE_TRACER_EXTENSION", "TRUE")
68+
reported_ttl, stop_path_path = only_if_error(dummy_span, monkeypatch, tmpdir)
69+
stop_file_content = json.loads(open(stop_path_path, "r").read())
70+
assert json.dumps(stop_file_content) == json.dumps([{}])
71+
assert reported_ttl is None
72+
73+
74+
def test_spans_container_end_shoudnt_create_file_if_not_using_extension(
75+
monkeypatch, dummy_span, tmpdir
76+
):
77+
reported_ttl, stop_path_path = only_if_error(dummy_span, monkeypatch, tmpdir)
78+
assert os.path.isfile(stop_path_path) is False
79+
assert reported_ttl is None
80+
81+
82+
def only_if_error(dummy_span, monkeypatch, tmpdir):
6583
extension_dir = tmpdir.mkdir("tmp")
6684
monkeypatch.setenv("LUMIGO_EXTENSION_SPANS_DIR_KEY", extension_dir)
6785
monkeypatch.setattr(uuid, "uuid4", lambda *args, **kwargs: "span_name")
6886
Configuration.send_only_if_error = True
69-
7087
SpansContainer.create_span()
7188
SpansContainer.get_span().start()
72-
SpansContainer.get_span().add_span(dummy_span)
7389

90+
SpansContainer.get_span().add_span(dummy_span)
7491
reported_ttl = SpansContainer.get_span().end({})
7592
stop_path_path = f"{lumigo_utils.get_extension_dir()}/span_name_stop"
76-
stop_file_content = json.loads(open(stop_path_path, "r").read())
77-
assert json.dumps(stop_file_content) == json.dumps([{}])
78-
assert reported_ttl is None
93+
return reported_ttl, stop_path_path
7994

8095

8196
def test_spans_container_end_function_send_spans_on_send_only_on_errors_mode(
@@ -99,7 +114,6 @@ def test_spans_container_end_function_send_spans_on_send_only_on_errors_mode(
99114
def test_spans_container_end_function_send_only_on_errors_mode_false_not_effecting(
100115
monkeypatch, dummy_span
101116
):
102-
103117
SpansContainer.create_span()
104118
SpansContainer.get_span().start()
105119

0 commit comments

Comments
 (0)