Skip to content

Commit 1ab5540

Browse files
retry in case of error with edge (#149)
1 parent a91d594 commit 1ab5540

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/lumigo_tracer/utils.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,23 +215,26 @@ def prepare_host(host):
215215
return host
216216

217217

218-
def report_json(region: Union[None, str], msgs: List[dict]) -> int:
218+
def report_json(region: Union[None, str], msgs: List[dict], should_retry: bool = True) -> int:
219219
"""
220220
This function sends the information back to the edge.
221221
222222
:param region: The region to use as default if not configured otherwise.
223223
:param msgs: the message to send.
224+
:param should_retry: False to disable the default retry on unsuccessful sending
224225
:return: The duration of reporting (in milliseconds),
225226
or 0 if we didn't send (due to configuration or fail).
226227
"""
227228
global edge_connection
228229
get_logger().info(f"reporting the messages: {msgs[:10]}")
229-
host = prepare_host(Configuration.host or EDGE_HOST.format(region=region))
230-
duration = 0
231-
if not edge_connection or edge_connection.host != host:
232-
edge_connection = establish_connection(host)
233-
if not edge_connection:
234-
return duration
230+
host = None
231+
with lumigo_safe_execute("report json: establish connection"):
232+
host = prepare_host(Configuration.host or EDGE_HOST.format(region=region))
233+
duration = 0
234+
if not edge_connection or edge_connection.host != host:
235+
edge_connection = establish_connection(host)
236+
if not edge_connection:
237+
return duration
235238
if Configuration.should_report:
236239
try:
237240
prune_trace: bool = not os.environ.get("LUMIGO_PRUNE_TRACE_OFF", "").lower() == "true"
@@ -245,10 +248,12 @@ def report_json(region: Union[None, str], msgs: List[dict]) -> int:
245248
duration = int((time.time() - start_time) * 1000)
246249
get_logger().info(f"successful reporting, code: {getattr(response, 'code', 'unknown')}")
247250
except Exception as e:
248-
get_logger().exception(
249-
f"Could not report json to {host}. Retrying to establish connection.", exc_info=e
250-
)
251-
edge_connection = establish_connection(host)
251+
if should_retry:
252+
get_logger().exception(f"Could not report to {host}. Retrying.", exc_info=e)
253+
edge_connection = establish_connection(host)
254+
report_json(region, msgs, should_retry=False)
255+
else:
256+
get_logger().exception("Could not report: A span was lost.", exc_info=e)
252257
return duration
253258

254259

src/test/unit/test_main_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from collections import OrderedDict
33
from decimal import Decimal
44
import datetime
5+
import http.client
6+
from mock import Mock
57

68
import pytest
79
from lumigo_tracer.utils import (
@@ -26,6 +28,7 @@
2628
lumigo_dumps,
2729
prepare_host,
2830
EDGE_PATH,
31+
report_json,
2932
)
3033
import json
3134

@@ -362,3 +365,18 @@ def test_get_timeout_buffer(remaining_time, conf, expected):
362365
)
363366
def test_prepare_host(arg, host):
364367
assert prepare_host(arg) == host
368+
369+
370+
@pytest.mark.parametrize(
371+
"errors, final_log", [(ValueError, "ERROR"), ([ValueError, Mock()], "INFO")]
372+
)
373+
def test_report_json_retry(monkeypatch, reporter_mock, caplog, errors, final_log):
374+
reporter_mock.side_effect = report_json
375+
monkeypatch.setattr(Configuration, "host", "force_reconnect")
376+
monkeypatch.setattr(Configuration, "should_report", True)
377+
monkeypatch.setattr(http.client, "HTTPSConnection", Mock())
378+
http.client.HTTPSConnection("force_reconnect").getresponse.side_effect = errors
379+
380+
report_json(None, [{"a": "b"}])
381+
382+
assert caplog.records[-1].levelname == final_log

0 commit comments

Comments
 (0)