metrics

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package metrics provides standardized metrics collection for hop applications

Index

Constants

This section is empty.

Variables

View Source
var DefaultThresholds = Thresholds{
	CPUPercent:              75.0,
	ClientErrorRatePercent:  40.0,
	DiskPercent:             85.0,
	GCPauseMs:               100.0,
	GoroutineCount:          1000,
	MaxGCFrequency:          100.0,
	MemoryGrowthRatePercent: 20.0,
	MemoryPercent:           80.0,
	ServerErrorRatePercent:  1.0,
}

DefaultThresholds provides default threshold values

Functions

This section is empty.

Types

type Collector

type Collector interface {

	// Counter is for cumulative metrics that only increase
	Counter(name string) Counter
	// Gauge is for metrics that can go up and down
	Gauge(name string) Gauge
	// Histogram tracks the distribution of a metric
	Histogram(name string) Histogram

	// RecordMemStats records memory statistics
	RecordMemStats()
	// RecordGoroutineCount records the number of goroutines
	RecordGoroutineCount()

	// RecordHTTPRequest records an HTTP request
	RecordHTTPRequest(method, path string, duration time.Duration, statusCode int)

	// Handler returns an http.Handler for the metrics endpoint
	Handler() http.Handler
}

Collector defines the interface for metrics collection

type Config

type Config struct {
	// Path where metrics are exposed
	MetricsPath string
	// Enable pprof endpoints
	EnablePprof bool
	// How often to collect system metrics
	CollectionInterval time.Duration
}

type Counter

type Counter interface {
	Inc()
	Add(delta float64)
	Value() float64
}

Counter is for cumulative metrics that only increase

type Gauge

type Gauge interface {
	Set(value float64)
	Add(delta float64)
	Sub(delta float64)
	Value() float64
}

Gauge is for metrics that can go up and down

type Histogram

type Histogram interface {
	Observe(value float64)
	Count() uint64
	Sum() float64
}

Histogram tracks the distribution of a metric

type Labels

type Labels map[string]string

Labels represents a set of metric labels/tags

type MemoryStatus

type MemoryStatus struct {
	Level     ThresholdLevel
	Reason    string
	Current   float64
	Threshold float64
	TrendInfo string // Additional information about trends
}

MemoryStatus represents the status of a specific memory metric

type Module

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

Module implements hop.Module for metrics collection

func NewModule

func NewModule(collector Collector, config *Config) *Module

func (*Module) ID

func (m *Module) ID() string

func (*Module) Init

func (m *Module) Init() error

func (*Module) Middleware

func (m *Module) Middleware() route.Middleware

Middleware creates route.Middleware for collecting HTTP metrics

func (*Module) RegisterRoutes

func (m *Module) RegisterRoutes(router *route.Mux)

func (*Module) Start

func (m *Module) Start(ctx context.Context) error

Start begins periodic collection of system metrics

func (*Module) Stop

func (m *Module) Stop(ctx context.Context) error

Stop halts metric collection

type StandardCollector

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

StandardCollector implements Collector using the standard library

func NewStandardCollector

func NewStandardCollector(opts ...StandardCollectorOption) *StandardCollector

NewStandardCollector creates a new StandardCollector

func (*StandardCollector) Counter

func (c *StandardCollector) Counter(name string) Counter

Counter returns a counter metric

func (*StandardCollector) Gauge

func (c *StandardCollector) Gauge(name string) Gauge

Gauge returns a gauge metric

func (*StandardCollector) Handler

func (c *StandardCollector) Handler() http.Handler

Handler returns an http.Handler for the metrics endpoint as an HTML page

func (*StandardCollector) Histogram

func (c *StandardCollector) Histogram(name string) Histogram

Histogram returns a histogram metric

func (*StandardCollector) RecordCPUStats

func (c *StandardCollector) RecordCPUStats()

RecordCPUStats collects CPU usage statistics

func (*StandardCollector) RecordDiskStats

func (c *StandardCollector) RecordDiskStats()

RecordDiskStats collects disk space usage statistics

func (*StandardCollector) RecordGoroutineCount

func (c *StandardCollector) RecordGoroutineCount()

RecordGoroutineCount captures the number of goroutines

func (*StandardCollector) RecordHTTPRequest

func (c *StandardCollector) RecordHTTPRequest(method, path string, duration time.Duration, statusCode int)

RecordHTTPRequest records metrics about an HTTP request

func (*StandardCollector) RecordMemStats

func (c *StandardCollector) RecordMemStats()

RecordMemStats captures memory statistics

type StandardCollectorOption

type StandardCollectorOption func(*StandardCollector)

StandardCollectorOption is a functional option for configuring a StandardCollector

func WithServerName

func WithServerName(name string) StandardCollectorOption

WithServerName sets the server name for the collector

func WithThresholds

func WithThresholds(thresholds Thresholds) StandardCollectorOption

WithThresholds sets the alert thresholds for the collector

type ThresholdLevel

type ThresholdLevel int

ThresholdLevel is an enumeration of threshold levels

const (
	// ThresholdInfo indicates a threshold is informational
	ThresholdInfo ThresholdLevel = iota
	// ThresholdOK indicates a threshold is within acceptable limits
	ThresholdOK
	// ThresholdWarning indicates a threshold is approaching a warning level
	ThresholdWarning
	// ThresholdCritical indicates a threshold has exceeded a critical level
	ThresholdCritical
)

type Thresholds

type Thresholds struct {
	CPUPercent              float64 // CPU usage percentage
	ClientErrorRatePercent  float64 // Higher threshold for 4xx errors
	DiskPercent             float64 // Percentage of disk space used
	GCPauseMs               float64 // Warning when GC pauses exceed this duration
	GoroutineCount          int     // Number of goroutines
	MaxGCFrequency          float64 // Warning when GC runs too frequently (times per minute)
	MemoryGrowthRatePercent float64 // Warning when memory grows too fast (percent per minute)
	MemoryPercent           float64 // Percentage of total memory used
	ServerErrorRatePercent  float64 // Lower threshold for 5xx errors
}

Thresholds configuration

Jump to

Keyboard shortcuts

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