## TL;DR
Datasource errors for Loki are connectivity or auth between Grafana and Loki: wrong URL, auth headers missing or expired, or a proxy in between breaking the connection. Test the datasource from Grafana's server side (not your browser), check what changed since it worked, and verify the exact URL Grafana uses. Browser-side vs server-side access mode is the classic confusion.

## The query
```text
Grafana "Datasource error" with Loki: connection debugging
```

## Use this when
- Loki panels show datasource errors
- A working datasource suddenly breaks
- Setting up the Loki datasource for the first time
- After Grafana or Loki upgrades

## Not for when
- LogQL syntax or query errors (different message)
- Slow queries (performance, different topic)
- Loki ingestion problems

## Steps

### Step 1: Use Save and Test and read the message
Grafana's datasource Save and Test button returns the specific error: connection refused, 401, 404, timeout. Each maps to a different layer. Start here, not in the panel.
Expected output: the specific connection error classified.

### Step 2: Check server vs browser access mode
Grafana can query Loki from its backend (server access) or from your browser (browser access). Browser mode fails on CORS and network reachability from your laptop; server mode fails on Grafana-to-Loki connectivity. Know which mode you configured.
Expected output: the mode identified; the failing network path known.

### Step 3: Verify the URL Grafana uses
Check the datasource URL character by character: scheme, host, port, path. Common misses: http vs https, missing port, a path prefix the Loki deployment requires. Test the URL from the Grafana host directly.
Expected output: the URL proven reachable from the right network location.

### Step 4: Check authentication
Verify auth headers, basic auth credentials, or tokens configured on the datasource. Rotated credentials break working datasources silently; the error looks like a connection problem.
Expected output: valid credentials confirmed; auth errors resolved.

### Step 5: Look for proxies in the path
Reverse proxies between Grafana and Loki (auth proxies, ingress) can break the connection with their own errors. Check proxy logs when direct connections work but the datasource fails.
Expected output: the proxy hop identified and fixed or ruled out.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_JjpVCl9426qbHM3gZK1teQ
