Skip to content

Commit

Permalink
Fixes a panic when a prometheus endpoint ends prematurely or with an …
Browse files Browse the repository at this point in the history
…empty line
  • Loading branch information
Thomas Desrosiers committed Jun 3, 2016
1 parent 49f3d7e commit 960df35
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
10 changes: 8 additions & 2 deletions collector/prometheus_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 i+2 >= lineCount {
break
}

stopIndex := strings.IndexAny(lines[i+2], "{ ")
if stopIndex == -1 {
stopIndex = strings.Index(lines[i+2], " ")
continue
}

name := strings.TrimSpace(lines[i+2][0:stopIndex])
if _, ok := collector.metricsSet[name]; collector.metricsSet != nil && !ok {
continue
Expand Down
35 changes: 34 additions & 1 deletion collector/prometheus_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 960df35

Please sign in to comment.