diff --git a/notebook/log.py b/notebook/log.py index 64b35d811..3621a70ca 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 000000000..e69de29bb 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 24a08d8c8..338b59d0d 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 000000000..c4defa772 --- /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 059cc9165..b6f4df155 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 @@ class TerminalRootHandler(APIHandler): 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 @@ class TerminalHandler(APIHandler): 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)