Skip to content

Commit a67f4d7

Browse files
committed
[feat/1315] Support Serving Metrics On an Alternate Port
1 parent 759d420 commit a67f4d7

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

jupyter_server/prometheus/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
"""
66

77
from .metrics import (
8-
ACTIVE_DURATION,
9-
HTTP_REQUEST_DURATION_SECONDS,
108
KERNEL_CURRENTLY_RUNNING_TOTAL,
119
LAST_ACTIVITY,
1210
SERVER_EXTENSION_INFO,
@@ -17,8 +15,7 @@
1715
from .server import PrometheusMetricsServer, start_metrics_server
1816

1917
__all__ = [
20-
"HTTP_REQUEST_DURATION_SECONDS",
21-
"KERNEL_CURRENTLY_RUNNING_TOTAL",
18+
"KERNEL_CURRENTLY_RUNNING_TOTAL",
2219
"TERMINAL_CURRENTLY_RUNNING_TOTAL",
2320
"SERVER_INFO",
2421
"SERVER_EXTENSION_INFO",

jupyter_server/prometheus/server.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@
2626
import prometheus_client
2727
import tornado.httpserver
2828
import tornado.ioloop
29-
import tornado.web
29+
import prometheus_client
3030

3131
from jupyter_server._version import __version__
3232
from jupyter_server.base.handlers import PrometheusMetricsHandler
3333
from jupyter_server.prometheus.metrics import (
3434
ACTIVE_DURATION,
35-
HTTP_REQUEST_DURATION_SECONDS,
3635
KERNEL_CURRENTLY_RUNNING_TOTAL,
3736
LAST_ACTIVITY,
3837
SERVER_EXTENSION_INFO,

jupyter_server/serverapp.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@
119119
SERVER_EXTENSION_INFO,
120120
SERVER_INFO,
121121
SERVER_STARTED,
122-
TERMINAL_CURRENTLY_RUNNING_TOTAL,
123122
)
124123
from jupyter_server.services.config import ConfigManager
125124
from jupyter_server.services.contents.filemanager import (
@@ -2757,7 +2756,7 @@ def init_metrics(self) -> None:
27572756
for ext in self.extension_manager.extensions.values():
27582757
SERVER_EXTENSION_INFO.labels(
27592758
name=ext.name, version=ext.version, enabled=str(ext.enabled).lower()
2760-
).info({})
2759+
)
27612760

27622761
started = self.web_app.settings["started"]
27632762
SERVER_STARTED.set(started.timestamp())
@@ -3126,6 +3125,23 @@ def start_app(self) -> None:
31263125
if self.identity_provider.token and self.identity_provider.token_generated:
31273126
# log full URL with generated token, so there's a copy/pasteable link
31283127
# with auth info.
3128+
3129+
# Determine metrics URL based on whether separate metrics server is running
3130+
if self.metrics_port:
3131+
# Separate metrics server is running
3132+
if self.authenticate_prometheus:
3133+
metrics_url = f"http://localhost:{self.metrics_port}/metrics?token={self.identity_provider.token}"
3134+
else:
3135+
metrics_url = f"http://localhost:{self.metrics_port}/metrics"
3136+
else:
3137+
# Metrics are served on main server
3138+
# Use the connection_url as base and append /metrics
3139+
base_url = self.connection_url.rstrip('/')
3140+
if self.authenticate_prometheus:
3141+
metrics_url = f"{base_url}/metrics?token={self.identity_provider.token}"
3142+
else:
3143+
metrics_url = f"{base_url}/metrics"
3144+
31293145
if self.sock:
31303146
self.log.critical(
31313147
"\n".join(
@@ -3141,7 +3157,7 @@ def start_app(self) -> None:
31413157
_i18n(
31423158
"To access metrics, open this endpoint in a browser:",
31433159
),
3144-
f" http://localhost:{self.metrics_port}/metrics",
3160+
f" {metrics_url}",
31453161
]
31463162
)
31473163
)
@@ -3154,7 +3170,7 @@ def start_app(self) -> None:
31543170
_i18n(
31553171
"To access metrics, open this endpoint in a browser:",
31563172
),
3157-
f" http://localhost:{self.metrics_port}/metrics",
3173+
f" {metrics_url}",
31583174
]
31593175
else:
31603176
message = [
@@ -3170,7 +3186,7 @@ def start_app(self) -> None:
31703186
_i18n(
31713187
"To access metrics, open this endpoint in a browser:",
31723188
),
3173-
f" http://localhost:{self.metrics_port}/metrics",
3189+
f" {metrics_url}",
31743190
]
31753191

31763192
self.log.critical("\n".join(message))

0 commit comments

Comments
 (0)