log_functions.py 1.0 KB

1234567891011121314151617181920212223242526
  1. """Log functions for prometheus"""
  2. from .metrics import HTTP_REQUEST_DURATION_SECONDS # type:ignore[unused-ignore]
  3. def prometheus_log_method(handler):
  4. """
  5. Tornado log handler for recording RED metrics.
  6. We record the following metrics:
  7. Rate - the number of requests, per second, your services are serving.
  8. Errors - the number of failed requests per second.
  9. Duration - The amount of time each request takes expressed as a time interval.
  10. We use a fully qualified name of the handler as a label,
  11. rather than every url path to reduce cardinality.
  12. This function should be either the value of or called from a function
  13. that is the 'log_function' tornado setting. This makes it get called
  14. at the end of every request, allowing us to record the metrics we need.
  15. """
  16. HTTP_REQUEST_DURATION_SECONDS.labels(
  17. method=handler.request.method,
  18. handler=f"{handler.__class__.__module__}.{type(handler).__name__}",
  19. status_code=handler.get_status(),
  20. ).observe(handler.request.request_time())