Skip to content

Commit d1dd670

Browse files
RD-3530 - support events with list for step functions (#143)
* support events with list for step functions
1 parent fb9e3ed commit d1dd670

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

src/lumigo_tracer/parsers/utils.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ def parse_triggered_by(event: dict):
163163
"""
164164
with lumigo_safe_execute("triggered by"):
165165
if not isinstance(event, dict):
166+
if _is_step_function(event):
167+
return _parse_step_function(event)
166168
return None
167169
if _is_supported_http_method(event):
168170
return parse_http_method(event)
@@ -183,9 +185,11 @@ def _parse_unknown(event: dict):
183185
return result
184186

185187

186-
def _is_step_function(event):
187-
return Configuration.is_step_function and STEP_FUNCTION_UID_KEY in recursive_get_key(
188-
event, LUMIGO_EVENT_KEY, default={}
188+
def _is_step_function(event: Union[List, Dict]):
189+
return (
190+
Configuration.is_step_function
191+
and isinstance(event, (list, dict)) # noqa
192+
and STEP_FUNCTION_UID_KEY in recursive_get_key(event, LUMIGO_EVENT_KEY, default={}) # noqa
189193
)
190194

191195

@@ -346,16 +350,22 @@ def str_to_tuple(val: str) -> Optional[Tuple]:
346350
return None
347351

348352

349-
def recursive_get_key(d: Dict[str, Union[Dict, str]], key, depth=None, default=None):
353+
def recursive_get_key(d: Union[List, Dict[str, Union[Dict, str]]], key, depth=None, default=None):
350354
if depth is None:
351355
depth = Configuration.get_key_depth
352356
if depth == 0:
353357
return default
354358
if key in d:
355359
return d[key]
356-
for v in d.values():
357-
if isinstance(v, dict):
360+
if isinstance(d, list):
361+
for v in d:
358362
recursive_result = recursive_get_key(v, key, depth - 1, default)
359363
if recursive_result:
360364
return recursive_result
365+
if isinstance(d, dict):
366+
for v in d.values():
367+
if isinstance(v, (list, dict)):
368+
recursive_result = recursive_get_key(v, key, depth - 1, default)
369+
if recursive_result:
370+
return recursive_result
361371
return default

src/lumigo_tracer/utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
WARN_CLIENT_PREFIX = "Lumigo Warning"
5252
TRUNCATE_SUFFIX = "...[too long]"
5353
NUMBER_OF_SPANS_IN_REPORT_OPTIMIZATION = 200
54+
DEFAULT_KEY_DEPTH = 4
5455

5556
_logger: Union[logging.Logger, None] = None
5657

@@ -78,7 +79,7 @@ class Configuration:
7879
send_only_if_error: bool = False
7980
domains_scrubber: Optional[List] = None
8081
max_entry_size: int = DEFAULT_MAX_ENTRY_SIZE
81-
get_key_depth: int = 3
82+
get_key_depth: int = DEFAULT_KEY_DEPTH
8283

8384

8485
def config(
@@ -108,7 +109,7 @@ def config(
108109
The default is 10% of the duration of the lambda (with upper and lower bounds of 0.5 and 3 seconds).
109110
:param domains_scrubber: List of regexes. We will not collect data of requests with hosts that match it.
110111
:param max_entry_size: The maximum size of each entry when sending back the events.
111-
:param get_key_depth: Max depth to search the lumigo key in the event (relevant to step functions). default 3.
112+
:param get_key_depth: Max depth to search the lumigo key in the event (relevant to step functions). default 4.
112113
"""
113114
if should_report is not None:
114115
Configuration.should_report = should_report
@@ -120,7 +121,9 @@ def config(
120121
enhance_print or os.environ.get("LUMIGO_ENHANCED_PRINT", "").lower() == "true"
121122
)
122123
Configuration.verbose = verbose and os.environ.get("LUMIGO_VERBOSE", "").lower() != "false"
123-
Configuration.get_key_depth = get_key_depth or int(os.environ.get("LUMIGO_EVENT_KEY_DEPTH", 3))
124+
Configuration.get_key_depth = get_key_depth or int(
125+
os.environ.get("LUMIGO_EVENT_KEY_DEPTH", DEFAULT_KEY_DEPTH)
126+
)
124127
Configuration.is_step_function = (
125128
step_function or os.environ.get("LUMIGO_STEP_FUNCTION", "").lower() == "true"
126129
)

src/test/unit/parsers/test_utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,28 @@ def test_recursive_json_join(d1, d2, result):
208208
},
209209
{"triggeredBy": "stepFunction", "messageId": "54589cfc-5ed8-4799-8fc0-5b45f6f225d1"},
210210
),
211+
( # Step Function from list
212+
[
213+
{
214+
"bla": "saart",
215+
"inner": {
216+
"_lumigo": {"step_function_uid": "54589cfc-5ed8-4799-8fc0-5b45f6f225d1"}
217+
},
218+
},
219+
{"something": "else"},
220+
],
221+
{"triggeredBy": "stepFunction", "messageId": "54589cfc-5ed8-4799-8fc0-5b45f6f225d1"},
222+
),
223+
( # Step Function from inner list
224+
{
225+
"bla": "saart",
226+
"inner": [
227+
{"_lumigo": {"step_function_uid": "54589cfc-5ed8-4799-8fc0-5b45f6f225d1"}},
228+
{"something": "else"},
229+
],
230+
},
231+
{"triggeredBy": "stepFunction", "messageId": "54589cfc-5ed8-4799-8fc0-5b45f6f225d1"},
232+
),
211233
( # Step Function - too deep
212234
{
213235
"bla": "saart",

0 commit comments

Comments
 (0)