diff --git a/receiver/splunkenterprisereceiver/scraper.go b/receiver/splunkenterprisereceiver/scraper.go index e0d3abf76283..792e9e014205 100644 --- a/receiver/splunkenterprisereceiver/scraper.go +++ b/receiver/splunkenterprisereceiver/scraper.go @@ -56,13 +56,22 @@ func (s *splunkScraper) start(ctx context.Context, h component.Host) (err error) // listens to the error channel and combines errors sent from different metric scrape functions, // returning the combined error list should context timeout or a nil error value is sent in the // channel signifying the end of a scrape cycle -func errorListener(eQueue <-chan error, eOut chan<- *scrapererror.ScrapeErrors) { +func errorListener(ctx context.Context, eQueue <-chan error, eOut chan<- *scrapererror.ScrapeErrors) { errs := &scrapererror.ScrapeErrors{} - for err := range eQueue { - errs.Add(err) + for { + select { + case <-ctx.Done(): + eOut <- errs + return + case err, ok := <-eQueue: + if !ok { + eOut <- errs + return + } + errs.Add(err) + } } - eOut <- errs } // The big one: Describes how all scraping tasks should be performed. Part of the scraper interface @@ -96,7 +105,7 @@ func (s *splunkScraper) scrape(ctx context.Context) (pmetric.Metrics, error) { errChan := make(chan error, len(metricScrapes)) go func() { - errorListener(errChan, errOut) + errorListener(ctx, errChan, errOut) }() for _, fn := range metricScrapes {