diff --git a/jupyter_server/prometheus/metrics.py b/jupyter_server/prometheus/metrics.py index c4defa772e..abc9d0e16b 100644 --- a/jupyter_server/prometheus/metrics.py +++ b/jupyter_server/prometheus/metrics.py @@ -19,3 +19,9 @@ 'terminal_currently_running_total', 'counter for how many terminals are running', ) + +KERNEL_CURRENTLY_RUNNING_TOTAL = Gauge( + 'kernel_currently_running_total', + 'counter for how many kernels are running labeled by type', + ['type'] +) diff --git a/jupyter_server/services/kernels/kernelmanager.py b/jupyter_server/services/kernels/kernelmanager.py index bb5dba34b8..b7f6d9f9a3 100644 --- a/jupyter_server/services/kernels/kernelmanager.py +++ b/jupyter_server/services/kernels/kernelmanager.py @@ -26,6 +26,8 @@ from jupyter_server._tz import utcnow, isoformat from ipython_genutils.py3compat import getcwd +from notebook.prometheus.metrics import KERNEL_CURRENTLY_RUNNING_TOTAL + class MappingKernelManager(MultiKernelManager): """A KernelManager that handles @@ -178,6 +180,13 @@ def start_kernel(self, kernel_id=None, path=None, **kwargs): lambda : self._handle_kernel_died(kernel_id), 'dead', ) + + # Increase the metric of number of kernels running + # for the relevant kernel type by 1 + KERNEL_CURRENTLY_RUNNING_TOTAL.labels( + type=self._kernels[kernel_id].kernel_name + ).inc() + else: self._check_kernel_id(kernel_id) self.log.info("Using existing kernel: %s" % kernel_id) @@ -288,6 +297,13 @@ def shutdown_kernel(self, kernel_id, now=False): self.stop_buffering(kernel_id) self._kernel_connections.pop(kernel_id, None) self.last_kernel_activity = utcnow() + + # Decrease the metric of number of kernels + # running for the relevant kernel type by 1 + KERNEL_CURRENTLY_RUNNING_TOTAL.labels( + type=self._kernels[kernel_id].kernel_name + ).dec() + return super(MappingKernelManager, self).shutdown_kernel(kernel_id, now=now) def restart_kernel(self, kernel_id): diff --git a/jupyter_server/terminal/api_handlers.py b/jupyter_server/terminal/api_handlers.py index 4ffec14825..d64e1acb3f 100644 --- a/jupyter_server/terminal/api_handlers.py +++ b/jupyter_server/terminal/api_handlers.py @@ -12,6 +12,7 @@ def get(self): tm = self.terminal_manager terms = [{'name': name} for name in tm.terminals] self.finish(json.dumps(terms)) + # Update the metric below to the length of the list 'terms' TERMINAL_CURRENTLY_RUNNING_TOTAL.set( len(terms) @@ -22,6 +23,7 @@ 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})) + # Increase the metric by one because a new terminal was created TERMINAL_CURRENTLY_RUNNING_TOTAL.inc() @@ -45,7 +47,10 @@ def delete(self, name): yield tm.terminate(name, force=True) self.set_status(204) self.finish() - # Decrease the metric below by one because a terminal has been shutdown - TERMINAL_CURRENTLY_RUNNING_TOTAL.dec(1) + + # Decrease the metric below by one + # because a terminal has been shutdown + TERMINAL_CURRENTLY_RUNNING_TOTAL.dec() + else: raise web.HTTPError(404, "Terminal not found: %r" % name)