diff --git a/jupyter_server/log.py b/jupyter_server/log.py index aed024bb32..b1211114e9 100644 --- a/jupyter_server/log.py +++ b/jupyter_server/log.py @@ -41,13 +41,16 @@ def _scrub_uri(uri: str) -> str: return uri -def log_request(handler): +def log_request(handler, record_prometheus_metrics=True): """log a bit more information about each request than tornado's default - move static file get success to debug-level (reduces noise) - get proxied IP instead of proxy IP - log referer for redirect and failed requests - log user-agent for failed requests + + if record_prometheus_metrics is true, will record a histogram prometheus + metric (http_request_duration_seconds) for each request handler """ status = handler.get_status() request = handler.request @@ -97,4 +100,5 @@ def log_request(handler): headers[header] = request.headers[header] log_method(json.dumps(headers, indent=2)) log_method(msg.format(**ns)) - prometheus_log_method(handler) + if record_prometheus_metrics: + prometheus_log_method(handler) diff --git a/jupyter_server/serverapp.py b/jupyter_server/serverapp.py index 2766509abc..8dd9d02515 100644 --- a/jupyter_server/serverapp.py +++ b/jupyter_server/serverapp.py @@ -28,6 +28,7 @@ import urllib import warnings from base64 import encodebytes +from functools import partial from pathlib import Path import jupyter_client @@ -410,7 +411,9 @@ def init_settings( settings = { # basics - "log_function": log_request, + "log_function": partial( + log_request, record_prometheus_metrics=jupyter_app.record_http_request_metrics + ), "base_url": base_url, "default_url": default_url, "template_path": template_path, @@ -1993,6 +1996,18 @@ def _default_terminals_enabled(self) -> bool: config=True, ) + record_http_request_metrics = Bool( + True, + help=""" + Record http_request_duration_seconds metric in the metrics endpoint. + + Since a histogram is exposed for each request handler, this can create a + *lot* of metrics, creating operational challenges for multitenant deployments. + + Set to False to disable recording the http_request_duration_seconds metric. + """, + ) + static_immutable_cache = List( Unicode(), help="""