collectors

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateMemoryEfficiency

func CalculateMemoryEfficiency(m *runtime.MemStats) map[string]float64

CalculateMemoryEfficiency calculates memory efficiency metrics.

func CreateHTTPMetricsMiddleware

func CreateHTTPMetricsMiddleware(collector *HTTPCollector) func(http.Handler) http.Handler

CreateHTTPMetricsMiddleware creates HTTP metrics middleware.

func FormatBytes

func FormatBytes(bytes uint64) string

FormatBytes formats bytes to a human-readable string.

func FormatDuration

func FormatDuration(ns uint64) string

FormatDuration formats a duration in nanoseconds to a human-readable string.

func NewCustomCollector

func NewCustomCollector(logger logger.Logger) shared.CustomCollector

NewCustomCollector creates a new custom collector.

func NewCustomCollectorWithConfig

func NewCustomCollectorWithConfig(config *CustomCollectorConfig, logger logger.Logger) shared.CustomCollector

NewCustomCollectorWithConfig creates a new custom collector with configuration.

func NewFeatureUsageCollector

func NewFeatureUsageCollector() shared.CustomCollector

NewFeatureUsageCollector creates a new feature usage collector.

func NewHTTPCollector

func NewHTTPCollector() metrics.CustomCollector

NewHTTPCollector creates a new HTTP collector.

func NewHTTPCollectorWithConfig

func NewHTTPCollectorWithConfig(config *HTTPCollectorConfig) metrics.CustomCollector

NewHTTPCollectorWithConfig creates a new HTTP collector with configuration.

func NewRuntimeCollector

func NewRuntimeCollector() metrics.CustomCollector

NewRuntimeCollector creates a new runtime collector.

func NewRuntimeCollectorWithConfig

func NewRuntimeCollectorWithConfig(config *RuntimeCollectorConfig) metrics.CustomCollector

NewRuntimeCollectorWithConfig creates a new runtime collector with configuration.

func NewSystemCollector

func NewSystemCollector() metrics.CustomCollector

NewSystemCollector creates a new system collector.

func NewSystemCollectorWithConfig

func NewSystemCollectorWithConfig(config *SystemCollectorConfig) metrics.CustomCollector

NewSystemCollectorWithConfig creates a new system collector with configuration.

func RecordHTTPRequestMetrics

func RecordHTTPRequestMetrics(collector *HTTPCollector, method, path string, statusCode int, duration time.Duration)

RecordHTTPRequestMetrics records HTTP request metrics.

Types

type BusinessMetric

type BusinessMetric struct {
	Name        string            `json:"name"`
	Value       any               `json:"value"`
	Type        string            `json:"type"`
	Unit        string            `json:"unit"`
	Description string            `json:"description"`
	Tags        map[string]string `json:"tags"`
	Timestamp   time.Time         `json:"timestamp"`
	Source      string            `json:"source"`
}

BusinessMetric represents a business-specific metric.

type BusinessMetricsCollector

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

BusinessMetricsCollector is an example custom collector for business metrics.

func (*BusinessMetricsCollector) Collect

func (bmc *BusinessMetricsCollector) Collect() (map[string]any, error)

func (*BusinessMetricsCollector) IsEnabled

func (bmc *BusinessMetricsCollector) IsEnabled() bool

func (*BusinessMetricsCollector) Name

func (bmc *BusinessMetricsCollector) Name() string

func (*BusinessMetricsCollector) Reset

func (bmc *BusinessMetricsCollector) Reset() error

type CPUStats

type CPUStats struct {
	User      uint64
	Nice      uint64
	System    uint64
	Idle      uint64
	IOWait    uint64
	IRQ       uint64
	SoftIRQ   uint64
	Steal     uint64
	Guest     uint64
	GuestNice uint64
	Total     uint64
}

CPUStats represents CPU statistics.

type CounterMetric

type CounterMetric struct {
	Name        string            `json:"name"`
	Value       int64             `json:"value"`
	Description string            `json:"description"`
	Tags        map[string]string `json:"tags"`
	Timestamp   time.Time         `json:"timestamp"`
	Increment   int64             `json:"increment"`
	Rate        float64           `json:"rate"`
	Unit        string            `json:"unit"`
}

CounterMetric represents a counter metric.

type CustomCallback

type CustomCallback func() (map[string]any, error)

CustomCallback defines a custom callback function for metrics collection.

type CustomCollector

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

CustomCollector provides a framework for creating custom application metrics collectors.

func (*CustomCollector) Collect

func (cc *CustomCollector) Collect() map[string]any

Collect collects custom metrics.

func (*CustomCollector) CreateMetricsWrapper

func (cc *CustomCollector) CreateMetricsWrapper(metricsCollector shared.Metrics) *CustomMetricsWrapper

CreateMetricsWrapper creates a wrapper for metrics integration.

func (*CustomCollector) Disable

func (cc *CustomCollector) Disable()

Disable disables the collector.

func (*CustomCollector) Enable

func (cc *CustomCollector) Enable()

Enable enables the collector.

func (*CustomCollector) GetBusinessMetrics

func (cc *CustomCollector) GetBusinessMetrics() map[string]*BusinessMetric

GetBusinessMetrics returns all business metrics.

func (*CustomCollector) GetCounterMetrics

func (cc *CustomCollector) GetCounterMetrics() map[string]*CounterMetric

GetCounterMetrics returns all counter metrics.

func (*CustomCollector) GetGaugeMetrics

func (cc *CustomCollector) GetGaugeMetrics() map[string]*GaugeMetric

GetGaugeMetrics returns all gauge metrics.

func (*CustomCollector) GetHistogramMetrics

func (cc *CustomCollector) GetHistogramMetrics() map[string]*HistogramMetric

GetHistogramMetrics returns all histogram metrics.

func (*CustomCollector) GetInterval

func (cc *CustomCollector) GetInterval() time.Duration

GetInterval returns the collection interval.

func (*CustomCollector) GetStats

func (cc *CustomCollector) GetStats() map[string]any

GetStats returns collector statistics.

func (*CustomCollector) GetTimerMetrics

func (cc *CustomCollector) GetTimerMetrics() map[string]*TimerMetric

GetTimerMetrics returns all timer metrics.

func (*CustomCollector) IncrementCounter

func (cc *CustomCollector) IncrementCounter(name string, increment int64, description, unit string, tags map[string]string)

IncrementCounter increments a counter metric.

func (*CustomCollector) IsEnabled

func (cc *CustomCollector) IsEnabled() bool

IsEnabled returns whether the collector is enabled.

func (*CustomCollector) Name

func (cc *CustomCollector) Name() string

Name returns the collector name.

func (*CustomCollector) RecordBusinessMetric

func (cc *CustomCollector) RecordBusinessMetric(name string, value any, metricType, unit, description string, tags map[string]string)

RecordBusinessMetric records a business-specific metric.

func (*CustomCollector) RecordHistogramValue

func (cc *CustomCollector) RecordHistogramValue(name string, value float64, description, unit string, tags map[string]string)

RecordHistogramValue records a value in a histogram metric.

func (*CustomCollector) RecordTimerValue

func (cc *CustomCollector) RecordTimerValue(name string, duration time.Duration, description string, tags map[string]string)

RecordTimerValue records a duration in a timer metric.

func (*CustomCollector) RegisterCustomCallback

func (cc *CustomCollector) RegisterCustomCallback(callback CustomCallback)

RegisterCustomCallback registers a custom callback function.

func (*CustomCollector) RegisterCustomCollector

func (cc *CustomCollector) RegisterCustomCollector(collector shared.CustomCollector) error

RegisterCustomCollector registers a custom metric collector.

func (*CustomCollector) Reset

func (cc *CustomCollector) Reset() error

Reset resets the collector.

func (*CustomCollector) SetGaugeMetric

func (cc *CustomCollector) SetGaugeMetric(name string, value float64, description, unit string, tags map[string]string)

SetGaugeMetric sets a gauge metric value.

func (*CustomCollector) SetInterval

func (cc *CustomCollector) SetInterval(interval time.Duration)

SetInterval sets the collection interval.

func (*CustomCollector) UnregisterCustomCollector

func (cc *CustomCollector) UnregisterCustomCollector(name string) error

UnregisterCustomCollector unregisters a custom metric collector.

type CustomCollectorConfig

type CustomCollectorConfig struct {
	Interval                 time.Duration `json:"interval"                   yaml:"interval"`
	EnableBusinessMetrics    bool          `json:"enable_business_metrics"    yaml:"enable_business_metrics"`
	EnablePerformanceMetrics bool          `json:"enable_performance_metrics" yaml:"enable_performance_metrics"`
	EnableUserMetrics        bool          `json:"enable_user_metrics"        yaml:"enable_user_metrics"`
	EnableFeatureMetrics     bool          `json:"enable_feature_metrics"     yaml:"enable_feature_metrics"`
	MaxCustomMetrics         int           `json:"max_custom_metrics"         yaml:"max_custom_metrics"`
	CallbackTimeout          time.Duration `json:"callback_timeout"           yaml:"callback_timeout"`
}

CustomCollectorConfig contains configuration for the custom collector.

func DefaultCustomCollectorConfig

func DefaultCustomCollectorConfig() *CustomCollectorConfig

DefaultCustomCollectorConfig returns default configuration.

type CustomMetricsWrapper

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

CustomMetricsWrapper wraps the custom collector with metrics integration.

func (*CustomMetricsWrapper) IncrementCustomCounter

func (cmw *CustomMetricsWrapper) IncrementCustomCounter(name string, increment int64, tags map[string]string)

IncrementCustomCounter increments a custom counter via the wrapper.

func (*CustomMetricsWrapper) RecordBusinessMetric

func (cmw *CustomMetricsWrapper) RecordBusinessMetric(name string, value float64, tags map[string]string)

RecordBusinessMetric records a business metric via the wrapper.

func (*CustomMetricsWrapper) RecordCustomHistogram

func (cmw *CustomMetricsWrapper) RecordCustomHistogram(name string, value float64, tags map[string]string)

RecordCustomHistogram records a custom histogram value via the wrapper.

func (*CustomMetricsWrapper) RecordCustomTimer

func (cmw *CustomMetricsWrapper) RecordCustomTimer(name string, duration time.Duration, tags map[string]string)

RecordCustomTimer records a custom timer value via the wrapper.

func (*CustomMetricsWrapper) SetGaugeValue

func (cmw *CustomMetricsWrapper) SetGaugeValue(name string, value float64, tags map[string]string)

SetGaugeValue sets a gauge value via the wrapper.

type DiskStats

type DiskStats struct {
	MountPoint  string
	Device      string
	Total       uint64
	Used        uint64
	Available   uint64
	UsedPercent float64
}

DiskStats represents disk statistics.

type FeatureUsageCollector

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

FeatureUsageCollector is an example custom collector for feature usage metrics.

func (*FeatureUsageCollector) Collect

func (fuc *FeatureUsageCollector) Collect() map[string]any

func (*FeatureUsageCollector) IsEnabled

func (fuc *FeatureUsageCollector) IsEnabled() bool

func (*FeatureUsageCollector) Name

func (fuc *FeatureUsageCollector) Name() string

func (*FeatureUsageCollector) Reset

func (fuc *FeatureUsageCollector) Reset() error

type GaugeMetric

type GaugeMetric struct {
	Name        string            `json:"name"`
	Value       float64           `json:"value"`
	Description string            `json:"description"`
	Tags        map[string]string `json:"tags"`
	Timestamp   time.Time         `json:"timestamp"`
	MinValue    float64           `json:"min_value"`
	MaxValue    float64           `json:"max_value"`
	Unit        string            `json:"unit"`
}

GaugeMetric represents a gauge metric.

type HTTPCollector

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

HTTPCollector collects HTTP request/response metrics.

func CreateHTTPCollectorWithMetrics

func CreateHTTPCollectorWithMetrics(metricsCollector metrics.Metrics, config *HTTPCollectorConfig) *HTTPCollector

CreateHTTPCollectorWithMetrics creates an HTTP collector with pre-configured metrics.

func (*HTTPCollector) Collect

func (hc *HTTPCollector) Collect() map[string]any

Collect collects HTTP metrics.

func (*HTTPCollector) Disable

func (hc *HTTPCollector) Disable()

Disable disables the collector.

func (*HTTPCollector) Enable

func (hc *HTTPCollector) Enable()

Enable enables the collector.

func (*HTTPCollector) EndRequest

func (hc *HTTPCollector) EndRequest()

EndRequest records the end of an HTTP request.

func (*HTTPCollector) GetActiveRequests

func (hc *HTTPCollector) GetActiveRequests() int64

GetActiveRequests returns the number of active requests.

func (*HTTPCollector) GetRequestsByMethod

func (hc *HTTPCollector) GetRequestsByMethod() map[string]int64

GetRequestsByMethod returns requests grouped by method.

func (*HTTPCollector) GetRequestsByPath

func (hc *HTTPCollector) GetRequestsByPath() map[string]int64

GetRequestsByPath returns requests grouped by path.

func (*HTTPCollector) GetRequestsByStatus

func (hc *HTTPCollector) GetRequestsByStatus() map[int]int64

GetRequestsByStatus returns requests grouped by status code.

func (*HTTPCollector) GetTopPaths

func (hc *HTTPCollector) GetTopPaths(n int) []string

GetTopPaths returns the top N paths by request count.

func (*HTTPCollector) IsEnabled

func (hc *HTTPCollector) IsEnabled() bool

IsEnabled returns whether the collector is enabled.

func (*HTTPCollector) Middleware

func (hc *HTTPCollector) Middleware() func(http.Handler) http.Handler

Middleware returns HTTP middleware that automatically records metrics.

func (*HTTPCollector) Name

func (hc *HTTPCollector) Name() string

Name returns the collector name.

func (*HTTPCollector) RecordRequest

func (hc *HTTPCollector) RecordRequest(reqMetrics HTTPRequestMetrics)

RecordRequest records metrics for an HTTP request.

func (*HTTPCollector) Reset

func (hc *HTTPCollector) Reset() error

Reset resets the collector.

func (*HTTPCollector) StartRequest

func (hc *HTTPCollector) StartRequest()

StartRequest records the start of an HTTP request.

type HTTPCollectorConfig

type HTTPCollectorConfig struct {
	TrackPaths         bool     `json:"track_paths"          yaml:"track_paths"`
	TrackUserAgents    bool     `json:"track_user_agents"    yaml:"track_user_agents"`
	TrackStatusCodes   bool     `json:"track_status_codes"   yaml:"track_status_codes"`
	TrackMethods       bool     `json:"track_methods"        yaml:"track_methods"`
	TrackSizes         bool     `json:"track_sizes"          yaml:"track_sizes"`
	PathWhitelist      []string `json:"path_whitelist"       yaml:"path_whitelist"`
	PathBlacklist      []string `json:"path_blacklist"       yaml:"path_blacklist"`
	MaxPathsTracked    int      `json:"max_paths_tracked"    yaml:"max_paths_tracked"`
	GroupSimilarPaths  bool     `json:"group_similar_paths"  yaml:"group_similar_paths"`
	IncludeQueryParams bool     `json:"include_query_params" yaml:"include_query_params"`
}

HTTPCollectorConfig contains configuration for the HTTP collector.

func DefaultHTTPCollectorConfig

func DefaultHTTPCollectorConfig() *HTTPCollectorConfig

DefaultHTTPCollectorConfig returns default configuration.

type HTTPRequestMetrics

type HTTPRequestMetrics struct {
	Method        string
	Path          string
	StatusCode    int
	UserAgent     string
	ContentLength int64
	ResponseSize  int64
	Duration      time.Duration
	Timestamp     time.Time
}

HTTPRequestMetrics represents metrics for a single HTTP request.

type HistogramMetric

type HistogramMetric struct {
	Name        string            `json:"name"`
	Count       int64             `json:"count"`
	Sum         float64           `json:"sum"`
	Mean        float64           `json:"mean"`
	Min         float64           `json:"min"`
	Max         float64           `json:"max"`
	Buckets     map[float64]int64 `json:"buckets"`
	Description string            `json:"description"`
	Tags        map[string]string `json:"tags"`
	Timestamp   time.Time         `json:"timestamp"`
	Unit        string            `json:"unit"`
}

HistogramMetric represents a histogram metric.

type LoadStats

type LoadStats struct {
	Load1  float64
	Load5  float64
	Load15 float64
}

LoadStats represents system load statistics.

type MemoryStats

type MemoryStats struct {
	Total     uint64
	Available uint64
	Used      uint64
	Free      uint64
	Buffers   uint64
	Cached    uint64
	SwapTotal uint64
	SwapFree  uint64
	SwapUsed  uint64
}

MemoryStats represents memory statistics.

type NetworkStats

type NetworkStats struct {
	Interface   string
	BytesRecv   uint64
	BytesSent   uint64
	PacketsRecv uint64
	PacketsSent uint64
	ErrorsRecv  uint64
	ErrorsSent  uint64
	DropsRecv   uint64
	DropsSent   uint64
}

NetworkStats represents network interface statistics.

type RuntimeCollector

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

RuntimeCollector collects Go runtime metrics.

func (*RuntimeCollector) Collect

func (rc *RuntimeCollector) Collect() map[string]any

Collect collects runtime metrics.

func (*RuntimeCollector) Disable

func (rc *RuntimeCollector) Disable()

Disable disables the collector.

func (*RuntimeCollector) Enable

func (rc *RuntimeCollector) Enable()

Enable enables the collector.

func (*RuntimeCollector) GetGCStats

func (rc *RuntimeCollector) GetGCStats() map[string]any

GetGCStats returns the current GC statistics.

func (*RuntimeCollector) GetGoroutineStats

func (rc *RuntimeCollector) GetGoroutineStats() map[string]any

GetGoroutineStats returns the current goroutine statistics.

func (*RuntimeCollector) GetInterval

func (rc *RuntimeCollector) GetInterval() time.Duration

GetInterval returns the collection interval.

func (*RuntimeCollector) GetLastCollectionTime

func (rc *RuntimeCollector) GetLastCollectionTime() time.Time

GetLastCollectionTime returns the last collection time.

func (*RuntimeCollector) GetMemoryStats

func (rc *RuntimeCollector) GetMemoryStats() map[string]any

GetMemoryStats returns the current memory statistics.

func (*RuntimeCollector) GetMetricsCount

func (rc *RuntimeCollector) GetMetricsCount() int

GetMetricsCount returns the number of metrics collected.

func (*RuntimeCollector) IsEnabled

func (rc *RuntimeCollector) IsEnabled() bool

IsEnabled returns whether the collector is enabled.

func (*RuntimeCollector) Name

func (rc *RuntimeCollector) Name() string

Name returns the collector name.

func (*RuntimeCollector) Reset

func (rc *RuntimeCollector) Reset() error

Reset resets the collector.

func (*RuntimeCollector) SetInterval

func (rc *RuntimeCollector) SetInterval(interval time.Duration)

SetInterval sets the collection interval.

func (*RuntimeCollector) TriggerGC

func (rc *RuntimeCollector) TriggerGC()

TriggerGC triggers a garbage collection and collects immediate stats.

type RuntimeCollectorConfig

type RuntimeCollectorConfig struct {
	Interval          time.Duration `json:"interval"           yaml:"interval"`
	CollectGC         bool          `json:"collect_gc"         yaml:"collect_gc"`
	CollectMemory     bool          `json:"collect_memory"     yaml:"collect_memory"`
	CollectGoroutines bool          `json:"collect_goroutines" yaml:"collect_goroutines"`
	CollectCGO        bool          `json:"collect_cgo"        yaml:"collect_cgo"`
	CollectBuildInfo  bool          `json:"collect_build_info" yaml:"collect_build_info"`
	EnableGCStats     bool          `json:"enable_gc_stats"    yaml:"enable_gc_stats"`
}

RuntimeCollectorConfig contains configuration for the runtime collector.

func DefaultRuntimeCollectorConfig

func DefaultRuntimeCollectorConfig() *RuntimeCollectorConfig

DefaultRuntimeCollectorConfig returns default configuration.

type SystemCollector

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

SystemCollector collects system metrics (CPU, memory, disk).

func (*SystemCollector) Collect

func (sc *SystemCollector) Collect() map[string]any

Collect collects system metrics.

func (*SystemCollector) Disable

func (sc *SystemCollector) Disable()

Disable disables the collector.

func (*SystemCollector) Enable

func (sc *SystemCollector) Enable()

Enable enables the collector.

func (*SystemCollector) GetInterval

func (sc *SystemCollector) GetInterval() time.Duration

GetInterval returns the collection interval.

func (*SystemCollector) GetLastCollectionTime

func (sc *SystemCollector) GetLastCollectionTime() time.Time

GetLastCollectionTime returns the last collection time.

func (*SystemCollector) GetMetricsCount

func (sc *SystemCollector) GetMetricsCount() int

GetMetricsCount returns the number of metrics collected.

func (*SystemCollector) IsEnabled

func (sc *SystemCollector) IsEnabled() bool

IsEnabled returns whether the collector is enabled.

func (*SystemCollector) Name

func (sc *SystemCollector) Name() string

Name returns the collector name.

func (*SystemCollector) Reset

func (sc *SystemCollector) Reset() error

Reset resets the collector.

func (*SystemCollector) SetInterval

func (sc *SystemCollector) SetInterval(interval time.Duration)

SetInterval sets the collection interval.

type SystemCollectorConfig

type SystemCollectorConfig struct {
	Interval          time.Duration `json:"interval"           yaml:"interval"`
	CollectCPU        bool          `json:"collect_cpu"        yaml:"collect_cpu"`
	CollectMemory     bool          `json:"collect_memory"     yaml:"collect_memory"`
	CollectDisk       bool          `json:"collect_disk"       yaml:"collect_disk"`
	CollectNetwork    bool          `json:"collect_network"    yaml:"collect_network"`
	CollectLoad       bool          `json:"collect_load"       yaml:"collect_load"`
	DiskMountPoints   []string      `json:"disk_mount_points"  yaml:"disk_mount_points"`
	NetworkInterfaces []string      `json:"network_interfaces" yaml:"network_interfaces"`
}

SystemCollectorConfig contains configuration for the system collector.

func DefaultSystemCollectorConfig

func DefaultSystemCollectorConfig() *SystemCollectorConfig

DefaultSystemCollectorConfig returns default configuration.

type TimerMetric

type TimerMetric struct {
	Name        string            `json:"name"`
	Count       int64             `json:"count"`
	TotalTime   time.Duration     `json:"total_time"`
	MeanTime    time.Duration     `json:"mean_time"`
	MinTime     time.Duration     `json:"min_time"`
	MaxTime     time.Duration     `json:"max_time"`
	P50Time     time.Duration     `json:"p50_time"`
	P95Time     time.Duration     `json:"p95_time"`
	P99Time     time.Duration     `json:"p99_time"`
	Description string            `json:"description"`
	Tags        map[string]string `json:"tags"`
	Timestamp   time.Time         `json:"timestamp"`
	Durations   []time.Duration   `json:"-"`
}

TimerMetric represents a timer metric.

Jump to

Keyboard shortcuts

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