@@ -18,7 +18,10 @@ import (
18
18
"encoding/json"
19
19
"fmt"
20
20
"io/ioutil"
21
+ "net/http"
21
22
"regexp"
23
+ "strconv"
24
+ "strings"
22
25
"time"
23
26
24
27
"github.com/google/cadvisor/info/v1"
@@ -92,6 +95,70 @@ func (collector *GenericCollector) Name() string {
92
95
93
96
//Returns collected metrics and the next collection time of the collector
94
97
func (collector * GenericCollector ) Collect () (time.Time , []v1.Metric , error ) {
95
- //TO BE IMPLEMENTED
96
- return time .Now (), nil , nil
98
+ minNextColTime := collector .configFile .MetricsConfig [0 ].PollingFrequency
99
+ for _ , metricConfig := range collector .configFile .MetricsConfig {
100
+ if metricConfig .PollingFrequency < minNextColTime {
101
+ minNextColTime = metricConfig .PollingFrequency
102
+ }
103
+ }
104
+ currentTime := time .Now ()
105
+ nextCollectionTime := currentTime .Add (time .Duration (minNextColTime * time .Second ))
106
+
107
+ uri := collector .configFile .Endpoint
108
+ response , err := http .Get (uri )
109
+ if err != nil {
110
+ return nextCollectionTime , nil , err
111
+ }
112
+
113
+ defer response .Body .Close ()
114
+
115
+ pageContent , err := ioutil .ReadAll (response .Body )
116
+ if err != nil {
117
+ return nextCollectionTime , nil , err
118
+ }
119
+
120
+ metrics := make ([]v1.Metric , len (collector .configFile .MetricsConfig ))
121
+ var errorSlice []error
122
+
123
+ for ind , metricConfig := range collector .configFile .MetricsConfig {
124
+ regex , err := regexp .Compile (metricConfig .Regex )
125
+ if err != nil {
126
+ return nextCollectionTime , nil , err
127
+ }
128
+
129
+ matchString := regex .FindStringSubmatch (string (pageContent ))
130
+ if matchString != nil {
131
+ if metricConfig .Units == "float" {
132
+ regVal , err := strconv .ParseFloat (strings .TrimSpace (matchString [1 ]), 64 )
133
+ if err != nil {
134
+ errorSlice = append (errorSlice , err )
135
+ }
136
+ metrics [ind ].FloatPoints = []v1.FloatPoint {
137
+ {Value : regVal , Timestamp : currentTime },
138
+ }
139
+ } else if metricConfig .Units == "integer" || metricConfig .Units == "int" {
140
+ regVal , err := strconv .ParseInt (strings .TrimSpace (matchString [1 ]), 10 , 64 )
141
+ if err != nil {
142
+ errorSlice = append (errorSlice , err )
143
+ }
144
+ metrics [ind ].IntPoints = []v1.IntPoint {
145
+ {Value : regVal , Timestamp : currentTime },
146
+ }
147
+
148
+ } else {
149
+ errorSlice = append (errorSlice , fmt .Errorf ("Unexpected value of 'units' for metric '%v' in config " , metricConfig .Name ))
150
+ }
151
+ } else {
152
+ errorSlice = append (errorSlice , fmt .Errorf ("No match found for regexp: %v for metric '%v' in config" , metricConfig .Regex , metricConfig .Name ))
153
+ }
154
+
155
+ metrics [ind ].Name = metricConfig .Name
156
+ if metricConfig .MetricType == "gauge" {
157
+ metrics [ind ].Type = v1 .MetricGauge
158
+ } else if metricConfig .MetricType == "counter" {
159
+ metrics [ind ].Type = v1 .MetricCumulative
160
+ }
161
+ }
162
+
163
+ return nextCollectionTime , metrics , compileErrors (errorSlice )
97
164
}
0 commit comments