From 2e7de92e5f9cd83f01222eb06385d66fe0211777 Mon Sep 17 00:00:00 2001 From: nifflets <5343516+nifflets@users.noreply.github.com> Date: Mon, 13 May 2024 11:36:19 -0700 Subject: [PATCH] feat: support disabling execution id logging (#325) * feat: support disabling execution id logging * Update test_execution_id.py * Update __init__.py * Update __init__.py --- src/functions_framework/__init__.py | 5 +++- tests/test_execution_id.py | 46 ++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/functions_framework/__init__.py b/src/functions_framework/__init__.py index 7474c01e..df4683cb 100644 --- a/src/functions_framework/__init__.py +++ b/src/functions_framework/__init__.py @@ -443,7 +443,10 @@ def _configure_app_execution_id_logging(): def _enable_execution_id_logging(): - return os.environ.get("LOG_EXECUTION_ID") + # Based on distutils.util.strtobool + truthy_values = ("y", "yes", "t", "true", "on", "1") + env_var_value = os.environ.get("LOG_EXECUTION_ID") + return env_var_value in truthy_values app = LazyWSGIApp() diff --git a/tests/test_execution_id.py b/tests/test_execution_id.py index bfddfc3c..c650ee31 100644 --- a/tests/test_execution_id.py +++ b/tests/test_execution_id.py @@ -46,7 +46,7 @@ def test_user_function_can_retrieve_execution_id_from_header(): def test_uncaught_exception_in_user_function_sets_execution_id(capsys, monkeypatch): - monkeypatch.setenv("LOG_EXECUTION_ID", "True") + monkeypatch.setenv("LOG_EXECUTION_ID", "true") source = TEST_FUNCTIONS_DIR / "execution_id" / "main.py" target = "error" app = create_app(target, source) @@ -64,7 +64,7 @@ def test_uncaught_exception_in_user_function_sets_execution_id(capsys, monkeypat def test_print_from_user_function_sets_execution_id(capsys, monkeypatch): - monkeypatch.setenv("LOG_EXECUTION_ID", "True") + monkeypatch.setenv("LOG_EXECUTION_ID", "true") source = TEST_FUNCTIONS_DIR / "execution_id" / "main.py" target = "print_message" app = create_app(target, source) @@ -83,7 +83,7 @@ def test_print_from_user_function_sets_execution_id(capsys, monkeypatch): def test_log_from_user_function_sets_execution_id(capsys, monkeypatch): - monkeypatch.setenv("LOG_EXECUTION_ID", "True") + monkeypatch.setenv("LOG_EXECUTION_ID", "true") source = TEST_FUNCTIONS_DIR / "execution_id" / "main.py" target = "log_message" app = create_app(target, source) @@ -136,6 +136,44 @@ def test_does_not_set_execution_id_when_not_enabled(capsys): assert "some-message" in record.out +def test_does_not_set_execution_id_when_env_var_is_false(capsys, monkeypatch): + monkeypatch.setenv("LOG_EXECUTION_ID", "false") + source = TEST_FUNCTIONS_DIR / "execution_id" / "main.py" + target = "print_message" + app = create_app(target, source) + client = app.test_client() + client.post( + "/", + headers={ + "Function-Execution-Id": TEST_EXECUTION_ID, + "Content-Type": "application/json", + }, + json={"message": "some-message"}, + ) + record = capsys.readouterr() + assert f'"execution_id": "{TEST_EXECUTION_ID}"' not in record.out + assert "some-message" in record.out + + +def test_does_not_set_execution_id_when_env_var_is_not_bool_like(capsys, monkeypatch): + monkeypatch.setenv("LOG_EXECUTION_ID", "maybe") + source = TEST_FUNCTIONS_DIR / "execution_id" / "main.py" + target = "print_message" + app = create_app(target, source) + client = app.test_client() + client.post( + "/", + headers={ + "Function-Execution-Id": TEST_EXECUTION_ID, + "Content-Type": "application/json", + }, + json={"message": "some-message"}, + ) + record = capsys.readouterr() + assert f'"execution_id": "{TEST_EXECUTION_ID}"' not in record.out + assert "some-message" in record.out + + def test_generate_execution_id(): expected_matching_regex = "^[0-9a-zA-Z]{12}$" actual_execution_id = execution_id._generate_execution_id() @@ -283,7 +321,7 @@ def test_log_handler_omits_empty_execution_context(monkeypatch, capsys): @pytest.mark.asyncio async def test_maintains_execution_id_for_concurrent_requests(monkeypatch, capsys): - monkeypatch.setenv("LOG_EXECUTION_ID", "True") + monkeypatch.setenv("LOG_EXECUTION_ID", "true") monkeypatch.setattr( execution_id, "_generate_execution_id",