@@ -25,7 +25,7 @@ import (
2525 log "github.com/sirupsen/logrus"
2626)
2727
28- var alphaRegex , _ = regexp .Compile ("[^a-zA-Z0-9]+" )
28+ var alphaRegex = regexp .MustCompile ("[^a-zA-Z0-9]+" )
2929
3030// TagDescription represents an AWS tag key value pair
3131type TagDescription struct {
@@ -120,7 +120,7 @@ func (rd *RegionDescription) BuildARN(s *string, r *string) (string, error) {
120120 return a .String (), nil
121121}
122122
123- func (rd * RegionDescription ) buildFilters () error {
123+ func (rd * RegionDescription ) saveFilters () {
124124 filters := []* ec2.Filter {}
125125 for _ , tag := range rd .Tags {
126126 f := & ec2.Filter {
@@ -130,16 +130,20 @@ func (rd *RegionDescription) buildFilters() error {
130130 filters = append (filters , f )
131131 }
132132 rd .Filters = filters
133- return nil
134133}
135134
136135func (rd * RegionDescription ) saveAccountID () error {
137136 session := iam .New (rd .Session )
138137 input := iam.GetUserInput {}
139138 user , err := session .GetUser (& input )
140- h .LogError (err )
139+ if err != nil {
140+ return err
141+ }
142+
141143 a , err := arn .Parse (* user .User .Arn )
142- h .LogError (err )
144+ if err != nil {
145+ return err
146+ }
143147 rd .AccountID = & a .AccountID
144148
145149 return nil
@@ -153,13 +157,16 @@ func (rd *RegionDescription) Init(s *session.Session, td []*TagDescription, metr
153157 rd .Tags = td
154158
155159 err := rd .saveAccountID ()
156- h .LogErrorExit (err )
160+ if err != nil {
161+ return fmt .Errorf ("error saving account id: %s" , err )
162+ }
157163
158- err = rd .buildFilters ()
159- h .LogErrorExit (err )
164+ rd .saveFilters ()
160165
161166 err = rd .CreateNamespaceDescriptions (metrics )
162- h .LogErrorExit (err )
167+ if err != nil {
168+ return fmt .Errorf ("error creating namespaces: %s" , err )
169+ }
163170
164171 return nil
165172}
@@ -196,9 +203,9 @@ func (nd *NamespaceDescription) GatherMetrics(cw *cloudwatch.CloudWatch) {
196203 for _ , md := range nd .Metrics {
197204 go func (md * MetricDescription ) {
198205 nd .Mutex .RLock ()
199- result , err := md .getData (cw , nd .Resources , nd )
206+ result , err := md .getData (cw , nd .Resources )
200207 nd .Mutex .RUnlock ()
201- h .LogError (err )
208+ h .LogIfError (err )
202209 md .saveData (result , * nd .Parent .Region )
203210 }(md )
204211 }
@@ -295,7 +302,7 @@ func (md *MetricDescription) saveData(c *cloudwatch.GetMetricDataOutput, region
295302
296303 labels , err := awsLabelsFromString (* data .Label )
297304 if err != nil {
298- h .LogError (err )
305+ h .LogIfError (err )
299306 continue
300307 }
301308
@@ -320,7 +327,7 @@ func (md *MetricDescription) saveData(c *cloudwatch.GetMetricDataOutput, region
320327 err = fmt .Errorf ("unknown statistic type: %s" , labels .statistic )
321328 }
322329 if err != nil {
323- h .LogError (err )
330+ h .LogIfError (err )
324331 continue
325332 }
326333
@@ -448,12 +455,12 @@ func (rd *RegionDescription) TagsFound(tl interface{}) bool {
448455 return false
449456}
450457
451- func (md * MetricDescription ) getData (cw * cloudwatch.CloudWatch , rds []* ResourceDescription , nd * NamespaceDescription ) (* cloudwatch.GetMetricDataOutput , error ) {
458+ func (md * MetricDescription ) getData (cw * cloudwatch.CloudWatch , rds []* ResourceDescription ) (* cloudwatch.GetMetricDataOutput , error ) {
452459 query , err := md .BuildQuery (rds )
453- if len (query ) < 1 {
460+ if len (query ) == 0 {
454461 return & cloudwatch.GetMetricDataOutput {}, nil
455462 }
456- h .LogError (err )
463+ h .LogIfError (err )
457464
458465 end := time .Now ().Round (5 * time .Minute )
459466 start := end .Add (- time .Duration (md .RangeSeconds ) * time .Second )
@@ -464,7 +471,7 @@ func (md *MetricDescription) getData(cw *cloudwatch.CloudWatch, rds []*ResourceD
464471 MetricDataQueries : query ,
465472 }
466473 result , err := cw .GetMetricData (& input )
467- h .LogError (err )
474+ h .LogIfError (err )
468475
469476 return result , err
470477}
0 commit comments