Skip to content

Commit

Permalink
feat(tracer): disable tracer when for non-Lambda envs (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Brewer authored Aug 10, 2021
1 parent b9a1d54 commit 06be626
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 27 deletions.
3 changes: 1 addition & 2 deletions aws_lambda_powertools/shared/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@

EVENT_HANDLER_DEBUG_ENV: str = "POWERTOOLS_EVENT_HANDLER_DEBUG"

SAM_LOCAL_ENV: str = "AWS_SAM_LOCAL"
CHALICE_LOCAL_ENV: str = "AWS_CHALICE_CLI_MODE"
SERVICE_NAME_ENV: str = "POWERTOOLS_SERVICE_NAME"
XRAY_TRACE_ID_ENV: str = "_X_AMZN_TRACE_ID"
LAMBDA_TASK_ROOT_ENV: str = "LAMBDA_TASK_ROOT"

XRAY_SDK_MODULE: str = "aws_xray_sdk"
XRAY_SDK_CORE_MODULE: str = "aws_xray_sdk.core"
7 changes: 3 additions & 4 deletions aws_lambda_powertools/tracing/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,16 +719,15 @@ def _is_tracer_disabled() -> Union[bool, str]:
Union[bool, str]
"""
logger.debug("Verifying whether Tracing has been disabled")
is_lambda_sam_cli = os.getenv(constants.SAM_LOCAL_ENV)
is_chalice_cli = os.getenv(constants.CHALICE_LOCAL_ENV)
is_lambda_env = os.getenv(constants.LAMBDA_TASK_ROOT_ENV)
is_disabled = resolve_truthy_env_var_choice(env=os.getenv(constants.TRACER_DISABLED_ENV, "false"))

if is_disabled:
logger.debug("Tracing has been disabled via env var POWERTOOLS_TRACE_DISABLED")
return is_disabled

if is_lambda_sam_cli or is_chalice_cli:
logger.debug("Running under SAM CLI env or not in Lambda env; disabling Tracing")
if not is_lambda_env:
logger.debug("Running outside Lambda env; disabling Tracing")
return True

return False
Expand Down
21 changes: 3 additions & 18 deletions tests/functional/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,11 @@ def greeting(name, message):
greeting(name="Foo", message="Bar")


def test_tracer_lambda_emulator(monkeypatch, dummy_response):
# GIVEN tracer runs locally
monkeypatch.setenv("AWS_SAM_LOCAL", "true")
def test_tracer_lambda_outside_lambda_env(monkeypatch, dummy_response):
# GIVEN tracer runs locally (ie: `LAMBDA_TASK_ROOT` is not set)
tracer = Tracer()

# WHEN a lambda function is run through SAM CLI
@tracer.capture_lambda_handler
def handler(event, context):
return dummy_response

# THEN tracer should run in disabled mode, and not raise an Exception
handler({}, {})


def test_tracer_chalice_cli_mode(monkeypatch, dummy_response):
# GIVEN tracer runs locally
monkeypatch.setenv("AWS_CHALICE_CLI_MODE", "true")
tracer = Tracer()

# WHEN a lambda function is run through the Chalice CLI.
# WHEN a lambda function is run through outside of a lambda
@tracer.capture_lambda_handler
def handler(event, context):
return dummy_response
Expand Down
10 changes: 7 additions & 3 deletions tests/unit/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ def greeting(name, message):
)


def test_tracer_custom_metadata(mocker, dummy_response, provider_stub):
def test_tracer_custom_metadata(monkeypatch, mocker, dummy_response, provider_stub):
# GIVEN Tracer is initialized with booking as the service name
monkeypatch.setenv("LAMBDA_TASK_ROOT", "/opt/")
put_metadata_mock = mocker.MagicMock()

provider = provider_stub(put_metadata_mock=put_metadata_mock)
tracer = Tracer(provider=provider, service="booking")

Expand All @@ -143,8 +145,9 @@ def test_tracer_custom_metadata(mocker, dummy_response, provider_stub):
)


def test_tracer_custom_annotation(mocker, dummy_response, provider_stub):
def test_tracer_custom_annotation(monkeypatch, mocker, dummy_response, provider_stub):
# GIVEN Tracer is initialized
monkeypatch.setenv("LAMBDA_TASK_ROOT", "/opt/")
put_annotation_mock = mocker.MagicMock()
provider = provider_stub(put_annotation_mock=put_annotation_mock)
tracer = Tracer(provider=provider)
Expand Down Expand Up @@ -214,8 +217,9 @@ def greeting(name, message):


@mock.patch("aws_lambda_powertools.tracing.tracer.aws_xray_sdk.core.patch")
def test_tracer_patch_modules(xray_patch_mock, mocker):
def test_tracer_patch_modules(xray_patch_mock, monkeypatch, mocker):
# GIVEN tracer is initialized with a list of modules to patch
monkeypatch.setenv("LAMBDA_TASK_ROOT", "/opt/")
modules = ["boto3"]

# WHEN modules are supported by X-Ray
Expand Down

0 comments on commit 06be626

Please sign in to comment.