Skip to content

Commit 2e7de92

Browse files
authored
feat: support disabling execution id logging (#325)
* feat: support disabling execution id logging * Update test_execution_id.py * Update __init__.py * Update __init__.py
1 parent 662bf4c commit 2e7de92

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/functions_framework/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,10 @@ def _configure_app_execution_id_logging():
443443

444444

445445
def _enable_execution_id_logging():
446-
return os.environ.get("LOG_EXECUTION_ID")
446+
# Based on distutils.util.strtobool
447+
truthy_values = ("y", "yes", "t", "true", "on", "1")
448+
env_var_value = os.environ.get("LOG_EXECUTION_ID")
449+
return env_var_value in truthy_values
447450

448451

449452
app = LazyWSGIApp()

tests/test_execution_id.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_user_function_can_retrieve_execution_id_from_header():
4646

4747

4848
def test_uncaught_exception_in_user_function_sets_execution_id(capsys, monkeypatch):
49-
monkeypatch.setenv("LOG_EXECUTION_ID", "True")
49+
monkeypatch.setenv("LOG_EXECUTION_ID", "true")
5050
source = TEST_FUNCTIONS_DIR / "execution_id" / "main.py"
5151
target = "error"
5252
app = create_app(target, source)
@@ -64,7 +64,7 @@ def test_uncaught_exception_in_user_function_sets_execution_id(capsys, monkeypat
6464

6565

6666
def test_print_from_user_function_sets_execution_id(capsys, monkeypatch):
67-
monkeypatch.setenv("LOG_EXECUTION_ID", "True")
67+
monkeypatch.setenv("LOG_EXECUTION_ID", "true")
6868
source = TEST_FUNCTIONS_DIR / "execution_id" / "main.py"
6969
target = "print_message"
7070
app = create_app(target, source)
@@ -83,7 +83,7 @@ def test_print_from_user_function_sets_execution_id(capsys, monkeypatch):
8383

8484

8585
def test_log_from_user_function_sets_execution_id(capsys, monkeypatch):
86-
monkeypatch.setenv("LOG_EXECUTION_ID", "True")
86+
monkeypatch.setenv("LOG_EXECUTION_ID", "true")
8787
source = TEST_FUNCTIONS_DIR / "execution_id" / "main.py"
8888
target = "log_message"
8989
app = create_app(target, source)
@@ -136,6 +136,44 @@ def test_does_not_set_execution_id_when_not_enabled(capsys):
136136
assert "some-message" in record.out
137137

138138

139+
def test_does_not_set_execution_id_when_env_var_is_false(capsys, monkeypatch):
140+
monkeypatch.setenv("LOG_EXECUTION_ID", "false")
141+
source = TEST_FUNCTIONS_DIR / "execution_id" / "main.py"
142+
target = "print_message"
143+
app = create_app(target, source)
144+
client = app.test_client()
145+
client.post(
146+
"/",
147+
headers={
148+
"Function-Execution-Id": TEST_EXECUTION_ID,
149+
"Content-Type": "application/json",
150+
},
151+
json={"message": "some-message"},
152+
)
153+
record = capsys.readouterr()
154+
assert f'"execution_id": "{TEST_EXECUTION_ID}"' not in record.out
155+
assert "some-message" in record.out
156+
157+
158+
def test_does_not_set_execution_id_when_env_var_is_not_bool_like(capsys, monkeypatch):
159+
monkeypatch.setenv("LOG_EXECUTION_ID", "maybe")
160+
source = TEST_FUNCTIONS_DIR / "execution_id" / "main.py"
161+
target = "print_message"
162+
app = create_app(target, source)
163+
client = app.test_client()
164+
client.post(
165+
"/",
166+
headers={
167+
"Function-Execution-Id": TEST_EXECUTION_ID,
168+
"Content-Type": "application/json",
169+
},
170+
json={"message": "some-message"},
171+
)
172+
record = capsys.readouterr()
173+
assert f'"execution_id": "{TEST_EXECUTION_ID}"' not in record.out
174+
assert "some-message" in record.out
175+
176+
139177
def test_generate_execution_id():
140178
expected_matching_regex = "^[0-9a-zA-Z]{12}$"
141179
actual_execution_id = execution_id._generate_execution_id()
@@ -283,7 +321,7 @@ def test_log_handler_omits_empty_execution_context(monkeypatch, capsys):
283321

284322
@pytest.mark.asyncio
285323
async def test_maintains_execution_id_for_concurrent_requests(monkeypatch, capsys):
286-
monkeypatch.setenv("LOG_EXECUTION_ID", "True")
324+
monkeypatch.setenv("LOG_EXECUTION_ID", "true")
287325
monkeypatch.setattr(
288326
execution_id,
289327
"_generate_execution_id",

0 commit comments

Comments
 (0)