Skip to content

Commit

Permalink
Added metrics for number of kernels running labeled by type
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyaxia authored and Zsailer committed Sep 25, 2019
1 parent 25b9f80 commit 9eafe12
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
6 changes: 6 additions & 0 deletions jupyter_server/prometheus/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
)
16 changes: 16 additions & 0 deletions jupyter_server/services/kernels/kernelmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 7 additions & 2 deletions jupyter_server/terminal/api_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

Expand All @@ -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)

0 comments on commit 9eafe12

Please sign in to comment.