How cmdrsharp/mimir-cardinality-analyzer works
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: lib.rs lib · exporter.rs exporter · analysis.rs analysis · main.rs main
Components 18
Analyzer core
Process bootstrap, orchestration, and shared analysis state
- main · Starts the daemon and wires config, analysis loop, and serving
src/main.rs - lib · Reusable service wiring and top-level runtime coordination
src/lib.rs - config · Loads and validates YAML config plus CLI flags
src/config.rs - metrics · Owns metric classification state and analysis data model
src/metrics.rs - analysis · Correlates tenant metrics with dashboard and alert usage
src/metrics/analysis.rs - exporter · Publishes analysis results and operational metrics
src/exporter.rs - http · Serves the scrape endpoint over HTTP
src/http.rs - analysis metrics · Tracks runs, tenants, request latency, and classifier results
src/metrics/analysis.rs - external metrics · Records outbound request health and latency by integration
src/metrics/external.rs - process metrics · Exports build and process information
src/metrics/process.rs
External integrations
Mimir, Grafana, and subprocess boundaries
- mimir metrics · Fetches top metric names by cardinality per tenant
src/mimir/cardinality.rs - mimir · Tenant discovery and cardinality access against Mimir
src/mimir.rs - grafana · Grafana API access for dashboards and alert rules
src/grafana.rs - alerts · Extracts metric references from provisioned alert rules
src/grafana/alert.rs - datasource · Wraps mimirtool dashboard analysis and datasource handling
src/grafana/datasource.rs
Delivery
Container, Helm, dashboards, and CI packaging
- dashboards · Grafana dashboards that consume exported metrics and Mimir data
dashboards/overview.json - helm chart · Kubernetes deployment, service, and config templates
helm/mimir-cardinality-analyzer/Chart.yaml - ci workflows · Builds the binary, image, and Helm artifacts
.github/workflows/build.yaml
Connections 22
- 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