2015-06-20 12:38:01 +00:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-20 20:19:03 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-06-20 12:38:01 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
const sampleTextFormat = `# HELP go_gc_duration_seconds A summary of the GC invocation durations.
|
|
|
|
# TYPE go_gc_duration_seconds summary
|
|
|
|
go_gc_duration_seconds{quantile="0"} 0.00010425500000000001
|
|
|
|
go_gc_duration_seconds{quantile="0.25"} 0.000139108
|
|
|
|
go_gc_duration_seconds{quantile="0.5"} 0.00015749400000000002
|
|
|
|
go_gc_duration_seconds{quantile="0.75"} 0.000331463
|
|
|
|
go_gc_duration_seconds{quantile="1"} 0.000667154
|
|
|
|
go_gc_duration_seconds_sum 0.0018183950000000002
|
|
|
|
go_gc_duration_seconds_count 7
|
|
|
|
# HELP go_goroutines Number of goroutines that currently exist.
|
|
|
|
# TYPE go_goroutines gauge
|
|
|
|
go_goroutines 15
|
|
|
|
`
|
|
|
|
|
|
|
|
func TestPrometheusGeneratesMetrics(t *testing.T) {
|
2015-08-04 22:09:59 +00:00
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, sampleTextFormat)
|
|
|
|
}))
|
2015-06-20 12:38:01 +00:00
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
p := &Prometheus{
|
|
|
|
Urls: []string{ts.URL},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
err := p.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := []struct {
|
|
|
|
name string
|
|
|
|
value float64
|
2015-08-04 22:09:59 +00:00
|
|
|
tags map[string]string
|
2015-06-20 12:38:01 +00:00
|
|
|
}{
|
2016-01-07 00:56:30 +00:00
|
|
|
{"prometheus_go_gc_duration_seconds_count", 7, map[string]string{}},
|
|
|
|
{"prometheus_go_goroutines", 15, map[string]string{}},
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range expected {
|
2016-01-07 00:56:30 +00:00
|
|
|
assert.True(t, acc.HasFloatField(e.name, "value"))
|
2015-06-20 12:38:01 +00:00
|
|
|
}
|
|
|
|
}
|