Merge pull request #1318 from thomaso-mirodin/fix-prometheus-collector
Fixes a panic when a prometheus endpoint ends with a newline
This commit is contained in:
commit
1c8d7896a5
@ -120,12 +120,18 @@ func (collector *PrometheusCollector) GetSpec() []v1.MetricSpec {
|
||||
}
|
||||
|
||||
lines := strings.Split(string(pageContent), "\n")
|
||||
lineCount := len(lines)
|
||||
for i, line := range lines {
|
||||
if strings.HasPrefix(line, "# HELP") {
|
||||
stopIndex := strings.Index(lines[i+2], "{")
|
||||
if stopIndex == -1 {
|
||||
stopIndex = strings.Index(lines[i+2], " ")
|
||||
if i+2 >= lineCount {
|
||||
break
|
||||
}
|
||||
|
||||
stopIndex := strings.IndexAny(lines[i+2], "{ ")
|
||||
if stopIndex == -1 {
|
||||
continue
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(lines[i+2][0:stopIndex])
|
||||
if _, ok := collector.metricsSet[name]; collector.metricsSet != nil && !ok {
|
||||
continue
|
||||
|
@ -24,6 +24,7 @@ import (
|
||||
"github.com/google/cadvisor/info/v1"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPrometheus(t *testing.T) {
|
||||
@ -44,13 +45,23 @@ func TestPrometheus(t *testing.T) {
|
||||
text += "go_gc_duration_seconds{quantile=\"1\"} 0.000499764\n"
|
||||
text += "# HELP go_goroutines Number of goroutines that currently exist.\n"
|
||||
text += "# TYPE go_goroutines gauge\n"
|
||||
text += "go_goroutines 16"
|
||||
text += "go_goroutines 16\n"
|
||||
text += "# HELP empty_metric A metric without any values\n"
|
||||
text += "# TYPE empty_metric counter\n"
|
||||
text += "\n"
|
||||
fmt.Fprintln(w, text)
|
||||
}))
|
||||
|
||||
defer tempServer.Close()
|
||||
|
||||
collector.configFile.Endpoint = tempServer.URL
|
||||
|
||||
var spec []v1.MetricSpec
|
||||
require.NotPanics(t, func() { spec = collector.GetSpec() })
|
||||
assert.Len(spec, 2)
|
||||
assert.Equal(spec[0].Name, "go_gc_duration_seconds")
|
||||
assert.Equal(spec[1].Name, "go_goroutines")
|
||||
|
||||
metrics := map[string][]v1.MetricVal{}
|
||||
_, metrics, errMetric := collector.Collect(metrics)
|
||||
|
||||
@ -64,6 +75,28 @@ func TestPrometheus(t *testing.T) {
|
||||
assert.Equal(goRoutines[0].FloatValue, 16)
|
||||
}
|
||||
|
||||
func TestPrometheusShortResponse(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
//Create a prometheus collector using the config file 'sample_config_prometheus.json'
|
||||
configFile, err := ioutil.ReadFile("config/sample_config_prometheus.json")
|
||||
collector, err := NewPrometheusCollector("Prometheus", configFile, 100)
|
||||
assert.NoError(err)
|
||||
assert.Equal(collector.name, "Prometheus")
|
||||
assert.Equal(collector.configFile.Endpoint, "http://localhost:8080/metrics")
|
||||
|
||||
tempServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
text := "# HELP empty_metric A metric without any values"
|
||||
fmt.Fprint(w, text)
|
||||
}))
|
||||
|
||||
defer tempServer.Close()
|
||||
|
||||
collector.configFile.Endpoint = tempServer.URL
|
||||
|
||||
assert.NotPanics(func() { collector.GetSpec() })
|
||||
}
|
||||
|
||||
func TestPrometheusMetricCountLimit(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user