2020-02-23 11:35:47 +00:00
|
|
|
package promscrape
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var tsmGlobal = newTargetStatusMap()
|
|
|
|
|
|
|
|
// WriteHumanReadableTargetsStatus writes human-readable status for all the scrape targets to w.
|
2020-10-08 15:50:22 +00:00
|
|
|
func WriteHumanReadableTargetsStatus(w io.Writer, showOriginalLabels bool) {
|
|
|
|
tsmGlobal.WriteHumanReadable(w, showOriginalLabels)
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type targetStatusMap struct {
|
|
|
|
mu sync.Mutex
|
2020-04-14 10:08:48 +00:00
|
|
|
m map[uint64]targetStatus
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTargetStatusMap() *targetStatusMap {
|
|
|
|
return &targetStatusMap{
|
2020-04-14 10:08:48 +00:00
|
|
|
m: make(map[uint64]targetStatus),
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tsm *targetStatusMap) Reset() {
|
|
|
|
tsm.mu.Lock()
|
2020-04-14 10:08:48 +00:00
|
|
|
tsm.m = make(map[uint64]targetStatus)
|
2020-02-23 11:35:47 +00:00
|
|
|
tsm.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-05-03 09:41:13 +00:00
|
|
|
func (tsm *targetStatusMap) Register(sw *ScrapeWork) {
|
2020-03-11 01:19:56 +00:00
|
|
|
tsm.mu.Lock()
|
2020-05-03 09:41:13 +00:00
|
|
|
tsm.m[sw.ID] = targetStatus{
|
|
|
|
sw: sw,
|
2020-03-11 01:19:56 +00:00
|
|
|
}
|
|
|
|
tsm.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-05-03 09:41:13 +00:00
|
|
|
func (tsm *targetStatusMap) Unregister(sw *ScrapeWork) {
|
2020-03-11 01:19:56 +00:00
|
|
|
tsm.mu.Lock()
|
2020-05-03 09:41:13 +00:00
|
|
|
delete(tsm.m, sw.ID)
|
2020-03-11 01:19:56 +00:00
|
|
|
tsm.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-07-13 18:52:03 +00:00
|
|
|
func (tsm *targetStatusMap) Update(sw *ScrapeWork, group string, up bool, scrapeTime, scrapeDuration int64, err error) {
|
2020-02-23 11:35:47 +00:00
|
|
|
tsm.mu.Lock()
|
2020-04-14 10:08:48 +00:00
|
|
|
tsm.m[sw.ID] = targetStatus{
|
2020-02-23 11:35:47 +00:00
|
|
|
sw: sw,
|
|
|
|
up: up,
|
2020-07-13 18:52:03 +00:00
|
|
|
scrapeGroup: group,
|
2020-02-23 11:35:47 +00:00
|
|
|
scrapeTime: scrapeTime,
|
|
|
|
scrapeDuration: scrapeDuration,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
tsm.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-07-13 18:52:03 +00:00
|
|
|
// StatusByGroup returns the number of targets with status==up
|
|
|
|
// for the given group name
|
|
|
|
func (tsm *targetStatusMap) StatusByGroup(group string, up bool) int {
|
|
|
|
var count int
|
|
|
|
tsm.mu.Lock()
|
|
|
|
for _, st := range tsm.m {
|
|
|
|
if st.scrapeGroup == group && st.up == up {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tsm.mu.Unlock()
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2020-10-08 15:50:22 +00:00
|
|
|
func (tsm *targetStatusMap) WriteHumanReadable(w io.Writer, showOriginalLabels bool) {
|
2020-02-23 11:35:47 +00:00
|
|
|
byJob := make(map[string][]targetStatus)
|
|
|
|
tsm.mu.Lock()
|
2020-03-11 01:19:56 +00:00
|
|
|
for _, st := range tsm.m {
|
2020-04-14 10:32:55 +00:00
|
|
|
job := st.sw.Job()
|
2020-02-23 11:35:47 +00:00
|
|
|
byJob[job] = append(byJob[job], st)
|
|
|
|
}
|
|
|
|
tsm.mu.Unlock()
|
|
|
|
|
|
|
|
var jss []jobStatus
|
|
|
|
for job, statuses := range byJob {
|
|
|
|
jss = append(jss, jobStatus{
|
|
|
|
job: job,
|
|
|
|
statuses: statuses,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
sort.Slice(jss, func(i, j int) bool {
|
|
|
|
return jss[i].job < jss[j].job
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, js := range jss {
|
|
|
|
sts := js.statuses
|
|
|
|
sort.Slice(sts, func(i, j int) bool {
|
|
|
|
return sts[i].sw.ScrapeURL < sts[j].sw.ScrapeURL
|
|
|
|
})
|
|
|
|
ups := 0
|
|
|
|
for _, st := range sts {
|
|
|
|
if st.up {
|
|
|
|
ups++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "job=%q (%d/%d up)\n", js.job, ups, len(sts))
|
|
|
|
for _, st := range sts {
|
|
|
|
state := "up"
|
|
|
|
if !st.up {
|
|
|
|
state = "down"
|
|
|
|
}
|
2020-04-16 20:34:37 +00:00
|
|
|
labelsStr := st.sw.LabelsString()
|
2020-10-08 15:50:22 +00:00
|
|
|
if showOriginalLabels {
|
|
|
|
labelsStr += ", originalLabels=" + promLabelsString(st.sw.OriginalLabels)
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
lastScrape := st.getDurationFromLastScrape()
|
|
|
|
errMsg := ""
|
|
|
|
if st.err != nil {
|
|
|
|
errMsg = st.err.Error()
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "\tstate=%s, endpoint=%s, labels=%s, last_scrape=%.3fs ago, scrape_duration=%.3fs, error=%q\n",
|
|
|
|
state, st.sw.ScrapeURL, labelsStr, lastScrape.Seconds(), float64(st.scrapeDuration)/1000, errMsg)
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 11:51:52 +00:00
|
|
|
fmt.Fprintf(w, "\n")
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type jobStatus struct {
|
|
|
|
job string
|
|
|
|
statuses []targetStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
type targetStatus struct {
|
|
|
|
sw *ScrapeWork
|
|
|
|
up bool
|
2020-07-13 18:52:03 +00:00
|
|
|
scrapeGroup string
|
2020-02-23 11:35:47 +00:00
|
|
|
scrapeTime int64
|
|
|
|
scrapeDuration int64
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (st *targetStatus) getDurationFromLastScrape() time.Duration {
|
|
|
|
return time.Since(time.Unix(st.scrapeTime/1000, (st.scrapeTime%1000)*1e6))
|
|
|
|
}
|