Skip to content
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

Add support for Sanic 20.3.0 and up #52

Merged
merged 1 commit into from
Oct 5, 2020
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
22 changes: 12 additions & 10 deletions src/dockerflow/sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ def _request_middleware(self, request):
"""
The request middleware.
"""
request["_id"] = str(uuid.uuid4())
request["_start_timestamp"] = time.time()
request.ctx.id = str(uuid.uuid4())
request.ctx.start_timestamp = time.time()

def _response_middleware(self, request, response):
"""
The response middleware.
"""
if not request.get("_logged"):
if not getattr(request.ctx, "logged", False):
extra = self.summary_extra(request)
self.summary_logger.info("", extra=extra)

Expand All @@ -159,7 +159,7 @@ def _exception_handler(self, request, exception):
extra = self.summary_extra(request)
extra["errno"] = 500
self.summary_logger.error(str(exception), extra=extra)
request["_logged"] = True
request.ctx.logged = True

def summary_extra(self, request):
"""
Expand All @@ -175,15 +175,17 @@ def summary_extra(self, request):
}

# the rid value to the current request ID
request_id = request.get("_id", None)
if request_id is not None:
out["rid"] = request_id
try:
out["rid"] = request.ctx.id
except AttributeError:
pass

# and the t value to the time it took to render
start_timestamp = request.get("_start_timestamp", None)
if start_timestamp is not None:
try:
# Duration of request, in milliseconds.
out["t"] = int(1000 * (time.time() - start_timestamp))
out["t"] = int(1000 * (time.time() - request.ctx.start_timestamp))
except AttributeError:
pass

return out

Expand Down
1 change: 1 addition & 0 deletions tests/constraints/sanic-20.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sanic<21
12 changes: 6 additions & 6 deletions tests/sanic/test_sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ def assert_log_record(caplog, errno=0, level=logging.INFO, rid=None, t=int, path

def test_request_summary(caplog, dockerflow, test_client):
request, _ = test_client.get(headers=headers)
assert isinstance(request.get("_start_timestamp"), float)
assert request.get("_id") is not None
assert_log_record(caplog, rid=request.get("_id"))
assert isinstance(request.ctx.start_timestamp, float)
assert request.ctx.id is not None
assert_log_record(caplog, rid=request.ctx.id)


def test_request_summary_exception(app, caplog, dockerflow, test_client):
Expand All @@ -240,7 +240,7 @@ def exception_raiser(request):

request, _ = test_client.get("/exception", headers=headers)
record = assert_log_record(
caplog, 500, logging.ERROR, request.get("_id"), path="/exception"
caplog, 500, logging.ERROR, request.ctx.id, path="/exception"
)
assert record.getMessage() == "exception message"

Expand All @@ -249,8 +249,8 @@ def test_request_summary_failed_request(app, caplog, dockerflow, test_client):
@app.middleware
def hostile_callback(request):
# simulating resetting request changes
del request["_id"]
del request["_start_timestamp"]
del request.ctx.id
del request.ctx.start_timestamp

test_client.get(headers=headers)
assert_log_record(caplog, rid=None, t=None)
7 changes: 4 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ envlist =
py{36,37}-dj{21,22}-tests,
# py{36,37,38}-dj30-tests,
py{27,36,37,38}-fl{011,012,10}-tests,
py{36,37,38}-s19-tests,
py{36,37,38}-s{19,20}-tests,

[testenv]
basepython =
Expand All @@ -27,7 +27,7 @@ deps =
-rtests/requirements/default.txt
dj{111,21,22,30}: -rtests/requirements/django.txt
fl{011,012,10}: -rtests/requirements/flask.txt
s19: -rtests/requirements/sanic.txt
s{19,20}: -rtests/requirements/sanic.txt
dj111: -ctests/constraints/django-1.11.txt
dj21: -ctests/constraints/django-2.1.txt
dj22: -ctests/constraints/django-2.2.txt
Expand All @@ -36,12 +36,13 @@ deps =
fl012: -ctests/constraints/flask-0.12.txt
fl10: -ctests/constraints/flask-1.0.txt
s19: -ctests/constraints/sanic-19.txt
s20: -ctests/constraints/sanic-20.txt
py27: -ctests/constraints/python-2.7.txt
commands =
python --version
dj{111,21,22,30}-tests: pytest tests/core/ tests/django --nomigrations {posargs:}
fl{011,012,10}-tests: pytest tests/core/ tests/flask/ {posargs:}
s19: pytest tests/core/ tests/sanic/
s{19,20}: pytest tests/core/ tests/sanic/ {posargs:}

[testenv:py38-docs]
basepython = python3.8
Expand Down