diff --git a/jupyter_server/prometheus/metrics.py b/jupyter_server/prometheus/metrics.py index c7ec308964..71192b4eaf 100644 --- a/jupyter_server/prometheus/metrics.py +++ b/jupyter_server/prometheus/metrics.py @@ -5,23 +5,29 @@ conventions for metrics & labels. """ +try: + # Jupyter Notebook also defines these metrics. Re-defining them results in a ValueError. + # Try to de-duplicate by using the ones in Notebook if available. + # See https://github.com/jupyter/jupyter_server/issues/209 + from notebook.prometheus.metrics import HTTP_REQUEST_DURATION_SECONDS, TERMINAL_CURRENTLY_RUNNING_TOTAL, KERNEL_CURRENTLY_RUNNING_TOTAL -from prometheus_client import Histogram, Gauge +except ImportError: + from prometheus_client import Histogram, Gauge -HTTP_REQUEST_DURATION_SECONDS = Histogram( - 'http_request_duration_seconds', - 'duration in seconds for all HTTP requests', - ['method', 'handler', 'status_code'], -) + HTTP_REQUEST_DURATION_SECONDS = Histogram( + 'http_request_duration_seconds', + 'duration in seconds for all HTTP requests', + ['method', 'handler', 'status_code'], + ) -TERMINAL_CURRENTLY_RUNNING_TOTAL = Gauge( - 'terminal_currently_running_total', - 'counter for how many terminals are running', -) + TERMINAL_CURRENTLY_RUNNING_TOTAL = Gauge( + 'terminal_currently_running_total', + 'counter for how many terminals are running', + ) -KERNEL_CURRENTLY_RUNNING_TOTAL = Gauge( - 'kernel_currently_running_total', - 'counter for how many kernels are running labeled by type', - ['type'] -) + KERNEL_CURRENTLY_RUNNING_TOTAL = Gauge( + 'kernel_currently_running_total', + 'counter for how many kernels are running labeled by type', + ['type'] + )