mirror of
https://github.com/luxfi/metric.git
synced 2026-07-27 04:12:44 +00:00
export: add DTOMetricFamily, TextParser, NewPrometheusRegistry; fix HistogramVec to implement Collector
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/prometheus/common/expfmt"
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
)
|
||||
|
||||
@@ -41,6 +42,15 @@ type HTTPHandlerOpts = promhttp.HandlerOpts
|
||||
// MetricFamilies is a slice of metric families
|
||||
type MetricFamilies = []*dto.MetricFamily
|
||||
|
||||
// DTOMetricFamily is an alias for dto.MetricFamily for backward compatibility
|
||||
type DTOMetricFamily = dto.MetricFamily
|
||||
|
||||
// TextParser is an alias for expfmt.TextParser
|
||||
type TextParser = expfmt.TextParser
|
||||
|
||||
// NewPrometheusRegistry is an alias for NewRegistry for backward compatibility
|
||||
var NewPrometheusRegistry = NewRegistry
|
||||
|
||||
// WrapPrometheusRegistry wraps a prometheus registry in our Registry interface
|
||||
func WrapPrometheusRegistry(promReg *prometheus.Registry) Registry {
|
||||
return promReg
|
||||
|
||||
@@ -135,6 +135,7 @@ type GaugeVec interface {
|
||||
|
||||
// HistogramVec is a vector of histograms
|
||||
type HistogramVec interface {
|
||||
prometheus.Collector
|
||||
// With returns a histogram with the given label values
|
||||
With(Labels) Histogram
|
||||
// WithLabelValues returns a histogram with the given label values
|
||||
@@ -227,10 +228,13 @@ func NewGaugeVec(opts GaugeOpts, labelNames []string) GaugeVec {
|
||||
return WrapPrometheusGaugeVec(prometheus.NewGaugeVec(opts, labelNames))
|
||||
}
|
||||
|
||||
func NewHistogramVec(opts HistogramOpts, labelNames []string) HistogramVec {
|
||||
return WrapPrometheusHistogramVec(prometheus.NewHistogramVec(opts, labelNames))
|
||||
}
|
||||
|
||||
// Keep these as direct aliases since they don't need wrapping
|
||||
var (
|
||||
NewHistogram = prometheus.NewHistogram
|
||||
NewHistogramVec = prometheus.NewHistogramVec
|
||||
NewSummary = prometheus.NewSummary
|
||||
NewSummaryVec = prometheus.NewSummaryVec
|
||||
NewRegistry = prometheus.NewRegistry
|
||||
|
||||
@@ -67,6 +67,8 @@ type noopHistogramVec struct{}
|
||||
|
||||
func (n *noopHistogramVec) With(Labels) Histogram { return &noopHistogram{} }
|
||||
func (n *noopHistogramVec) WithLabelValues(...string) Histogram { return &noopHistogram{} }
|
||||
func (n *noopHistogramVec) Describe(ch chan<- *prometheus.Desc) {}
|
||||
func (n *noopHistogramVec) Collect(ch chan<- prometheus.Metric) {}
|
||||
|
||||
// noopSummaryVec is a summary vector that does nothing
|
||||
type noopSummaryVec struct{}
|
||||
|
||||
@@ -105,6 +105,10 @@ func (p *prometheusHistogramVec) WithLabelValues(labelValues ...string) Histogra
|
||||
return &prometheusHistogram{histogram: p.vec.WithLabelValues(labelValues...).(prometheus.Histogram)}
|
||||
}
|
||||
|
||||
// Implement prometheus.Collector interface
|
||||
func (p *prometheusHistogramVec) Describe(ch chan<- *prometheus.Desc) { p.vec.Describe(ch) }
|
||||
func (p *prometheusHistogramVec) Collect(ch chan<- prometheus.Metric) { p.vec.Collect(ch) }
|
||||
|
||||
// prometheusSummaryVec wraps prometheus.SummaryVec
|
||||
type prometheusSummaryVec struct {
|
||||
vec *prometheus.SummaryVec
|
||||
|
||||
+6
-1
@@ -22,11 +22,16 @@ func WrapPrometheusCounterVec(cv *prometheus.CounterVec) CounterVec {
|
||||
return &prometheusCounterVec{vec: cv}
|
||||
}
|
||||
|
||||
// WrapPrometheusGaugeVec wraps a prometheus.GaugeVec to implement our GaugeVec interface
|
||||
// WrapPrometheusGaugeVec wraps a prometheus.GaugeVec to implement our GaugeVec interface
|
||||
func WrapPrometheusGaugeVec(gv *prometheus.GaugeVec) GaugeVec {
|
||||
return &prometheusGaugeVec{vec: gv}
|
||||
}
|
||||
|
||||
// WrapPrometheusHistogramVec wraps a prometheus.HistogramVec to implement our HistogramVec interface
|
||||
func WrapPrometheusHistogramVec(hv *prometheus.HistogramVec) HistogramVec {
|
||||
return &prometheusHistogramVec{vec: hv}
|
||||
}
|
||||
|
||||
// NewCounterWithOpts creates a wrapped counter from options
|
||||
func NewCounterWithOpts(opts prometheus.CounterOpts) Counter {
|
||||
return WrapPrometheusCounter(prometheus.NewCounter(opts))
|
||||
|
||||
Reference in New Issue
Block a user