1"""
2Prometheus metrics exported by Jupyter Server
3
4Read https://prometheus.io/docs/practices/naming/ for naming
5conventions for metrics & labels.
6"""
7
8from prometheus_client import Gauge, Histogram, Info
9
10from jupyter_server._version import version_info as server_version_info
11
12try:
13 from notebook._version import version_info as notebook_version_info
14except ImportError:
15 notebook_version_info = None
16
17
18if (
19 notebook_version_info is not None # No notebook package found
20 and notebook_version_info < (7,) # Notebook package found, is version 6
21 # Notebook package found, but its version is the same as jupyter_server
22 # version. This means some package (looking at you, nbclassic) has shimmed
23 # the notebook package to instead be imports from the jupyter_server package.
24 # In such cases, notebook.prometheus.metrics is actually *this file*, so
25 # trying to import it will cause a circular import. So we don't.
26 and notebook_version_info != server_version_info
27):
28 # Jupyter Notebook v6 also defined these metrics. Re-defining them results in a ValueError,
29 # so we simply re-export them if we are co-existing with the notebook v6 package.
30 # See https://github.com/jupyter/jupyter_server/issues/209
31 from notebook.prometheus.metrics import (
32 HTTP_REQUEST_DURATION_SECONDS,
33 KERNEL_CURRENTLY_RUNNING_TOTAL,
34 TERMINAL_CURRENTLY_RUNNING_TOTAL,
35 )
36else:
37 HTTP_REQUEST_DURATION_SECONDS = Histogram(
38 "http_request_duration_seconds",
39 "duration in seconds for all HTTP requests",
40 ["method", "handler", "status_code"],
41 )
42
43 TERMINAL_CURRENTLY_RUNNING_TOTAL = Gauge(
44 "terminal_currently_running_total",
45 "counter for how many terminals are running",
46 )
47
48 KERNEL_CURRENTLY_RUNNING_TOTAL = Gauge(
49 "kernel_currently_running_total",
50 "counter for how many kernels are running labeled by type",
51 ["type"],
52 )
53
54# New prometheus metrics that do not exist in notebook v6 go here
55SERVER_INFO = Info("jupyter_server", "Jupyter Server Version information")
56SERVER_EXTENSION_INFO = Info(
57 "jupyter_server_extension",
58 "Jupyter Server Extensiom Version Information",
59 ["name", "version", "enabled"],
60)
61LAST_ACTIVITY = Gauge(
62 "jupyter_server_last_activity_timestamp_seconds",
63 "Timestamp of last seen activity on this Jupyter Server",
64)
65SERVER_STARTED = Gauge(
66 "jupyter_server_started_timestamp_seconds", "Timestamp of when this Jupyter Server was started"
67)
68ACTIVE_DURATION = Gauge(
69 "jupyter_server_active_duration_seconds",
70 "Number of seconds this Jupyter Server has been active",
71)
72
73__all__ = [
74 "HTTP_REQUEST_DURATION_SECONDS",
75 "TERMINAL_CURRENTLY_RUNNING_TOTAL",
76 "KERNEL_CURRENTLY_RUNNING_TOTAL",
77 "SERVER_INFO",
78]