diff --git a/src/dockerflow/sanic/app.py b/src/dockerflow/sanic/app.py index 00d5cb3..fea87c7 100644 --- a/src/dockerflow/sanic/app.py +++ b/src/dockerflow/sanic/app.py @@ -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) @@ -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): """ @@ -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 diff --git a/tests/constraints/sanic-20.txt b/tests/constraints/sanic-20.txt new file mode 100644 index 0000000..d450454 --- /dev/null +++ b/tests/constraints/sanic-20.txt @@ -0,0 +1 @@ +Sanic<21 diff --git a/tests/sanic/test_sanic.py b/tests/sanic/test_sanic.py index 14b7e1b..f3682a2 100644 --- a/tests/sanic/test_sanic.py +++ b/tests/sanic/test_sanic.py @@ -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): @@ -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" @@ -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) diff --git a/tox.ini b/tox.ini index aa190c0..5e5bb8b 100644 --- a/tox.ini +++ b/tox.ini @@ -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 = @@ -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 @@ -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