scraper.go 714 B

123456789101112131415161718192021222324252627282930
  1. package scrape
  2. import (
  3. "github.com/opencost/opencost/modules/collector-source/pkg/metric"
  4. )
  5. type Scraper interface {
  6. // Scrape performs the metrics scrape and returns a slice of `Update` instances to apply.
  7. Scrape() []metric.Update
  8. }
  9. type ScrapeFunc func() []metric.Update
  10. func concurrentScrape(scrapeFuncs ...ScrapeFunc) []metric.Update {
  11. resultCh := make(chan []metric.Update)
  12. defer close(resultCh)
  13. for _, scrapeFunc := range scrapeFuncs {
  14. go func() {
  15. scrapeResults := scrapeFunc()
  16. resultCh <- scrapeResults
  17. }()
  18. }
  19. var scrapeResults []metric.Update
  20. for range scrapeFuncs {
  21. targetResults := <-resultCh
  22. scrapeResults = append(scrapeResults, targetResults...)
  23. }
  24. return scrapeResults
  25. }