-
Notifications
You must be signed in to change notification settings - Fork 40
/
instrumentation.go
70 lines (59 loc) · 2.14 KB
/
instrumentation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package probe
import (
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
"github.com/bnb-chain/greenfield-storage-provider/pkg/metrics"
)
const (
ready = "ready"
unready = "unready"
healthy = "healthy"
unhealthy = "unhealthy"
)
// InstrumentationProbe stores instrumentation state of Probe.
// This is created with an intention to combine with other Probe's using prober.Combine.
type InstrumentationProbe struct {
statusMetric *prometheus.GaugeVec
mu sync.Mutex
statusString string
}
// NewInstrumentation returns InstrumentationProbe records readiness and healthiness for given module.
func NewInstrumentation() *InstrumentationProbe {
status := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "status_check",
Help: "Represents status (0 indicates failure, 1 indicates success) of the module.",
}, []string{"status_check"})
metrics.AddMetrics(status)
return &InstrumentationProbe{statusMetric: status}
}
// Ready records the module status when Ready is called, if combined with other Probes.
func (p *InstrumentationProbe) Ready() {
p.statusMetric.WithLabelValues(ready).Set(1)
p.mu.Lock()
defer p.mu.Unlock()
if p.statusString != ready {
log.Infof("change probe status to %s", ready)
p.statusString = ready
}
}
// Unready records the module status when Unready is called, if combined with other Probes.
func (p *InstrumentationProbe) Unready(err error) {
p.statusMetric.WithLabelValues(ready).Set(0)
p.mu.Lock()
defer p.mu.Unlock()
if p.statusString != unready {
log.Infof("change probe status to %s, reason: %v", unready, err)
p.statusString = unready
}
}
// Healthy records the module status when Healthy is called, if combined with other Probes.
func (p *InstrumentationProbe) Healthy() {
p.statusMetric.WithLabelValues(healthy).Set(1)
log.Infof("change probe status to %s", healthy)
}
// Unhealthy records the module status when UnHealthy is called, if combined with other Probes.
func (p *InstrumentationProbe) Unhealthy(err error) {
p.statusMetric.WithLabelValues(healthy).Set(0)
log.Infof("change probe status to %s, reason: %v", unhealthy, err)
}