Skip to content

Commit

Permalink
Moved the 'metrics' file to a new package named 'promethues'.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Hyaxia committed Sep 26, 2018
1 parent d772277 commit 03e5dc0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
3 changes: 2 additions & 1 deletion notebook/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file added notebook/prometheus/__init__.py
Empty file.
15 changes: 1 addition & 14 deletions notebook/metrics.py → notebook/prometheus/log_functions.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand Down
21 changes: 21 additions & 0 deletions notebook/prometheus/metrics.py
Original file line number Diff line number Diff line change
@@ -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',
)
8 changes: 7 additions & 1 deletion notebook/terminal/api_handlers.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
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
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):
Expand All @@ -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)

0 comments on commit 03e5dc0

Please sign in to comment.