Skip to content

Commit

Permalink
Added mutex for access to the err variable written by all metrics col…
Browse files Browse the repository at this point in the history
…lector functions
  • Loading branch information
valrusu committed Jul 2, 2024
1 parent 29c8869 commit d9c0e1f
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion 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,7 +315,12 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
}

scrapeStart := time.Now()
if err = e.ScrapeMetric(e.db, ch, metric); err != nil {
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", err.Error())
e.scrapeErrors.WithLabelValues(metric.Context).Inc()
} else {
Expand Down

0 comments on commit d9c0e1f

Please sign in to comment.