Overview
The monitoring system collects comprehensive system metrics using thegopsutil library. Metrics are cached for 2 seconds to reduce overhead.
Type Definition
core/monitoring/stats.go:18
Collection Functions
CollectSystemStats
context.Context
required
Context for cancellation and timeouts
time.Time
required
Application start time (for uptime calculation)
*SystemStats- Complete system metricserror- Aggregated errors from individual collectors (non-fatal)
core/monitoring/stats.go:48
Caching: Results are cached for 2 seconds (StatsRefreshInterval)
Example:
CollectSystemStatsWithoutContext
context.Background().
Location: core/monitoring/stats.go:158
CPU Metrics
CPUInfo
core/monitoring/cpu.go:12
CollectCPUInfoWithContext
core/monitoring/cpu.go:21
Example:
Temperature Detection
coretemp(Intel)k10temp(AMD)cpu_thermal,cpu-thermalcpu temperature
core/monitoring/cpu.go:91
Memory Metrics
MemoryInfo
core/monitoring/memory.go:9
CollectMemoryInfoWithContext
core/monitoring/memory.go:21
Example:
Network Metrics
NetworkInterface
core/monitoring/network.go:11
NetworkStats
core/monitoring/network.go:20
CollectNetworkInfoWithContext
- Skips loopback interfaces
- Skips interfaces without addresses
- Extracts first non-local IPv4 address
- Counts all connection types
core/monitoring/network.go:29
Example:
Runtime Metrics
RuntimeStats
core/monitoring/runtime.go:12
CollectRuntimeStats
core/monitoring/runtime.go:36
Example:
Complete Examples
Health Check Endpoint
Metrics Dashboard
Alerting System
Caching
- Stats are cached for 2 seconds
- Thread-safe with RWMutex
- Double-check locking pattern for efficiency
core/monitoring/stats.go:14
Error Handling
CollectSystemStats returns a multi-error if individual collectors fail:
Best Practices
- Use Contexts: Always pass contexts for timeout control
- Cache Results: Don’t collect metrics on every request - use the built-in cache or add your own
- Handle Partial Failures: Some metrics may be unavailable on certain systems
- Monitor Goroutines: High goroutine counts indicate leaks
- Set Thresholds: Define reasonable alert thresholds for your workload
- Format Units: Convert bytes to GB/MB for human readability
- Trend Analysis: Track metrics over time, not just current values
Related
- Monitoring Dashboard - Viewing metrics in the UI
- Health Endpoints - Server health checks
- Performance Guide - Optimizing application performance