Skip to content

Commit 205a10b

Browse files
committed
prober: export probe counters and cumulative latency
Updates #cleanup Signed-off-by: Anton Tolchanov <anton@tailscale.com>
1 parent 7429e89 commit 205a10b

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

prober/prober.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ func (p *Prober) Run(name string, interval time.Duration, labels map[string]stri
9393
mEndTime: prometheus.NewDesc("end_secs", "Latest probe end time (seconds since epoch)", nil, l),
9494
mLatency: prometheus.NewDesc("latency_millis", "Latest probe latency (ms)", nil, l),
9595
mResult: prometheus.NewDesc("result", "Latest probe result (1 = success, 0 = failure)", nil, l),
96+
mAttempts: prometheus.NewCounterVec(prometheus.CounterOpts{
97+
Name: "attempts_total", Help: "Total number of probing attempts", ConstLabels: l,
98+
}, []string{"status"}),
99+
mSeconds: prometheus.NewCounterVec(prometheus.CounterOpts{
100+
Name: "seconds_total", Help: "Total amount of time spent executing the probe", ConstLabels: l,
101+
}, []string{"status"}),
96102
}
97103

98104
prometheus.WrapRegistererWithPrefix(p.namespace+"_", p.metrics).MustRegister(probe.metrics)
@@ -185,6 +191,8 @@ type Probe struct {
185191
mEndTime *prometheus.Desc
186192
mLatency *prometheus.Desc
187193
mResult *prometheus.Desc
194+
mAttempts *prometheus.CounterVec
195+
mSeconds *prometheus.CounterVec
188196

189197
mu sync.Mutex
190198
start time.Time // last time doProbe started
@@ -282,10 +290,15 @@ func (p *Probe) recordEnd(start time.Time, err error) {
282290
p.end = end
283291
p.succeeded = err == nil
284292
p.lastErr = err
293+
latency := end.Sub(p.start)
285294
if p.succeeded {
286-
p.latency = end.Sub(p.start)
295+
p.latency = latency
296+
p.mAttempts.WithLabelValues("ok").Inc()
297+
p.mSeconds.WithLabelValues("ok").Add(latency.Seconds())
287298
} else {
288299
p.latency = 0
300+
p.mAttempts.WithLabelValues("fail").Inc()
301+
p.mSeconds.WithLabelValues("fail").Add(latency.Seconds())
289302
}
290303
}
291304

@@ -334,6 +347,8 @@ func (p *Probe) Describe(ch chan<- *prometheus.Desc) {
334347
ch <- p.mEndTime
335348
ch <- p.mResult
336349
ch <- p.mLatency
350+
p.mAttempts.Describe(ch)
351+
p.mSeconds.Describe(ch)
337352
}
338353

339354
// Collect implements prometheus.Collector.
@@ -356,6 +371,8 @@ func (p *Probe) Collect(ch chan<- prometheus.Metric) {
356371
if p.latency > 0 {
357372
ch <- prometheus.MustNewConstMetric(p.mLatency, prometheus.GaugeValue, float64(p.latency.Milliseconds()))
358373
}
374+
p.mAttempts.Collect(ch)
375+
p.mSeconds.Collect(ch)
359376
}
360377

361378
// ticker wraps a time.Ticker in a way that can be faked for tests.

0 commit comments

Comments
 (0)