Documentation
¶
Overview ¶
Package monitor provides a lightweight, high-performance metrics collection SDK for Go applications with Prometheus Remote Write support.
Design goals:
- Minimal overhead and allocations for hot paths
- Thread-safe primitives built with atomic operations
- Bounded memory with TTL and max-series limits for labeled counters
- Prometheus-compatible format with standard labels
Basic usage:
config := monitor.Config{
Namespace: "myapp",
Subsystem: "prod",
ServiceName: "service",
RemoteWriteURL: "http://prometheus:9090/api/v1/write",
RemoteWriteInterval: 15 * time.Second,
}
if err := monitor.Init(config); err != nil {
log.Fatal(err)
}
defer monitor.Shutdown()
monitor.IncrementCounter("requests_total")
monitor.SetLabeledCounter("connections", 42, "type", "websocket")
monitor.ObserveHistogram("response_time", 0.123)
Index ¶
- func AddCounter(name string, delta int64)
- func DecrementCounter(name string)
- func DecrementLabeledCounter(name string, labels ...string)
- func DeleteLabeledCounter(name string, labels ...string)
- func ForceWrite() error
- func GetCounter(name string) int64
- func GetLabeledCounter(name string, labels ...string) int64
- func GetOutboundIPv4() (string, error)
- func GetProcessMemory() (alloc, sys, heapInUse uint64)
- func GetStatus() map[string]interface{}
- func HealthCheck() error
- func IncrementCounter(name string)
- func IncrementLabeledCounter(name string, labels ...string)
- func Init(config Config) error
- func ObserveHistogram(name string, value float64)
- func RefreshConnection() error
- func RegisterCollector(collector Collector) error
- func RegisterHistogramBuckets(name string, buckets []float64)
- func RegisterSystemMetricsCollector(logger *zap.Logger) error
- func SetCounter(name string, value int64)
- func SetLabeledCounter(name string, value float64, labels ...string)
- func Shutdown()
- func TriggerGC()
- type BaseCollector
- type Collector
- type Config
- type CounterCollector
- type GCStats
- type HistogramCollector
- type LabeledCounterCollector
- func (c *LabeledCounterCollector) Collect() []Metric
- func (c *LabeledCounterCollector) Dec(metricName string, labels ...string)
- func (c *LabeledCounterCollector) Delete(metricName string, labels ...string)
- func (c *LabeledCounterCollector) ForceCleanup()
- func (c *LabeledCounterCollector) Get(metricName string, labels ...string) int64
- func (c *LabeledCounterCollector) Inc(metricName string, labels ...string)
- func (c *LabeledCounterCollector) Set(metricName string, value float64, labels ...string)
- func (c *LabeledCounterCollector) SetMaxSeries(n int)
- func (c *LabeledCounterCollector) SetTTL(ttl time.Duration)
- type Manager
- type Metric
- type MetricType
- type SystemMetricsCollector
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddCounter ¶
AddCounter adds a specific value to a counter
func DecrementCounter ¶
func DecrementCounter(name string)
DecrementCounter decrements a counter by 1
func DecrementLabeledCounter ¶
DecrementLabeledCounter decrements a labeled counter
func DeleteLabeledCounter ¶
DeleteLabeledCounter deletes a specific labeled counter
func ForceWrite ¶
func ForceWrite() error
ForceWrite immediately writes all current metrics to the remote endpoint This is useful for health checks and testing
func GetLabeledCounter ¶
GetLabeledCounter gets the current value of a labeled counter
func GetOutboundIPv4 ¶
GetOutboundIPv4 gets the outbound IPv4 address of the local machine
func GetProcessMemory ¶
func GetProcessMemory() (alloc, sys, heapInUse uint64)
GetProcessMemory returns current process memory usage
func GetStatus ¶
func GetStatus() map[string]interface{}
GetStatus returns the current status of the monitoring system
func HealthCheck ¶
func HealthCheck() error
HealthCheck performs a health check on the monitoring system
func IncrementCounter ¶
func IncrementCounter(name string)
IncrementCounter increments a counter by 1
func IncrementLabeledCounter ¶
IncrementLabeledCounter increments a labeled counter labels should be provided as [key1, value1, key2, value2, ...]
func ObserveHistogram ¶
ObserveHistogram records a value in a histogram
func RefreshConnection ¶
func RefreshConnection() error
RefreshConnection attempts to refresh the remote write connection This is useful for DNS changes or network connectivity issues
func RegisterCollector ¶
RegisterCollector registers a custom metrics collector
func RegisterHistogramBuckets ¶
RegisterHistogramBuckets registers custom histogram buckets
func RegisterSystemMetricsCollector ¶
RegisterSystemMetricsCollector registers the system metrics collector with the global monitor
func SetCounter ¶
SetCounter sets a counter to a specific value
func SetLabeledCounter ¶
SetLabeledCounter sets a labeled counter to a specific value
Types ¶
type BaseCollector ¶
type BaseCollector struct {
// contains filtered or unexported fields
}
BaseCollector provides basic collector functionality
func NewBaseCollector ¶
func NewBaseCollector(name string, logger *zap.Logger) BaseCollector
NewBaseCollector creates a base collector
func (*BaseCollector) Name ¶
func (b *BaseCollector) Name() string
Name implements Collector interface
type Config ¶
type Config struct {
// Service identification
Namespace string
Subsystem string
ServiceName string
// Remote write configuration
RemoteWriteURL string
RemoteWriteInterval time.Duration
// Instance information
InstanceIP string
Version string
BuildCommit string
BuildTime string
CustomLabels map[string]string
// Optional logger
Logger *zap.Logger
// DNS resolver options (optional, for advanced use cases)
DNSEnable bool
DNSCacheTTL time.Duration
DNSRefreshInterval time.Duration
DNSTimeout time.Duration
DNSUDPServers []string // e.g. ["1.1.1.1:53", "8.8.8.8:53"]
DNSTLSServers []string // e.g. ["1.1.1.1:853", "9.9.9.9:853"]
DNSDoHEndpoints []string // e.g. ["https://cloudflare-dns.com/dns-query"]
}
Config defines the configuration for the metrics system
type CounterCollector ¶
type CounterCollector struct {
BaseCollector
// contains filtered or unexported fields
}
CounterCollector provides simple counter metrics
func NewCounterCollector ¶
func NewCounterCollector(name string, logger *zap.Logger) *CounterCollector
NewCounterCollector creates a new counter collector
func (*CounterCollector) Add ¶
func (c *CounterCollector) Add(name string, delta int64)
Add adds a specific value to a counter
func (*CounterCollector) Collect ¶
func (c *CounterCollector) Collect() []Metric
Collect implements Collector interface
func (*CounterCollector) Get ¶
func (c *CounterCollector) Get(name string) int64
Get gets the current value of a counter
func (*CounterCollector) Inc ¶
func (c *CounterCollector) Inc(name string)
Inc increments a counter by 1
func (*CounterCollector) Set ¶
func (c *CounterCollector) Set(name string, value int64)
Set sets a counter to a specific value (makes it a gauge)
type HistogramCollector ¶
type HistogramCollector struct {
BaseCollector
// contains filtered or unexported fields
}
HistogramCollector provides histogram metrics
func NewHistogramCollector ¶
func NewHistogramCollector(name string, logger *zap.Logger) *HistogramCollector
NewHistogramCollector creates a new histogram collector
func (*HistogramCollector) Collect ¶
func (h *HistogramCollector) Collect() []Metric
Collect implements Collector interface
func (*HistogramCollector) Observe ¶
func (h *HistogramCollector) Observe(name string, value float64)
Observe records a value in a histogram
func (*HistogramCollector) RegisterHistogram ¶
func (h *HistogramCollector) RegisterHistogram(name string, buckets []float64)
RegisterHistogram registers a histogram with specified buckets
type LabeledCounterCollector ¶
type LabeledCounterCollector struct {
BaseCollector
// contains filtered or unexported fields
}
LabeledCounterCollector provides labeled counter metrics
func NewApplicationCollector ¶
func NewApplicationCollector(name string, logger *zap.Logger) *LabeledCounterCollector
NewApplicationCollector creates an application-specific collector
func NewLabeledCounterCollector ¶
func NewLabeledCounterCollector(name string, logger *zap.Logger) *LabeledCounterCollector
NewLabeledCounterCollector creates a new labeled counter collector
func (*LabeledCounterCollector) Collect ¶
func (c *LabeledCounterCollector) Collect() []Metric
Collect implements Collector interface
func (*LabeledCounterCollector) Dec ¶
func (c *LabeledCounterCollector) Dec(metricName string, labels ...string)
Dec decrements a labeled counter
func (*LabeledCounterCollector) Delete ¶
func (c *LabeledCounterCollector) Delete(metricName string, labels ...string)
Delete removes a specific labeled counter entry
func (*LabeledCounterCollector) ForceCleanup ¶
func (c *LabeledCounterCollector) ForceCleanup()
ForceCleanup forces immediate cleanup regardless of time intervals
func (*LabeledCounterCollector) Get ¶
func (c *LabeledCounterCollector) Get(metricName string, labels ...string) int64
Get gets the current value of a labeled counter
func (*LabeledCounterCollector) Inc ¶
func (c *LabeledCounterCollector) Inc(metricName string, labels ...string)
Inc increments a labeled counter
func (*LabeledCounterCollector) Set ¶
func (c *LabeledCounterCollector) Set(metricName string, value float64, labels ...string)
Set sets a labeled counter to a specific value
func (*LabeledCounterCollector) SetMaxSeries ¶
func (c *LabeledCounterCollector) SetMaxSeries(n int)
SetMaxSeries sets the maximum number of time series (0 means no limit)
func (*LabeledCounterCollector) SetTTL ¶
func (c *LabeledCounterCollector) SetTTL(ttl time.Duration)
SetTTL sets the TTL for time series
type Manager ¶
type Manager interface {
Start() error
Stop()
RegisterCollector(collector Collector)
GetMetrics() []Metric
}
Manager is the main interface for metrics collection and reporting
func NewManager ¶
NewManager creates a new metrics manager
type Metric ¶
type Metric struct {
Name string
Value float64
Labels map[string]string
MetricType MetricType
Timestamp time.Time
}
Metric represents a single metric data point
type MetricType ¶
type MetricType int
MetricType represents the type of a metric
const ( Counter MetricType = iota Gauge Histogram Summary )
type SystemMetricsCollector ¶
type SystemMetricsCollector struct {
BaseCollector
}
SystemMetricsCollector collects basic system metrics
func NewSystemMetricsCollector ¶
func NewSystemMetricsCollector(logger *zap.Logger) *SystemMetricsCollector
NewSystemMetricsCollector creates a new system metrics collector
func (*SystemMetricsCollector) Collect ¶
func (s *SystemMetricsCollector) Collect() []Metric
Collect implements Collector interface