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

fix access logging in gaiohttp worker #1193

Merged
merged 1 commit into from Feb 3, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions gunicorn/glogging.py
Expand Up @@ -237,7 +237,9 @@ def log(self, lvl, msg, *args, **kwargs):
def atoms(self, resp, req, environ, request_time):
""" Gets atoms for log formating.
"""
status = resp.status.split(None, 1)[0]
status = resp.status
if isinstance(status, str):
status = status.split(None, 1)[0]
atoms = {
'h': environ.get('REMOTE_ADDR', '-'),
'l': '-',
Expand All @@ -250,8 +252,8 @@ def atoms(self, resp, req, environ, request_time):
'U': environ.get('PATH_INFO'),
'q': environ.get('QUERY_STRING'),
'H': environ.get('SERVER_PROTOCOL'),
'b': resp.sent and str(resp.sent) or '-',
'B': resp.sent,
'b': getattr(resp, 'sent', None) and str(resp.sent) or '-',
'B': getattr(resp, 'sent', None),
'f': environ.get('HTTP_REFERER', '-'),
'a': environ.get('HTTP_USER_AGENT', '-'),
'T': request_time.seconds,
Expand All @@ -266,10 +268,17 @@ def atoms(self, resp, req, environ, request_time):
else:
req_headers = req

if hasattr(req_headers, "items"):
req_headers = req_headers.items()

atoms.update(dict([("{%s}i" % k.lower(), v) for k, v in req_headers]))

resp_headers = resp.headers
if hasattr(resp_headers, "items"):
resp_headers = resp_headers.items()

# add response headers
atoms.update(dict([("{%s}o" % k.lower(), v) for k, v in resp.headers]))
atoms.update(dict([("{%s}o" % k.lower(), v) for k, v in resp_headers]))

return atoms

Expand Down
5 changes: 4 additions & 1 deletion gunicorn/instrument/statsd.py
Expand Up @@ -93,9 +93,12 @@ def access(self, resp, req, environ, request_time):
"""
Logger.access(self, resp, req, environ, request_time)
duration_in_ms = request_time.seconds * 1000 + float(request_time.microseconds) / 10 ** 3
status = resp.status
if isinstance(status, str):
status = int(status.split(None, 1)[0])
self.histogram("gunicorn.request.duration", duration_in_ms)
self.increment("gunicorn.requests", 1)
self.increment("gunicorn.request.status.%d" % int(resp.status.split()[0]), 1)
self.increment("gunicorn.request.status.%d" % status, 1)

# statsD methods
# you can use those directly if you want
Expand Down
8 changes: 7 additions & 1 deletion gunicorn/workers/_gaiohttp.py
Expand Up @@ -4,6 +4,7 @@
# See the NOTICE for more information.

import asyncio
import datetime
import functools
import logging
import os
Expand All @@ -15,7 +16,12 @@

import gunicorn.workers.base as base

from aiohttp.wsgi import WSGIServerHttpProtocol
from aiohttp.wsgi import WSGIServerHttpProtocol as OldWSGIServerHttpProtocol


class WSGIServerHttpProtocol(OldWSGIServerHttpProtocol):
def log_access(self, request, environ, response, time):
self.logger.access(response, request, environ, datetime.timedelta(0, 0, time))


class AiohttpWorker(base.Worker):
Expand Down