# Grafana Loki logQueryUri convergence

## Current Design

Clinic log entrypoints are split across three layers:

- `clinic-ui/packages/clinic-web/src/utils/links.ts` builds Grafana Explore links and still synthesizes physical Loki datasource names for premium and BYOC clusters.
- Grafana provisioning in `app-delivery` must contain every datasource name the UI emits. Missing names make Grafana fall back to the built-in `grafana` datasource.
- `pkg/o11y/urls.ClusterURLsResolver.ResolveLogsTarget` proxies `/log/query` requests but previously derived Loki from the metrics URL, so it did not use O11Y's log routing source of truth.

The current failing staging case is an Azure premium cluster whose UI link emits `Loki-azure-westus3-premium`, while staging provisioning only has `Loki-azure-westus3`.

## Target Contract

For TiDB Cloud cluster logs that are routed through Clinic's virtual Grafana datasource:

- Grafana datasource is `o11y-loki-data`.
- The UI must pass the TiDB Cloud cluster ID with `var-cluster_id`.
- Backend `/log/query` resolves the target by calling O11Y `/api/v1/clusters` and uses `ClusterInfo.logQueryUri`.
- `logQueryUri` is the canonical Loki target. Do not infer Loki from provider, region, datasource name, or metrics URL.

Dedicated physical Loki entrypoints are left unchanged in this slice.

## Non-Goals

- Do not remove old physical `Loki-*` datasources.
- Do not migrate `data-proxy/system-logs`; it is datasource scoped and not TiDB cluster scoped.
- Do not fold slow query, statements, or TopSQL deployTypeV2 cleanup into this change.
- Do not change Grafana7 until the active traffic path is confirmed.

## Implementation Steps

1. Add `o11y-loki-data` to the target Grafana9 provisioning in app-delivery before rollout. Staging has been provisioned; production rollout remains separate.
2. Change premium, nextgen, BYOC, and resource-pool UI log links to use `o11y-loki-data` and include `var-cluster_id`.
3. Change backend child log target resolution to select the O11Y cluster for the deploy type and return `logQueryUri`.
4. Add local resolver tests that prove serverless and Azure premium logs use `logQueryUri`.

## Verification Checklist

- `go test ./pkg/o11y/urls ./pkg/proxy ./pkg/api`
- `pnpm exec eslint packages/clinic-web/src/utils/links.ts`
- app-delivery `kubectl kustomize` for staging/prod-cloud Grafana9 datasource overlays
- Manual staging check: premium Azure log link should send Grafana datasource `o11y-loki-data`, not `grafana` or `Loki-azure-westus3-premium`.
