From 9e1588b2a27532676e9264ffbb755adde6715cc7 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Tue, 29 Aug 2017 14:43:37 +0200 Subject: [PATCH] Pull `With` and `WithLabelValues` up into exported types The "panic in case of error" code was so far in metricVec. This pulls it up into the exported types like CounterVec. This is code replication, but it avoids an explicit type conversion. Mostly, however, this is preparation to make the wrapped metricVec an interface (required for curried vec's). --- prometheus/counter.go | 12 ++++++++++-- prometheus/gauge.go | 12 ++++++++++-- prometheus/histogram.go | 12 ++++++++++-- prometheus/summary.go | 12 ++++++++++-- prometheus/vec.go | 16 ---------------- 5 files changed, 40 insertions(+), 24 deletions(-) diff --git a/prometheus/counter.go b/prometheus/counter.go index 273db5f..07f99f4 100644 --- a/prometheus/counter.go +++ b/prometheus/counter.go @@ -152,14 +152,22 @@ func (m *CounterVec) GetMetricWith(labels Labels) (Counter, error) { // error, WithLabelValues allows shortcuts like // myVec.WithLabelValues("404", "GET").Add(42) func (m *CounterVec) WithLabelValues(lvs ...string) Counter { - return m.metricVec.withLabelValues(lvs...).(Counter) + c, err := m.GetMetricWithLabelValues(lvs...) + if err != nil { + panic(err) + } + return c } // With works as GetMetricWith, but panics where GetMetricWithLabels would have // returned an error. By not returning an error, With allows shortcuts like // myVec.With(Labels{"code": "404", "method": "GET"}).Add(42) func (m *CounterVec) With(labels Labels) Counter { - return m.metricVec.with(labels).(Counter) + c, err := m.GetMetricWith(labels) + if err != nil { + panic(err) + } + return c } // CounterFunc is a Counter whose value is determined at collect time by calling a diff --git a/prometheus/gauge.go b/prometheus/gauge.go index 13064da..a0cacec 100644 --- a/prometheus/gauge.go +++ b/prometheus/gauge.go @@ -138,14 +138,22 @@ func (m *GaugeVec) GetMetricWith(labels Labels) (Gauge, error) { // error, WithLabelValues allows shortcuts like // myVec.WithLabelValues("404", "GET").Add(42) func (m *GaugeVec) WithLabelValues(lvs ...string) Gauge { - return m.metricVec.withLabelValues(lvs...).(Gauge) + g, err := m.GetMetricWithLabelValues(lvs...) + if err != nil { + panic(err) + } + return g } // With works as GetMetricWith, but panics where GetMetricWithLabels would have // returned an error. By not returning an error, With allows shortcuts like // myVec.With(Labels{"code": "404", "method": "GET"}).Add(42) func (m *GaugeVec) With(labels Labels) Gauge { - return m.metricVec.with(labels).(Gauge) + g, err := m.GetMetricWith(labels) + if err != nil { + panic(err) + } + return g } // GaugeFunc is a Gauge whose value is determined at collect time by calling a diff --git a/prometheus/histogram.go b/prometheus/histogram.go index 6cc6e68..1b2d9b4 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -363,14 +363,22 @@ func (m *HistogramVec) GetMetricWith(labels Labels) (Observer, error) { // error, WithLabelValues allows shortcuts like // myVec.WithLabelValues("404", "GET").Observe(42.21) func (m *HistogramVec) WithLabelValues(lvs ...string) Observer { - return m.metricVec.withLabelValues(lvs...).(Observer) + h, err := m.GetMetricWithLabelValues(lvs...) + if err != nil { + panic(err) + } + return h } // With works as GetMetricWith, but panics where GetMetricWithLabels would have // returned an error. By not returning an error, With allows shortcuts like // myVec.With(Labels{"code": "404", "method": "GET"}).Observe(42.21) func (m *HistogramVec) With(labels Labels) Observer { - return m.metricVec.with(labels).(Observer) + h, err := m.GetMetricWith(labels) + if err != nil { + panic(err) + } + return h } type constHistogram struct { diff --git a/prometheus/summary.go b/prometheus/summary.go index 21c031e..b075301 100644 --- a/prometheus/summary.go +++ b/prometheus/summary.go @@ -480,14 +480,22 @@ func (m *SummaryVec) GetMetricWith(labels Labels) (Observer, error) { // error, WithLabelValues allows shortcuts like // myVec.WithLabelValues("404", "GET").Observe(42.21) func (m *SummaryVec) WithLabelValues(lvs ...string) Observer { - return m.metricVec.withLabelValues(lvs...).(Observer) + s, err := m.GetMetricWithLabelValues(lvs...) + if err != nil { + panic(err) + } + return s } // With works as GetMetricWith, but panics where GetMetricWithLabels would have // returned an error. By not returning an error, With allows shortcuts like // myVec.With(Labels{"code": "404", "method": "GET"}).Observe(42.21) func (m *SummaryVec) With(labels Labels) Observer { - return m.metricVec.with(labels).(Observer) + s, err := m.GetMetricWith(labels) + if err != nil { + panic(err) + } + return s } type constSummary struct { diff --git a/prometheus/vec.go b/prometheus/vec.go index 65d13fe..6e3a376 100644 --- a/prometheus/vec.go +++ b/prometheus/vec.go @@ -89,22 +89,6 @@ func (m *metricVec) getMetricWith(labels Labels) (Metric, error) { return m.getOrCreateMetricWithLabels(h, labels), nil } -func (m *metricVec) withLabelValues(lvs ...string) Metric { - metric, err := m.getMetricWithLabelValues(lvs...) - if err != nil { - panic(err) - } - return metric -} - -func (m *metricVec) with(labels Labels) Metric { - metric, err := m.getMetricWith(labels) - if err != nil { - panic(err) - } - return metric -} - // DeleteLabelValues removes the metric where the variable labels are the same // as those passed in as labels (same order as the VariableLabels in Desc). It // returns true if a metric was deleted.