diff --git a/gunicorn/instrument/statsd.py b/gunicorn/instrument/statsd.py index 2c54b2e7..ad5bb0a9 100644 --- a/gunicorn/instrument/statsd.py +++ b/gunicorn/instrument/statsd.py @@ -98,6 +98,8 @@ class Statsd(Logger): 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, bytes): + status = status.decode('utf-8') if isinstance(status, str): status = int(status.split(None, 1)[0]) self.histogram("gunicorn.request.duration", duration_in_ms) diff --git a/gunicorn/workers/ggevent.py b/gunicorn/workers/ggevent.py index 2125a32d..cad021c4 100644 --- a/gunicorn/workers/ggevent.py +++ b/gunicorn/workers/ggevent.py @@ -166,7 +166,11 @@ class PyWSGIHandler(pywsgi.WSGIHandler): finish = datetime.fromtimestamp(self.time_finish) response_time = finish - start resp_headers = getattr(self, 'response_headers', {}) - resp = GeventResponse(self.status, resp_headers, self.response_length) + + # Status is expected to be a string but is encoded to bytes in gevent for PY3 + # Except when it isn't because gevent uses hardcoded strings for network errors. + status = self.status.decode() if isinstance(self.status, bytes) else self.status + resp = GeventResponse(status, resp_headers, self.response_length) if hasattr(self, 'headers'): req_headers = self.headers.items() else: