mirror of
https://github.com/prometheus/statsd_exporter.git
synced 2024-12-23 14:00:30 +00:00
Adding metric testing on relay
Signed-off-by: Pedro Tanaka <pedro.tanaka@shopify.com>
This commit is contained in:
parent
78dcd3b7b2
commit
60742e1bd6
1 changed files with 90 additions and 1 deletions
|
@ -1,7 +1,10 @@
|
||||||
package relay
|
package relay
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/go-kit/log"
|
"github.com/go-kit/log"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
dto "github.com/prometheus/client_model/go"
|
||||||
"github.com/prometheus/statsd_exporter/pkg/clock"
|
"github.com/prometheus/statsd_exporter/pkg/clock"
|
||||||
"github.com/stvp/go-udp-testing"
|
"github.com/stvp/go-udp-testing"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -51,11 +54,97 @@ func TestRelay_RelayLine(t *testing.T) {
|
||||||
udp.ShouldReceive(t, tt.args.expected, func() {
|
udp.ShouldReceive(t, tt.args.expected, func() {
|
||||||
for _, line := range tt.args.lines {
|
for _, line := range tt.args.lines {
|
||||||
r.RelayLine(line)
|
r.RelayLine(line)
|
||||||
// Tick time forward to trigger a flush
|
|
||||||
}
|
}
|
||||||
|
// Tick time forward to trigger a packet send.
|
||||||
clock.ClockInstance.Instant = time.Unix(20000, 0)
|
clock.ClockInstance.Instant = time.Unix(20000, 0)
|
||||||
clock.ClockInstance.TickerCh <- time.Unix(20000, 0)
|
clock.ClockInstance.TickerCh <- time.Unix(20000, 0)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
metrics, err := prometheus.DefaultGatherer.Gather()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Cannot gather from DefaultGatherer: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
metricNames := map[string]float64{
|
||||||
|
"statsd_exporter_relay_long_lines_total": 0,
|
||||||
|
}
|
||||||
|
for metricName, expectedValue := range metricNames {
|
||||||
|
firstMetric := getFloat64(metrics, metricName, prometheus.Labels{"target": "localhost:1160"})
|
||||||
|
|
||||||
|
if firstMetric == nil {
|
||||||
|
t.Fatalf("Could not find time series with first label set for metric: %s", metricName)
|
||||||
|
}
|
||||||
|
if *firstMetric != expectedValue {
|
||||||
|
t.Errorf("Expected metric %s to be %f, got %f", metricName, expectedValue, *firstMetric)
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getFloat64 search for metric by name in array of MetricFamily and then search a value by labels.
|
||||||
|
// Method returns a value or nil if metric is not found.
|
||||||
|
func getFloat64(metrics []*dto.MetricFamily, name string, labels prometheus.Labels) *float64 {
|
||||||
|
var metricFamily *dto.MetricFamily
|
||||||
|
for _, m := range metrics {
|
||||||
|
if *m.Name == name {
|
||||||
|
metricFamily = m
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if metricFamily == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var metric *dto.Metric
|
||||||
|
labelStr := fmt.Sprintf("%v", labels)
|
||||||
|
for _, m := range metricFamily.Metric {
|
||||||
|
l := labelPairsAsLabels(m.GetLabel())
|
||||||
|
ls := fmt.Sprintf("%v", l)
|
||||||
|
if labelStr == ls {
|
||||||
|
metric = m
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if metric == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var value float64
|
||||||
|
if metric.Gauge != nil {
|
||||||
|
value = metric.Gauge.GetValue()
|
||||||
|
return &value
|
||||||
|
}
|
||||||
|
if metric.Counter != nil {
|
||||||
|
value = metric.Counter.GetValue()
|
||||||
|
return &value
|
||||||
|
}
|
||||||
|
if metric.Histogram != nil {
|
||||||
|
value = metric.Histogram.GetSampleSum()
|
||||||
|
return &value
|
||||||
|
}
|
||||||
|
if metric.Summary != nil {
|
||||||
|
value = metric.Summary.GetSampleSum()
|
||||||
|
return &value
|
||||||
|
}
|
||||||
|
if metric.Untyped != nil {
|
||||||
|
value = metric.Untyped.GetValue()
|
||||||
|
return &value
|
||||||
|
}
|
||||||
|
panic(fmt.Errorf("collected a non-gauge/counter/histogram/summary/untyped metric: %s", metric))
|
||||||
|
}
|
||||||
|
|
||||||
|
func labelPairsAsLabels(pairs []*dto.LabelPair) (labels prometheus.Labels) {
|
||||||
|
labels = prometheus.Labels{}
|
||||||
|
for _, pair := range pairs {
|
||||||
|
if pair.Name == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
value := ""
|
||||||
|
if pair.Value != nil {
|
||||||
|
value = *pair.Value
|
||||||
|
}
|
||||||
|
labels[*pair.Name] = value
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue