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