Documentation
¶
Overview ¶
Package metrics provides an abstract interface for recording and managing various types of metrics within an application. It is designed to offer a unified and simple API for common metric operations, such as registering and recording standard and labeled metrics.
The Metrics interface defined in this package serves as the foundation for implementing specific metrics systems, such as a Prometheus-based metrics system.
Key functionalities include:
- Register: To define and set up new metrics.
- Record: To record values for the standard metrics.
- RegisterWithLabels: To create new metrics with associated labels.
- RecordWithLabels: To record values for labeled metrics, providing label values dynamically.
Usage Example:
metricsSystem := metrics.NewPrometheusMetrics() // Assuming a Prometheus implementation metricsSystem.Register("requests_total", "Counter", "Total number of HTTP requests") metricsSystem.Record("requests_total", 1) metricsSystem.RegisterWithLabels("http_requests_total", "Counter", "HTTP requests with method and status", []string{"method", "status"}) metricsSystem.RecordWithLabels("http_requests_total", 1, "GET", "200")
Index ¶
- type Metrics
- type PrometheusMetrics
- func (p *PrometheusMetrics) Record(name string, value float64)
- func (p *PrometheusMetrics) RecordWithLabels(name string, value float64, labelValues ...string)
- func (p *PrometheusMetrics) Register(name, metricType, help string)
- func (p *PrometheusMetrics) RegisterWithLabels(name, metricType, help string, labels []string)
- func (p *PrometheusMetrics) SetCustomBuckets(name string, buckets []float64)
- func (p *PrometheusMetrics) StartMetricsServer(port string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PrometheusMetrics ¶
type PrometheusMetrics struct {
// contains filtered or unexported fields
}
PrometheusMetrics is a structure that implements the Metrics interface using Prometheus as the backend. It stores mappings for different Prometheus metric types (Counter, Gauge, Histogram) and their vector counterparts.
func NewPrometheusMetrics ¶
func NewPrometheusMetrics() *PrometheusMetrics
NewPrometheusMetrics creates and initializes a new instance of PrometheusMetrics. This function sets up the internal maps used to store various types of Prometheus metrics, including counters, gauges, histograms, and their labeled (vector) versions, as well as custom buckets for histograms.
func (*PrometheusMetrics) Record ¶
func (p *PrometheusMetrics) Record(name string, value float64)
Record updates the value of a Prometheus metric without labels. It is used for recording values for counters, gauges, and histograms based on the metric 'name'. The method identifies the correct metric type and performs the appropriate action: 'Add' for counters, 'Set' for gauges, and 'Observe' for histograms. The 'value' parameter is the value to record.
func (*PrometheusMetrics) RecordWithLabels ¶
func (p *PrometheusMetrics) RecordWithLabels(name string, value float64, labelValues ...string)
RecordWithLabels updates the value of a labeled Prometheus metric. This method is used for metrics that were registered with labels, such as those created via 'RegisterWithLabels'. It finds the appropriate metric based on 'name' and updates it with the given 'value' and 'labelValues'. The 'labelValues' are variadic parameters that should match the order and number of labels defined during registration.
func (*PrometheusMetrics) Register ¶
func (p *PrometheusMetrics) Register(name, metricType, help string)
Register creates and registers a new metric in the Prometheus registry based on the provided type. Supported metric types include 'Counter', 'Gauge', and 'Histogram'. The method takes the metric 'name', its 'metricType', and a 'help' string describing the metric. For 'Histogram' types, it uses custom buckets if they have been set; otherwise, it falls back to default buckets.
func (*PrometheusMetrics) RegisterWithLabels ¶
func (p *PrometheusMetrics) RegisterWithLabels(name, metricType, help string, labels []string)
RegisterWithLabels creates and registers a new labeled metric. This method is similar to 'Register' but for metrics with labels (like CounterVec, GaugeVec, HistogramVec). It takes the metric 'name', 'metricType', a 'help' description, and a slice of 'labels' (the label keys). For 'HistogramVec', it respects custom buckets if set for the given metric name.
func (*PrometheusMetrics) SetCustomBuckets ¶
func (p *PrometheusMetrics) SetCustomBuckets(name string, buckets []float64)
SetCustomBuckets allows setting custom bucket sizes for histograms. requiring finer or broader granularity. The 'name' parameter specifies the metric name, and 'buckets' is a slice of float64 values defining the bucket thresholds.
func (*PrometheusMetrics) StartMetricsServer ¶
func (p *PrometheusMetrics) StartMetricsServer(port string)
StartMetricsServer initializes and starts an HTTP server on the specified 'port' to expose Prometheus metrics. This server provides an endpoint for Prometheus to scrape the collected metrics. Typically it would be used to start a metrics server in a separate goroutine to keep it running independently.