metrics.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """
  2. Prometheus metrics exported by Jupyter Server
  3. Read https://prometheus.io/docs/practices/naming/ for naming
  4. conventions for metrics & labels.
  5. """
  6. from prometheus_client import Gauge, Histogram, Info
  7. from jupyter_server._version import version_info as server_version_info
  8. try:
  9. from notebook._version import version_info as notebook_version_info
  10. except ImportError:
  11. notebook_version_info = None
  12. if (
  13. notebook_version_info is not None # No notebook package found
  14. and notebook_version_info < (7,) # Notebook package found, is version 6
  15. # Notebook package found, but its version is the same as jupyter_server
  16. # version. This means some package (looking at you, nbclassic) has shimmed
  17. # the notebook package to instead be imports from the jupyter_server package.
  18. # In such cases, notebook.prometheus.metrics is actually *this file*, so
  19. # trying to import it will cause a circular import. So we don't.
  20. and notebook_version_info != server_version_info
  21. ):
  22. # Jupyter Notebook v6 also defined these metrics. Re-defining them results in a ValueError,
  23. # so we simply re-export them if we are co-existing with the notebook v6 package.
  24. # See https://github.com/jupyter/jupyter_server/issues/209
  25. from notebook.prometheus.metrics import (
  26. HTTP_REQUEST_DURATION_SECONDS,
  27. KERNEL_CURRENTLY_RUNNING_TOTAL,
  28. TERMINAL_CURRENTLY_RUNNING_TOTAL,
  29. )
  30. else:
  31. HTTP_REQUEST_DURATION_SECONDS = Histogram(
  32. "http_request_duration_seconds",
  33. "duration in seconds for all HTTP requests",
  34. ["method", "handler", "status_code"],
  35. )
  36. TERMINAL_CURRENTLY_RUNNING_TOTAL = Gauge(
  37. "terminal_currently_running_total",
  38. "counter for how many terminals are running",
  39. )
  40. KERNEL_CURRENTLY_RUNNING_TOTAL = Gauge(
  41. "kernel_currently_running_total",
  42. "counter for how many kernels are running labeled by type",
  43. ["type"],
  44. )
  45. # New prometheus metrics that do not exist in notebook v6 go here
  46. SERVER_INFO = Info("jupyter_server", "Jupyter Server Version information")
  47. SERVER_EXTENSION_INFO = Info(
  48. "jupyter_server_extension",
  49. "Jupyter Server Extension Version Information",
  50. ["name", "version", "enabled"],
  51. )
  52. LAST_ACTIVITY = Gauge(
  53. "jupyter_server_last_activity_timestamp_seconds",
  54. "Timestamp of last seen activity on this Jupyter Server",
  55. )
  56. SERVER_STARTED = Gauge(
  57. "jupyter_server_started_timestamp_seconds", "Timestamp of when this Jupyter Server was started"
  58. )
  59. ACTIVE_DURATION = Gauge(
  60. "jupyter_server_active_duration_seconds",
  61. "Number of seconds this Jupyter Server has been active",
  62. )
  63. __all__ = [
  64. "HTTP_REQUEST_DURATION_SECONDS",
  65. "TERMINAL_CURRENTLY_RUNNING_TOTAL",
  66. "KERNEL_CURRENTLY_RUNNING_TOTAL",
  67. "SERVER_INFO",
  68. ]