Skip to content

Commit 44c2481

Browse files
committed
Fix circular import when nbclassic shim is present?
1 parent a114351 commit 44c2481

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

jupyter_server/prometheus/metrics.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,23 @@
44
Read https://prometheus.io/docs/practices/naming/ for naming
55
conventions for metrics & labels.
66
"""
7-
# Do this to see if we end up in an import loop?
8-
from notebook.prometheus.metrics import (
9-
HTTP_REQUEST_DURATION_SECONDS,
10-
KERNEL_CURRENTLY_RUNNING_TOTAL,
11-
TERMINAL_CURRENTLY_RUNNING_TOTAL,
12-
)
7+
from prometheus_client import Info, Gauge, Histogram
138

149
try:
15-
# Jupyter Notebook also defines these metrics. Re-defining them results in a ValueError.
16-
# Try to de-duplicate by using the ones in Notebook if available.
10+
from notebook._version import version_info as notebook_version_info
11+
except ImportError:
12+
notebook_version_info = None
13+
14+
if not notebook_version_info >= (7,):
15+
# Jupyter Notebook v6 also defined these metrics. Re-defining them results in a ValueError,
16+
# so we simply re-export them if we are co-existing with the notebook v6 package.
1717
# See https://github.com/jupyter/jupyter_server/issues/209
1818
from notebook.prometheus.metrics import (
1919
HTTP_REQUEST_DURATION_SECONDS,
2020
KERNEL_CURRENTLY_RUNNING_TOTAL,
2121
TERMINAL_CURRENTLY_RUNNING_TOTAL,
2222
)
23-
24-
except ImportError:
25-
from prometheus_client import Gauge, Histogram
26-
23+
else:
2724
HTTP_REQUEST_DURATION_SECONDS = Histogram(
2825
"http_request_duration_seconds",
2926
"duration in seconds for all HTTP requests",
@@ -41,9 +38,7 @@
4138
["type"],
4239
)
4340

44-
45-
from prometheus_client import Info
46-
41+
# New prometheus metrics that do not exist in notebook v6 go here
4742
SERVER_INFO = Info("jupyter_server", "Jupyter Server Version information")
4843

4944
__all__ = [

0 commit comments

Comments
 (0)