Skip to content

Commit

Permalink
Fix for #459 (#460)
Browse files Browse the repository at this point in the history
* Added mutex for access to the err variable written by all metrics collector functions

* Fixed race condition for err variable
  • Loading branch information
valrusu authored Jul 29, 2024
1 parent 29c8869 commit 6ab50a9
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ func (e *Exporter) scheduledScrape() {
func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
e.totalScrapes.Inc()
var err error
var errmutex sync.Mutex

defer func(begun time.Time) {
e.duration.Set(time.Since(begun).Seconds())
if err == nil {
Expand Down Expand Up @@ -313,8 +315,13 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
}

scrapeStart := time.Now()
if err = e.ScrapeMetric(e.db, ch, metric); err != nil {
level.Error(e.logger).Log("scrapeMetricContext", metric.Context, "ScrapeDuration", time.Since(scrapeStart), "msg", err.Error())
if err1 := e.ScrapeMetric(e.db, ch, metric); err1 != nil {
errmutex.Lock()
{
err = err1
}
errmutex.Unlock()
level.Error(e.logger).Log("scrapeMetricContext", metric.Context, "ScrapeDuration", time.Since(scrapeStart), "msg", err1.Error())
e.scrapeErrors.WithLabelValues(metric.Context).Inc()
} else {
level.Debug(e.logger).Log("successfully scraped metric: ", metric.Context, metric.MetricsDesc, time.Since(scrapeStart))
Expand Down

0 comments on commit 6ab50a9

Please sign in to comment.