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).
This commit is contained in:
parent
661e31bf84
commit
9e1588b2a2
|
@ -152,14 +152,22 @@ func (m *CounterVec) GetMetricWith(labels Labels) (Counter, error) {
|
||||||
// error, WithLabelValues allows shortcuts like
|
// error, WithLabelValues allows shortcuts like
|
||||||
// myVec.WithLabelValues("404", "GET").Add(42)
|
// myVec.WithLabelValues("404", "GET").Add(42)
|
||||||
func (m *CounterVec) WithLabelValues(lvs ...string) Counter {
|
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
|
// With works as GetMetricWith, but panics where GetMetricWithLabels would have
|
||||||
// returned an error. By not returning an error, With allows shortcuts like
|
// returned an error. By not returning an error, With allows shortcuts like
|
||||||
// myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
|
// myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
|
||||||
func (m *CounterVec) With(labels Labels) Counter {
|
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
|
// CounterFunc is a Counter whose value is determined at collect time by calling a
|
||||||
|
|
|
@ -138,14 +138,22 @@ func (m *GaugeVec) GetMetricWith(labels Labels) (Gauge, error) {
|
||||||
// error, WithLabelValues allows shortcuts like
|
// error, WithLabelValues allows shortcuts like
|
||||||
// myVec.WithLabelValues("404", "GET").Add(42)
|
// myVec.WithLabelValues("404", "GET").Add(42)
|
||||||
func (m *GaugeVec) WithLabelValues(lvs ...string) Gauge {
|
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
|
// With works as GetMetricWith, but panics where GetMetricWithLabels would have
|
||||||
// returned an error. By not returning an error, With allows shortcuts like
|
// returned an error. By not returning an error, With allows shortcuts like
|
||||||
// myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
|
// myVec.With(Labels{"code": "404", "method": "GET"}).Add(42)
|
||||||
func (m *GaugeVec) With(labels Labels) Gauge {
|
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
|
// GaugeFunc is a Gauge whose value is determined at collect time by calling a
|
||||||
|
|
|
@ -363,14 +363,22 @@ func (m *HistogramVec) GetMetricWith(labels Labels) (Observer, error) {
|
||||||
// error, WithLabelValues allows shortcuts like
|
// error, WithLabelValues allows shortcuts like
|
||||||
// myVec.WithLabelValues("404", "GET").Observe(42.21)
|
// myVec.WithLabelValues("404", "GET").Observe(42.21)
|
||||||
func (m *HistogramVec) WithLabelValues(lvs ...string) Observer {
|
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
|
// With works as GetMetricWith, but panics where GetMetricWithLabels would have
|
||||||
// returned an error. By not returning an error, With allows shortcuts like
|
// returned an error. By not returning an error, With allows shortcuts like
|
||||||
// myVec.With(Labels{"code": "404", "method": "GET"}).Observe(42.21)
|
// myVec.With(Labels{"code": "404", "method": "GET"}).Observe(42.21)
|
||||||
func (m *HistogramVec) With(labels Labels) Observer {
|
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 {
|
type constHistogram struct {
|
||||||
|
|
|
@ -480,14 +480,22 @@ func (m *SummaryVec) GetMetricWith(labels Labels) (Observer, error) {
|
||||||
// error, WithLabelValues allows shortcuts like
|
// error, WithLabelValues allows shortcuts like
|
||||||
// myVec.WithLabelValues("404", "GET").Observe(42.21)
|
// myVec.WithLabelValues("404", "GET").Observe(42.21)
|
||||||
func (m *SummaryVec) WithLabelValues(lvs ...string) Observer {
|
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
|
// With works as GetMetricWith, but panics where GetMetricWithLabels would have
|
||||||
// returned an error. By not returning an error, With allows shortcuts like
|
// returned an error. By not returning an error, With allows shortcuts like
|
||||||
// myVec.With(Labels{"code": "404", "method": "GET"}).Observe(42.21)
|
// myVec.With(Labels{"code": "404", "method": "GET"}).Observe(42.21)
|
||||||
func (m *SummaryVec) With(labels Labels) Observer {
|
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 {
|
type constSummary struct {
|
||||||
|
|
|
@ -89,22 +89,6 @@ func (m *metricVec) getMetricWith(labels Labels) (Metric, error) {
|
||||||
return m.getOrCreateMetricWithLabels(h, labels), nil
|
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
|
// 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
|
// as those passed in as labels (same order as the VariableLabels in Desc). It
|
||||||
// returns true if a metric was deleted.
|
// returns true if a metric was deleted.
|
||||||
|
|
Loading…
Reference in New Issue