feat(vec): add Reset() to all {Counter,Gauge,Histogram,Summary}Vec — prom-compat

This commit is contained in:
Hanzo AI
2026-05-22 20:21:22 -07:00
parent 2c62e83ce0
commit f634539ce0
3 changed files with 54 additions and 0 deletions
+4
View File
@@ -105,24 +105,28 @@ type Factory interface {
type CounterVec interface {
With(Labels) Counter
WithLabelValues(...string) Counter
Reset()
}
// GaugeVec is a labeled gauge collection.
type GaugeVec interface {
With(Labels) Gauge
WithLabelValues(...string) Gauge
Reset()
}
// HistogramVec is a labeled histogram collection.
type HistogramVec interface {
With(Labels) Histogram
WithLabelValues(...string) Histogram
Reset()
}
// SummaryVec is a labeled summary collection.
type SummaryVec interface {
With(Labels) Summary
WithLabelValues(...string) Summary
Reset()
}
// MetricsHTTPHandler handles HTTP requests for metrics.
+46
View File
@@ -665,6 +665,19 @@ func (hpr *registry) RegisterLabeledSummary(name string, labels Labels, summary
hpr.summaries[name][key] = &labeledSummary{labels: cloneLabels(labels), summary: summary}
}
// deregisterLabeled drops all label-permutation children for the named
// metric across counter/gauge/histogram/summary registries. Called by
// {Counter,Gauge,Histogram,Summary}Vec.Reset() to mirror prometheus
// vec semantics.
func (hpr *registry) deregisterLabeled(name string) {
hpr.mu.Lock()
defer hpr.mu.Unlock()
delete(hpr.counters, name)
delete(hpr.gauges, name)
delete(hpr.histograms, name)
delete(hpr.summaries, name)
}
// NewCounter creates and registers a counter.
func (hpr *registry) NewCounter(name, help string) Counter {
counter := newCounter(name, help)
@@ -862,6 +875,15 @@ func (v *counterVec) getOrCreate(labels Labels) Counter {
return counter
}
// Reset drops every label-permutation child from this vec. Mirrors
// prometheus/client_golang.CounterVec.Reset semantics.
func (v *counterVec) Reset() {
v.mu.Lock()
defer v.mu.Unlock()
v.registry.deregisterLabeled(v.name)
v.counters = make(map[string]Counter)
}
// gaugeVec is a labeled gauge collection.
type gaugeVec struct {
registry *registry
@@ -904,6 +926,14 @@ func (v *gaugeVec) getOrCreate(labels Labels) Gauge {
return gauge
}
// Reset drops every label-permutation child from this vec.
func (v *gaugeVec) Reset() {
v.mu.Lock()
defer v.mu.Unlock()
v.registry.deregisterLabeled(v.name)
v.gauges = make(map[string]Gauge)
}
// histogramVec is a labeled histogram collection.
type histogramVec struct {
registry *registry
@@ -948,6 +978,14 @@ func (v *histogramVec) getOrCreate(labels Labels) Histogram {
return histogram
}
// Reset drops every label-permutation child from this vec.
func (v *histogramVec) Reset() {
v.mu.Lock()
defer v.mu.Unlock()
v.registry.deregisterLabeled(v.name)
v.histograms = make(map[string]Histogram)
}
// summaryVec is a labeled summary collection.
type summaryVec struct {
registry *registry
@@ -996,6 +1034,14 @@ func (v *summaryVec) getOrCreate(labels Labels) Summary {
return summary
}
// Reset drops every label-permutation child from this vec.
func (v *summaryVec) Reset() {
v.mu.Lock()
defer v.mu.Unlock()
v.registry.deregisterLabeled(v.name)
v.summaries = make(map[string]Summary)
}
func labelsFromValues(labelNames []string, values []string) Labels {
labels := make(Labels, len(labelNames))
for i, name := range labelNames {
+4
View File
@@ -35,24 +35,28 @@ type noopCounterVec struct{}
func (n *noopCounterVec) With(Labels) Counter { return &noopCounter{} }
func (n *noopCounterVec) WithLabelValues(...string) Counter { return &noopCounter{} }
func (n *noopCounterVec) Reset() {}
// noopGaugeVec is a gauge vector that does nothing.
type noopGaugeVec struct{}
func (n *noopGaugeVec) With(Labels) Gauge { return &noopGauge{} }
func (n *noopGaugeVec) WithLabelValues(...string) Gauge { return &noopGauge{} }
func (n *noopGaugeVec) Reset() {}
// noopHistogramVec is a histogram vector that does nothing.
type noopHistogramVec struct{}
func (n *noopHistogramVec) With(Labels) Histogram { return &noopHistogram{} }
func (n *noopHistogramVec) WithLabelValues(...string) Histogram { return &noopHistogram{} }
func (n *noopHistogramVec) Reset() {}
// noopSummaryVec is a summary vector that does nothing.
type noopSummaryVec struct{}
func (n *noopSummaryVec) With(Labels) Summary { return &noopSummary{} }
func (n *noopSummaryVec) WithLabelValues(...string) Summary { return &noopSummary{} }
func (n *noopSummaryVec) Reset() {}
// noopRegistry provides a registry that gathers nothing.
type noopRegistry struct{}