Skip to content

Commit

Permalink
Parallelize getting metric descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
frodenas committed Jun 3, 2017
1 parent 54bda7f commit 6d10015
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions collectors/monitoring_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,34 @@ func (c *MonitoringCollector) reportMonitoringMetrics(ch chan<- prometheus.Metri
return nil
}

var wg = &sync.WaitGroup{}

doneChannel := make(chan bool, 1)
errChannel := make(chan error, 1)

for _, metricsTypePrefix := range c.metricsTypePrefixes {
log.Debugf("Listing Google Stackdriver Monitoring metric descriptors starting with `%s`...", metricsTypePrefix)
ctx := context.Background()
if err := c.monitoringService.Projects.MetricDescriptors.List(utils.ProjectResource(c.projectID)).
Filter(fmt.Sprintf("metric.type = starts_with(\"%s\")", metricsTypePrefix)).
Pages(ctx, metricDescriptorsFunction); err != nil {
return err
}
wg.Add(1)
go func(metricsTypePrefix string) {
defer wg.Done()
log.Debugf("Listing Google Stackdriver Monitoring metric descriptors starting with `%s`...", metricsTypePrefix)
ctx := context.Background()
if err := c.monitoringService.Projects.MetricDescriptors.List(utils.ProjectResource(c.projectID)).
Filter(fmt.Sprintf("metric.type = starts_with(\"%s\")", metricsTypePrefix)).
Pages(ctx, metricDescriptorsFunction); err != nil {
errChannel <- err
}
}(metricsTypePrefix)
}

go func() {
wg.Wait()
close(doneChannel)
}()

select {
case <-doneChannel:
case err := <-errChannel:
return err
}

return nil
Expand Down

0 comments on commit 6d10015

Please sign in to comment.