Skip to content

Commit

Permalink
bugfix: dont record latency metrics if there is no start time for the…
Browse files Browse the repository at this point in the history
… request
  • Loading branch information
edaniszewski committed Oct 27, 2020
1 parent 6d139c8 commit 3246599
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions synse_server/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,24 @@ async def before_request(request: Request) -> None:

@self.app.middleware('response')
async def before_response(request: Request, response: HTTPResponse) -> None:
latency = time.time() - request.ctx.req_start_time
# If there is no request start time captured in the request context, we cannot
# measure request latency, so do not use it.
#
# This case is indicative of a 404 or some bad request coming in and the request
# immediately resolving to an error response via the Sanic framework.
if hasattr(request.ctx, 'req_start_time'):
latency = time.time() - request.ctx.req_start_time
else:
latency = None

# WebSocket handler ignores response logic, so default
# to a 200 response in such case.
code = response.status if response else 200
labels = (request.method, request.uri_template, request.path, code, request.ip)

if request.path != '/metrics':
self.http_req_latency.labels(*labels).observe(latency)
if latency is not None:
self.http_req_latency.labels(*labels).observe(latency)
self.http_req_count.labels(*labels).inc()

# We cannot use Content-Length header since that has not yet been
Expand Down

0 comments on commit 3246599

Please sign in to comment.