|
| 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. We generally prefer naming them |
| 6 | +`<noun>_<verb>_<type_suffix>`. So a histogram that's tracking |
| 7 | +the duration (in seconds) of servers spawning would be called |
| 8 | +SERVER_SPAWN_DURATION_SECONDS. |
| 9 | +""" |
| 10 | + |
| 11 | +from prometheus_client import Histogram |
| 12 | + |
| 13 | +REQUEST_DURATION_SECONDS = Histogram( |
| 14 | + 'request_duration_seconds', |
| 15 | + 'request duration for all HTTP requests', |
| 16 | + ['method', 'handler', 'code'], |
| 17 | +) |
| 18 | + |
| 19 | +def prometheus_log_method(handler): |
| 20 | + """ |
| 21 | + Tornado log handler for recording RED metrics. |
| 22 | +
|
| 23 | + We record the following metrics: |
| 24 | + Rate – the number of requests, per second, your services are serving. |
| 25 | + Errors – the number of failed requests per second. |
| 26 | + Duration – The amount of time each request takes expressed as a time interval. |
| 27 | +
|
| 28 | + We use a fully qualified name of the handler as a label, |
| 29 | + rather than every url path to reduce cardinality. |
| 30 | +
|
| 31 | + This function should be either the value of or called from a function |
| 32 | + that is the 'log_function' tornado setting. This makes it get called |
| 33 | + at the end of every request, allowing us to record the metrics we need. |
| 34 | + """ |
| 35 | + REQUEST_DURATION_SECONDS.labels( |
| 36 | + method=handler.request.method, |
| 37 | + handler='{}.{}'.format(handler.__class__.__module__, type(handler).__name__), |
| 38 | + code=handler.get_status() |
| 39 | + ).observe(handler.request.request_time()) |
0 commit comments