Gather implements Gatherer.
()
| 410 | |
| 411 | // Gather implements Gatherer. |
| 412 | func (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 |