Skip to content

feat: support disabling execution id logging #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading