Skip to content

Commit

Permalink
Merge pull request #6 from hchargois/improve-app-start
Browse files Browse the repository at this point in the history
Improve app start
  • Loading branch information
jirwin authored Nov 30, 2017
2 parents fa695f7 + 5d4b092 commit 8d1a907
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions burrow_exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,43 @@ func (be *BurrowExporter) Start(ctx context.Context) {
be.mainLoop(ctx)
}

func (be *BurrowExporter) scrape() {
start := time.Now()
log.WithField("timestamp", start.UnixNano()).Info("Scraping burrow...")
clusters, err := be.client.ListClusters()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("error listing clusters. Continuing.")
return
}

wg := sync.WaitGroup{}

for _, cluster := range clusters.Clusters {
wg.Add(1)

go func(c string) {
defer wg.Done()
be.processCluster(c)
}(cluster)
}

wg.Wait()

end := time.Now()
log.WithFields(log.Fields{
"timestamp": end.UnixNano(),
"took": end.Sub(start),
}).Info("Finished scraping burrow.")
}

func (be *BurrowExporter) mainLoop(ctx context.Context) {
timer := time.NewTicker(time.Duration(be.interval) * time.Second)

// scrape at app start without waiting for the first interval to elapse
be.scrape()

for {
select {
case <-ctx.Done():
Expand All @@ -113,29 +147,7 @@ func (be *BurrowExporter) mainLoop(ctx context.Context) {
return

case <-timer.C:
log.WithField("timestamp", time.Now().UnixNano()).Info("Scraping burrow...")
clusters, err := be.client.ListClusters()
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Error("error listing clusters. Continuing.")
continue
}

wg := sync.WaitGroup{}

for _, cluster := range clusters.Clusters {
wg.Add(1)

go func(c string) {
defer wg.Done()
be.processCluster(c)
}(cluster)
}

wg.Wait()

log.WithField("timestamp", time.Now().UnixNano()).Info("Finished scraping burrow.")
be.scrape()
}
}
}
Expand Down

0 comments on commit 8d1a907

Please sign in to comment.