From 03e5dc00e1639137d9ed5ce4caeb2639ca2650d6 Mon Sep 17 00:00:00 2001 From: Maxim Vov Date: Wed, 26 Sep 2018 17:15:40 +0300 Subject: [PATCH] Moved the 'metrics' file to a new package named 'promethues'. Then split the file into two files, one containing the metrics themselves, the other containing any function that have something to do with prometheus. Finally, Added new metrics into the prometheus.metrics file that represent the number of running terminals and added the functionality for that metric to be recorded in the terminal.api_handlers file. --- notebook/log.py | 3 ++- notebook/prometheus/__init__.py | 0 .../log_functions.py} | 15 +------------ notebook/prometheus/metrics.py | 21 +++++++++++++++++++ notebook/terminal/api_handlers.py | 8 ++++++- 5 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 notebook/prometheus/__init__.py rename notebook/{metrics.py => prometheus/log_functions.py} (68%) create mode 100644 notebook/prometheus/metrics.py diff --git a/notebook/log.py b/notebook/log.py index 64b35d811d..3621a70cae 100644 --- a/notebook/log.py +++ b/notebook/log.py @@ -7,7 +7,8 @@ import json from tornado.log import access_log -from .metrics import prometheus_log_method +from .prometheus.log_functions import prometheus_log_method + def log_request(handler): """log a bit more information about each request than tornado's default diff --git a/notebook/prometheus/__init__.py b/notebook/prometheus/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/notebook/metrics.py b/notebook/prometheus/log_functions.py similarity index 68% rename from notebook/metrics.py rename to notebook/prometheus/log_functions.py index 24a08d8c88..338b59d0d1 100644 --- a/notebook/metrics.py +++ b/notebook/prometheus/log_functions.py @@ -1,18 +1,5 @@ -""" -Prometheus metrics exported by Jupyter Notebook Server +from notebook.prometheus.metrics import HTTP_REQUEST_DURATION_SECONDS -Read https://prometheus.io/docs/practices/naming/ for naming -conventions for metrics & labels. -""" - -from prometheus_client import Histogram - -# This is a fairly standard name for HTTP duration latency reporting -HTTP_REQUEST_DURATION_SECONDS = Histogram( - 'http_request_duration_seconds', - 'duration in seconds for all HTTP requests', - ['method', 'handler', 'status_code'], -) def prometheus_log_method(handler): """ diff --git a/notebook/prometheus/metrics.py b/notebook/prometheus/metrics.py new file mode 100644 index 0000000000..c4defa772e --- /dev/null +++ b/notebook/prometheus/metrics.py @@ -0,0 +1,21 @@ +""" +Prometheus metrics exported by Jupyter Notebook Server + +Read https://prometheus.io/docs/practices/naming/ for naming +conventions for metrics & labels. +""" + + +from prometheus_client import Histogram, Gauge + + +HTTP_REQUEST_DURATION_SECONDS = Histogram( + 'http_request_duration_seconds', + 'duration in seconds for all HTTP requests', + ['method', 'handler', 'status_code'], +) + +TERMINAL_CURRENTLY_RUNNING_TOTAL = Gauge( + 'terminal_currently_running_total', + 'counter for how many terminals are running', +) diff --git a/notebook/terminal/api_handlers.py b/notebook/terminal/api_handlers.py index 059cc91657..b6f4df1554 100644 --- a/notebook/terminal/api_handlers.py +++ b/notebook/terminal/api_handlers.py @@ -1,7 +1,8 @@ import json from tornado import web, gen from ..base.handlers import APIHandler -from ..utils import url_path_join +from notebook.prometheus.metrics import TERMINAL_CURRENTLY_RUNNING_TOTAL + class TerminalRootHandler(APIHandler): @web.authenticated @@ -9,12 +10,16 @@ def get(self): tm = self.terminal_manager terms = [{'name': name} for name in tm.terminals] self.finish(json.dumps(terms)) + TERMINAL_CURRENTLY_RUNNING_TOTAL.set( + len(terms) + ) @web.authenticated def post(self): """POST /terminals creates a new terminal and redirects to it""" name, _ = self.terminal_manager.new_named_terminal() self.finish(json.dumps({'name': name})) + TERMINAL_CURRENTLY_RUNNING_TOTAL.inc() class TerminalHandler(APIHandler): @@ -36,5 +41,6 @@ def delete(self, name): yield tm.terminate(name, force=True) self.set_status(204) self.finish() + TERMINAL_CURRENTLY_RUNNING_TOTAL.dec(1) else: raise web.HTTPError(404, "Terminal not found: %r" % name)