Skip to content

Collection of custom metrics #802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions collector/generic_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"time"

"github.com/google/cadvisor/info/v1"
Expand Down Expand Up @@ -92,6 +95,70 @@ func (collector *GenericCollector) Name() string {

//Returns collected metrics and the next collection time of the collector
func (collector *GenericCollector) Collect() (time.Time, []v1.Metric, error) {
//TO BE IMPLEMENTED
return time.Now(), nil, nil
minNextColTime := collector.configFile.MetricsConfig[0].PollingFrequency
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: minPollingFrequency?

for _, metricConfig := range collector.configFile.MetricsConfig {
if metricConfig.PollingFrequency < minNextColTime {
minNextColTime = metricConfig.PollingFrequency
}
}
currentTime := time.Now()
nextCollectionTime := currentTime.Add(time.Duration(minNextColTime * time.Second))

uri := collector.configFile.Endpoint
response, err := http.Get(uri)
if err != nil {
return nextCollectionTime, nil, err
}

defer response.Body.Close()

pageContent, err := ioutil.ReadAll(response.Body)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to close the body:

defer response.Body.Close()

if err != nil {
return nextCollectionTime, nil, err
}

metrics := make([]v1.Metric, len(collector.configFile.MetricsConfig))
var errorSlice []error

for ind, metricConfig := range collector.configFile.MetricsConfig {
regex, err := regexp.Compile(metricConfig.Regex)
if err != nil {
return nextCollectionTime, nil, err
}

matchString := regex.FindStringSubmatch(string(pageContent))
if matchString != nil {
if metricConfig.Units == "float" {
regVal, err := strconv.ParseFloat(strings.TrimSpace(matchString[1]), 64)
if err != nil {
errorSlice = append(errorSlice, err)
}
metrics[ind].FloatPoints = []v1.FloatPoint{
{Value: regVal, Timestamp: currentTime},
}
} else if metricConfig.Units == "integer" || metricConfig.Units == "int" {
regVal, err := strconv.ParseInt(strings.TrimSpace(matchString[1]), 10, 64)
if err != nil {
errorSlice = append(errorSlice, err)
}
metrics[ind].IntPoints = []v1.IntPoint{
{Value: regVal, Timestamp: currentTime},
}

} else {
errorSlice = append(errorSlice, fmt.Errorf("Unexpected value of 'units' for metric '%v' in config ", metricConfig.Name))
}
} else {
errorSlice = append(errorSlice, fmt.Errorf("No match found for regexp: %v for metric '%v' in config", metricConfig.Regex, metricConfig.Name))
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be skipped if we want to continue collection of other valid metrics.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably keep a slice of errors that we then compile together. We try to get all the metrics we can and only return errors at the end. We can use:

https://github.com/GoogleCloudPlatform/kubernetes/blob/master/pkg/util/errors/errors.go#L31


metrics[ind].Name = metricConfig.Name
if metricConfig.MetricType == "gauge" {
metrics[ind].Type = v1.MetricGauge
} else if metricConfig.MetricType == "counter" {
metrics[ind].Type = v1.MetricCumulative
}
}

return nextCollectionTime, metrics, compileErrors(errorSlice)
}
31 changes: 31 additions & 0 deletions collector/generic_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
package collector

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/google/cadvisor/info/v1"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -115,3 +119,30 @@ func TestConfig(t *testing.T) {
assert.Equal(collector.configFile.Endpoint, "http://localhost:8000/nginx_status")
assert.Equal(collector.configFile.MetricsConfig[0].Name, "activeConnections")
}

func TestMetricCollection(t *testing.T) {
assert := assert.New(t)

//Collect nginx metrics from a fake nginx endpoint
fakeCollector, err := NewCollector("nginx", "config/sample_config.json")
assert.NoError(err)

tempServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Active connections: 3\nserver accepts handled requests")
fmt.Fprintln(w, "5 5 32\nReading: 0 Writing: 1 Waiting: 2")
}))
defer tempServer.Close()
fakeCollector.configFile.Endpoint = tempServer.URL

_, metrics, errMetric := fakeCollector.Collect()
assert.NoError(errMetric)
assert.Equal(metrics[0].Name, "activeConnections")
assert.Equal(metrics[0].Type, v1.MetricGauge)
assert.Nil(metrics[0].FloatPoints)
assert.Equal(metrics[1].Name, "reading")
assert.Equal(metrics[2].Name, "writing")
assert.Equal(metrics[3].Name, "waiting")

//Assert: Number of active connections = Number of connections reading + Number of connections writing + Number of connections waiting
assert.Equal(metrics[0].IntPoints[0].Value, (metrics[1].IntPoints[0].Value)+(metrics[2].IntPoints[0].Value)+(metrics[3].IntPoints[0].Value))
}