metrics

package
v0.0.0-...-d527ca8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CPUMetrics

type CPUMetrics struct {
	TotalUsage   float64            `json:"total_usage"`    // Overall CPU usage percentage
	PerCoreUsage map[int]float64    `json:"per_core_usage"` // Per-core usage percentage
	LoadAverage  map[string]float64 `json:"load_average"`   // 1, 5, 15 min load averages
}

CPUMetrics contains CPU utilization metrics.

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector is responsible for collecting and storing system metrics.

func NewCollector

func NewCollector(config CollectorConfig) (*Collector, error)

NewCollector creates a new metrics collector with the provided configuration.

func (*Collector) Collect

func (c *Collector) Collect(ctx context.Context) error

Collect gathers metrics based on the enabled configurations in CollectorConfig. It runs collection for each enabled metric type (CPU, Memory, Disk, Network) concurrently and aggregates results into a MetricSnapshot that gets stored for later retrieval. The function includes proper error handling and will return an error if any of the collection operations fail.

The context passed is propagated to all metric collection operations and can be used for cancellation or tracing. Spans are created for observability.

func (*Collector) GetMetrics

func (c *Collector) GetMetrics() []MetricSnapshot

GetMetrics returns a deep copy of all collected metrics snapshots. This method is thread-safe and can be called concurrently with Collect. The returned slice contains all metric snapshots in chronological order, with the earliest snapshot at index 0.

If no metrics have been collected yet, an empty slice is returned.

func (*Collector) SaveMetrics

func (c *Collector) SaveMetrics(outputPath string) error

SaveMetrics saves all collected metrics to a JSON file.

func (*Collector) StartCollection

func (c *Collector) StartCollection(ctx context.Context) (trace.Span, context.Context)

StartCollection begins a new metrics collection span.

type CollectorConfig

type CollectorConfig struct {
	// CollectCPU determines if CPU metrics should be collected
	CollectCPU bool `json:"collect_cpu" validate:"required_without_all=CollectMemory CollectDisk CollectNetwork"`
	// CollectMemory determines if memory metrics should be collected
	CollectMemory bool `json:"collect_memory" validate:"required_without_all=CollectCPU CollectDisk CollectNetwork"`
	// CollectDisk determines if disk metrics should be collected
	CollectDisk bool `json:"collect_disk" validate:"required_without_all=CollectCPU CollectMemory CollectNetwork"`
	// CollectNetwork determines if network metrics should be collected
	CollectNetwork bool `json:"collect_network" validate:"required_without_all=CollectCPU CollectMemory CollectDisk"`
}

CollectorConfig holds the configuration for the metrics collector.

type DiskIO

type DiskIO struct {
	ReadCount  uint64 `json:"read_count"`  // Number of reads
	WriteCount uint64 `json:"write_count"` // Number of writes
	ReadBytes  uint64 `json:"read_bytes"`  // Bytes read
	WriteBytes uint64 `json:"write_bytes"` // Bytes written
	ReadTime   uint64 `json:"read_time"`   // Time spent reading (ms)
	WriteTime  uint64 `json:"write_time"`  // Time spent writing (ms)
	IoTime     uint64 `json:"io_time"`     // Time spent on I/O (ms)
}

DiskIO contains disk I/O statistics.

type DiskMetrics

type DiskMetrics struct {
	Total       uint64            `json:"total"`        // Total disk space
	Used        uint64            `json:"used"`         // Used disk space
	Free        uint64            `json:"free"`         // Free disk space
	UsedPercent float64           `json:"used_percent"` // Percentage of disk used
	IOCounters  map[string]DiskIO `json:"io_counters"`  // IO statistics by device
}

DiskMetrics contains disk utilization metrics.

type MemMetrics

type MemMetrics struct {
	Total       uint64  `json:"total"`        // Total physical memory
	Used        uint64  `json:"used"`         // Used memory
	Free        uint64  `json:"free"`         // Free memory
	UsedPercent float64 `json:"used_percent"` // Percentage of memory used
	SwapTotal   uint64  `json:"swap_total"`   // Total swap space
	SwapUsed    uint64  `json:"swap_used"`    // Used swap space
	SwapFree    uint64  `json:"swap_free"`    // Free swap space
	SwapPercent float64 `json:"swap_percent"` // Percentage of swap used
}

MemMetrics contains memory utilization metrics.

type MetricSnapshot

type MetricSnapshot struct {
	Timestamp time.Time    `json:"timestamp"`
	CPU       *CPUMetrics  `json:"cpu,omitempty"`
	Memory    *MemMetrics  `json:"memory,omitempty"`
	Disk      *DiskMetrics `json:"disk,omitempty"`
	Network   *NetMetrics  `json:"network,omitempty"`
}

MetricSnapshot represents a single point-in-time collection of all metrics.

type NetInterfaceIO

type NetInterfaceIO struct {
	BytesSent   uint64 `json:"bytes_sent"`   // Bytes sent
	BytesRecv   uint64 `json:"bytes_recv"`   // Bytes received
	PacketsSent uint64 `json:"packets_sent"` // Packets sent
	PacketsRecv uint64 `json:"packets_recv"` // Packets received
}

NetInterfaceIO contains network interface I/O statistics.

type NetMetrics

type NetMetrics struct {
	BytesSent      uint64                    `json:"bytes_sent"`   // Total bytes sent
	BytesRecv      uint64                    `json:"bytes_recv"`   // Total bytes received
	PacketsSent    uint64                    `json:"packets_sent"` // Total packets sent
	PacketsRecv    uint64                    `json:"packets_recv"` // Total packets received
	IOCounters     map[string]NetInterfaceIO `json:"io_counters"`  // IO statistics by interface
	PrevIOCounters map[string]NetInterfaceIO `json:"-"`            // Previous IO counters for delta calculation
}

NetMetrics contains network utilization metrics.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL