Skip to content

Commit d3a6c60

Browse files
prometheus: Expose 3 activity metrics (#1471)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 0d3eeb0 commit d3a6c60

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

jupyter_server/prometheus/metrics.py

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

6273
__all__ = [
6374
"HTTP_REQUEST_DURATION_SECONDS",

jupyter_server/serverapp.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@
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 (
114+
ACTIVE_DURATION,
115+
LAST_ACTIVITY,
116+
SERVER_EXTENSION_INFO,
117+
SERVER_INFO,
118+
SERVER_STARTED,
119+
)
114120
from jupyter_server.services.config import ConfigManager
115121
from jupyter_server.services.contents.filemanager import (
116122
AsyncFileContentsManager,
@@ -2708,6 +2714,16 @@ def init_metrics(self) -> None:
27082714
name=ext.name, version=ext.version, enabled=str(ext.enabled).lower()
27092715
)
27102716

2717+
started = self.web_app.settings["started"]
2718+
SERVER_STARTED.set(started.timestamp())
2719+
2720+
LAST_ACTIVITY.set_function(lambda: self.web_app.last_activity().timestamp())
2721+
ACTIVE_DURATION.set_function(
2722+
lambda: (
2723+
self.web_app.last_activity() - self.web_app.settings["started"]
2724+
).total_seconds()
2725+
)
2726+
27112727
@catch_config_error
27122728
def initialize(
27132729
self,

0 commit comments

Comments
 (0)