Skip to content

Commit 41e5b3f

Browse files
vladlLumigoCircleCI
andauthored
Rd 9297 disable addexecutiontag apis when lambda function not traced (#264)
* add execution_tag only if lambda is traced * add execution_tag only if lambda is traced * add execution_tag only if lambda is traced * fix existing unit tests Co-authored-by: CircleCI <no-reply@build.com>
1 parent abd996b commit 41e5b3f

File tree

8 files changed

+67
-20
lines changed

8 files changed

+67
-20
lines changed

src/lumigo_tracer/lumigo_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,3 +860,7 @@ def is_python_37() -> bool:
860860
InternalState.mark_timeout_to_edge()
861861
except Exception:
862862
pass
863+
864+
865+
def is_lambda_traced() -> bool:
866+
return (not is_kill_switch_on()) and is_aws_environment()

src/lumigo_tracer/user_utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Dict, Optional
55

66
from lumigo_tracer.spans_container import SpansContainer
7-
from lumigo_tracer.lumigo_utils import warn_client
7+
from lumigo_tracer.lumigo_utils import warn_client, is_lambda_traced
88

99
LUMIGO_REPORT_ERROR_STRING = "[LUMIGO_LOG]"
1010
MAX_TAGS = 50
@@ -139,12 +139,17 @@ def add_execution_tag(key: str, value: str, should_log_errors: bool = True) -> b
139139
:param should_log_errors: Should a log message be printed in case the tag can't be added.
140140
"""
141141
try:
142-
key = str(key)
143-
value = str(value)
144-
tags_len = SpansContainer.get_span().get_tags_len()
145-
if validate_tag(key, value, tags_len, should_log_errors):
146-
SpansContainer.get_span().add_tag(key, value)
142+
if is_lambda_traced():
143+
key = str(key)
144+
value = str(value)
145+
tags_len = SpansContainer.get_span().get_tags_len()
146+
if validate_tag(key, value, tags_len, should_log_errors):
147+
SpansContainer.get_span().add_tag(key, value)
148+
else:
149+
return False
147150
else:
151+
if should_log_errors:
152+
warn_client(f"{ADD_TAG_ERROR_MSG_PREFIX}: lambda is not traced")
148153
return False
149154
except Exception:
150155
if should_log_errors:

src/test/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,9 @@ def aws_env(monkeypatch):
121121
"_X_AMZN_TRACE_ID",
122122
"Root=1-12345678-111111111111111111111111;Parent=blablablablabla;Sampled=0",
123123
)
124+
125+
126+
@pytest.fixture
127+
def lambda_traced(monkeypatch):
128+
monkeypatch.setenv("AWS_LAMBDA_FUNCTION_VERSION", "true")
129+
monkeypatch.setenv("LUMIGO_SWITCH_OFF", "false")

src/test/unit/auto_tag/test_auto_tag_event.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def test_auto_tag_key_not_in_header(monkeypatch):
159159
assert SpansContainer.get_span().execution_tags == []
160160

161161

162-
def test_auto_tag_key_in_header(monkeypatch):
162+
def test_auto_tag_key_in_header(monkeypatch, lambda_traced):
163163
set_header_key(monkeypatch, "Accept")
164164

165165
event = {
@@ -323,7 +323,7 @@ def test_configuration_handler_is_supported(config, event, expected):
323323
),
324324
],
325325
)
326-
def test_configuration_handler_auto_tag(auto_tag_keys, event, result_tags):
326+
def test_configuration_handler_auto_tag(auto_tag_keys, event, result_tags, lambda_traced):
327327
Configuration.auto_tag = auto_tag_keys
328328
ConfigurationHandler.auto_tag(event)
329329
tags = SpansContainer.get_span().execution_tags
@@ -332,7 +332,7 @@ def test_configuration_handler_auto_tag(auto_tag_keys, event, result_tags):
332332
assert tag in tags
333333

334334

335-
def test_configuration_handler_auto_tag_failure(capsys):
335+
def test_configuration_handler_auto_tag_failure(capsys, lambda_traced):
336336
Configuration.auto_tag = [None, "key2"]
337337
ConfigurationHandler.auto_tag({"key1": datetime, "key2": "value"})
338338
tags = SpansContainer.get_span().execution_tags
@@ -347,7 +347,7 @@ def test_configuration_handler_auto_tag_failure(capsys):
347347
({"a": "b"}, "{'a': 'b'}"), # dict
348348
],
349349
)
350-
def test_configuration_handler_auto_tag_non_string(value, expected):
350+
def test_configuration_handler_auto_tag_non_string(value, expected, lambda_traced):
351351
Configuration.auto_tag = ["key1"]
352352

353353
ConfigurationHandler.auto_tag({"key1": value})

src/test/unit/test_spans_container.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def test_timeout_mechanism_timeout_occurred_send_new_spans(monkeypatch, context,
202202

203203

204204
def test_timeout_mechanism_timeout_occurred_but_finish_check_enrichment(
205-
monkeypatch, context, dummy_span, reporter_mock
205+
monkeypatch, context, dummy_span, reporter_mock, lambda_traced
206206
):
207207
SpansContainer.create_span()
208208
SpansContainer.get_span().start(context=context)

src/test/unit/test_tracer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def get_enrichment_spans(reporter_mock):
423423
return [s for s in final_send if s["type"] == ENRICHMENT_TYPE]
424424

425425

426-
def test_wrapping_with_tags(context, reporter_mock):
426+
def test_wrapping_with_tags(context, reporter_mock, lambda_traced):
427427
key = "my_key"
428428
value = "my_value"
429429

@@ -443,7 +443,7 @@ def lambda_test_function(event, context):
443443
"key, event",
444444
[("my_key", {"my_key": "my_value"}), ("my_key.key2", {"my_key": {"key2": "my_value"}})],
445445
)
446-
def test_wrapping_with_auto_tags(context, key, event, reporter_mock):
446+
def test_wrapping_with_auto_tags(context, key, event, reporter_mock, lambda_traced):
447447
@lumigo_tracer(auto_tag=[key])
448448
def lambda_test_function(event, context):
449449
return "ret_value"

src/test/unit/test_user_utils.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import pytest
23

34
from lumigo_tracer.spans_container import SpansContainer
45
from lumigo_tracer.user_utils import (
@@ -103,7 +104,7 @@ def test_basic_info_warn_error(capsys):
103104
assert captured[2] == error_msg
104105

105106

106-
def test_add_execution_tag():
107+
def test_add_execution_tag(lambda_traced):
107108
key = "my_key"
108109
value = "my_value"
109110
assert add_execution_tag(key, value) is True
@@ -116,31 +117,31 @@ def test_start_manual_trace_simple_flow():
116117
assert SpansContainer.get_span().function_span[MANUAL_TRACES_KEY]
117118

118119

119-
def test_add_execution_key_tag_empty(capsys):
120+
def test_add_execution_key_tag_empty(capsys, lambda_traced):
120121
assert add_execution_tag("", "value") is False
121122
assert "Unable to add tag: key length" in capsys.readouterr().out
122123
assert SpansContainer.get_span().execution_tags == []
123124

124125

125-
def test_add_execution_value_tag_empty(capsys):
126+
def test_add_execution_value_tag_empty(capsys, lambda_traced):
126127
assert add_execution_tag("key", "") is False
127128
assert "Unable to add tag: value length" in capsys.readouterr().out
128129
assert SpansContainer.get_span().execution_tags == []
129130

130131

131-
def test_add_execution_tag_key_pass_max_chars(capsys):
132+
def test_add_execution_tag_key_pass_max_chars(capsys, lambda_traced):
132133
assert add_execution_tag("k" * (MAX_TAG_KEY_LEN + 1), "value") is False
133134
assert "Unable to add tag: key length" in capsys.readouterr().out
134135
assert SpansContainer.get_span().execution_tags == []
135136

136137

137-
def test_add_execution_tag_value_pass_max_chars(capsys):
138+
def test_add_execution_tag_value_pass_max_chars(capsys, lambda_traced):
138139
assert add_execution_tag("key", "v" * (MAX_TAG_VALUE_LEN + 1)) is False
139140
assert "Unable to add tag: value length" in capsys.readouterr().out
140141
assert SpansContainer.get_span().execution_tags == []
141142

142143

143-
def test_add_execution_tag_pass_max_tags(capsys):
144+
def test_add_execution_tag_pass_max_tags(capsys, lambda_traced):
144145
key = "my_key"
145146
value = "my_value"
146147

@@ -166,3 +167,34 @@ def __str__(self):
166167
assert add_execution_tag("key", ExceptionOnStr()) is False
167168
assert "Unable to add tag" in capsys.readouterr().out
168169
assert SpansContainer.get_span().execution_tags == []
170+
171+
172+
@pytest.mark.parametrize(
173+
["kill_switch_value", "is_aws_environment_value", "expected_ret_value", "expected_tags"],
174+
[
175+
( # happy flow - lambda is traced
176+
"false",
177+
"true",
178+
True,
179+
[{"key": "key", "value": "my-value"}],
180+
),
181+
("true", "true", False, []), # kill switch on, is_aws_env true
182+
("true", "", False, []), # kill switch on, is_aws_env false
183+
("false", "", False, []), # kill switch off, is_aws_env false
184+
],
185+
)
186+
def test_add_execution_tag_lambda_not_traced(
187+
kill_switch_value,
188+
is_aws_environment_value,
189+
expected_ret_value,
190+
expected_tags,
191+
capsys,
192+
monkeypatch,
193+
):
194+
monkeypatch.setenv("LUMIGO_SWITCH_OFF", kill_switch_value)
195+
monkeypatch.setenv("AWS_LAMBDA_FUNCTION_VERSION", is_aws_environment_value)
196+
197+
assert add_execution_tag("key", "my-value") is expected_ret_value
198+
if expected_ret_value is False:
199+
assert "Unable to add tag" in capsys.readouterr().out
200+
assert SpansContainer.get_span().execution_tags == expected_tags

src/test/unit/wrappers/http/test_sync_http_wrappers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ def lambda_test_function(event, context):
344344
assert span["info"]["httpInfo"]["request"]["method"] == "POST"
345345

346346

347-
def test_wrapping_with_tags_for_api_gw_headers(monkeypatch, context, token):
347+
def test_wrapping_with_tags_for_api_gw_headers(monkeypatch, context, token, lambda_traced):
348348
monkeypatch.setattr(auto_tag_event, "AUTO_TAG_API_GW_HEADERS", ["Accept"])
349349

350350
@lumigo_tracer.lumigo_tracer()

0 commit comments

Comments
 (0)