Skip to content

Commit f63c856

Browse files
saartochner-lumigoCircleCI
andauthored
feat: add nested-auto-execution-tags (#241)
Co-authored-by: CircleCI <[email protected]>
1 parent 2cc2374 commit f63c856

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/lumigo_tracer/auto_tag/auto_tag_event.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from typing import Dict, List, Optional
44

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

99
AUTO_TAG_API_GW_HEADERS: Optional[List[str]] = (
1010
str_to_list(os.environ.get("LUMIGO_AUTO_TAG_API_GW_HEADERS", "")) or []
@@ -56,8 +56,12 @@ def is_supported(event: dict) -> bool:
5656
@staticmethod
5757
def auto_tag(event: dict):
5858
for key in Configuration.auto_tag:
59-
if key in event:
60-
add_execution_tag(key, event[key])
59+
try:
60+
value = safe_get(event, key.split(".")) # type: ignore
61+
if value:
62+
add_execution_tag(key, value)
63+
except Exception as err:
64+
warn_client(f"Failed to auto tag key {key}: {err}")
6165

6266

6367
class AutoTagEvent:

src/test/unit/auto_tag/test_auto_tag_event.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import datetime
2+
13
import pytest
24

35
from lumigo_tracer.auto_tag import auto_tag_event
@@ -299,13 +301,43 @@ def test_configuration_handler_is_supported(config, event, expected):
299301
assert ConfigurationHandler.is_supported(event) == expected
300302

301303

302-
def test_configuration_handler_auto_tag():
303-
Configuration.auto_tag = ["key1", "key2", "key3"]
304-
ConfigurationHandler.auto_tag({"key1": "value1", "key2": "value2", "other": "other"})
304+
@pytest.mark.parametrize(
305+
"auto_tag_keys, event, result_tags",
306+
[
307+
( # happy flow non-nested
308+
["key1", "key2", "key3"],
309+
{"key1": "value1", "key2": "value2", "other": "other"},
310+
[{"key": "key1", "value": "value1"}, {"key": "key2", "value": "value2"}],
311+
),
312+
(["key1.key2"], {"key1": "value1"}, []), # not exists inner key
313+
(["key1.key2"], {"other": "other"}, []), # not exists outer key
314+
( # happy flow nested
315+
["key1.key2"],
316+
{"key1": {"key2": "value"}, "key3": "other"},
317+
[{"key": "key1.key2", "value": "value"}],
318+
),
319+
( # happy flow nested multiple keys
320+
["key1.key2", "key3.key4"],
321+
{"key1": {"key2": "value"}, "key3": {"key4": "value2"}, "key5": "other"},
322+
[{"key": "key1.key2", "value": "value"}, {"key": "key3.key4", "value": "value2"}],
323+
),
324+
],
325+
)
326+
def test_configuration_handler_auto_tag(auto_tag_keys, event, result_tags):
327+
Configuration.auto_tag = auto_tag_keys
328+
ConfigurationHandler.auto_tag(event)
329+
tags = SpansContainer.get_span().function_span[EXECUTION_TAGS_KEY]
330+
assert len(tags) == len(result_tags)
331+
for tag in result_tags:
332+
assert tag in tags
333+
334+
335+
def test_configuration_handler_auto_tag_failure(capsys):
336+
Configuration.auto_tag = [None, "key2"]
337+
ConfigurationHandler.auto_tag({"key1": datetime, "key2": "value"})
305338
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
339+
assert tags == [{"key": "key2", "value": "value"}]
340+
assert "Failed to auto tag" in capsys.readouterr().out
309341

310342

311343
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)