/
philaeprobe.go
58 lines (46 loc) · 1.11 KB
/
philaeprobe.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
package philaeprobe
import (
"context"
"encoding/json"
"errors"
"io"
errgo "gopkg.in/errgo.v1"
"github.com/Scalingo/go-philae/v5/httpprobe"
"github.com/Scalingo/go-philae/v5/prober"
)
type PhilaeProbe struct {
http httpprobe.HTTPProbe
}
type PhilaeChecker struct{}
func (_ PhilaeChecker) Check(body io.Reader) error {
var result prober.Result
err := json.NewDecoder(body).Decode(&result)
if err != nil {
return errgo.Notef(err, "Invalid json")
}
if !result.Healthy {
reason := ""
for _, probe := range result.Probes {
if !probe.Healthy {
reason += probe.Name + " is down (" + probe.Comment + "),"
}
}
return errors.New(reason)
}
return nil
}
func NewPhilaeProbe(name, endpoint string, dialTimeout, responseTimeout int) PhilaeProbe {
return PhilaeProbe{
http: httpprobe.NewHTTPProbe(name, endpoint, httpprobe.HTTPOptions{
Checker: PhilaeChecker{},
DialTimeout: dialTimeout,
ResponseTimeout: responseTimeout,
}),
}
}
func (p PhilaeProbe) Name() string {
return p.http.Name()
}
func (p PhilaeProbe) Check(ctx context.Context) error {
return p.http.Check(ctx)
}