Skip to content

Commit

Permalink
feat: support disabling execution id logging (#325)
Browse files Browse the repository at this point in the history
* feat: support disabling execution id logging

* Update test_execution_id.py

* Update __init__.py

* Update __init__.py
  • Loading branch information
nifflets committed May 13, 2024
1 parent 662bf4c commit 2e7de92
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/functions_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
46 changes: 42 additions & 4 deletions tests/test_execution_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 2e7de92

Please sign in to comment.