Merge pull request #2336 from elendiastarman/gevent-statsd-fix

Fixed two bugs related to gevent + gunicorn + statsd.
This commit is contained in:
Benoit Chesneau 2024-08-06 18:08:18 +02:00 committed by GitHub
commit 26c22af2e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 1 deletions

View File

@ -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)

View File

@ -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: