# cmdrsharp/mimir-cardinality-analyzer architecture

> Architecture diagram of the GitHub repository cmdrsharp/mimir-cardinality-analyzer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cmdrsharp/mimir-cardinality-analyzer
- Repository: https://github.com/cmdrsharp/mimir-cardinality-analyzer

## Overview

1. **What the service is**
   - This is a Rust daemon called **mimir-cardinality-analyzer**.
   - Its job is to identify **unused Mimir metrics** by comparing what is stored in Mimir with what appears in Grafana dashboards and, optionally, alert rules.
   - It runs continuously, repeats the analysis on an interval, and exposes the results as **Prometheus metrics**.

2. **High-level runtime shape**
   - The process has two main responsibilities:
     - a **periodic analysis loop** that gathers and correlates data
     - an **HTTP metrics endpoint** that exports the latest results
   - The analysis output is not a database or file artifact; it is published as in-memory metrics for Prometheus scraping.

3. **Configuration and startup boundary**
   - Startup is driven by a **YAML config file** plus CLI flags such as output directory, interval, and whether alert correlation is enabled.
   - Config covers three external boundaries:
     - **Grafana** URL and token
     - **Mimir** querier and store-gateway URLs
     - **HTTP server** bind host/port
   - The binary and library split suggest `main.rs` handles process bootstrap while `lib.rs` and module code hold the reusable orchestration and integrations.

4. **Tenant discovery from Mimir**
   - The first step in each cycle is discovering active tenants from **Mimir store-gateway**.
   - This is the tenant boundary for the rest of the analysis: everything downstream is scoped per tenant.
   - The store-gateway is therefore the source of truth for “which tenants exist right now.”

5. **Cardinality lookup from Mimir querier**
   - For each tenant, the analyzer queries Mimir’s **cardinality API** through the **querier**.
   - It fetches the **top 100 metric names by cardinality** per tenant.
   - This forms the “stored metrics” side of the comparison and is the main input for determining which metric names are worth evaluating.

6. **Dashboard usage analysis via Grafana tooling**
   - Dashboard usage is discovered by running **`mimirtool analyze grafana`** against Grafana.
   - This is an important architectural boundary: the tool delegates dashboard parsing to an external CLI rather than reimplementing Grafana dashboard analysis itself.
   - The repository’s `grafana` module family likely wraps this integration and parses the resulting usage data.

7. **Optional alert-rule correlation**
   - Alert analysis is toggleable and uses the **Grafana API** to fetch provisioned alert rules.
   - The analyzer checks alert expressions for metric references and correlates them with tenants by assuming the **tenant ID appears in the datasource name**.
   - This is a weaker coupling than dashboard analysis and is explicitly optional because it depends on a naming convention in Grafana.

8. **Correlation and classification logic**
   - The core business logic lives in the metrics-analysis layer: it cross-references
     - top stored metrics from Mimir
     - dashboard references from mimirtool
     - optional alert references from Grafana rules
   - Each metric is classified as **active** or **inactive** for a tenant.
   - The primary exported signal is `metric_active{metric, tenant}` with `1` or `0`.

9. **Prometheus exporter boundary**
   - Results are exposed through a Prometheus-compatible exporter, not pushed elsewhere.
   - Besides `metric_active`, the service also exports operational metrics for:
     - analysis successes/failures
     - discovered tenants
     - last successful run
     - outbound request latency/failures
     - mimirtool subprocess execution stats
     - HTTP server request stats
     - process/build info
   - This makes the service itself observable and scrape-friendly.

10. **Internal module boundaries**
   - The file layout suggests clear subsystem separation:
     - `config.rs` for configuration loading/validation
     - `mimir/*` for Mimir-specific API interactions
     - `grafana/*` for Grafana API and alert/datasource handling
     - `metrics/*` for analysis data model and instrumentation
     - `external.rs` for outbound dependency tracking
     - `exporter.rs` for Prometheus exposition
     - `http.rs` for serving the HTTP endpoint(s)
   - This split maps well to a diagram with external systems on the edge and the analyzer core in the center.

11. **Operational packaging**
   - The repository is built as a **containerized Rust service** with a `Dockerfile`.
   - It also ships a **Helm chart** for Kubernetes deployment, including configmap/service/deployment templates.
   - That makes the intended deployment model a long-running pod scraped by Prometheus.

12. **Visualization and usage of the output**
   - The included Grafana dashboards consume the analyzer’s exported metrics.
   - They also query Mimir cardinality data directly using the **Infinity datasource** plugin.
   - So Grafana is both an input source for analysis and a visualization target for the analyzer’s output.

13. **CI/release and operational guardrails**
   - GitHub Actions workflows build the binary, package the image, and publish Helm artifacts.
   - This indicates the repo is structured as a deployable operator-style utility rather than a library.

14. **Key diagram takeaway**
   - The central flow is:
     - **Mimir store-gateway / querier + Grafana / mimirtool** feed data into the analyzer
     - the analyzer correlates by tenant and metric name
     - the result is emitted as **Prometheus metrics**
     - Grafana dashboards and Prometheus scrape that output for human consumption.

Main entry points: [`src/lib.rs`](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/lib.rs) (lib), [`src/exporter.rs`](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/exporter.rs) (exporter), [`src/metrics/analysis.rs`](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/metrics/analysis.rs) (analysis), [`src/main.rs`](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/main.rs) (main)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Analyzer core"]
  node_main(("main<br/>binary bootstrap<br/>[main.rs]"))
  node_lib["lib<br/>orchestration<br/>[lib.rs]"]
  node_config["config<br/>settings<br/>[config.rs]"]
  node_metrics["metrics<br/>analysis model<br/>[metrics.rs]"]
  node_analysis["analysis<br/>correlation engine<br/>[analysis.rs]"]
  node_exporter["exporter<br/>[exporter.rs]"]
  node_http["http<br/>metrics server<br/>[http.rs]"]
  node_analysis_metrics["analysis metrics<br/>instrumentation<br/>[analysis.rs]"]
  node_external_metrics["external metrics<br/>dependency tracking<br/>[external.rs]"]
  node_process_metrics["process metrics<br/>runtime telemetry<br/>[process.rs]"]
end

subgraph group_integrations["External integrations"]
  node_mimir_metrics["mimir metrics<br/>cardinality client<br/>[cardinality.rs]"]
  node_mimir_api["mimir<br/>tenant/cardinality boundary<br/>[mimir.rs]"]
  node_grafana_api["grafana<br/>usage/alerts boundary<br/>[grafana.rs]"]
  node_grafana_alert["alerts<br/>rule correlation<br/>[alert.rs]"]
  node_grafana_ds["datasource<br/>dashboard usage<br/>[datasource.rs]"]
end

subgraph group_delivery["Delivery"]
  node_alerts_page["dashboards<br/>visualization<br/>[overview.json]"]
  node_chart["helm chart<br/>deployment packaging<br/>[Chart.yaml]"]
  node_ci["ci workflows<br/>release automation<br/>[build.yaml]"]
end

node_main -->|"loads"| node_config
node_main -->|"starts"| node_lib
node_lib -->|"discovers tenants"| node_mimir_api
node_lib -->|"fetches cardinality"| node_mimir_metrics
node_lib -->|"analyzes dashboards"| node_grafana_ds
node_lib -.->|"optionally correlates"| node_grafana_alert
node_mimir_api -->|"scopes"| node_mimir_metrics
node_grafana_api -->|"serves rules"| node_grafana_alert
node_analysis -->|"compares"| node_mimir_metrics
node_analysis -->|"matches usage"| node_grafana_ds
node_analysis -.->|"matches alerts"| node_grafana_alert
node_analysis -->|"updates state"| node_metrics
node_lib -->|"runs"| node_analysis
node_metrics -->|"publishes"| node_exporter
node_exporter -->|"serves"| node_http
node_exporter -->|"emits"| node_analysis_metrics
node_exporter -->|"emits"| node_external_metrics
node_exporter -->|"emits"| node_process_metrics
node_http -->|"scraped by"| node_alerts_page
node_chart -->|"deploys"| node_main
node_ci -->|"publishes"| node_chart
node_ci -->|"builds"| node_main

click node_main "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/main.rs"
click node_lib "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/lib.rs"
click node_config "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/config.rs"
click node_metrics "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/metrics.rs"
click node_analysis "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/metrics/analysis.rs"
click node_mimir_metrics "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/mimir/cardinality.rs"
click node_mimir_api "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/mimir.rs"
click node_grafana_api "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/grafana.rs"
click node_grafana_alert "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/grafana/alert.rs"
click node_grafana_ds "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/grafana/datasource.rs"
click node_exporter "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/exporter.rs"
click node_http "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/http.rs"
click node_analysis_metrics "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/metrics/analysis.rs"
click node_external_metrics "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/metrics/external.rs"
click node_process_metrics "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/metrics/process.rs"
click node_alerts_page "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/dashboards/overview.json"
click node_chart "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/helm/mimir-cardinality-analyzer/Chart.yaml"
click node_ci "https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/.github/workflows/build.yaml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_lib,node_config,node_metrics,node_analysis,node_exporter,node_http,node_analysis_metrics,node_external_metrics,node_process_metrics toneBlue
class node_mimir_metrics,node_mimir_api,node_grafana_api,node_grafana_alert,node_grafana_ds toneAmber
class node_alerts_page,node_chart,node_ci toneMint
```

## Components

### Analyzer core

Process bootstrap, orchestration, and shared analysis state

- [main](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/main.rs): binary bootstrap · Starts the daemon and wires config, analysis loop, and serving · `src/main.rs`
- [lib](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/lib.rs): orchestration · Reusable service wiring and top-level runtime coordination · `src/lib.rs`
- [config](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/config.rs): settings · Loads and validates YAML config plus CLI flags · `src/config.rs`
- [metrics](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/metrics.rs): analysis model · Owns metric classification state and analysis data model · `src/metrics.rs`
- [analysis](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/metrics/analysis.rs): correlation engine · Correlates tenant metrics with dashboard and alert usage · `src/metrics/analysis.rs`
- [exporter](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/exporter.rs): prometheus exporter · Publishes analysis results and operational metrics · `src/exporter.rs`
- [http](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/http.rs): metrics server · Serves the scrape endpoint over HTTP · `src/http.rs`
- [analysis metrics](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/metrics/analysis.rs): instrumentation · Tracks runs, tenants, request latency, and classifier results · `src/metrics/analysis.rs`
- [external metrics](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/metrics/external.rs): dependency tracking · Records outbound request health and latency by integration · `src/metrics/external.rs`
- [process metrics](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/metrics/process.rs): runtime telemetry · Exports build and process information · `src/metrics/process.rs`

### External integrations

Mimir, Grafana, and subprocess boundaries

- [mimir metrics](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/mimir/cardinality.rs): cardinality client · Fetches top metric names by cardinality per tenant · `src/mimir/cardinality.rs`
- [mimir](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/mimir.rs): tenant/cardinality boundary · Tenant discovery and cardinality access against Mimir · `src/mimir.rs`
- [grafana](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/grafana.rs): usage/alerts boundary · Grafana API access for dashboards and alert rules · `src/grafana.rs`
- [alerts](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/grafana/alert.rs): rule correlation · Extracts metric references from provisioned alert rules · `src/grafana/alert.rs`
- [datasource](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/src/grafana/datasource.rs): dashboard usage · Wraps mimirtool dashboard analysis and datasource handling · `src/grafana/datasource.rs`

### Delivery

Container, Helm, dashboards, and CI packaging

- [dashboards](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/dashboards/overview.json): visualization · Grafana dashboards that consume exported metrics and Mimir data · `dashboards/overview.json`
- [helm chart](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/helm/mimir-cardinality-analyzer/Chart.yaml): deployment packaging · Kubernetes deployment, service, and config templates · `helm/mimir-cardinality-analyzer/Chart.yaml`
- [ci workflows](https://github.com/cmdrsharp/mimir-cardinality-analyzer/blob/main/.github/workflows/build.yaml): release automation · Builds the binary, image, and Helm artifacts · `.github/workflows/build.yaml`

## Connections

- main → config: loads · Bootstraps runtime configuration
- main → lib: starts · Initializes the reusable service orchestration
- lib → mimir: discovers tenants · Repeatedly queries store-gateway for active tenants
- lib → mimir metrics: fetches cardinality · Reads top metric names per tenant
- lib → datasource: analyzes dashboards · Invokes mimirtool-based dashboard usage analysis
- lib → alerts: optionally correlates · Fetches and scans alert rules when enabled
- mimir → mimir metrics: scopes · Tenant boundary for cardinality lookups
- grafana → alerts: serves rules · Provides provisioned alert rules
- analysis → mimir metrics: compares · Uses stored metric names as the left side of classification
- analysis → datasource: matches usage · Checks whether metrics appear in dashboards
- analysis → alerts: matches alerts · Checks whether metrics appear in alert expressions
- analysis → metrics: updates state · Writes active/inactive classification results
- lib → analysis: runs · Coordinates the periodic analysis cycle
- metrics → exporter: publishes · Supplies analyzer results and telemetry to exposition
- exporter → http: serves · Exposes Prometheus scrape endpoint
- exporter → analysis metrics: emits · Exports run success, tenant, and classification metrics
- exporter → external metrics: emits · Exports outbound request latency and failures
- exporter → process metrics: emits · Exports process and build information
- http → dashboards: scraped by · Grafana dashboards visualize exported metrics
- helm chart → main: deploys · Packages the daemon as a Kubernetes workload
- ci workflows → helm chart: publishes · Build pipeline releases Helm artifacts
- ci workflows → main: builds · Workflow compiles and packages the Rust binary
