Overview
The Metrics Scraper is a specialized Go module that continuously scrapes metrics from the Kubernetes Metrics Server and stores them in a local SQLite database. It provides the API module with historical metrics data for visualizing resource usage over time.The Metrics Scraper is optional but recommended for viewing CPU and memory usage trends in the Dashboard.
Module Architecture
Design Philosophy
The Metrics Scraper operates independently from other Dashboard modules:- Autonomous: Runs its own scraping loop
- Lightweight: Stores only a small time window of metrics
- Stateful: Maintains state in SQLite database
- Simple API: Exposes HTTP endpoints for metrics queries
Entry Point
The module starts inmodules/metrics-scraper/main.go:
modules/metrics-scraper/main.go:42-98
Package Structure
Scraping Architecture
Metrics Collection Flow
Update Function
Core scraping logic:modules/metrics-scraper/main.go:103-147
Database Schema
SQLite Tables
The Metrics Scraper uses two main tables:Node Metrics Table
Pod Metrics Table
Database Operations
modules/metrics-scraper/pkg/database/database.go
HTTP API
Endpoints
The Metrics Scraper exposes a simple REST API:GET /api/v1/node
Returns metrics for all nodes:GET /api/v1/node/
Returns metrics for a specific node.GET /api/v1/pod/
Returns metrics for all pods in a namespace:GET /api/v1/pod//
Returns metrics for a specific pod.GET /api/v1/pod///
Returns metrics for a specific container. Reference:modules/metrics-scraper/pkg/api/dashboard/dashboard.go
API Router Setup
modules/metrics-scraper/pkg/api/api.go
Configuration
Command-Line Arguments
Reference:
modules/metrics-scraper/pkg/args/args.go
Examples
Integration with API Module
The API module queries the Metrics Scraper via HTTP:Request Flow
Reference:modules/api/main.go:122-135
Performance Considerations
Storage Efficiency
Limited Time Window
Limited Time Window
Only stores recent metrics (default 15 minutes):Typical database size: 1-10 MB depending on cluster size
Indexed Queries
Indexed Queries
Database indexes optimize common queries:
In-Memory Database Option
In-Memory Database Option
For ephemeral storage:
Scraping Efficiency
- Namespace Filtering: Only scrape required namespaces
- Batched Inserts: All metrics inserted in single transaction
- Error Resilience: Failed scrapes don’t stop the loop
Deployment
Helm Chart Configuration
charts/kubernetes-dashboard/templates/deployments/metrics-scraper.yaml
Resource Requirements
Typical resource usage:Monitoring and Observability
Logging
Structured logging with klog:Health Checks
The API module performs health checks:Troubleshooting
Common Issues
No Metrics Displayed
No Metrics Displayed
Symptoms: Dashboard shows “No metrics available”Causes:
- Metrics Server not installed in cluster
- Metrics Scraper not running
- API module can’t reach Metrics Scraper
Database Errors
Database Errors
Symptoms: Logs show database errorsCauses:
- Read-only filesystem
- Insufficient disk space
- Corrupted database
High Memory Usage
High Memory Usage
Symptoms: Pod using too much memoryCauses:
- Too many namespaces
- Long retention period
- High scrape frequency
Testing
Unit Tests
modules/metrics-scraper/pkg/database/database_test.go
Manual Testing
Alternative: Disabling Metrics
Metrics are optional. To disable:Related Resources
API Module Integration
How API module consumes metrics
Kubernetes Metrics Server
Install Metrics Server in your cluster
SQLite Documentation
SQLite database documentation
Deployment Configuration
Helm chart values for metrics scraper