Skip to content

Commit 39ea4e4

Browse files
authored
Add querystring to MozLog "request.summary" (#97)
* Add querystring to MozLog request.summary * Put querystring logging behing a flag
1 parent 2ba0254 commit 39ea4e4

File tree

12 files changed

+87
-4
lines changed

12 files changed

+87
-4
lines changed

docs/django.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ spec:
287287
.. http:get:: /__heartbeat__
288288
289289
The heartbeat view will go through the list of configured Dockerflow
290-
checks in the :ref:`DOCKERFLOW_CHECKS` setting, run each check, and, if
290+
checks in the :ref:`DOCKERFLOW_CHECKS` setting, run each check, and, if
291291
`settings.DEBUG` is `True`, add their results to a JSON response.
292292

293293
The view will return HTTP responses with either a status code of 200 if
@@ -398,6 +398,12 @@ configure **at least** the ``request.summary`` logger that way::
398398
}
399399
}
400400

401+
In order to include querystrings in the request summary log, set this flag in settings:
402+
403+
.. code-block:: python
404+
405+
DOCKERFLOW_SUMMARY_LOG_QUERYSTRING = True
406+
401407
402408
.. _django-static:
403409

docs/fastapi.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,15 @@ for at least the ``request.summary`` logger:
307307
}
308308
})
309309
310+
311+
In order to include querystrings in the request summary log, set this flag in the application state:
312+
313+
.. code-block:: python
314+
315+
app.state.DOCKERFLOW_SUMMARY_LOG_QUERYSTRING = True
316+
317+
318+
310319
.. _fastapi-static:
311320

312321
Static content

docs/flask.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ for at least the ``request.summary`` logger::
453453
}
454454
})
455455

456+
In order to include querystrings in the request summary log, set this flag in :ref:`configuration <flask-config>`::
457+
458+
DOCKERFLOW_SUMMARY_LOG_QUERYSTRING = True
459+
456460
.. _flask-static:
457461

458462
Static content

docs/sanic.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,11 @@ Alternatively you can also pass the same logging config dictionary to the
452452

453453
sanic = Sanic(__name__)
454454

455+
In order to include querystrings in the request summary log, set this flag in :ref:`configuration <sanic-config>`::
456+
457+
DOCKERFLOW_SUMMARY_LOG_QUERYSTRING = True
458+
459+
455460
.. _sanic-static:
456461

457462
Static content

src/dockerflow/django/middleware.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import logging
22
import re
33
import time
4+
import urllib
45
import uuid
56

7+
from django.conf import settings
68
from django.utils.deprecation import MiddlewareMixin
79

810
from . import views
@@ -44,6 +46,11 @@ def _build_extra_meta(self, request):
4446
"path": request.path,
4547
}
4648

49+
if getattr(settings, "DOCKERFLOW_SUMMARY_LOG_QUERYSTRING", False):
50+
out["querystring"] = urllib.parse.unquote(
51+
request.META.get("QUERY_STRING", "")
52+
)
53+
4754
# HACK: It's possible some other middleware has replaced the request we
4855
# modified earlier, so be sure to check for existence of these
4956
# attributes before trying to use them.

src/dockerflow/fastapi/middleware.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import sys
55
import time
6+
import urllib
67
from typing import Any, Dict
78

89
from asgiref.typing import (
@@ -66,11 +67,15 @@ def _format(self, scope: HTTPScope, info) -> Dict[str, Any]:
6667
info["request_headers"][header_key] = header_val
6768

6869
request_duration_ms = (info["end_time"] - info["start_time"]) * 1000.0
69-
return {
70+
fields = {
7071
"agent": info["request_headers"].get("user-agent", ""),
7172
"path": scope["path"],
7273
"method": scope["method"],
7374
"code": info["response"]["status"],
7475
"lang": info["request_headers"].get("accept-language"),
7576
"t": int(request_duration_ms),
7677
}
78+
79+
if getattr(scope["app"].state, "DOCKERFLOW_SUMMARY_LOG_QUERYSTRING", False):
80+
fields["querystring"] = urllib.parse.unquote(scope["query_string"].decode())
81+
return fields

src/dockerflow/flask/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@ def summary_extra(self):
259259
"path": flask.request.path,
260260
}
261261

262+
if flask.current_app.config.get("DOCKERFLOW_SUMMARY_LOG_QUERYSTRING", False):
263+
out["querystring"] = flask.request.query_string.decode()
264+
262265
# set the uid value to the current user ID
263266
user_id = self.user_id()
264267
if user_id is None:

src/dockerflow/sanic/app.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
import time
7+
import urllib
78
import uuid
89
import warnings
910
from inspect import isawaitable
@@ -162,6 +163,9 @@ def summary_extra(self, request):
162163
"uid": "",
163164
}
164165

166+
if request.app.config.get("DOCKERFLOW_SUMMARY_LOG_QUERYSTRING", False):
167+
out["querystring"] = urllib.parse.unquote(request.query_string)
168+
165169
# the rid value to the current request ID
166170
try:
167171
out["rid"] = request.ctx.id

tests/django/test_django.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,21 @@ def test_request_summary(admin_user, caplog, dockerflow_middleware, dockerflow_r
169169
assert getattr(dockerflow_request, "uid", None) is None
170170

171171

172+
def test_request_summary_querystring(
173+
settings, admin_user, caplog, dockerflow_middleware, rf
174+
):
175+
settings.DOCKERFLOW_SUMMARY_LOG_QUERYSTRING = True
176+
177+
request = rf.get("/?x=%D8%B4%D9%83%D8%B1")
178+
response = dockerflow_middleware.process_request(request)
179+
response = dockerflow_middleware.process_response(request, response)
180+
181+
assert len(caplog.records) == 1
182+
record = caplog.records[0]
183+
assert record.querystring == "x=شكر"
184+
assert isinstance(record.t, int)
185+
186+
172187
def test_request_summary_admin_user(
173188
admin_user, caplog, dockerflow_middleware, dockerflow_request
174189
):

tests/fastapi/test_fastapi.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ def test_lbheartbeat_head(client):
4242
assert response.content == b""
4343

4444

45-
def test_mozlog(client, caplog):
45+
def test_mozlog(app, client, caplog):
46+
app.state.DOCKERFLOW_SUMMARY_LOG_QUERYSTRING = True
47+
4648
client.get(
47-
"/__lbheartbeat__",
49+
"/__lbheartbeat__?x=شكر",
4850
headers={
4951
"User-Agent": "dockerflow/tests",
5052
"Accept-Language": "en-US",
@@ -58,6 +60,7 @@ def test_mozlog(client, caplog):
5860
assert record.method == "GET"
5961
assert record.code == 200
6062
assert record.path == "/__lbheartbeat__"
63+
assert record.querystring == "x=شكر"
6164
assert isinstance(record.t, int)
6265

6366

0 commit comments

Comments
 (0)