How can Prometheus scrape metrics from JupyterHub?

I was not able to get the jupyterhub token prometheus method to work, as it complained that the resulting token didn’t have the read:metrics scope. This is with JupyterHub 3.1.1, which has RBAC features.

See Prometheus Config for Jupyterhub via Helm for the method that worked for me. I created a JupyterHub service with a predefined API key, and a role with the required scope attached to that service.

First define a random API key to use:

export JUPYTERHUB_METRICS_API_KEY=$(openssl rand -hex 16)

Then in jupyterhub_config.py

    c.JupyterHub.services = [
        {
            "name": "service-prometheus",
            "api_token": os.environ["JUPYTERHUB_METRICS_API_KEY"]
        },
    ]

    # Add a service role to scrape prometheus metrics
    c.JupyterHub.load_roles = [
        {
            "name": "service-metrics-role",
            "description": "access metrics",
            "scopes": [
                "read:metrics",
            ],
            "services": [
                "service-prometheus",
            ],
        }
    ]

You should then be able to access the metrics with

curl -H "Authorization: Bearer $JUPYTERHUB_METRICS_API_KEY" http://<jupyterhub-server>/hub/metrics
1 Like