Skip to content

Commit 808ebf8

Browse files
deprecate enhance print (#238)
* deprecate enhance print (no need anymore)
1 parent a4c9c58 commit 808ebf8

File tree

8 files changed

+19
-155
lines changed

8 files changed

+19
-155
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ redispy==3.0.0
1111
sqlalchemy==1.3.20
1212
moto==1.3.16
1313
urllib3==1.26.6
14-
aiohttp==3.7.4
14+
aiohttp==3.7.4

scripts/ci_deploy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ set -eo pipefail
44
pushd "$(dirname "$0")" &> /dev/null
55
# Go back one spot because we are on scripts dir. The other scripts assume you are in the root folder
66
cd ..
7-
../utils/common_bash/defaults/ci_deploy.sh java-tracer
7+
../utils/common_bash/defaults/ci_deploy.sh python_tracer
88
popd &> /dev/null

src/lumigo_tracer/lumigo_utils.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ class Configuration:
127127
host: str = ""
128128
token: Optional[str] = ""
129129
verbose: bool = True
130-
enhanced_print: bool = False
131130
is_step_function: bool = False
132131
timeout_timer: bool = True
133132
timeout_timer_buffer: Optional[float] = None
@@ -171,7 +170,7 @@ def config(
171170
:param edge_host: The host to send the events. Leave empty for default.
172171
:param should_report: Whether we should send the events. Change to True in the production.
173172
:param token: The token to use when sending back the events.
174-
:param enhance_print: Should we add prefix to the print (so the logs will be in the platform).
173+
:param enhance_print: Deprecated - Should we add prefix to the print (so the logs will be in the platform).
175174
:param step_function: Is this function is a part of a step function?
176175
:param timeout_timer: Should we start a timer to send the traced data before timeout acceded.
177176
:param timeout_timer_buffer: The buffer (seconds) that we take before reaching timeout to send the traces to lumigo.
@@ -195,9 +194,6 @@ def config(
195194
elif not is_aws_environment():
196195
Configuration.should_report = False
197196
Configuration.host = edge_host or os.environ.get("LUMIGO_TRACER_HOST", "")
198-
Configuration.enhanced_print = (
199-
enhance_print or os.environ.get("LUMIGO_ENHANCED_PRINT", "").lower() == "true"
200-
)
201197
Configuration.verbose = verbose and os.environ.get("LUMIGO_VERBOSE", "").lower() != "false"
202198
Configuration.get_key_depth = get_key_depth or int(
203199
os.environ.get("LUMIGO_EVENT_KEY_DEPTH", DEFAULT_KEY_DEPTH)

src/lumigo_tracer/tracer.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import inspect
2-
import logging
3-
import builtins
42
from functools import wraps
53

64
from lumigo_tracer.auto_tag.auto_tag_event import AutoTagEvent
75
from lumigo_tracer.lumigo_utils import (
86
config,
9-
Configuration,
107
get_logger,
118
lumigo_safe_execute,
129
is_aws_environment,
@@ -49,11 +46,7 @@ def lambda_wrapper(*args, **kwargs):
4946
_add_wrap_flag_to_context(*args)
5047
executed = False
5148
ret_val = None
52-
local_print = print
53-
local_logging_format = logging.Formatter.format
5449
try:
55-
if Configuration.enhanced_print:
56-
_enhance_output(args, local_print, local_logging_format)
5750
SpansContainer.create_span(*args, is_new_invocation=True)
5851
with lumigo_safe_execute("auto tag"):
5952
AutoTagEvent.auto_tag_event(args[0])
@@ -66,10 +59,8 @@ def lambda_wrapper(*args, **kwargs):
6659
SpansContainer.get_span().add_exception_event(e, inspect.trace())
6760
raise
6861
finally:
69-
SpansContainer.get_span().end(ret_val, *args)
70-
if Configuration.enhanced_print:
71-
builtins.print = local_print
72-
logging.Formatter.format = local_logging_format
62+
with lumigo_safe_execute("end"):
63+
SpansContainer.get_span().end(ret_val, *args)
7364
return ret_val
7465
except Exception:
7566
# The case where our wrapping raised an exception
@@ -83,19 +74,6 @@ def lambda_wrapper(*args, **kwargs):
8374
return lambda_wrapper
8475

8576

86-
def _enhance_output(args, local_print, local_logging_format):
87-
if len(args) < 2:
88-
return
89-
request_id = getattr(args[1], "aws_request_id", "")
90-
prefix = f"RequestId: {request_id}"
91-
builtins.print = lambda *args, **kwargs: local_print(
92-
*[_add_prefix_for_each_line(prefix, str(arg)) for arg in args], **kwargs
93-
)
94-
logging.Formatter.format = lambda self, record: _add_prefix_for_each_line(
95-
prefix, local_logging_format(self, record)
96-
)
97-
98-
9977
def _add_prefix_for_each_line(prefix: str, text: str):
10078
enhanced_lines = []
10179
for line in text.split("\n"):

src/lumigo_tracer/user_utils.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import Dict, Optional
44

55
from lumigo_tracer.spans_container import SpansContainer
6-
from lumigo_tracer.lumigo_utils import Configuration, warn_client
6+
from lumigo_tracer.lumigo_utils import warn_client
77

88
LUMIGO_REPORT_ERROR_STRING = "[LUMIGO_LOG]"
99
MAX_TAGS = 50
@@ -80,11 +80,10 @@ def log(level: int, msg: str, error_type: str, extra: Optional[Dict[str, str]]):
8080

8181
def report_error(msg: str):
8282
message_with_initials = f"{LUMIGO_REPORT_ERROR_STRING} {msg}"
83-
if Configuration.enhanced_print:
84-
print(message_with_initials)
85-
else:
86-
message_with_request_id = f"RequestId: {SpansContainer.get_span().function_span.get('id')} {message_with_initials}"
87-
print(message_with_request_id)
83+
message_with_request_id = (
84+
f"RequestId: {SpansContainer.get_span().function_span.get('id')} {message_with_initials}"
85+
)
86+
print(message_with_request_id)
8887

8988

9089
def validate_tag(key, value, tags_len, should_log_errors):

src/test/unit/test_lumigo_utils.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -385,28 +385,14 @@ def test_omit_keys_environment(monkeypatch):
385385

386386

387387
@pytest.mark.parametrize("configuration_value", (True, False))
388-
def test_config_enhanced_print_with_envs(monkeypatch, configuration_value):
389-
monkeypatch.setenv("LUMIGO_ENHANCED_PRINT", "TRUE")
390-
config(enhance_print=configuration_value)
391-
assert Configuration.enhanced_print is True
392-
393-
394-
@pytest.mark.parametrize("configuration_value", (True, False))
395-
def test_config_enhanced_print_without_envs(monkeypatch, configuration_value):
396-
monkeypatch.delenv("LUMIGO_ENHANCED_PRINT", raising=False)
397-
config(enhance_print=configuration_value)
398-
assert Configuration.enhanced_print == configuration_value
399-
400-
401-
@pytest.mark.parametrize("configuration_value", (True, False))
402-
def test_config_enhanced_printstep_function_with_envs(monkeypatch, configuration_value):
388+
def test_config_step_function_with_envs(monkeypatch, configuration_value):
403389
monkeypatch.setenv("LUMIGO_STEP_FUNCTION", "TRUE")
404390
config(step_function=configuration_value)
405391
assert Configuration.is_step_function is True
406392

407393

408394
@pytest.mark.parametrize("configuration_value", (True, False))
409-
def test_config_enhanced_printstep_function_without_envs(monkeypatch, configuration_value):
395+
def test_config_step_function_without_envs(monkeypatch, configuration_value):
410396
monkeypatch.delenv("LUMIGO_STEP_FUNCTION", raising=False)
411397
config(step_function=configuration_value)
412398
assert Configuration.is_step_function == configuration_value

src/test/unit/test_tracer.py

Lines changed: 6 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
from decimal import Decimal
66
import http.client
77
import os
8-
import sys
98
from functools import wraps
10-
import logging
119
from unittest.mock import MagicMock, ANY
1210

1311
import boto3
@@ -183,28 +181,25 @@ def lambda_test_function(event, context):
183181
assert Configuration.should_report == "123"
184182

185183

186-
def test_wrapping_with_print_override(context):
187-
@lumigo_tracer(enhance_print=True)
184+
def test_wrapping_print_happy_flow(context):
185+
@lumigo_tracer()
188186
def lambda_test_function(event, context):
189-
print("hello\nworld")
187+
print("hello")
190188
return 1
191189

192190
with CaptureOutput() as capturer:
193191
assert lambda_test_function({}, context) == 1
194-
assert Configuration.enhanced_print is True
195-
assert "RequestId: 1234 hello" in capturer.get_lines()
196-
assert "RequestId: 1234 world" in capturer.get_lines()
192+
assert any(line == "hello" for line in capturer.get_lines())
197193

198194

199-
def test_wrapping_without_print_override(context):
200-
@lumigo_tracer()
195+
def test_wrapping_enhanced_print_backward_compatible(context):
196+
@lumigo_tracer(enhance_print=True)
201197
def lambda_test_function(event, context):
202198
print("hello")
203199
return 1
204200

205201
with CaptureOutput() as capturer:
206202
assert lambda_test_function({}, context) == 1
207-
assert Configuration.enhanced_print is False
208203
assert any(line == "hello" for line in capturer.get_lines())
209204

210205

@@ -273,78 +268,6 @@ def handler(event, context):
273268
assert SpansContainer._span
274269

275270

276-
def test_wrapping_with_logging_override_default_usage(caplog, context):
277-
@lumigo_tracer(enhance_print=True)
278-
def lambda_test_function(event, context):
279-
logging.warning("hello\nworld")
280-
return 1
281-
282-
assert lambda_test_function({}, context) == 1
283-
assert Configuration.enhanced_print is True
284-
assert any("RequestId: 1234" in line and "hello" in line for line in caplog.text.split("\n"))
285-
assert any("RequestId: 1234" in line and "world" in line for line in caplog.text.split("\n"))
286-
287-
288-
def test_wrapping_with_logging_exception(caplog, context):
289-
@lumigo_tracer(enhance_print=True)
290-
def lambda_test_function(event, context):
291-
logger = logging.getLogger("logger_name")
292-
handler = logging.StreamHandler()
293-
logger.addHandler(handler)
294-
295-
try:
296-
1 / 0
297-
except Exception: # You must call the logging.exception method just inside the except part.
298-
logger.exception("hello")
299-
return 1
300-
301-
assert lambda_test_function({}, context) == 1
302-
# Check all lines have exactly one RequestId.
303-
for line in caplog.text.splitlines():
304-
assert line.startswith("RequestId: 1234") and line.count("RequestId: 1234") == 1
305-
# Check the message was logged.
306-
test_message = [line for line in caplog.text.splitlines() if line.endswith("hello")][0].replace(
307-
" ", ""
308-
)
309-
assert "ERROR" in test_message and "hello" in test_message
310-
311-
312-
def test_wrapping_with_logging_override_complex_usage(context):
313-
@lumigo_tracer(enhance_print=True)
314-
def lambda_test_function(event, context):
315-
handler = logging.StreamHandler(sys.stdout)
316-
formatter = logging.Formatter("%(name)s [%(levelname)s] %(message)s") # Format of a client.
317-
handler.setFormatter(formatter)
318-
logger = logging.getLogger("my_test")
319-
logger.handlers = [handler]
320-
logger.setLevel("INFO")
321-
322-
logger.info("hello\nworld")
323-
return 1
324-
325-
with CaptureOutput() as capturer:
326-
assert lambda_test_function({}, context) == 1
327-
assert Configuration.enhanced_print is True
328-
assert "RequestId: 1234 my_test [INFO] hello" in capturer.get_lines()
329-
assert "RequestId: 1234 world" in capturer.get_lines()
330-
331-
332-
def test_wrapping_without_logging_override(caplog, context):
333-
@lumigo_tracer()
334-
def lambda_test_function(event, context):
335-
logging.warning("hello\nworld")
336-
return 1
337-
338-
assert lambda_test_function({}, context) == 1
339-
assert Configuration.enhanced_print is False
340-
assert any(
341-
"RequestId: 1234" not in line and "world" in line for line in caplog.text.split("\n")
342-
)
343-
assert any(
344-
"RequestId: 1234" not in line and "hello" in line for line in caplog.text.split("\n")
345-
)
346-
347-
348271
@pytest.mark.parametrize(
349272
"event, expected_triggered_by, expected_message_id",
350273
[

src/test/unit/test_user_utils.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from lumigo_tracer.spans_container import SpansContainer
22
from lumigo_tracer.user_utils import (
3-
report_error,
43
warn,
54
info,
65
error,
@@ -11,15 +10,7 @@
1110
MAX_TAGS,
1211
MAX_ELEMENTS_IN_EXTRA,
1312
)
14-
from lumigo_tracer.lumigo_utils import Configuration, EXECUTION_TAGS_KEY
15-
16-
17-
def test_report_error_with_enhance_print(capsys):
18-
Configuration.enhanced_print = True
19-
msg = "oh no - an error"
20-
report_error(msg)
21-
captured = capsys.readouterr()
22-
assert captured.out == f"{LUMIGO_REPORT_ERROR_STRING} {msg}\n"
13+
from lumigo_tracer.lumigo_utils import EXECUTION_TAGS_KEY
2314

2415

2516
def test_err_without_alert_type_with_exception(capsys):
@@ -81,15 +72,6 @@ def test_basic_info_warn_error(capsys):
8172
assert captured[2] == error_msg
8273

8374

84-
def test_report_error_without_enhance_print(capsys):
85-
Configuration.enhanced_print = False
86-
SpansContainer.get_span().function_span["id"] = "123"
87-
msg = "oh no - an error"
88-
report_error(msg)
89-
captured = capsys.readouterr()
90-
assert captured.out == f"RequestId: 123 {LUMIGO_REPORT_ERROR_STRING} {msg}\n"
91-
92-
9375
def test_add_execution_tag():
9476
key = "my_key"
9577
value = "my_value"

0 commit comments

Comments
 (0)