Skip to content

Commit

Permalink
Merge pull request #1193 from urbaniak/gaiohttp-logging
Browse files Browse the repository at this point in the history
fix access logging in gaiohttp worker
  • Loading branch information
tilgovi committed Feb 3, 2016
2 parents 012d958 + 353508c commit d6a47e8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
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

0 comments on commit d6a47e8

Please sign in to comment.