Skip to content

Commit 9e1101f

Browse files
committed
Merge pull request google#810 from anushree-n/modifyConfig
Modify Collector structure
2 parents 87dcb70 + 1083213 commit 9e1101f

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-6
lines changed

collector/generic_collector.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ package collector
1616

1717
import (
1818
"encoding/json"
19+
"fmt"
1920
"io/ioutil"
21+
"regexp"
2022
"time"
2123

2224
"github.com/google/cadvisor/info/v1"
@@ -28,6 +30,17 @@ type GenericCollector struct {
2830

2931
//holds information extracted from the config file for a collector
3032
configFile Config
33+
34+
//holds information necessary to extract metrics
35+
info *collectorInfo
36+
}
37+
38+
type collectorInfo struct {
39+
//minimum polling frequency among all metrics
40+
minPollingFrequency time.Duration
41+
42+
//regular expresssions for all metrics
43+
regexps []*regexp.Regexp
3144
}
3245

3346
//Returns a new collector using the information extracted from the configfile
@@ -45,8 +58,30 @@ func NewCollector(collectorName string, configfile string) (*GenericCollector, e
4558

4659
//TODO : Add checks for validity of config file (eg : Accurate JSON fields)
4760

61+
if len(configInJSON.MetricsConfig) == 0 {
62+
return nil, fmt.Errorf("No metrics provided in config")
63+
}
64+
65+
minPollFrequency := configInJSON.MetricsConfig[0].PollingFrequency
66+
regexprs := make([]*regexp.Regexp, len(configInJSON.MetricsConfig))
67+
68+
for ind, metricConfig := range configInJSON.MetricsConfig {
69+
if metricConfig.PollingFrequency < minPollFrequency {
70+
minPollFrequency = metricConfig.PollingFrequency
71+
}
72+
73+
regexprs[ind], err = regexp.Compile(metricConfig.Regex)
74+
if err != nil {
75+
return nil, fmt.Errorf("Invalid regexp %v for metric %v", metricConfig.Regex, metricConfig.Name)
76+
}
77+
}
78+
4879
return &GenericCollector{
49-
name: collectorName, configFile: configInJSON,
80+
name: collectorName,
81+
configFile: configInJSON,
82+
info: &collectorInfo{
83+
minPollingFrequency: minPollFrequency,
84+
regexps: regexprs},
5085
}, nil
5186
}
5287

collector/generic_collector_test.go

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,39 @@ import (
2222
"github.com/stretchr/testify/assert"
2323
)
2424

25+
func TestEmptyConfig(t *testing.T) {
26+
assert := assert.New(t)
27+
28+
emptyConfig := `
29+
{
30+
"endpoint" : "http://localhost:8000/nginx_status",
31+
"metrics_config" : [
32+
]
33+
}
34+
`
35+
36+
//Create a temporary config file 'temp.json' with invalid json format
37+
assert.NoError(ioutil.WriteFile("temp.json", []byte(emptyConfig), 0777))
38+
39+
_, err := NewCollector("tempCollector", "temp.json")
40+
assert.Error(err)
41+
42+
assert.NoError(os.Remove("temp.json"))
43+
}
44+
2545
func TestConfigWithErrors(t *testing.T) {
2646
assert := assert.New(t)
2747

28-
//Syntax error: Missed '"' after active connections
48+
//Syntax error: Missed '"' after activeConnections
2949
invalid := `
3050
{
31-
"endpoint" : "host:port/nginx_status",
32-
"metricsConfig" : [
51+
"endpoint" : "http://localhost:8000/nginx_status",
52+
"metrics_config" : [
3353
{
3454
"name" : "activeConnections,
35-
"metricType" : "gauge",
55+
"metric_type" : "gauge",
3656
"units" : "integer",
37-
"pollingFrequency" : "10s",
57+
"polling_frequency" : 10,
3858
"regex" : "Active connections: ([0-9]+)"
3959
}
4060
]
@@ -50,6 +70,41 @@ func TestConfigWithErrors(t *testing.T) {
5070
assert.NoError(os.Remove("temp.json"))
5171
}
5272

73+
func TestConfigWithRegexErrors(t *testing.T) {
74+
assert := assert.New(t)
75+
76+
//Error: Missed operand for '+' in activeConnections regex
77+
invalid := `
78+
{
79+
"endpoint" : "host:port/nginx_status",
80+
"metrics_config" : [
81+
{
82+
"name" : "activeConnections",
83+
"metric_type" : "gauge",
84+
"units" : "integer",
85+
"polling_frequency" : 10,
86+
"regex" : "Active connections: (+)"
87+
},
88+
{
89+
"name" : "reading",
90+
"metric_type" : "gauge",
91+
"units" : "integer",
92+
"polling_frequency" : 10,
93+
"regex" : "Reading: ([0-9]+) .*"
94+
}
95+
]
96+
}
97+
`
98+
99+
//Create a temporary config file 'temp.json'
100+
assert.NoError(ioutil.WriteFile("temp.json", []byte(invalid), 0777))
101+
102+
_, err := NewCollector("tempCollector", "temp.json")
103+
assert.Error(err)
104+
105+
assert.NoError(os.Remove("temp.json"))
106+
}
107+
53108
func TestConfig(t *testing.T) {
54109
assert := assert.New(t)
55110

0 commit comments

Comments
 (0)