Skip to content

Commit 32679b9

Browse files
Add prometheus metric with version information (#1467)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 74655ce commit 32679b9

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

jupyter_server/prometheus/metrics.py

+25-6
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,35 @@
55
conventions for metrics & labels.
66
"""
77

8+
from prometheus_client import Gauge, Histogram, Info
9+
10+
from jupyter_server._version import version_info as server_version_info
11+
812
try:
9-
# Jupyter Notebook also defines these metrics. Re-defining them results in a ValueError.
10-
# Try to de-duplicate by using the ones in Notebook if available.
13+
from notebook._version import version_info as notebook_version_info
14+
except ImportError:
15+
notebook_version_info = None
16+
17+
18+
if (
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.
1130
# See https://github.com/jupyter/jupyter_server/issues/209
1231
from notebook.prometheus.metrics import (
1332
HTTP_REQUEST_DURATION_SECONDS,
1433
KERNEL_CURRENTLY_RUNNING_TOTAL,
1534
TERMINAL_CURRENTLY_RUNNING_TOTAL,
1635
)
17-
18-
except ImportError:
19-
from prometheus_client import Gauge, Histogram
20-
36+
else:
2137
HTTP_REQUEST_DURATION_SECONDS = Histogram(
2238
"http_request_duration_seconds",
2339
"duration in seconds for all HTTP requests",
@@ -35,9 +51,12 @@
3551
["type"],
3652
)
3753

54+
# New prometheus metrics that do not exist in notebook v6 go here
55+
SERVER_INFO = Info("jupyter_server", "Jupyter Server Version information")
3856

3957
__all__ = [
4058
"HTTP_REQUEST_DURATION_SECONDS",
4159
"TERMINAL_CURRENTLY_RUNNING_TOTAL",
4260
"KERNEL_CURRENTLY_RUNNING_TOTAL",
61+
"SERVER_INFO",
4362
]

jupyter_server/serverapp.py

+8
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
GatewaySessionManager,
111111
)
112112
from jupyter_server.log import log_request
113+
from jupyter_server.prometheus.metrics import SERVER_INFO
113114
from jupyter_server.services.config import ConfigManager
114115
from jupyter_server.services.contents.filemanager import (
115116
AsyncFileContentsManager,
@@ -2696,6 +2697,12 @@ def _init_asyncio_patch() -> None:
26962697
# prefer Selector to Proactor for tornado + pyzmq
26972698
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
26982699

2700+
def init_metrics(self) -> None:
2701+
"""
2702+
Initialize any prometheus metrics that need to be set up on server startup
2703+
"""
2704+
SERVER_INFO.info({"version": __version__})
2705+
26992706
@catch_config_error
27002707
def initialize(
27012708
self,
@@ -2763,6 +2770,7 @@ def initialize(
27632770
self.load_server_extensions()
27642771
self.init_mime_overrides()
27652772
self.init_shutdown_no_activity()
2773+
self.init_metrics()
27662774
if new_httpserver:
27672775
self.init_httpserver()
27682776

0 commit comments

Comments
 (0)