Skip to content

Commit

Permalink
Merge pull request google#839 from rjnagal/docker
Browse files Browse the repository at this point in the history
Add units and data type to spec for custom metrics.
  • Loading branch information
vmarmol committed Jul 30, 2015
2 parents 843fc13 + dd0d0dd commit 78419de
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 13 deletions.
6 changes: 5 additions & 1 deletion collector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ type MetricConfig struct {
//enum type for the 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"`

//data type of the metric (eg: int, float)
DataType v1.DataType `json:"data_type"`

//the frequency at which the metric should be collected
PollingFrequency time.Duration `json:"polling_frequency"`

Expand Down
12 changes: 8 additions & 4 deletions collector/config/sample_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
"metrics_config" : [
{ "name" : "activeConnections",
"metric_type" : "gauge",
"units" : "integer",
"units" : "number of active connections",
"data_type" : "int",
"polling_frequency" : 10,
"regex" : "Active connections: ([0-9]+)"
},
{ "name" : "reading",
"metric_type" : "gauge",
"units" : "integer",
"units" : "number of reading connections",
"data_type" : "int",
"polling_frequency" : 10,
"regex" : "Reading: ([0-9]+) .*"
},
{ "name" : "writing",
"metric_type" : "gauge",
"units" : "integer",
"data_type" : "int",
"units" : "number of writing connections",
"polling_frequency" : 10,
"regex" : ".*Writing: ([0-9]+).*"
},
{ "name" : "waiting",
"metric_type" : "gauge",
"units" : "integer",
"units" : "number of waiting connections",
"data_type" : "int",
"polling_frequency" : 10,
"regex" : ".*Waiting: ([0-9]+)"
}
Expand Down
12 changes: 7 additions & 5 deletions collector/generic_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ func (collector *GenericCollector) Name() string {

func (collector *GenericCollector) configToSpec(config MetricConfig) v1.MetricSpec {
return v1.MetricSpec{
Name: config.Name,
Type: config.MetricType,
Name: config.Name,
Type: config.MetricType,
Format: config.DataType,
Units: config.Units,
}
}

Expand Down Expand Up @@ -135,15 +137,15 @@ func (collector *GenericCollector) Collect(metrics map[string]v1.MetricVal) (tim
for ind, metricConfig := range collector.configFile.MetricsConfig {
matchString := collector.info.regexps[ind].FindStringSubmatch(string(pageContent))
if matchString != nil {
if metricConfig.Units == "float" {
if metricConfig.DataType == v1.FloatType {
regVal, err := strconv.ParseFloat(strings.TrimSpace(matchString[1]), 64)
if err != nil {
errorSlice = append(errorSlice, err)
}
metrics[metricConfig.Name] = v1.MetricVal{
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)
if err != nil {
errorSlice = append(errorSlice, err)
Expand All @@ -153,7 +155,7 @@ func (collector *GenericCollector) Collect(metrics map[string]v1.MetricVal) (tim
}

} 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 {
errorSlice = append(errorSlice, fmt.Errorf("No match found for regexp: %v for metric '%v' in config", metricConfig.Regex, metricConfig.Name))
Expand Down
6 changes: 3 additions & 3 deletions collector/generic_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestConfigWithErrors(t *testing.T) {
{
"name" : "activeConnections,
"metric_type" : "gauge",
"units" : "integer",
"data_type" : "int",
"polling_frequency" : 10,
"regex" : "Active connections: ([0-9]+)"
}
Expand Down Expand Up @@ -90,14 +90,14 @@ func TestConfigWithRegexErrors(t *testing.T) {
{
"name" : "activeConnections",
"metric_type" : "gauge",
"units" : "integer",
"data_type" : "int",
"polling_frequency" : 10,
"regex" : "Active connections: (+)"
},
{
"name" : "reading",
"metric_type" : "gauge",
"units" : "integer",
"data_type" : "int",
"polling_frequency" : 10,
"regex" : "Reading: ([0-9]+) .*"
}
Expand Down
14 changes: 14 additions & 0 deletions info/v1/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,27 @@ const (
MetricDelta = "delta"
)

// DataType for metric being exported.
type DataType string

const (
IntType DataType = "int"
FloatType = "float"
)

// Spec for custom metric.
type MetricSpec struct {
// The name of the metric.
Name string `json:"name"`

// Type of the metric.
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.
Expand Down

0 comments on commit 78419de

Please sign in to comment.