|
5 | 5 | conventions for metrics & labels. |
6 | 6 | """ |
7 | 7 |
|
| 8 | +from prometheus_client import Gauge, Histogram, Info |
| 9 | + |
| 10 | +from jupyter_server._version import version_info as server_version_info |
| 11 | + |
8 | 12 | 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. |
| 13 | + from notebook._version import version_info as notebook_version_info |
| 14 | +except ImportError: |
| 15 | + notebook_version_info = None |
| 16 | + |
| 17 | + |
| 18 | +if ( |
| 19 | + notebook_version_info is not None # No notebook package found |
| 20 | + and notebook_version_info < (7,) # Notebook package found, is version 6 |
| 21 | + # Notebook package found, but its version is the same as jupyter_server |
| 22 | + # version. This means some package (looking at you, nbclassic) has shimmed |
| 23 | + # the notebook package to instead be imports from the jupyter_server package. |
| 24 | + # In such cases, notebook.prometheus.metrics is actually *this file*, so |
| 25 | + # trying to import it will cause a circular import. So we don't. |
| 26 | + and notebook_version_info != server_version_info |
| 27 | +): |
| 28 | + # Jupyter Notebook v6 also defined these metrics. Re-defining them results in a ValueError, |
| 29 | + # so we simply re-export them if we are co-existing with the notebook v6 package. |
11 | 30 | # See https://github.com/jupyter/jupyter_server/issues/209 |
12 | 31 | from notebook.prometheus.metrics import ( |
13 | 32 | HTTP_REQUEST_DURATION_SECONDS, |
14 | 33 | KERNEL_CURRENTLY_RUNNING_TOTAL, |
15 | 34 | TERMINAL_CURRENTLY_RUNNING_TOTAL, |
16 | 35 | ) |
17 | | - |
18 | | -except ImportError: |
19 | | - from prometheus_client import Gauge, Histogram |
20 | | - |
| 36 | +else: |
21 | 37 | HTTP_REQUEST_DURATION_SECONDS = Histogram( |
22 | 38 | "http_request_duration_seconds", |
23 | 39 | "duration in seconds for all HTTP requests", |
|
35 | 51 | ["type"], |
36 | 52 | ) |
37 | 53 |
|
| 54 | +# New prometheus metrics that do not exist in notebook v6 go here |
| 55 | +SERVER_INFO = Info("jupyter_server", "Jupyter Server Version information") |
38 | 56 |
|
39 | 57 | __all__ = [ |
40 | 58 | "HTTP_REQUEST_DURATION_SECONDS", |
41 | 59 | "TERMINAL_CURRENTLY_RUNNING_TOTAL", |
42 | 60 | "KERNEL_CURRENTLY_RUNNING_TOTAL", |
| 61 | + "SERVER_INFO", |
43 | 62 | ] |
0 commit comments