Skip to content

Commit df74e3a

Browse files
saartochner-lumigoCircleCI
andauthored
avoid return-value warning when the runtime version is not python3.7 (#263)
* avoid return-value warning when the runtime version is not python3.7 Co-authored-by: CircleCI <[email protected]>
1 parent ff26018 commit df74e3a

File tree

6 files changed

+67
-13
lines changed

6 files changed

+67
-13
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ repos:
1919
hooks:
2020
- id: flake8
2121
args: ["--ignore","E501,W503","--exclude","src/lumigo_tracer/libs/*"]
22+
additional_dependencies: ['importlib-metadata==4.13.0']
2223

2324
- repo: https://github.com/PyCQA/bandit
2425
rev: '1.7.0'
2526
hooks:
2627
- id: bandit
2728
exclude: ^src/test/
2829
args: ["-lll"]
30+
additional_dependencies: ['importlib-metadata==4.13.0']
2931

3032
- repo: https://github.com/Yelp/detect-secrets
3133
rev: v1.2.0

.secrets.baseline

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
"filename": "src/test/unit/test_lumigo_utils.py",
185185
"hashed_secret": "f32b67c7e26342af42efabc674d441dca0a281c5",
186186
"is_verified": false,
187-
"line_number": 262
187+
"line_number": 263
188188
}
189189
],
190190
"src/test/unit/test_tracer.py": [
@@ -193,21 +193,21 @@
193193
"filename": "src/test/unit/test_tracer.py",
194194
"hashed_secret": "9b55630d152a6501889a2378eb53fe4b260fb91f",
195195
"is_verified": false,
196-
"line_number": 58
196+
"line_number": 60
197197
},
198198
{
199199
"type": "Secret Keyword",
200200
"filename": "src/test/unit/test_tracer.py",
201201
"hashed_secret": "f13de0da12f8375756b1bc56b248cc8e62e0c302",
202202
"is_verified": false,
203-
"line_number": 395
203+
"line_number": 397
204204
},
205205
{
206206
"type": "Secret Keyword",
207207
"filename": "src/test/unit/test_tracer.py",
208208
"hashed_secret": "dabe695c9892b5cb937379817679c0fb0be6a3df",
209209
"is_verified": false,
210-
"line_number": 478
210+
"line_number": 502
211211
}
212212
],
213213
"src/test/unit/wrappers/http/test_http_parser.py": [
@@ -220,5 +220,5 @@
220220
}
221221
]
222222
},
223-
"generated_at": "2022-09-01T11:44:39Z"
223+
"generated_at": "2022-10-12T08:37:22Z"
224224
}

src/lumigo_tracer/lumigo_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,10 @@ def get_stacktrace(exception: Exception) -> str:
844844
return "".join(filter(lambda line: STACKTRACE_LINE_TO_DROP not in line, original_traceback))
845845

846846

847+
def is_python_37() -> bool:
848+
return os.environ.get("AWS_EXECUTION_ENV") == "AWS_Lambda_python3.7"
849+
850+
847851
try:
848852
# Try to establish the connection in initialization
849853
if (

src/lumigo_tracer/spans_container.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
should_use_tracer_extension,
2727
MANUAL_TRACES_KEY,
2828
lumigo_safe_execute,
29+
is_python_37,
2930
)
3031
from lumigo_tracer import lumigo_utils
3132
from lumigo_tracer.event.event_dumper import EventDumper
@@ -292,13 +293,18 @@ def end(self, ret_val=None, event: Optional[dict] = None, context=None) -> Optio
292293
if ret_val is not None:
293294
parsed_ret_val = lumigo_dumps(ret_val, enforce_jsonify=True, decimal_safe=True)
294295
except Exception as err:
295-
suffix = ""
296-
if err.args:
297-
suffix = f'Original message: "{err.args[0]}"'
298-
self.function_span["error"] = self._create_exception_event(
299-
"ReturnValueError",
300-
"The lambda will probably fail due to bad return value. " + suffix,
301-
)
296+
if is_python_37():
297+
suffix = ""
298+
if err.args:
299+
suffix = f'Original message: "{err.args[0]}"'
300+
self.function_span["error"] = self._create_exception_event(
301+
"ReturnValueError",
302+
"The lambda will probably fail due to bad return value. " + suffix,
303+
)
304+
else:
305+
get_logger().exception(
306+
"Could not serialize the return value of the lambda", exc_info=True
307+
)
302308
self.function_span.update({"return_value": parsed_ret_val})
303309
if _is_span_has_error(self.function_span):
304310
self._set_error_extra_data(event)

src/test/unit/test_lumigo_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
TRUNCATE_SUFFIX,
5252
DEFAULT_AUTO_TAG_KEY,
5353
lumigo_safe_execute,
54+
is_python_37,
5455
)
5556
import json
5657

@@ -686,3 +687,20 @@ def test_lumigo_safe_execute_with_level(severity, caplog):
686687
with lumigo_safe_execute("test", severity=severity):
687688
raise ValueError("Failing")
688689
assert caplog.records[-1].levelno == severity
690+
691+
692+
@pytest.mark.parametrize(
693+
"env_value, expected",
694+
[
695+
("AWS_Lambda_python3.8", False),
696+
("AWS_Lambda_python3.7", True),
697+
],
698+
)
699+
def test_is_python_37(monkeypatch, env_value, expected):
700+
monkeypatch.setenv("AWS_EXECUTION_ENV", env_value)
701+
assert is_python_37() == expected
702+
703+
704+
def test_is_python_37_without_env(monkeypatch):
705+
monkeypatch.delenv("AWS_EXECUTION_ENV", raising=False)
706+
assert is_python_37() is False

src/test/unit/test_tracer.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
import datetime
24
import json
35
import re
@@ -453,7 +455,9 @@ def lambda_test_function(event, context):
453455
assert enrichment_spans[0][EXECUTION_TAGS_KEY] == [{"key": key, "value": "my_value"}]
454456

455457

456-
def test_not_jsonable_return(monkeypatch, context):
458+
def test_not_jsonable_return_value_python37(monkeypatch, context):
459+
monkeypatch.setenv("AWS_EXECUTION_ENV", "AWS_Lambda_python3.7")
460+
457461
@lumigo_tracer()
458462
def lambda_test_function(event, context):
459463
return {"a": datetime.datetime.now()}
@@ -468,6 +472,26 @@ def lambda_test_function(event, context):
468472
assert function_span["error"]["message"] == expected_message
469473

470474

475+
def test_not_jsonable_return_value_non_python37(monkeypatch, context, caplog):
476+
monkeypatch.setenv("AWS_EXECUTION_ENV", "AWS_Lambda_python3.8")
477+
478+
@lumigo_tracer()
479+
def lambda_test_function(event, context):
480+
return {"a": datetime.datetime.now()}
481+
482+
lambda_test_function({}, context)
483+
484+
function_span = SpansContainer.get_span().function_span
485+
assert function_span["return_value"] is None
486+
assert "error" not in function_span
487+
assert next(
488+
log
489+
for log in caplog.records
490+
if log.levelno == logging.ERROR
491+
and "Could not serialize the return value of the lambda" in log.message
492+
)
493+
494+
471495
@mock_kinesis
472496
def test_china(context, reporter_mock, monkeypatch):
473497
china_region_for_test = "ap-east-1" # Moto doesn't work for China

0 commit comments

Comments
 (0)