-
Notifications
You must be signed in to change notification settings - Fork 0
/
prober.go
187 lines (156 loc) · 4.28 KB
/
prober.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package prober
import (
"context"
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"gopkg.in/errgo.v1"
)
// Probe define a minimal set of methods that a probe should implement
type Probe interface {
Name() string
Check(context.Context) error
}
// Prober entrypoint of the philae api. It will retain a set of probe and run
// checks when asked to
type Prober struct {
timeout time.Duration
probes map[string]Probe
}
// ProberOption is a function modifying some parameters of the Prober
type ProberOption func(p *Prober)
// WithTimeout is a ProberOption which defines a timeout the prober have to get
// executed into Recommandation: it should be higher than the timeout of the
// probes included in it, otherwise it would mask the real errors.
func WithTimeout(d time.Duration) ProberOption {
return ProberOption(func(p *Prober) {
p.timeout = d
})
}
type ProberError struct {
errs []string
}
func (e *ProberError) AddError(name string, err error) {
msg := fmt.Sprintf("probe %s: %s", name, err.Error())
e.errs = append(e.errs, msg)
}
func (e *ProberError) IsEmpty() bool {
return e.errs == nil
}
func (e *ProberError) Error() string {
if e.errs == nil {
return "no error"
}
b := strings.Builder{}
b.WriteString("prober error: ")
b.WriteString(strings.Join(e.errs, ", "))
return b.String()
}
// ErrProbeNotFound is emitted when a check is performed on a probe that was not
// added to the prober
var ErrProbeNotFound = errors.New("probe not found")
// Result is the data structure used to retain the data fetched from a single run of each probes
type Result struct {
Healthy bool `json:"healthy"`
Probes []*ProbeResult `json:"probes"`
Error error `json:"error"`
}
// ProbeResult is the data structure used to retain the data fetched from a single probe
type ProbeResult struct {
Name string `json:"name"`
Healthy bool `json:"healthy"`
Comment string `json:"comment"`
Error error `json:"error"`
Duration time.Duration `json:"duration"`
}
// NewProber is the default constructor of a Prober
func NewProber(opts ...ProberOption) *Prober {
p := &Prober{
timeout: 10 * time.Second,
probes: map[string]Probe{},
}
for _, opt := range opts {
opt(p)
}
return p
}
func (p *Prober) AddProbe(probe Probe) {
p.probes[probe.Name()] = probe
}
// Check will run the check of each probes added and return the result in a Result struct
func (p *Prober) Check(ctx context.Context) *Result {
probesResults := make([]*ProbeResult, len(p.probes))
resultChan := make(chan *ProbeResult, len(p.probes))
healthy := true
proberErr := &ProberError{}
ctx, cancel := context.WithTimeout(ctx, p.timeout)
defer cancel()
for _, probe := range p.probes {
go p.checkOneProbe(ctx, probe, resultChan)
}
for i := 0; i < len(p.probes); i++ {
probeResult := <-resultChan
if !probeResult.Healthy {
healthy = false
proberErr.AddError(probeResult.Name, probeResult.Error)
}
probesResults[i] = probeResult
}
var err error
if !proberErr.IsEmpty() {
err = proberErr
}
return &Result{
Healthy: healthy,
Probes: probesResults,
Error: err,
}
}
func (p *Prober) CheckOneProbe(ctx context.Context, probeName string) *ProbeResult {
probe, ok := p.probes[probeName]
if !ok {
return &ProbeResult{
Error: ErrProbeNotFound,
}
}
resultChan := make(chan *ProbeResult, 1)
ctx, cancel := context.WithTimeout(ctx, p.timeout)
defer cancel()
go p.checkOneProbe(ctx, probe, resultChan)
probeResult := <-resultChan
return probeResult
}
func (p *Prober) checkOneProbe(ctx context.Context, probe Probe, res chan *ProbeResult) {
probeRes := make(chan error)
var err error
begin := time.Now()
go ProberWrapper(ctx, probe, probeRes)
select {
case e := <-probeRes:
err = e
case <-ctx.Done():
err = fmt.Errorf("prober: %s", ctx.Err())
}
probe_healthy := true
duration := time.Now().Sub(begin)
comment := fmt.Sprintf("took %v", duration)
if err != nil {
err = errgo.Notef(err, "probe check failed")
comment = "error"
probe_healthy = false
}
probeResult := &ProbeResult{
Name: probe.Name(),
Healthy: probe_healthy,
Comment: comment,
Error: err,
Duration: duration,
}
res <- probeResult
}
//nolint:revive
func ProberWrapper(ctx context.Context, probe Probe, res chan error) {
err := probe.Check(ctx)
res <- err
}