Skip to content

Commit 045dc46

Browse files
yuvipandapre-commit-ci[bot]Zsailer
authored
Add a traitlet to disable recording HTTP request metrics (#1472)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Zachary Sailer <[email protected]>
1 parent 432a9cc commit 045dc46

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

Diff for: jupyter_server/log.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ def _scrub_uri(uri: str) -> str:
4141
return uri
4242

4343

44-
def log_request(handler):
44+
def log_request(handler, record_prometheus_metrics=True):
4545
"""log a bit more information about each request than tornado's default
4646
4747
- move static file get success to debug-level (reduces noise)
4848
- get proxied IP instead of proxy IP
4949
- log referer for redirect and failed requests
5050
- log user-agent for failed requests
51+
52+
if record_prometheus_metrics is true, will record a histogram prometheus
53+
metric (http_request_duration_seconds) for each request handler
5154
"""
5255
status = handler.get_status()
5356
request = handler.request
@@ -97,4 +100,5 @@ def log_request(handler):
97100
headers[header] = request.headers[header]
98101
log_method(json.dumps(headers, indent=2))
99102
log_method(msg.format(**ns))
100-
prometheus_log_method(handler)
103+
if record_prometheus_metrics:
104+
prometheus_log_method(handler)

Diff for: jupyter_server/serverapp.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import urllib
2929
import warnings
3030
from base64 import encodebytes
31+
from functools import partial
3132
from pathlib import Path
3233

3334
import jupyter_client
@@ -410,7 +411,9 @@ def init_settings(
410411

411412
settings = {
412413
# basics
413-
"log_function": log_request,
414+
"log_function": partial(
415+
log_request, record_prometheus_metrics=jupyter_app.record_http_request_metrics
416+
),
414417
"base_url": base_url,
415418
"default_url": default_url,
416419
"template_path": template_path,
@@ -1993,6 +1996,18 @@ def _default_terminals_enabled(self) -> bool:
19931996
config=True,
19941997
)
19951998

1999+
record_http_request_metrics = Bool(
2000+
True,
2001+
help="""
2002+
Record http_request_duration_seconds metric in the metrics endpoint.
2003+
2004+
Since a histogram is exposed for each request handler, this can create a
2005+
*lot* of metrics, creating operational challenges for multitenant deployments.
2006+
2007+
Set to False to disable recording the http_request_duration_seconds metric.
2008+
""",
2009+
)
2010+
19962011
static_immutable_cache = List(
19972012
Unicode(),
19982013
help="""

0 commit comments

Comments
 (0)