How to dynamically set Grafana data source HTTP header based on the chosen value from a variable in dashboard?

I’m working with Cortex multi-tenancy where both write and read paths require X-Scope-OrgID HTTP header in the request.

The write path is working fine. But I can’t figure out how to pass the chosen tenant name in my dashboard to the Cortex data source as X-Scope-OrgID HTTP header.

My current Grafana data sources look like this:

apiVersion: 1
datasources:
  - name: Prometheus
    type: prometheus
    url: "http://prom-kube-prometheus-stack-prometheus.observability:9090"
  - name: Cortex
    type: prometheus
    url: "http://cortex-query-frontend-headless.cortex:8080/prometheus"
    isDefault: true
    jsonData:
      httpHeaderName1: 'X-Scope-OrgID'
    secureJsonData:
      httpHeaderValue1: 'cust-3b74e7dd-808d-default'

As you can see, I just hard coded the X-Scope-OrgID HTTP header value to a static tenant name (cust-3b74e7dd-808d-default). This works fine and the dashboard is able to load and display metrics from that specific hard coded tenant just fine.

And it looks like this:

Notes:

  • The dropdown/tenant names on left side is using Prometheus data source
  • The Foo panel on right side is using Cortex data source; scoped/hardcoded to cust-3b74e7dd-808d-default tenant

Question:

How do I pass the variable dropdown value to the Cortex data source as X-Scope-OrgID HTTP header?

For example, when I choose cust-test-1234 tenant from the dropdown, the request to Cortex should has X-Scope-OrgID HTTP header set to cust-test-1234 as well.

Is this something can be done? Do I need to use any plugins?

References (I’ve to put these links as code because new user is not allowed to put more than 2 links):

- https://cortexmetrics.io/docs/guides/auth/
- https://cortexmetrics.io/docs/guides/limitations/#tenant-id-naming

FWIW, this is my dashboard in JSON:

{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": {
          "type": "grafana",
          "uid": "-- Grafana --"
        },
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "target": {
          "limit": 100,
          "matchAny": false,
          "tags": [],
          "type": "dashboard"
        },
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 0,
  "id": 2,
  "links": [],
  "liveNow": false,
  "panels": [
    {
      "datasource": {
        "type": "prometheus",
        "uid": "P6693426190CB2316"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "palette-classic"
          },
          "custom": {
            "axisCenteredZero": false,
            "axisColorMode": "text",
            "axisLabel": "",
            "axisPlacement": "auto",
            "barAlignment": 0,
            "drawStyle": "line",
            "fillOpacity": 0,
            "gradientMode": "none",
            "hideFrom": {
              "legend": false,
              "tooltip": false,
              "viz": false
            },
            "lineInterpolation": "linear",
            "lineWidth": 1,
            "pointSize": 5,
            "scaleDistribution": {
              "type": "linear"
            },
            "showPoints": "auto",
            "spanNulls": false,
            "stacking": {
              "group": "A",
              "mode": "none"
            },
            "thresholdsStyle": {
              "mode": "off"
            }
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "red",
                "value": 80
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 9,
        "w": 12,
        "x": 7,
        "y": 0
      },
      "id": 2,
      "options": {
        "legend": {
          "calcs": [],
          "displayMode": "list",
          "placement": "bottom",
          "showLegend": true
        },
        "tooltip": {
          "mode": "single",
          "sort": "none"
        }
      },
      "targets": [
        {
          "datasource": {
            "type": "prometheus",
            "uid": "P6693426190CB2316"
          },
          "editorMode": "code",
          "expr": "sum by (id) (dummy_foo)",
          "legendFormat": "__auto",
          "range": true,
          "refId": "A"
        }
      ],
      "title": "Foo",
      "type": "timeseries"
    }
  ],
  "schemaVersion": 37,
  "style": "dark",
  "tags": [],
  "templating": {
    "list": [
      {
        "current": {
          "selected": true,
          "text": "cust-15276cac-808d-default",
          "value": "cust-15276cac-808d-default"
        },
        "datasource": {
          "type": "prometheus",
          "uid": "PBFA97CFB590B2093"
        },
        "definition": "label_values(namespace)",
        "hide": 0,
        "includeAll": false,
        "multi": false,
        "name": "tenant",
        "options": [],
        "query": {
          "query": "label_values(namespace)",
          "refId": "StandardVariableQuery"
        },
        "refresh": 1,
        "regex": "cust-.*",
        "skipUrlSync": false,
        "sort": 0,
        "type": "query"
      }
    ]
  },
  "time": {
    "from": "now-5m",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "",
  "title": "My dashboard",
  "uid": "nqdvuvK4z",
  "version": 1,
  "weekStart": ""
}

Hi @zulh2022,

Welcome to the :grafana: community support forums !!

I found a similar post which seems to be related to your question about passing the header.

Please check it and let us know if that helps.

Thanks

Thanks for your reply.

That’s different issue than mine where the OP was having issue pushing/writing metrics to Cortex with X-Scope-OrgID HTTP header.

Mine is about reading/querying tenant metrics using Grafana. Specifically, I want to have a dropdown of tenant names (derived from Prometheus data source - I have this already) and when I clicked on a tenant name from the dropdown, I want the tenant name to be passed to Cortex as X-Scope-OrgID: <tenant> HTTP header and update my dashboard panels.

From what I can see, Grafana doesn’t support this use case. I know I can hard-code all the tenants as separate data sources, but that’s not ideal because my tenants come and go. Having it in dynamic form is the best for me.

Do you have any idea how to achieve that?

1 Like

@zulh2022 did you manage to sort out your case? We want to do something similar: We have Mimir datasource configured for multiple tenants:
X-Scope-OrgID: tenantA|tenantB|tenantC
and we want to allow teams (tenants) to override this value on their dashboards, so they will query only their specific metrics