Skip to content

Commit

Permalink
Add prometheus metric with version information (#1467)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
yuvipanda and pre-commit-ci[bot] authored Nov 4, 2024
1 parent 74655ce commit 32679b9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
31 changes: 25 additions & 6 deletions jupyter_server/prometheus/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,35 @@
conventions for metrics & labels.
"""

from prometheus_client import Gauge, Histogram, Info

from jupyter_server._version import version_info as server_version_info

try:
# Jupyter Notebook also defines these metrics. Re-defining them results in a ValueError.
# Try to de-duplicate by using the ones in Notebook if available.
from notebook._version import version_info as notebook_version_info
except ImportError:
notebook_version_info = None


if (
notebook_version_info is not None # No notebook package found
and notebook_version_info < (7,) # Notebook package found, is version 6
# Notebook package found, but its version is the same as jupyter_server
# version. This means some package (looking at you, nbclassic) has shimmed
# the notebook package to instead be imports from the jupyter_server package.
# In such cases, notebook.prometheus.metrics is actually *this file*, so
# trying to import it will cause a circular import. So we don't.
and notebook_version_info != server_version_info
):
# Jupyter Notebook v6 also defined these metrics. Re-defining them results in a ValueError,
# so we simply re-export them if we are co-existing with the notebook v6 package.
# See https://github.com/jupyter/jupyter_server/issues/209
from notebook.prometheus.metrics import (
HTTP_REQUEST_DURATION_SECONDS,
KERNEL_CURRENTLY_RUNNING_TOTAL,
TERMINAL_CURRENTLY_RUNNING_TOTAL,
)

except ImportError:
from prometheus_client import Gauge, Histogram

else:
HTTP_REQUEST_DURATION_SECONDS = Histogram(
"http_request_duration_seconds",
"duration in seconds for all HTTP requests",
Expand All @@ -35,9 +51,12 @@
["type"],
)

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

__all__ = [
"HTTP_REQUEST_DURATION_SECONDS",
"TERMINAL_CURRENTLY_RUNNING_TOTAL",
"KERNEL_CURRENTLY_RUNNING_TOTAL",
"SERVER_INFO",
]
8 changes: 8 additions & 0 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
GatewaySessionManager,
)
from jupyter_server.log import log_request
from jupyter_server.prometheus.metrics import SERVER_INFO
from jupyter_server.services.config import ConfigManager
from jupyter_server.services.contents.filemanager import (
AsyncFileContentsManager,
Expand Down Expand Up @@ -2696,6 +2697,12 @@ def _init_asyncio_patch() -> None:
# prefer Selector to Proactor for tornado + pyzmq
asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())

def init_metrics(self) -> None:
"""
Initialize any prometheus metrics that need to be set up on server startup
"""
SERVER_INFO.info({"version": __version__})

@catch_config_error
def initialize(
self,
Expand Down Expand Up @@ -2763,6 +2770,7 @@ def initialize(
self.load_server_extensions()
self.init_mime_overrides()
self.init_shutdown_no_activity()
self.init_metrics()
if new_httpserver:
self.init_httpserver()

Expand Down

0 comments on commit 32679b9

Please sign in to comment.