From 3945bf9dec03ddf708b28719738ddb18dba1f934 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Tue, 14 Apr 2020 13:32:55 +0300 Subject: [PATCH] lib/promscrape: mention job name in error messages when target cannot be scraped This should improve debuggability --- lib/promscrape/scrapework.go | 13 +++++++++++-- lib/promscrape/targetstatus.go | 6 +----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/promscrape/scrapework.go b/lib/promscrape/scrapework.go index 28b28530f..155e5f15e 100644 --- a/lib/promscrape/scrapework.go +++ b/lib/promscrape/scrapework.go @@ -60,6 +60,15 @@ type ScrapeWork struct { SampleLimit int } +// Job returns job for the ScrapeWork +func (sw *ScrapeWork) Job() string { + label := promrelabel.GetLabelByName(sw.Labels, "job") + if label == nil { + return "" + } + return label.Value +} + type scrapeWork struct { // Config for the scrape. Config ScrapeWork @@ -114,12 +123,12 @@ func (sw *scrapeWork) run(stopCh <-chan struct{}) { } func (sw *scrapeWork) logError(s string) { - logger.ErrorfSkipframes(1, "error when scraping %q: %s", sw.Config.ScrapeURL, s) + logger.ErrorfSkipframes(1, "error when scraping %q from job %q: %s", sw.Config.ScrapeURL, sw.Config.Job(), s) } func (sw *scrapeWork) scrapeAndLogError(timestamp int64) { if err := sw.scrapeInternal(timestamp); err != nil { - logger.Errorf("error when scraping %q: %s", sw.Config.ScrapeURL, err) + logger.Errorf("error when scraping %q from job %q: %s", sw.Config.ScrapeURL, sw.Config.Job(), err) } } diff --git a/lib/promscrape/targetstatus.go b/lib/promscrape/targetstatus.go index 0c3759006..b362d9a84 100644 --- a/lib/promscrape/targetstatus.go +++ b/lib/promscrape/targetstatus.go @@ -70,11 +70,7 @@ func (tsm *targetStatusMap) WriteHumanReadable(w io.Writer) { byJob := make(map[string][]targetStatus) tsm.mu.Lock() for _, st := range tsm.m { - job := "" - label := promrelabel.GetLabelByName(st.sw.Labels, "job") - if label != nil { - job = label.Value - } + job := st.sw.Job() byJob[job] = append(byJob[job], st) } tsm.mu.Unlock()