Skip to content

Commit a7a0bd7

Browse files
committed
prometheus: Expose 3 activity metrics
This exposes some of the info from /api/status in the prometheus metrics endpoint, so they can be scraped over time and queried. - Server started timestamp, so we can easily see how long servers have been running - Last activity timestamp, so we can see when users were actually active - User activity duration (in seconds), so we can more easily build reports of how long users are actually active over time. All these would be very helpful in JupyterHub environments. Follow-up to jupyter-server#1470
1 parent c6d51e7 commit a7a0bd7

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

jupyter_server/prometheus/metrics.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
"Jupyter Server Extensiom Version Information",
5959
["name", "version", "enabled"],
6060
)
61+
LAST_ACTIVITY = Gauge("jupyter_server_last_activity_timestamp_seconds", "Timestamp of last seen activity on this Jupyter Server")
62+
SERVER_STARTED = Gauge("jupyter_server_started_timestamp_seconds", "Timestamp of when this Jupyter Server was started")
63+
ACTIVE_DURATION = Gauge("jupyter_server_active_duration_seconds", "Number of seconds this Jupyter Server has been active")
6164

6265
__all__ = [
6366
"HTTP_REQUEST_DURATION_SECONDS",

jupyter_server/serverapp.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
GatewaySessionManager,
111111
)
112112
from jupyter_server.log import log_request
113-
from jupyter_server.prometheus.metrics import SERVER_EXTENSION_INFO, SERVER_INFO
113+
from jupyter_server.prometheus.metrics import SERVER_EXTENSION_INFO, SERVER_INFO, SERVER_STARTED, LAST_ACTIVITY, ACTIVE_DURATION
114114
from jupyter_server.services.config import ConfigManager
115115
from jupyter_server.services.contents.filemanager import (
116116
AsyncFileContentsManager,
@@ -2708,6 +2708,12 @@ def init_metrics(self) -> None:
27082708
name=ext.name, version=ext.version, enabled=str(ext.enabled).lower()
27092709
)
27102710

2711+
started = self.web_app.settings["started"]
2712+
SERVER_STARTED.set(started.timestamp())
2713+
2714+
LAST_ACTIVITY.set_function(lambda: self.web_app.last_activity().timestamp())
2715+
ACTIVE_DURATION.set_function(lambda: (self.web_app.last_activity() - self.web_app.settings["started"]).total_seconds())
2716+
27112717
@catch_config_error
27122718
def initialize(
27132719
self,

0 commit comments

Comments
 (0)