Skip to content

Commit

Permalink
Add user agent to logs, if it is set on request. (#2048)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen authored Sep 11, 2024
1 parent 4cda331 commit 49c534b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/palace/manager/service/logging/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def ensure_str(s: Any) -> Any:
}
if flask_request.query_string:
data["request"]["query"] = flask_request.query_string.decode()
if user_agent := flask_request.headers.get("User-Agent"):
data["request"]["user_agent"] = user_agent

# If we are running in uwsgi context, we include the worker id in the log
if uwsgi:
Expand Down
4 changes: 3 additions & 1 deletion tests/manager/service/logging/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,18 @@ def test_flask_request(
assert request["method"] == "GET"
assert "host" in request
assert "query" not in request
assert "user-agent" not in request

with flask_app_fixture.test_request_context(
"/test?query=string&foo=bar", method="POST"
"/test?query=string&foo=bar", method="POST", headers={"User-Agent": "UA"}
):
data = json.loads(formatter.format(record))
assert "request" in data
request = data["request"]
assert request["path"] == "/test"
assert request["method"] == "POST"
assert request["query"] == "query=string&foo=bar"
assert request["user_agent"] == "UA"

# If flask is not installed, the request data is not included in the log.
with patch("palace.manager.service.logging.log.flask_request", None):
Expand Down

0 comments on commit 49c534b

Please sign in to comment.