Allow blocking and non-blocking metrics collection

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby
2017-10-06 12:04:05 -04:00
parent ed519bb5ce
commit d92f6eea1f
3 changed files with 16 additions and 10 deletions

View File

@@ -77,22 +77,23 @@ func (c *collector) Collect(ch chan<- prometheus.Metric) {
wg := &sync.WaitGroup{}
for _, t := range c.cgroups {
wg.Add(1)
go c.collect(t.id, t.namespace, t.cgroup, ch, wg)
go c.collect(t.id, t.namespace, t.cgroup, ch, true, wg)
}
SelectLoop:
storedLoop:
for {
// read stored metrics until the channel is flushed
select {
case value := <-c.storedMetrics:
ch <- value
case m := <-c.storedMetrics:
ch <- m
default:
break SelectLoop
break storedLoop
}
}
c.mu.RUnlock()
wg.Wait()
}
func (c *collector) collect(id, namespace string, cg cgroups.Cgroup, ch chan<- prometheus.Metric, wg *sync.WaitGroup) {
func (c *collector) collect(id, namespace string, cg cgroups.Cgroup, ch chan<- prometheus.Metric, block bool, wg *sync.WaitGroup) {
if wg != nil {
defer wg.Done()
}
@@ -102,7 +103,7 @@ func (c *collector) collect(id, namespace string, cg cgroups.Cgroup, ch chan<- p
return
}
for _, m := range c.metrics {
m.collect(id, namespace, stats, c.ns, ch)
m.collect(id, namespace, stats, c.ns, ch, block)
}
}