Skip to content

Commit 293bff0

Browse files
committed
Allow toggling auth for prometheus metrics
Equivalent to jupyterhub/jupyterhub#2224. Port of jupyter/notebook#5870 Prometheus metrics can potentially leak information about the user, so they should be kept behind auth by default. However, for many JupyterHub deployments, they would need to be scraped by a centralized Prometheus instance that can not really authenticate separately to each user notebook without a lot of work. Admins can use this setting to allow unauthenticated access to the /metrics endpoint.
1 parent e3de58b commit 293bff0

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

jupyter_server/base/handlers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,10 +842,12 @@ def get(self):
842842

843843
class PrometheusMetricsHandler(JupyterHandler):
844844
"""
845-
Return prometheus metrics for this Jupyter server
845+
Return prometheus metrics for this notebook server
846846
"""
847-
@web.authenticated
848847
def get(self):
848+
if self.settings['authenticate_prometheus'] and not self.logged_in:
849+
raise web.HTTPError(403)
850+
849851
self.set_header('Content-Type', prometheus_client.CONTENT_TYPE_LATEST)
850852
self.write(prometheus_client.generate_latest(prometheus_client.REGISTRY))
851853

jupyter_server/serverapp.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ def init_settings(self, jupyter_app, kernel_manager, contents_manager,
246246
disable_check_xsrf=jupyter_app.disable_check_xsrf,
247247
allow_remote_access=jupyter_app.allow_remote_access,
248248
local_hostnames=jupyter_app.local_hostnames,
249+
authenticate_prometheus=jupyter_app.authenticate_prometheus,
249250

250251
# managers
251252
kernel_manager=kernel_manager,
@@ -1199,6 +1200,14 @@ def _update_server_extensions(self, change):
11991200
is not available.
12001201
"""))
12011202

1203+
authenticate_prometheus = Bool(
1204+
True,
1205+
help=""""
1206+
Require authentication to access prometheus metrics.
1207+
""",
1208+
config=True
1209+
)
1210+
12021211
def parse_command_line(self, argv=None):
12031212

12041213
super(ServerApp, self).parse_command_line(argv)

0 commit comments

Comments
 (0)