From fefdc5139366f6ba19159730a8d2b1b584ec3a33 Mon Sep 17 00:00:00 2001 From: nadav3396 Date: Wed, 26 Feb 2025 10:02:21 +0200 Subject: [PATCH 1/2] feat: skip invocations from warmup plugin --- README.md | 1 + src/lumigo_tracer/lambda_tracer/tracer.py | 12 ++++++++++++ src/lumigo_tracer/lumigo_utils.py | 5 +++++ 3 files changed, 18 insertions(+) diff --git a/README.md b/README.md index a05d0ad8..5f16138f 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ def my_lambda(event, context): * `LUMIGO_DOMAINS_SCRUBBER=[".*secret.*"]` - Prevents Lumigo from collecting both request and response details from a list of domains. This accepts a comma-separated list of regular expressions that is JSON-formatted. By default, the tracer uses `["secretsmanager\..*\.amazonaws\.com", "ssm\..*\.amazonaws\.com", "kms\..*\.amazonaws\.com"]`. **Note** - These defaults are overridden when you define a different list of regular expressions. * `LUMIGO_PROPAGATE_W3C=TRUE` - Add W3C TraceContext headers to outgoing HTTP requests. This enables uninterrupted transactions with applications traced with OpenTelemetry. * `LUMIGO_SWITCH_OFF=TRUE` - In the event a critical issue arises, this turns off all actions that Lumigo takes in response to your code. This happens without a deployment, and is picked up on the next function run once the environment variable is present. +* `SKIP_WARMUP_INVOCATIONS=TRUE` - Prevents Lumigo from sending data during invocations from the serverless framework warmup plugin. ### Step Functions diff --git a/src/lumigo_tracer/lambda_tracer/tracer.py b/src/lumigo_tracer/lambda_tracer/tracer.py index 0c30e331..54f7cfea 100644 --- a/src/lumigo_tracer/lambda_tracer/tracer.py +++ b/src/lumigo_tracer/lambda_tracer/tracer.py @@ -9,6 +9,7 @@ config, is_aws_environment, is_kill_switch_on, + is_skip_warmup_on, lumigo_safe_execute, ) @@ -23,6 +24,15 @@ def _is_context_already_wrapped(*args) -> bool: # type: ignore[no-untyped-def] return len(args) >= 2 and hasattr(args[1], CONTEXT_WRAPPED_BY_LUMIGO_KEY) +def is_warmup_invocation(*args) -> bool: # type: ignore[no-untyped-def] + """ + This function is here in order to detect the serverless framework warmup plugin invocations. + """ + if len(args) > 0 and args[0] == "warmup": + return True + return False + + def _add_wrap_flag_to_context(*args): # type: ignore[no-untyped-def] """ This function is here in order to validate that we didn't already wrap this invocation @@ -42,6 +52,8 @@ def _lumigo_tracer(func): # type: ignore[no-untyped-def] def lambda_wrapper(*args, **kwargs): # type: ignore[no-untyped-def] if _is_context_already_wrapped(*args): return func(*args, **kwargs) + if is_skip_warmup_on() and is_warmup_invocation(*args): + return func(*args, **kwargs) _add_wrap_flag_to_context(*args) executed = False ret_val = None diff --git a/src/lumigo_tracer/lumigo_utils.py b/src/lumigo_tracer/lumigo_utils.py index 1d298fc8..ccbdc919 100644 --- a/src/lumigo_tracer/lumigo_utils.py +++ b/src/lumigo_tracer/lumigo_utils.py @@ -53,6 +53,7 @@ LUMIGO_TOKEN_KEY = "LUMIGO_TRACER_TOKEN" LUMIGO_USE_TRACER_EXTENSION = "LUMIGO_USE_TRACER_EXTENSION" KILL_SWITCH = "LUMIGO_SWITCH_OFF" +SKIP_WARMUP_INVOCATIONS = "SKIP_WARMUP_INVOCATIONS" EDGE_KINESIS_STREAM_NAME = "prod_trc-inges-edge_edge-kinesis-stream" STACKTRACE_LINE_TO_DROP = "lumigo_tracer/lambda_tracer/tracer.py" Container = TypeVar("Container", dict, list) # type: ignore[type-arg,type-arg] @@ -353,6 +354,10 @@ def is_kill_switch_on(): # type: ignore[no-untyped-def] return str(os.environ.get(KILL_SWITCH, "")).lower() == "true" +def is_skip_warmup_on(): # type: ignore[no-untyped-def] + return str(os.environ.get(SKIP_WARMUP_INVOCATIONS, "")).lower() == "true" + + def get_size_upper_bound() -> int: return CoreConfiguration.get_max_entry_size(True) From c0f61cba71d18fec2c0e6a9e092101f8e269552f Mon Sep 17 00:00:00 2001 From: nadav3396 Date: Wed, 26 Feb 2025 10:46:00 +0200 Subject: [PATCH 2/2] fix: add logs --- src/lumigo_tracer/lambda_tracer/tracer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lumigo_tracer/lambda_tracer/tracer.py b/src/lumigo_tracer/lambda_tracer/tracer.py index 54f7cfea..6390ddd8 100644 --- a/src/lumigo_tracer/lambda_tracer/tracer.py +++ b/src/lumigo_tracer/lambda_tracer/tracer.py @@ -53,6 +53,7 @@ def lambda_wrapper(*args, **kwargs): # type: ignore[no-untyped-def] if _is_context_already_wrapped(*args): return func(*args, **kwargs) if is_skip_warmup_on() and is_warmup_invocation(*args): + get_logger().info("skipping warmup invocation") return func(*args, **kwargs) _add_wrap_flag_to_context(*args) executed = False