Skip to content

Commit

Permalink
Improve metrics reporting (#672)
Browse files Browse the repository at this point in the history
* Improve metrics reporting

* Switch default status to 500
  • Loading branch information
gavronek authored and jmcs committed Sep 7, 2018
1 parent b6bb9dd commit 806bba1
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions connexion/decorators/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import time

from werkzeug.exceptions import HTTPException

try:
import uwsgi_metrics
HAS_UWSGI_METRICS = True # pragma: no cover
Expand Down Expand Up @@ -30,16 +32,19 @@ def __call__(self, function):

@functools.wraps(function)
def wrapper(request):
status_code = 500
status = 500
start_time_s = time.time()
try:
response = function(request)
status_code = response.status_code
status = response.status_code
except HTTPException as http_e:
status = http_e.code
raise http_e
finally:
end_time_s = time.time()
delta_s = end_time_s - start_time_s
delta_ms = delta_s * 1000
key = '{status}.{suffix}'.format(status=status_code, suffix=self.key_suffix)
key = '{status}.{suffix}'.format(status=status, suffix=self.key_suffix)
uwsgi_metrics.timer(self.prefix, key, delta_ms)
return response

Expand Down

0 comments on commit 806bba1

Please sign in to comment.