Skip to content

Commit 9962d4e

Browse files
committed
Fix concurrency problems, for real this time
1 parent c8d8acd commit 9962d4e

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

nomad-exporter.go

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ func main() {
296296
noEvalMetricsEnabled = flag.Bool("no-eval-metrics", false, "disable eval metrics collection")
297297
noDeploymentMetricsEnabled = flag.Bool("no-deployment-metrics", false, "disable deployment metrics collection")
298298
noAllocationStatsMetricsEnabled = flag.Bool("no-allocation-stats-metrics", false, "disable stats metrics collection")
299+
concurrency = flag.Int("concurrency", 20, "max number of goroutines to launch concurrently when poking the API")
299300
)
300301
flag.Parse()
301302

@@ -342,6 +343,7 @@ func main() {
342343
EvalMetricsEnabled: !*noEvalMetricsEnabled,
343344
DeploymentMetricsEnabled: !*noDeploymentMetricsEnabled,
344345
AllocationStatsMetricsEnabled: !*noAllocationStatsMetricsEnabled,
346+
Concurrency: *concurrency,
345347
}
346348

347349
logrus.Debugf("Created exporter %#v", *exporter)
@@ -385,6 +387,7 @@ type Exporter struct {
385387
EvalMetricsEnabled bool
386388
DeploymentMetricsEnabled bool
387389
AllocationStatsMetricsEnabled bool
390+
Concurrency int
388391
}
389392

390393
func (e *Exporter) shouldReadMetrics() bool {
@@ -560,47 +563,47 @@ func (e *Exporter) collectNodes(ch chan<- prometheus.Metric) error {
560563
logrus.Debugf("I've the nodes list with %d nodes", len(nodes))
561564

562565
var w sync.WaitGroup
563-
pool := make(chan func(), 10) // Only run 10 at a time
566+
pool := make(chan func(), e.Concurrency)
564567
go func() {
565-
f := <-pool
566-
f()
568+
for f := range pool {
569+
go f()
570+
}
567571
}()
568572

569573
for _, node := range nodes {
570-
state := 1
571-
drain := strconv.FormatBool(node.Drain)
572-
573-
ch <- prometheus.MustNewConstMetric(
574-
nodeInfo, prometheus.GaugeValue, 1,
575-
node.Name, node.Version, node.NodeClass, node.Status,
576-
drain, node.Datacenter, node.SchedulingEligibility,
577-
)
574+
w.Add(1)
575+
pool <- func(node api.NodeListStub) func() {
576+
return func() {
577+
defer w.Done()
578+
state := 1
579+
drain := strconv.FormatBool(node.Drain)
578580

579-
if node.Status == "down" {
580-
state = 0
581-
}
582-
ch <- prometheus.MustNewConstMetric(
583-
serfLanMembersStatus, prometheus.GaugeValue, float64(state),
584-
node.Datacenter, node.NodeClass, node.Name, drain,
585-
)
581+
ch <- prometheus.MustNewConstMetric(
582+
nodeInfo, prometheus.GaugeValue, 1,
583+
node.Name, node.Version, node.NodeClass, node.Status,
584+
drain, node.Datacenter, node.SchedulingEligibility,
585+
)
586586

587-
if !validVersion(node.Name, node.Version) {
588-
continue
589-
}
587+
if node.Status == "down" {
588+
state = 0
589+
}
590+
ch <- prometheus.MustNewConstMetric(
591+
serfLanMembersStatus, prometheus.GaugeValue, float64(state),
592+
node.Datacenter, node.NodeClass, node.Name, drain,
593+
)
590594

591-
if !e.AllocationStatsMetricsEnabled {
592-
continue
593-
}
595+
if !validVersion(node.Name, node.Version) {
596+
return
597+
}
594598

595-
w.Add(1)
596-
pool <- func(a api.NodeListStub) func() {
597-
return func() {
598-
defer w.Done()
599+
if !e.AllocationStatsMetricsEnabled {
600+
return
601+
}
599602

600-
logrus.Debugf("Fetching node %#v", a)
601-
node, _, err := e.client.Nodes().Info(a.ID, opts)
603+
logrus.Debugf("Fetching node %#v", node)
604+
node, _, err := e.client.Nodes().Info(node.ID, opts)
602605
if err != nil {
603-
logError(fmt.Errorf("failed to get node %s info: %s", a.Name, err))
606+
logError(fmt.Errorf("failed to get node %s info: %s", node.Name, err))
604607
return
605608
}
606609

@@ -648,7 +651,7 @@ func (e *Exporter) collectNodes(ch chan<- prometheus.Metric) error {
648651
nodeLabels...,
649652
)
650653

651-
nodeStats, err := e.client.Nodes().Stats(a.ID, opts)
654+
nodeStats, err := e.client.Nodes().Stats(node.ID, opts)
652655
if err != nil {
653656
logError(fmt.Errorf("failed to get node %s stats: %s", node.Name, err))
654657
return
@@ -723,7 +726,7 @@ func (e *Exporter) collectAllocations(ch chan<- prometheus.Metric) error {
723726
for _, allocStub := range allocStubs {
724727
w.Add(1)
725728

726-
go func(allocStub *api.AllocationListStub) {
729+
go func(allocStub api.AllocationListStub) {
727730
defer w.Done()
728731

729732
alloc, _, err := e.client.Allocations().Info(allocStub.ID, &api.QueryOptions{
@@ -814,7 +817,7 @@ func (e *Exporter) collectAllocations(ch chan<- prometheus.Metric) error {
814817
)
815818
}
816819

817-
}(allocStub)
820+
}(*allocStub)
818821
}
819822

820823
w.Wait()

0 commit comments

Comments
 (0)