Skip to content

Commit 7d41f0e

Browse files
add execution tags by env vars (#239)
* add execution tags by env vars
1 parent 37c32df commit 7d41f0e

File tree

5 files changed

+101
-4
lines changed

5 files changed

+101
-4
lines changed

src/lumigo_tracer/auto_tag/auto_tag_event.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from lumigo_tracer.user_utils import add_execution_tag
66
from lumigo_tracer.parsing_utils import str_to_list
7-
from lumigo_tracer.lumigo_utils import get_logger, is_api_gw_event
7+
from lumigo_tracer.lumigo_utils import get_logger, is_api_gw_event, Configuration
88

99
AUTO_TAG_API_GW_HEADERS: Optional[List[str]] = (
1010
str_to_list(os.environ.get("LUMIGO_AUTO_TAG_API_GW_HEADERS", "")) or []
@@ -48,13 +48,25 @@ def auto_tag(event: dict):
4848
add_execution_tag(key, headers[key])
4949

5050

51+
class ConfigurationHandler(EventAutoTagHandler):
52+
@staticmethod
53+
def is_supported(event: dict) -> bool:
54+
return bool(event) and any(key in event for key in Configuration.auto_tag)
55+
56+
@staticmethod
57+
def auto_tag(event: dict):
58+
for key in Configuration.auto_tag:
59+
if key in event:
60+
add_execution_tag(key, event[key])
61+
62+
5163
class AutoTagEvent:
5264
@staticmethod
5365
def auto_tag_event(
5466
event: Optional[Dict] = None, handlers: Optional[List[EventAutoTagHandler]] = None
5567
) -> None:
5668
if event:
57-
handlers = handlers or [ApiGWHandler()]
69+
handlers = handlers or [ApiGWHandler(), ConfigurationHandler()]
5870
for handler in handlers:
5971
try:
6072
if handler.is_supported(event):

src/lumigo_tracer/lumigo_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
EDGE_KINESIS_STREAM_NAME = "prod_trc-inges-edge_edge-kinesis-stream"
8383
STACKTRACE_LINE_TO_DROP = "lumigo_tracer/tracer.py"
8484
Container = TypeVar("Container", dict, list)
85+
DEFAULT_AUTO_TAG_KEY = "LUMIGO_AUTO_TAG"
8586

8687
_logger: Dict[str, logging.Logger] = {}
8788

@@ -139,6 +140,7 @@ class Configuration:
139140
edge_kinesis_aws_secret_access_key: Optional[str] = None
140141
should_scrub_known_services: bool = False
141142
is_sync_tracer: bool = False
143+
auto_tag: List[str] = []
142144

143145
@staticmethod
144146
def get_max_entry_size(has_error: bool = False) -> int:
@@ -162,6 +164,7 @@ def config(
162164
edge_kinesis_stream_name: Optional[str] = None,
163165
edge_kinesis_aws_access_key_id: Optional[str] = None,
164166
edge_kinesis_aws_secret_access_key: Optional[str] = None,
167+
auto_tag: Optional[List[str]] = None,
165168
) -> None:
166169
"""
167170
This function configure the lumigo wrapper.
@@ -181,6 +184,7 @@ def config(
181184
:param edge_kinesis_stream_name: The name of the Kinesis to push the spans in China region
182185
:param edge_kinesis_aws_access_key_id: The credentials to push to the Kinesis in China region
183186
:param edge_kinesis_aws_secret_access_key: The credentials to push to the Kinesis in China region
187+
:param auto_tag: The keys from the event that should be used as execution tags.
184188
"""
185189

186190
Configuration.token = token or os.environ.get(LUMIGO_TOKEN_KEY, "")
@@ -242,6 +246,9 @@ def config(
242246
os.environ.get("LUMIGO_SCRUB_KNOWN_SERVICES") == "true"
243247
)
244248
Configuration.is_sync_tracer = os.environ.get(LUMIGO_SYNC_TRACING, "FALSE").lower() == "true"
249+
Configuration.auto_tag = auto_tag or os.environ.get(
250+
"LUMIGO_AUTO_TAG", DEFAULT_AUTO_TAG_KEY
251+
).split(",")
245252

246253

247254
def _is_span_has_error(span: dict) -> bool:

src/test/unit/auto_tag/test_auto_tag_event.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import pytest
2+
13
from lumigo_tracer.auto_tag import auto_tag_event
2-
from lumigo_tracer.auto_tag.auto_tag_event import EventAutoTagHandler, AutoTagEvent
4+
from lumigo_tracer.auto_tag.auto_tag_event import (
5+
EventAutoTagHandler,
6+
AutoTagEvent,
7+
ConfigurationHandler,
8+
)
39
from lumigo_tracer.spans_container import SpansContainer
4-
from lumigo_tracer.lumigo_utils import EXECUTION_TAGS_KEY
10+
from lumigo_tracer.lumigo_utils import EXECUTION_TAGS_KEY, Configuration
511

612

713
class ExceptionHandler(EventAutoTagHandler):
@@ -278,3 +284,41 @@ def test_auto_tag_key_in_header(monkeypatch):
278284

279285
def set_header_key(monkeypatch, header: str):
280286
monkeypatch.setattr(auto_tag_event, "AUTO_TAG_API_GW_HEADERS", [header])
287+
288+
289+
@pytest.mark.parametrize(
290+
"config, event, expected",
291+
[
292+
(["key1"], {"key1": "value1"}, True),
293+
(["key1"], {"key2": "value2"}, False),
294+
([], {}, False),
295+
],
296+
)
297+
def test_configuration_handler_is_supported(config, event, expected):
298+
Configuration.auto_tag = config
299+
assert ConfigurationHandler.is_supported(event) == expected
300+
301+
302+
def test_configuration_handler_auto_tag():
303+
Configuration.auto_tag = ["key1", "key2", "key3"]
304+
ConfigurationHandler.auto_tag({"key1": "value1", "key2": "value2", "other": "other"})
305+
tags = SpansContainer.get_span().function_span[EXECUTION_TAGS_KEY]
306+
assert len(tags) == 2
307+
assert {"key": "key1", "value": "value1"} in tags
308+
assert {"key": "key2", "value": "value2"} in tags
309+
310+
311+
@pytest.mark.parametrize(
312+
"value, expected",
313+
[
314+
(1, "1"), # int
315+
({"a": "b"}, "{'a': 'b'}"), # dict
316+
],
317+
)
318+
def test_configuration_handler_auto_tag_non_string(value, expected):
319+
Configuration.auto_tag = ["key1"]
320+
321+
ConfigurationHandler.auto_tag({"key1": value})
322+
323+
tags = SpansContainer.get_span().function_span[EXECUTION_TAGS_KEY]
324+
assert {"key": "key1", "value": expected} in tags

src/test/unit/test_lumigo_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
InternalState,
5050
concat_old_body_to_new,
5151
TRUNCATE_SUFFIX,
52+
DEFAULT_AUTO_TAG_KEY,
5253
)
5354
import json
5455

@@ -398,6 +399,24 @@ def test_config_step_function_without_envs(monkeypatch, configuration_value):
398399
assert Configuration.is_step_function == configuration_value
399400

400401

402+
def test_config_lumigo_auto_tag(monkeypatch):
403+
monkeypatch.setenv("LUMIGO_AUTO_TAG", "key1,key2")
404+
config()
405+
assert Configuration.auto_tag == ["key1", "key2"]
406+
407+
408+
def test_config_lumigo_no_auto_tag_env(monkeypatch):
409+
monkeypatch.delenv("LUMIGO_AUTO_TAG", raising=False)
410+
config()
411+
assert Configuration.auto_tag == [DEFAULT_AUTO_TAG_KEY]
412+
413+
414+
def test_config_lumigo_auto_tag_kwarg(monkeypatch):
415+
monkeypatch.delenv("LUMIGO_AUTO_TAG", raising=False)
416+
config(auto_tag=["key1", "key2"])
417+
assert Configuration.auto_tag == ["key1", "key2"]
418+
419+
401420
def test_config_lumigo_domains_scrubber_with_envs(monkeypatch):
402421
monkeypatch.setenv("LUMIGO_DOMAINS_SCRUBBER", '["lambda.us-west-2.amazonaws.com"]')
403422
config()

src/test/unit/test_tracer.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,21 @@ def lambda_test_function(event, context):
341341
]
342342

343343

344+
def test_wrapping_with_auto_tags(context):
345+
key = "my_key"
346+
value = "my_value"
347+
348+
@lumigo_tracer(auto_tag=[key])
349+
def lambda_test_function(event, context):
350+
return "ret_value"
351+
352+
result = lambda_test_function({key: value}, context)
353+
assert result == "ret_value"
354+
assert SpansContainer.get_span().function_span[EXECUTION_TAGS_KEY] == [
355+
{"key": key, "value": value}
356+
]
357+
358+
344359
def test_not_jsonable_return(monkeypatch, context):
345360
@lumigo_tracer()
346361
def lambda_test_function(event, context):

0 commit comments

Comments
 (0)