MCPcopy
hub / github.com/prometheus/client_golang / Gather

Method Gather

prometheus/registry.go:412–560  ·  view source on GitHub ↗

Gather implements Gatherer.

()

Source from the content-addressed store, hash-verified

410
411// Gather implements Gatherer.
412func (r *Registry) Gather() ([]*dto.MetricFamily, error) {
413 r.mtx.RLock()
414
415 if len(r.collectorsByID) == 0 && len(r.uncheckedCollectors) == 0 {
416 // Fast path.
417 r.mtx.RUnlock()
418 return nil, nil
419 }
420
421 var (
422 checkedMetricChan = make(chan Metric, capMetricChan)
423 uncheckedMetricChan = make(chan Metric, capMetricChan)
424 metricHashes = map[uint64]struct{}{}
425 wg sync.WaitGroup
426 errs MultiError // The collected errors to return in the end.
427 registeredDescIDs map[uint64]struct{} // Only used for pedantic checks
428 )
429
430 goroutineBudget := len(r.collectorsByID) + len(r.uncheckedCollectors)
431 metricFamiliesByName := make(map[string]*dto.MetricFamily, len(r.dimHashesByName))
432 checkedCollectors := make(chan Collector, len(r.collectorsByID))
433 uncheckedCollectors := make(chan Collector, len(r.uncheckedCollectors))
434 for _, collector := range r.collectorsByID {
435 checkedCollectors <- collector
436 }
437 for _, collector := range r.uncheckedCollectors {
438 uncheckedCollectors <- collector
439 }
440 // In case pedantic checks are enabled, we have to copy the map before
441 // giving up the RLock.
442 if r.pedanticChecksEnabled {
443 registeredDescIDs = make(map[uint64]struct{}, len(r.descIDs))
444 for id := range r.descIDs {
445 registeredDescIDs[id] = struct{}{}
446 }
447 }
448 r.mtx.RUnlock()
449
450 wg.Add(goroutineBudget)
451
452 collectWorker := func() {
453 for {
454 select {
455 case collector := <-checkedCollectors:
456 collector.Collect(checkedMetricChan)
457 case collector := <-uncheckedCollectors:
458 collector.Collect(uncheckedMetricChan)
459 default:
460 return
461 }
462 wg.Done()
463 }
464 }
465
466 // Start the first worker now to make sure at least one is running.
467 go collectWorker()
468 goroutineBudget--
469

Callers 14

ExampleCounterVecFunction · 0.95
ExampleSummaryVecFunction · 0.95
ExampleCollectorFuncFunction · 0.95
ExampleWrapCollectorWithFunction · 0.95
ExampleMetricVecFunction · 0.95
toMetricFamiliesFunction · 0.95
TestPushFunction · 0.95
testWriteSummaryFunction · 0.95

Calls 7

NormalizeMetricFamiliesFunction · 0.92
processMetricFunction · 0.85
WaitMethod · 0.80
AppendMethod · 0.80
MaybeUnwrapMethod · 0.80
AddMethod · 0.65
CollectMethod · 0.65

Tested by 14

ExampleCounterVecFunction · 0.76
ExampleSummaryVecFunction · 0.76
ExampleCollectorFuncFunction · 0.76
ExampleWrapCollectorWithFunction · 0.76
ExampleMetricVecFunction · 0.76
toMetricFamiliesFunction · 0.76
TestPushFunction · 0.76
testWriteSummaryFunction · 0.76