# System Logs Proxy Plan

## Current Design

- Business-cluster logs are routed by Clinic backend `/log/query` through the Grafana `o11y-loki-data` datasource.
- The legacy data-proxy business-cluster Loki route has been removed.
- Control-plane logs are selected by labels such as `cluster_env`, `cluster`, `namespace`, and `container`; they do not have a business cluster id.

## Risks

- Reusing the cluster-scoped API would inject the wrong namespace or project labels.
- Calling Grafana APIs directly depends on browser session/JWT behavior instead of the Clinic API gateway contract.
- A system-log proxy must not expose arbitrary upstream URLs from request parameters.

## Target Contract

- Keep `/data-proxy/system-logs/api/v1/...` without `cluster_id`.
- Select the upstream Loki URL only from configured `loki_proxy.system_endpoints`.
- Require `datasource` as the canonical upstream selector; accept `datasource_uid` / `uid` as aliases for Grafana compatibility.
- Add datasource discovery at `/data-proxy/system-logs/api/v1/datasources`; expose names/metadata only, not upstream URLs.
- Mark `Loki-aws-us-west-2` as the recommended default via config/skill because it is the most common control-plane log datasource.
- Strip proxy routing params before forwarding upstream.
- Preserve Loki query semantics and existing query `limit` clamping.
- Do not inject business-cluster labels into system Loki requests.
- Authorization stays at API gateway via `clinic_api_key`; data service does not add JWT or cookie auth.

## Implementation Steps

- Keep system Loki endpoint config.
- Keep service methods for endpoint resolution and no-injection proxying.
- Register only system logs and serverless parent logs routes from the data service Loki handler.
- Keep unit tests for endpoint resolution, forwarding, query cleanup, and limit clamping.
- Document the API and config shape.

## Verification Checklist

- `cd clinic_data_service && go test ./pkg/service ./pkg/handlers`
- Verify a request with `provider=aws&region=us-west-2` forwards to the configured upstream and strips routing params.
- Verify `query` remains unchanged except URL encoding and `limit` enforcement.
- Verify the legacy `/data-proxy/loki/{cluster_id}` route is no longer registered.
