lib/promscrape: mention job name in error messages when target cannot be scraped

This should improve debuggability
This commit is contained in:
Aliaksandr Valialkin 2020-04-14 13:32:55 +03:00
parent 66da177fe9
commit 3945bf9dec
2 changed files with 12 additions and 7 deletions

View file

@ -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)
}
}

View file

@ -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()