Skip to content

Commit

Permalink
fix collection
Browse files Browse the repository at this point in the history
  • Loading branch information
ritbl committed Jun 30, 2022
1 parent b4e77ba commit 379971c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 12 additions & 2 deletions prometheus/collector_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

package prometheus

import (
"log"
)

const (
metricsBufferSize = 1024 * 8
)
Expand All @@ -38,20 +42,26 @@ func NewMetaMetricsCollector() *MetaMetrics {
}

func (m *MetaMetrics) Add(metric Metric) {
m.cache <- metric
select {
case m.cache <- metric:
default:
log.Println("MetaMetrics blocked")
}

}

func (m *MetaMetrics) Describe(ch chan<- *Desc) {
ch <- m.desc
}

func (m *MetaMetrics) Collect(ch chan<- Metric) {
collect:
for {
select {
case metric := <-m.cache:
ch <- metric
default:
break
break collect
}
}
}
8 changes: 4 additions & 4 deletions prometheus/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,9 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {

wg.Add(goroutineBudget)

var metaMetricCollector Collector
collectWorker := func() {
for {
var metaMetricCollector Collector
select {
case collector := <-checkedCollectors:
if _, ok := collector.(*MetaMetrics); ok {
Expand All @@ -460,9 +460,6 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
case collector := <-uncheckedCollectors:
collector.Collect(uncheckedMetricChan)
default:
if metaMetricCollector != nil {
metaMetricCollector.Collect(checkedMetricChan)
}
return
}
wg.Done()
Expand All @@ -477,6 +474,9 @@ func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
// are collected.
go func() {
wg.Wait()
if metaMetricCollector != nil {
metaMetricCollector.Collect(checkedMetricChan)
}
close(checkedMetricChan)
close(uncheckedMetricChan)
}()
Expand Down

0 comments on commit 379971c

Please sign in to comment.