mirror of
https://github.com/prometheus/statsd_exporter.git
synced 2024-11-26 17:21:01 +00:00
Add additional tests
This adds two tests. The first test is to validate that two successive counter events will increment a counter. This is more about ensuring we can look up the same metric twice in a row than checking increment functionality. The second test verifies that the hashLabels function returns different results when labels are changed. Signed-off-by: Clayton O'Neill <claytono@github.com>
This commit is contained in:
parent
f6c0dd965b
commit
680eb0e826
1 changed files with 76 additions and 0 deletions
|
@ -591,6 +591,51 @@ func TestHistogramUnits(t *testing.T) {
|
|||
t.Fatalf("Received unexpected value for histogram observation %f != .300", *value)
|
||||
}
|
||||
}
|
||||
func TestCounterIncrement(t *testing.T) {
|
||||
// Start exporter with a synchronous channel
|
||||
events := make(chan Events)
|
||||
go func() {
|
||||
testMapper := mapper.MetricMapper{}
|
||||
testMapper.InitCache(0)
|
||||
ex := NewExporter(&testMapper)
|
||||
ex.Listen(events)
|
||||
}()
|
||||
|
||||
// Synchronously send a statsd event to wait for handleEvent execution.
|
||||
// Then close events channel to stop a listener.
|
||||
name := "foo_counter"
|
||||
labels := map[string]string{
|
||||
"foo": "bar",
|
||||
}
|
||||
c := Events{
|
||||
&CounterEvent{
|
||||
metricName: name,
|
||||
value: 1,
|
||||
labels: labels,
|
||||
},
|
||||
&CounterEvent{
|
||||
metricName: name,
|
||||
value: 1,
|
||||
labels: labels,
|
||||
},
|
||||
}
|
||||
events <- c
|
||||
events <- Events{}
|
||||
close(events)
|
||||
|
||||
// Check histogram value
|
||||
metrics, err := prometheus.DefaultGatherer.Gather()
|
||||
if err != nil {
|
||||
t.Fatalf("Cannot gather from DefaultGatherer: %v", err)
|
||||
}
|
||||
value := getFloat64(metrics, name, labels)
|
||||
if value == nil {
|
||||
t.Fatal("Counter value should not be nil")
|
||||
}
|
||||
if *value != 2 {
|
||||
t.Fatalf("Counter wasn't incremented properly")
|
||||
}
|
||||
}
|
||||
|
||||
type statsDPacketHandler interface {
|
||||
handlePacket(packet []byte, e chan<- Events)
|
||||
|
@ -764,6 +809,37 @@ mappings:
|
|||
}
|
||||
}
|
||||
|
||||
func TestHashLabelNames(t *testing.T) {
|
||||
r := newRegistry(nil)
|
||||
// Validate value hash changes and name has doesn't when just the value changes.
|
||||
hash1, _ := r.hashLabels(map[string]string{
|
||||
"label": "value1",
|
||||
})
|
||||
hash2, _ := r.hashLabels(map[string]string{
|
||||
"label": "value2",
|
||||
})
|
||||
if hash1.names != hash2.names {
|
||||
t.Fatal("Hash of label names should match, but doesn't")
|
||||
}
|
||||
if hash1.values == hash2.values {
|
||||
t.Fatal("Hash of label names shouldn't match, but do")
|
||||
}
|
||||
|
||||
// Validate value and name hashes change when the name changes.
|
||||
hash1, _ = r.hashLabels(map[string]string{
|
||||
"label1": "value",
|
||||
})
|
||||
hash2, _ = r.hashLabels(map[string]string{
|
||||
"label2": "value",
|
||||
})
|
||||
if hash1.names == hash2.names {
|
||||
t.Fatal("Hash of label names shouldn't match, but do")
|
||||
}
|
||||
if hash1.values == hash2.values {
|
||||
t.Fatal("Hash of label names shouldn't match, but do")
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
|
Loading…
Reference in a new issue