Merge pull request #839 from rjnagal/docker

Add units and data type to spec for custom metrics.
This commit is contained in:
Victor Marmol 2015-07-30 10:21:51 +02:00
commit 78419de3ea
5 changed files with 37 additions and 13 deletions

View File

@ -35,9 +35,13 @@ type MetricConfig struct {
//enum type for the metric type //enum type for the metric type
MetricType v1.MetricType `json:"metric_type"` MetricType v1.MetricType `json:"metric_type"`
//data type of the metric (eg: integer, string) // metric units to display on UI and in storage (eg: MB, cores)
// this is only used for display.
Units string `json:"units"` Units string `json:"units"`
//data type of the metric (eg: int, float)
DataType v1.DataType `json:"data_type"`
//the frequency at which the metric should be collected //the frequency at which the metric should be collected
PollingFrequency time.Duration `json:"polling_frequency"` PollingFrequency time.Duration `json:"polling_frequency"`

View File

@ -3,25 +3,29 @@
"metrics_config" : [ "metrics_config" : [
{ "name" : "activeConnections", { "name" : "activeConnections",
"metric_type" : "gauge", "metric_type" : "gauge",
"units" : "integer", "units" : "number of active connections",
"data_type" : "int",
"polling_frequency" : 10, "polling_frequency" : 10,
"regex" : "Active connections: ([0-9]+)" "regex" : "Active connections: ([0-9]+)"
}, },
{ "name" : "reading", { "name" : "reading",
"metric_type" : "gauge", "metric_type" : "gauge",
"units" : "integer", "units" : "number of reading connections",
"data_type" : "int",
"polling_frequency" : 10, "polling_frequency" : 10,
"regex" : "Reading: ([0-9]+) .*" "regex" : "Reading: ([0-9]+) .*"
}, },
{ "name" : "writing", { "name" : "writing",
"metric_type" : "gauge", "metric_type" : "gauge",
"units" : "integer", "data_type" : "int",
"units" : "number of writing connections",
"polling_frequency" : 10, "polling_frequency" : 10,
"regex" : ".*Writing: ([0-9]+).*" "regex" : ".*Writing: ([0-9]+).*"
}, },
{ "name" : "waiting", { "name" : "waiting",
"metric_type" : "gauge", "metric_type" : "gauge",
"units" : "integer", "units" : "number of waiting connections",
"data_type" : "int",
"polling_frequency" : 10, "polling_frequency" : 10,
"regex" : ".*Waiting: ([0-9]+)" "regex" : ".*Waiting: ([0-9]+)"
} }

View File

@ -99,8 +99,10 @@ func (collector *GenericCollector) Name() string {
func (collector *GenericCollector) configToSpec(config MetricConfig) v1.MetricSpec { func (collector *GenericCollector) configToSpec(config MetricConfig) v1.MetricSpec {
return v1.MetricSpec{ return v1.MetricSpec{
Name: config.Name, Name: config.Name,
Type: config.MetricType, Type: config.MetricType,
Format: config.DataType,
Units: config.Units,
} }
} }
@ -135,7 +137,7 @@ func (collector *GenericCollector) Collect(metrics map[string]v1.MetricVal) (tim
for ind, metricConfig := range collector.configFile.MetricsConfig { for ind, metricConfig := range collector.configFile.MetricsConfig {
matchString := collector.info.regexps[ind].FindStringSubmatch(string(pageContent)) matchString := collector.info.regexps[ind].FindStringSubmatch(string(pageContent))
if matchString != nil { if matchString != nil {
if metricConfig.Units == "float" { if metricConfig.DataType == v1.FloatType {
regVal, err := strconv.ParseFloat(strings.TrimSpace(matchString[1]), 64) regVal, err := strconv.ParseFloat(strings.TrimSpace(matchString[1]), 64)
if err != nil { if err != nil {
errorSlice = append(errorSlice, err) errorSlice = append(errorSlice, err)
@ -143,7 +145,7 @@ func (collector *GenericCollector) Collect(metrics map[string]v1.MetricVal) (tim
metrics[metricConfig.Name] = v1.MetricVal{ metrics[metricConfig.Name] = v1.MetricVal{
FloatValue: regVal, Timestamp: currentTime, FloatValue: regVal, Timestamp: currentTime,
} }
} else if metricConfig.Units == "integer" || metricConfig.Units == "int" { } else if metricConfig.DataType == v1.IntType {
regVal, err := strconv.ParseInt(strings.TrimSpace(matchString[1]), 10, 64) regVal, err := strconv.ParseInt(strings.TrimSpace(matchString[1]), 10, 64)
if err != nil { if err != nil {
errorSlice = append(errorSlice, err) errorSlice = append(errorSlice, err)
@ -153,7 +155,7 @@ func (collector *GenericCollector) Collect(metrics map[string]v1.MetricVal) (tim
} }
} else { } else {
errorSlice = append(errorSlice, fmt.Errorf("Unexpected value of 'units' for metric '%v' in config ", metricConfig.Name)) errorSlice = append(errorSlice, fmt.Errorf("Unexpected value of 'data_type' for metric '%v' in config ", metricConfig.Name))
} }
} else { } else {
errorSlice = append(errorSlice, fmt.Errorf("No match found for regexp: %v for metric '%v' in config", metricConfig.Regex, metricConfig.Name)) errorSlice = append(errorSlice, fmt.Errorf("No match found for regexp: %v for metric '%v' in config", metricConfig.Regex, metricConfig.Name))

View File

@ -60,7 +60,7 @@ func TestConfigWithErrors(t *testing.T) {
{ {
"name" : "activeConnections, "name" : "activeConnections,
"metric_type" : "gauge", "metric_type" : "gauge",
"units" : "integer", "data_type" : "int",
"polling_frequency" : 10, "polling_frequency" : 10,
"regex" : "Active connections: ([0-9]+)" "regex" : "Active connections: ([0-9]+)"
} }
@ -90,14 +90,14 @@ func TestConfigWithRegexErrors(t *testing.T) {
{ {
"name" : "activeConnections", "name" : "activeConnections",
"metric_type" : "gauge", "metric_type" : "gauge",
"units" : "integer", "data_type" : "int",
"polling_frequency" : 10, "polling_frequency" : 10,
"regex" : "Active connections: (+)" "regex" : "Active connections: (+)"
}, },
{ {
"name" : "reading", "name" : "reading",
"metric_type" : "gauge", "metric_type" : "gauge",
"units" : "integer", "data_type" : "int",
"polling_frequency" : 10, "polling_frequency" : 10,
"regex" : "Reading: ([0-9]+) .*" "regex" : "Reading: ([0-9]+) .*"
} }

View File

@ -32,6 +32,14 @@ const (
MetricDelta = "delta" MetricDelta = "delta"
) )
// DataType for metric being exported.
type DataType string
const (
IntType DataType = "int"
FloatType = "float"
)
// Spec for custom metric. // Spec for custom metric.
type MetricSpec struct { type MetricSpec struct {
// The name of the metric. // The name of the metric.
@ -39,6 +47,12 @@ type MetricSpec struct {
// Type of the metric. // Type of the metric.
Type MetricType `json:"type"` Type MetricType `json:"type"`
// Data Type for the stats.
Format DataType `json:"format"`
// Display Units for the stats.
Units string `json:"units"`
} }
// An exported metric. // An exported metric.