You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.4 KiB

4 years ago
  1. """
  2. Prometheus metrics exported by Jupyter Notebook Server
  3. Read https://prometheus.io/docs/practices/naming/ for naming
  4. conventions for metrics & labels.
  5. """
  6. from prometheus_client import Histogram
  7. # This is a fairly standard name for HTTP duration latency reporting
  8. HTTP_REQUEST_DURATION_SECONDS = Histogram(
  9. 'http_request_duration_seconds',
  10. 'duration in seconds for all HTTP requests',
  11. ['method', 'handler', 'status_code'],
  12. )
  13. def prometheus_log_method(handler):
  14. """
  15. Tornado log handler for recording RED metrics.
  16. We record the following metrics:
  17. Rate - the number of requests, per second, your services are serving.
  18. Errors - the number of failed requests per second.
  19. Duration - The amount of time each request takes expressed as a time interval.
  20. We use a fully qualified name of the handler as a label,
  21. rather than every url path to reduce cardinality.
  22. This function should be either the value of or called from a function
  23. that is the 'log_function' tornado setting. This makes it get called
  24. at the end of every request, allowing us to record the metrics we need.
  25. """
  26. HTTP_REQUEST_DURATION_SECONDS.labels(
  27. method=handler.request.method,
  28. handler='{}.{}'.format(handler.__class__.__module__, type(handler).__name__),
  29. status_code=handler.get_status()
  30. ).observe(handler.request.request_time())