Documentation
¶
Overview ¶
Package metrics provides metrics collection and reporting for the OpenCTEM SDK. It includes interfaces for metric collection and a Prometheus-compatible implementation.
Package metrics provides Prometheus-compatible metrics collection.
Index ¶
- Variables
- func SetDefaultCollector(collector Collector)
- func WithCollector(ctx context.Context, collector Collector) context.Context
- type Collector
- type InMemoryCollector
- func (c *InMemoryCollector) CounterAdd(name string, value float64, labels ...string)
- func (c *InMemoryCollector) CounterInc(name string, labels ...string)
- func (c *InMemoryCollector) GaugeDec(name string, labels ...string)
- func (c *InMemoryCollector) GaugeInc(name string, labels ...string)
- func (c *InMemoryCollector) GaugeSet(name string, value float64, labels ...string)
- func (c *InMemoryCollector) GetCounter(name string, labels ...string) float64
- func (c *InMemoryCollector) GetGauge(name string, labels ...string) float64
- func (c *InMemoryCollector) GetHistogram(name string, labels ...string) []float64
- func (c *InMemoryCollector) GetSummary(name string, labels ...string) []float64
- func (c *InMemoryCollector) Handler() http.Handler
- func (c *InMemoryCollector) HistogramObserve(name string, value float64, labels ...string)
- func (c *InMemoryCollector) Reset()
- func (c *InMemoryCollector) SummaryObserve(name string, value float64, labels ...string)
- type MetricDefinition
- type MetricType
- type NopCollector
- func (c *NopCollector) CounterAdd(name string, value float64, labels ...string)
- func (c *NopCollector) CounterInc(name string, labels ...string)
- func (c *NopCollector) GaugeDec(name string, labels ...string)
- func (c *NopCollector) GaugeInc(name string, labels ...string)
- func (c *NopCollector) GaugeSet(name string, value float64, labels ...string)
- func (c *NopCollector) Handler() http.Handler
- func (c *NopCollector) HistogramObserve(name string, value float64, labels ...string)
- func (c *NopCollector) Reset()
- func (c *NopCollector) SummaryObserve(name string, value float64, labels ...string)
- type PrometheusCollector
- func (c *PrometheusCollector) CounterAdd(name string, value float64, labels ...string)
- func (c *PrometheusCollector) CounterInc(name string, labels ...string)
- func (c *PrometheusCollector) GaugeDec(name string, labels ...string)
- func (c *PrometheusCollector) GaugeInc(name string, labels ...string)
- func (c *PrometheusCollector) GaugeSet(name string, value float64, labels ...string)
- func (c *PrometheusCollector) Handler() http.Handler
- func (c *PrometheusCollector) HistogramObserve(name string, value float64, labels ...string)
- func (c *PrometheusCollector) RegisterCounter(def MetricDefinition) error
- func (c *PrometheusCollector) RegisterGauge(def MetricDefinition) error
- func (c *PrometheusCollector) RegisterHistogram(def MetricDefinition) error
- func (c *PrometheusCollector) RegisterSummary(def MetricDefinition) error
- func (c *PrometheusCollector) Registry() *prometheus.Registry
- func (c *PrometheusCollector) Reset()
- func (c *PrometheusCollector) SummaryObserve(name string, value float64, labels ...string)
- type PrometheusConfig
- type Timer
Constants ¶
This section is empty.
Variables ¶
var ( // Scanner metrics ScannerScansTotal = MetricDefinition{ Name: "openctem_scanner_scans_total", Type: MetricTypeCounter, Help: "Total number of scans executed", Labels: []string{"scanner", "status"}, } ScannerScanDuration = MetricDefinition{ Name: "openctem_scanner_scan_duration_seconds", Type: MetricTypeHistogram, Help: "Duration of scans in seconds", Labels: []string{"scanner"}, Buckets: []float64{1, 5, 10, 30, 60, 120, 300, 600}, } ScannerFindingsTotal = MetricDefinition{ Name: "openctem_scanner_findings_total", Type: MetricTypeCounter, Help: "Total number of findings discovered", Labels: []string{"scanner", "severity"}, } // Collector metrics CollectorCollectsTotal = MetricDefinition{ Name: "openctem_collector_collects_total", Type: MetricTypeCounter, Help: "Total number of collections executed", Labels: []string{"collector", "status"}, } CollectorItemsTotal = MetricDefinition{ Name: "openctem_collector_items_total", Type: MetricTypeCounter, Help: "Total number of items collected", Labels: []string{"collector"}, } // Pusher metrics PusherPushesTotal = MetricDefinition{ Name: "openctem_pusher_pushes_total", Type: MetricTypeCounter, Help: "Total number of push operations", Labels: []string{"status"}, } PusherFindingsPushed = MetricDefinition{ Name: "openctem_pusher_findings_pushed_total", Type: MetricTypeCounter, Help: "Total number of findings pushed", Labels: []string{}, } PusherAssetsPushed = MetricDefinition{ Name: "openctem_pusher_assets_pushed_total", Type: MetricTypeCounter, Help: "Total number of assets pushed", Labels: []string{}, } PusherRetries = MetricDefinition{ Name: "openctem_pusher_retries_total", Type: MetricTypeCounter, Help: "Total number of push retries", Labels: []string{}, } // Agent metrics AgentJobsTotal = MetricDefinition{ Name: "openctem_agent_jobs_total", Type: MetricTypeCounter, Help: "Total number of jobs processed", Labels: []string{"job_type", "status"}, } AgentJobDuration = MetricDefinition{ Name: "openctem_agent_job_duration_seconds", Type: MetricTypeHistogram, Help: "Duration of job execution in seconds", Labels: []string{"job_type"}, Buckets: []float64{1, 5, 10, 30, 60, 120, 300, 600, 1800, 3600}, } AgentQueueSize = MetricDefinition{ Name: "openctem_agent_queue_size", Type: MetricTypeGauge, Help: "Current number of jobs in queue", Labels: []string{}, } AgentActiveJobs = MetricDefinition{ Name: "openctem_agent_active_jobs", Type: MetricTypeGauge, Help: "Number of currently executing jobs", Labels: []string{}, } AgentHeartbeats = MetricDefinition{ Name: "openctem_agent_heartbeats_total", Type: MetricTypeCounter, Help: "Total number of heartbeats sent", Labels: []string{"status"}, } // Enricher metrics EnricherEnrichmentsTotal = MetricDefinition{ Name: "openctem_enricher_enrichments_total", Type: MetricTypeCounter, Help: "Total number of enrichment operations", Labels: []string{"enricher", "status"}, } EnricherCacheHits = MetricDefinition{ Name: "openctem_enricher_cache_hits_total", Type: MetricTypeCounter, Help: "Total number of cache hits", Labels: []string{"enricher"}, } EnricherCacheMisses = MetricDefinition{ Name: "openctem_enricher_cache_misses_total", Type: MetricTypeCounter, Help: "Total number of cache misses", Labels: []string{"enricher"}, } // HTTP client metrics HTTPRequestsTotal = MetricDefinition{ Name: "openctem_http_requests_total", Type: MetricTypeCounter, Help: "Total number of HTTP requests made", Labels: []string{"method", "host", "status"}, } HTTPRequestDuration = MetricDefinition{ Name: "openctem_http_request_duration_seconds", Type: MetricTypeHistogram, Help: "Duration of HTTP requests in seconds", Labels: []string{"method", "host"}, Buckets: []float64{0.01, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}, } )
Functions ¶
func SetDefaultCollector ¶
func SetDefaultCollector(collector Collector)
SetDefaultCollector sets the global default metrics collector.
Types ¶
type Collector ¶
type Collector interface {
// Counter operations
CounterInc(name string, labels ...string)
CounterAdd(name string, value float64, labels ...string)
// Gauge operations
GaugeSet(name string, value float64, labels ...string)
GaugeInc(name string, labels ...string)
GaugeDec(name string, labels ...string)
// Histogram operations
HistogramObserve(name string, value float64, labels ...string)
// Summary operations
SummaryObserve(name string, value float64, labels ...string)
// Handler returns an HTTP handler for metrics endpoint
Handler() http.Handler
// Reset clears all metrics (for testing)
Reset()
}
Collector is the interface for collecting and reporting metrics. Implement this interface to use custom metrics backends (Prometheus, StatsD, etc.).
func CollectorFromContext ¶
CollectorFromContext returns the collector from the context, or the default.
func GetDefaultCollector ¶
func GetDefaultCollector() Collector
GetDefaultCollector returns the global default metrics collector.
type InMemoryCollector ¶
type InMemoryCollector struct {
// contains filtered or unexported fields
}
InMemoryCollector stores metrics in memory for testing purposes.
func NewInMemoryCollector ¶
func NewInMemoryCollector() *InMemoryCollector
NewInMemoryCollector creates a new in-memory metrics collector.
func (*InMemoryCollector) CounterAdd ¶
func (c *InMemoryCollector) CounterAdd(name string, value float64, labels ...string)
func (*InMemoryCollector) CounterInc ¶
func (c *InMemoryCollector) CounterInc(name string, labels ...string)
func (*InMemoryCollector) GaugeDec ¶
func (c *InMemoryCollector) GaugeDec(name string, labels ...string)
func (*InMemoryCollector) GaugeInc ¶
func (c *InMemoryCollector) GaugeInc(name string, labels ...string)
func (*InMemoryCollector) GaugeSet ¶
func (c *InMemoryCollector) GaugeSet(name string, value float64, labels ...string)
func (*InMemoryCollector) GetCounter ¶
func (c *InMemoryCollector) GetCounter(name string, labels ...string) float64
GetCounter returns the value of a counter.
func (*InMemoryCollector) GetGauge ¶
func (c *InMemoryCollector) GetGauge(name string, labels ...string) float64
GetGauge returns the value of a gauge.
func (*InMemoryCollector) GetHistogram ¶
func (c *InMemoryCollector) GetHistogram(name string, labels ...string) []float64
GetHistogram returns all observations of a histogram.
func (*InMemoryCollector) GetSummary ¶
func (c *InMemoryCollector) GetSummary(name string, labels ...string) []float64
GetSummary returns all observations of a summary.
func (*InMemoryCollector) Handler ¶
func (c *InMemoryCollector) Handler() http.Handler
func (*InMemoryCollector) HistogramObserve ¶
func (c *InMemoryCollector) HistogramObserve(name string, value float64, labels ...string)
func (*InMemoryCollector) Reset ¶
func (c *InMemoryCollector) Reset()
func (*InMemoryCollector) SummaryObserve ¶
func (c *InMemoryCollector) SummaryObserve(name string, value float64, labels ...string)
type MetricDefinition ¶
type MetricDefinition struct {
Name string `json:"name"`
Type MetricType `json:"type"`
Help string `json:"help"`
Labels []string `json:"labels,omitempty"`
Buckets []float64 `json:"buckets,omitempty"` // For histograms
Objectives []float64 `json:"objectives,omitempty"` // For summaries
MaxAge int `json:"max_age,omitempty"` // For summaries (seconds)
AgeBuckets int `json:"age_buckets,omitempty"` // For summaries
}
MetricDefinition defines a metric with its metadata.
type MetricType ¶
type MetricType string
MetricType represents the type of metric.
const ( MetricTypeCounter MetricType = "counter" MetricTypeGauge MetricType = "gauge" MetricTypeHistogram MetricType = "histogram" MetricTypeSummary MetricType = "summary" )
type NopCollector ¶
type NopCollector struct{}
NopCollector is a no-op metrics collector that discards all metrics. Use this when metrics are not needed.
func (*NopCollector) CounterAdd ¶
func (c *NopCollector) CounterAdd(name string, value float64, labels ...string)
func (*NopCollector) CounterInc ¶
func (c *NopCollector) CounterInc(name string, labels ...string)
func (*NopCollector) GaugeDec ¶
func (c *NopCollector) GaugeDec(name string, labels ...string)
func (*NopCollector) GaugeInc ¶
func (c *NopCollector) GaugeInc(name string, labels ...string)
func (*NopCollector) GaugeSet ¶
func (c *NopCollector) GaugeSet(name string, value float64, labels ...string)
func (*NopCollector) Handler ¶
func (c *NopCollector) Handler() http.Handler
func (*NopCollector) HistogramObserve ¶
func (c *NopCollector) HistogramObserve(name string, value float64, labels ...string)
func (*NopCollector) Reset ¶
func (c *NopCollector) Reset()
func (*NopCollector) SummaryObserve ¶
func (c *NopCollector) SummaryObserve(name string, value float64, labels ...string)
type PrometheusCollector ¶
type PrometheusCollector struct {
// contains filtered or unexported fields
}
PrometheusCollector implements the Collector interface using Prometheus.
func NewPrometheusCollector ¶
func NewPrometheusCollector(cfg *PrometheusConfig) *PrometheusCollector
NewPrometheusCollector creates a new Prometheus metrics collector.
func (*PrometheusCollector) CounterAdd ¶
func (c *PrometheusCollector) CounterAdd(name string, value float64, labels ...string)
func (*PrometheusCollector) CounterInc ¶
func (c *PrometheusCollector) CounterInc(name string, labels ...string)
func (*PrometheusCollector) GaugeDec ¶
func (c *PrometheusCollector) GaugeDec(name string, labels ...string)
func (*PrometheusCollector) GaugeInc ¶
func (c *PrometheusCollector) GaugeInc(name string, labels ...string)
func (*PrometheusCollector) GaugeSet ¶
func (c *PrometheusCollector) GaugeSet(name string, value float64, labels ...string)
func (*PrometheusCollector) Handler ¶
func (c *PrometheusCollector) Handler() http.Handler
func (*PrometheusCollector) HistogramObserve ¶
func (c *PrometheusCollector) HistogramObserve(name string, value float64, labels ...string)
func (*PrometheusCollector) RegisterCounter ¶
func (c *PrometheusCollector) RegisterCounter(def MetricDefinition) error
RegisterCounter registers a counter metric.
func (*PrometheusCollector) RegisterGauge ¶
func (c *PrometheusCollector) RegisterGauge(def MetricDefinition) error
RegisterGauge registers a gauge metric.
func (*PrometheusCollector) RegisterHistogram ¶
func (c *PrometheusCollector) RegisterHistogram(def MetricDefinition) error
RegisterHistogram registers a histogram metric.
func (*PrometheusCollector) RegisterSummary ¶
func (c *PrometheusCollector) RegisterSummary(def MetricDefinition) error
RegisterSummary registers a summary metric.
func (*PrometheusCollector) Registry ¶
func (c *PrometheusCollector) Registry() *prometheus.Registry
Registry returns the underlying Prometheus registry.
func (*PrometheusCollector) Reset ¶
func (c *PrometheusCollector) Reset()
func (*PrometheusCollector) SummaryObserve ¶
func (c *PrometheusCollector) SummaryObserve(name string, value float64, labels ...string)
type PrometheusConfig ¶
type PrometheusConfig struct {
// Namespace prefixes all metric names (e.g., "openctem")
Namespace string
// Subsystem prefixes metric names after namespace (e.g., "agent")
Subsystem string
// Registry is the Prometheus registry to use (nil = new registry)
Registry *prometheus.Registry
// RegisterDefaultMetrics registers standard OpenCTEM SDK metrics
RegisterDefaultMetrics bool
}
PrometheusConfig configures the Prometheus collector.
type Timer ¶
type Timer struct {
// contains filtered or unexported fields
}
Timer is a helper for timing operations and recording to histograms.
func (*Timer) ObserveDuration ¶
ObserveDuration records the duration since the timer was created.