-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,10 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/google/cadvisor/info/v1" | ||
|
@@ -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 | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to close the body:
|
||
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)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: minPollingFrequency?