forked from mirrors/statsd_exporter
Checking conflict for gauges as well and refactoring into function
Signed-off-by: Pedro Tanaka <pedro.tanaka@shopify.com>
This commit is contained in:
parent
31b05ef232
commit
83cec219c8
2 changed files with 51 additions and 12 deletions
|
@ -443,6 +443,34 @@ func TestConflictingMetrics(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "histogram vs gauge with sum suffix",
|
||||||
|
expected: []float64{2},
|
||||||
|
in: event.Events{
|
||||||
|
&event.ObserverEvent{
|
||||||
|
OMetricName: "histogram_test1",
|
||||||
|
OValue: 2,
|
||||||
|
},
|
||||||
|
&event.GaugeEvent{
|
||||||
|
GMetricName: "histogram_test1_sum",
|
||||||
|
GValue: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "histogram vs gauge with count suffix",
|
||||||
|
expected: []float64{2},
|
||||||
|
in: event.Events{
|
||||||
|
&event.ObserverEvent{
|
||||||
|
OMetricName: "histogram_test1",
|
||||||
|
OValue: 2,
|
||||||
|
},
|
||||||
|
&event.GaugeEvent{
|
||||||
|
GMetricName: "histogram_test1_count",
|
||||||
|
GValue: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "counter vs histogram",
|
name: "counter vs histogram",
|
||||||
expected: []float64{1},
|
expected: []float64{1},
|
||||||
|
@ -562,11 +590,11 @@ mappings:
|
||||||
events <- s.in
|
events <- s.in
|
||||||
close(events)
|
close(events)
|
||||||
}()
|
}()
|
||||||
registerer := prometheus.NewRegistry()
|
reg := prometheus.NewRegistry()
|
||||||
ex := NewExporter(registerer, testMapper, log.NewNopLogger(), eventsActions, eventsUnmapped, errorEventStats, eventStats, conflictingEventStats, metricsCount)
|
ex := NewExporter(reg, testMapper, log.NewNopLogger(), eventsActions, eventsUnmapped, errorEventStats, eventStats, conflictingEventStats, metricsCount)
|
||||||
ex.Listen(events)
|
ex.Listen(events)
|
||||||
|
|
||||||
metrics, err := registerer.Gather()
|
metrics, err := reg.Gather()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Cannot gather from DefaultGatherer: %v", err)
|
t.Fatalf("Cannot gather from DefaultGatherer: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -166,13 +166,9 @@ func (r *Registry) GetCounter(metricName string, labels prometheus.Labels, help
|
||||||
return nil, fmt.Errorf("metric with name %s is already registered", metricName)
|
return nil, fmt.Errorf("metric with name %s is already registered", metricName)
|
||||||
}
|
}
|
||||||
|
|
||||||
histogramSuffixes := []string{"_bucket", "_count", "_sum"}
|
err := r.checkHistogramNameCollision(metricName)
|
||||||
for _, suffix := range histogramSuffixes {
|
if err != nil {
|
||||||
if strings.HasSuffix(metricName, suffix) {
|
return nil, err
|
||||||
if r.MetricConflicts(strings.TrimSuffix(metricName, suffix), metrics.CounterMetricType) {
|
|
||||||
return nil, fmt.Errorf("metric with name %s is already registered", metricName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var counterVec *prometheus.CounterVec
|
var counterVec *prometheus.CounterVec
|
||||||
|
@ -191,7 +187,6 @@ func (r *Registry) GetCounter(metricName string, labels prometheus.Labels, help
|
||||||
}
|
}
|
||||||
|
|
||||||
var counter prometheus.Counter
|
var counter prometheus.Counter
|
||||||
var err error
|
|
||||||
if counter, err = counterVec.GetMetricWith(labels); err != nil {
|
if counter, err = counterVec.GetMetricWith(labels); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -200,6 +195,18 @@ func (r *Registry) GetCounter(metricName string, labels prometheus.Labels, help
|
||||||
return counter, nil
|
return counter, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Registry) checkHistogramNameCollision(metricName string) error {
|
||||||
|
histogramSuffixes := []string{"_bucket", "_count", "_sum"}
|
||||||
|
for _, suffix := range histogramSuffixes {
|
||||||
|
if strings.HasSuffix(metricName, suffix) {
|
||||||
|
if r.MetricConflicts(strings.TrimSuffix(metricName, suffix), metrics.CounterMetricType) {
|
||||||
|
return fmt.Errorf("metric with name %s is already registered", metricName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Registry) GetGauge(metricName string, labels prometheus.Labels, help string, mapping *mapper.MetricMapping, metricsCount *prometheus.GaugeVec) (prometheus.Gauge, error) {
|
func (r *Registry) GetGauge(metricName string, labels prometheus.Labels, help string, mapping *mapper.MetricMapping, metricsCount *prometheus.GaugeVec) (prometheus.Gauge, error) {
|
||||||
hash, labelNames := r.HashLabels(labels)
|
hash, labelNames := r.HashLabels(labels)
|
||||||
vh, mh := r.Get(metricName, hash, metrics.GaugeMetricType)
|
vh, mh := r.Get(metricName, hash, metrics.GaugeMetricType)
|
||||||
|
@ -211,6 +218,11 @@ func (r *Registry) GetGauge(metricName string, labels prometheus.Labels, help st
|
||||||
return nil, fmt.Errorf("metrics.Metric with name %s is already registered", metricName)
|
return nil, fmt.Errorf("metrics.Metric with name %s is already registered", metricName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err := r.checkHistogramNameCollision(metricName)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("metrics.Metric with name %s conflicts with previously registered histogram", metricName)
|
||||||
|
}
|
||||||
|
|
||||||
var gaugeVec *prometheus.GaugeVec
|
var gaugeVec *prometheus.GaugeVec
|
||||||
if vh == nil {
|
if vh == nil {
|
||||||
metricsCount.WithLabelValues("gauge").Inc()
|
metricsCount.WithLabelValues("gauge").Inc()
|
||||||
|
@ -227,7 +239,6 @@ func (r *Registry) GetGauge(metricName string, labels prometheus.Labels, help st
|
||||||
}
|
}
|
||||||
|
|
||||||
var gauge prometheus.Gauge
|
var gauge prometheus.Gauge
|
||||||
var err error
|
|
||||||
if gauge, err = gaugeVec.GetMetricWith(labels); err != nil {
|
if gauge, err = gaugeVec.GetMetricWith(labels); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue