Skip to content

Commit a2e7a66

Browse files
authored
Prevent re-definition of prometheus metrics (#210)
* Prevent re-definition of prometheus metrics
1 parent 087ac5f commit a2e7a66

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

jupyter_server/prometheus/metrics.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,29 @@
55
conventions for metrics & labels.
66
"""
77

8+
try:
9+
# Jupyter Notebook also defines these metrics. Re-defining them results in a ValueError.
10+
# Try to de-duplicate by using the ones in Notebook if available.
11+
# See https://github.com/jupyter/jupyter_server/issues/209
12+
from notebook.prometheus.metrics import HTTP_REQUEST_DURATION_SECONDS, TERMINAL_CURRENTLY_RUNNING_TOTAL, KERNEL_CURRENTLY_RUNNING_TOTAL
813

9-
from prometheus_client import Histogram, Gauge
14+
except ImportError:
1015

16+
from prometheus_client import Histogram, Gauge
1117

12-
HTTP_REQUEST_DURATION_SECONDS = Histogram(
13-
'http_request_duration_seconds',
14-
'duration in seconds for all HTTP requests',
15-
['method', 'handler', 'status_code'],
16-
)
18+
HTTP_REQUEST_DURATION_SECONDS = Histogram(
19+
'http_request_duration_seconds',
20+
'duration in seconds for all HTTP requests',
21+
['method', 'handler', 'status_code'],
22+
)
1723

18-
TERMINAL_CURRENTLY_RUNNING_TOTAL = Gauge(
19-
'terminal_currently_running_total',
20-
'counter for how many terminals are running',
21-
)
24+
TERMINAL_CURRENTLY_RUNNING_TOTAL = Gauge(
25+
'terminal_currently_running_total',
26+
'counter for how many terminals are running',
27+
)
2228

23-
KERNEL_CURRENTLY_RUNNING_TOTAL = Gauge(
24-
'kernel_currently_running_total',
25-
'counter for how many kernels are running labeled by type',
26-
['type']
27-
)
29+
KERNEL_CURRENTLY_RUNNING_TOTAL = Gauge(
30+
'kernel_currently_running_total',
31+
'counter for how many kernels are running labeled by type',
32+
['type']
33+
)

0 commit comments

Comments
 (0)