# NGM O11Y Internal Read Routing

## Current Design

- NGM diagnosis APIs proxy TopSQL, TopRU, statements, continuous profiling, and some slow query metadata requests through `pkg/ngm/utils.ProxyO11yByQueryCluster`.
- Slow query `available_fields` has a separate proxy path in `pkg/ngm/slowquery.Service.o11ySlowQueryURL`.
- These paths previously resolved O11Y clusters with `utils.GetO11yCluster`, which returned `ErrNotFound` for starter/essential (`shared`/`serverless`) and used direct per-deploy-type client helpers for other deploy types.
- Metrics routing already has a central resolver in `pkg/o11y/urls`, but metrics URLs and diagnosis internal read URLs are not always the same semantic target.

## Target Contract

- Diagnosis proxy routing uses a single O11Y URLs entrypoint: `ClusterURLsResolver.ResolveInternalReadTarget`.
- The canonical input remains the TiDB Cloud cluster identity carried by request headers: deploy type, provider, region, tenant/org ID, project ID, and cluster ID.
- The diagnosis proxy URL is built from O11Y `internalReadUri`, then replaces the path with the requested diagnosis API path.
- BYOC keeps its existing datasource host override and internal path/header handling.
- Metrics routing behavior remains under `ResolveMetricsTarget`; this change does not move Grafana metrics or chart queries.

## Risks

- Starter/essential diagnosis requests depend on O11Y URLs returning a child cluster for the TiDB Cloud cluster ID, including deleted clusters via `showDeleted=true`.
- Premium/nextgen diagnosis requests must not fall back to multitenant metrics URLs; they must use `internalReadUri`.
- BYOC still needs the O11Y cluster object for headers and path rewriting.

## Implementation Steps

- Add `ResolveInternalReadTarget` to `pkg/o11y/urls`.
- Resolve the O11Y cluster per deploy type and return its `internalReadUri`.
- Switch `ProxyO11yByQueryCluster` and slow query `available_fields` to use the new resolver.
- Remove the old `utils.GetO11yCluster` diagnosis resolver.
- Add resolver tests for starter/essential and premium internal read targets.

## Verification Checklist

- `go test ./pkg/o11y/urls`
- `go test ./pkg/ngm/utils ./pkg/ngm/topru ./pkg/ngm/topsql ./pkg/ngm/statements ./pkg/ngm/slowquery`
- `git diff --check`
- After deployment, verify staging TopRU / slow query / SQL statements requests for starter, essential, dedicated, premium, and BYOC clusters.
