# Data Proxy Project Metrics Routing Plan

## Current Design

- Investibook `clinic-cli metrics` calls Clinic `/data-proxy/metrics/{cluster_id}/api/v1/query_range`.
- Clinic Data Service fetches `ClusterMetricsURL` and `DataPlaneMetricsURL` from Clinic internal `cluster-dataurls`.
- `MetricsQueryService.GetMetricCategory` currently extracts one metric name with string slicing and checks `metricsQuery.metricsMapping.dataPlaneMetrics`.
- Metrics in that list use `DataPlaneMetricsURL`; all others use `ClusterMetricsURL`.

## Risks

- Project/data-plane PromQL often uses aggregations and joins, for example `sum by(instance)(rate(node_disk_reads_completed_total[5m])) * ... kube_pod_info`.
- The current string extractor can misread aggregate modifiers or function arguments as the metric name, routing data-plane queries to cluster metrics and returning empty data.
- Adding broad fallback queries would hide routing errors and increase upstream load.

## Target Contract

- Route selection is based on actual PromQL vector selectors, not the first string token.
- If any vector selector is configured as data-plane, the whole query uses `DataPlaneMetricsURL`.
- Mixed cluster/data-plane queries are rejected before upstream execution with an `unsupported_query` error that names both data-plane and cluster metrics.
- If PromQL parsing fails, keep the existing conservative string-extraction fallback.
- Read/query execution does not probe both endpoints and does not mutate state.

## Implementation Steps

1. Extract vector selector metric names with the Prometheus PromQL parser.
2. Support exact data-plane metric names and simple glob patterns in `dataPlaneMetrics`.
3. Keep current default config entries and add common project-level infra metrics needed by the issue.
4. Add focused tests for `kube_pod_info`, `group by(...)`, `sum by(...)`, `topk(...)`, node-disk joins, and cross-source rejection.

## Verification Checklist

- Unit tests for `GetMetricCategory` pass.
- Existing metrics query service tests pass.
- Manual prod check: plain `kube_pod_info` uses data-plane and returns series.
- Manual prod check: `group by(node) (kube_pod_info{...})` currently routes incorrectly on prod and returns empty before this fix.
- Local check: data-plane-only joined node-disk + `kube_pod_info` routes to data-plane.
- Local check: mixed data-plane + cluster PromQL returns `unsupported_query` with metric names instead of querying either upstream.
