Skip to main content

Overview

The monitoring system collects comprehensive system metrics using the gopsutil library. Metrics are cached for 2 seconds to reduce overhead.

Type Definition

Location: core/monitoring/stats.go:18

Collection Functions

CollectSystemStats

Gathers all system statistics with context support and caching.
context.Context
required
Context for cancellation and timeouts
time.Time
required
Application start time (for uptime calculation)
Returns:
  • *SystemStats - Complete system metrics
  • error - Aggregated errors from individual collectors (non-fatal)
Location: core/monitoring/stats.go:48 Caching: Results are cached for 2 seconds (StatsRefreshInterval) Example:

CollectSystemStatsWithoutContext

Convenience wrapper that uses context.Background(). Location: core/monitoring/stats.go:158

CPU Metrics

CPUInfo

Location: core/monitoring/cpu.go:12

CollectCPUInfoWithContext

Gathers CPU information including usage and temperature. Location: core/monitoring/cpu.go:21 Example:

Temperature Detection

Identifies CPU temperature sensors by checking for common names:
  • coretemp (Intel)
  • k10temp (AMD)
  • cpu_thermal, cpu-thermal
  • cpu temperature
Location: core/monitoring/cpu.go:91

Memory Metrics

MemoryInfo

Location: core/monitoring/memory.go:9

CollectMemoryInfoWithContext

Gathers memory and swap information. Location: core/monitoring/memory.go:21 Example:

Network Metrics

NetworkInterface

Location: core/monitoring/network.go:11

NetworkStats

Location: core/monitoring/network.go:20

CollectNetworkInfoWithContext

Gathers network interface statistics and connection counts. Features:
  • Skips loopback interfaces
  • Skips interfaces without addresses
  • Extracts first non-local IPv4 address
  • Counts all connection types
Location: core/monitoring/network.go:29 Example:

Runtime Metrics

RuntimeStats

Location: core/monitoring/runtime.go:12

CollectRuntimeStats

Gathers Go runtime statistics (goroutines, memory, GC). Location: core/monitoring/runtime.go:36 Example:

Complete Examples

Health Check Endpoint

Metrics Dashboard

Alerting System

Caching

Behavior:
  • Stats are cached for 2 seconds
  • Thread-safe with RWMutex
  • Double-check locking pattern for efficiency
Location: core/monitoring/stats.go:14

Error Handling

CollectSystemStats returns a multi-error if individual collectors fail:
This allows partial results even if some metrics are unavailable (e.g., temperature on systems without sensors).

Best Practices

  1. Use Contexts: Always pass contexts for timeout control
  2. Cache Results: Don’t collect metrics on every request - use the built-in cache or add your own
  3. Handle Partial Failures: Some metrics may be unavailable on certain systems
  4. Monitor Goroutines: High goroutine counts indicate leaks
  5. Set Thresholds: Define reasonable alert thresholds for your workload
  6. Format Units: Convert bytes to GB/MB for human readability
  7. Trend Analysis: Track metrics over time, not just current values