/
root.go
153 lines (144 loc) · 4.11 KB
/
root.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
package http
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"time"
"github.com/pkg/errors"
prom "github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
"gopkg.in/tomb.v2"
"github.com/appclacks/cabourotte/healthcheck"
"github.com/appclacks/cabourotte/tls"
)
// HTTPDiscovery the http discovery struct
type HTTPDiscovery struct {
Logger *zap.Logger
requestHistogram *prom.HistogramVec
responseCounter *prom.CounterVec
Healthcheck *healthcheck.Component
URL string
Config *Configuration
Client *http.Client
t tomb.Tomb
tick *time.Ticker
}
// New creates a new HTTP Discovery
func New(logger *zap.Logger, config *Configuration, checkComponent *healthcheck.Component, counter *prom.CounterVec, histogram *prom.HistogramVec) (*HTTPDiscovery, error) {
protocol := "http"
tlsConfig, err := tls.GetTLSConfig(config.Key, config.Cert, config.Cacert, "", config.Insecure)
if err != nil {
return nil, err
}
if config.Protocol == healthcheck.HTTPS {
protocol = "https"
}
url := fmt.Sprintf(
"%s://%s%s",
protocol,
net.JoinHostPort(config.Host, fmt.Sprintf("%d", config.Port)),
config.Path)
transport := &http.Transport{
TLSClientConfig: tlsConfig,
}
component := HTTPDiscovery{
Healthcheck: checkComponent,
responseCounter: counter,
requestHistogram: histogram,
Logger: logger,
Config: config,
URL: url,
Client: &http.Client{
Transport: transport,
Timeout: time.Second * 5,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
},
}
return &component, nil
}
func (c *HTTPDiscovery) request() error {
req, err := http.NewRequest("GET", c.URL, nil)
if err != nil {
return errors.Wrapf(err, "HTTP discovery: fail to create request for %s", c.URL)
}
req.Header.Set("User-Agent", "Cabourotte")
for k, v := range c.Config.Headers {
req.Header.Set(k, v)
}
if len(c.Config.Query) != 0 {
q := req.URL.Query()
for k, v := range c.Config.Query {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
}
resp, err := c.Client.Do(req)
if err != nil {
return errors.Wrapf(err, "HTTP discovery: fail to send request to %s", c.URL)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("HTTP discovery: request failed, status %d", resp.StatusCode)
}
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrapf(err, "Fail to read request body")
}
if resp.StatusCode != 200 {
return fmt.Errorf("HTTP Discovery: request failed, status %d, body %s", resp.StatusCode, string(responseBody))
}
var payload ResultPayload
if err := json.Unmarshal(responseBody, &payload); err != nil {
return fmt.Errorf("HTTP Discovery: fail to convert the payload from json: %s", err.Error())
}
return c.Healthcheck.ReloadForSource(
fmt.Sprintf("%s-%s", healthcheck.SourceHTTPDiscovery, c.Config.Name),
nil,
payload.CommandChecks,
payload.DNSChecks,
payload.TCPChecks,
payload.HTTPChecks,
payload.TLSChecks)
}
// Start starts the HTTP discovery component
func (c *HTTPDiscovery) Start() error {
c.tick = time.NewTicker(time.Duration(c.Config.Interval))
c.t.Go(func() error {
c.Logger.Info(fmt.Sprintf("Starting the HTTP healthcheck discovery on %s:%d", c.Config.Host, c.Config.Port))
for {
select {
case <-c.tick.C:
c.Logger.Debug(fmt.Sprintf("HTTP discovery: polling %s", c.URL))
start := time.Now()
status := "success"
err := c.request()
duration := time.Since(start)
if err != nil {
status = "failure"
msg := fmt.Sprintf("HTTP discovery error: %s", err.Error())
c.Logger.Error(msg)
}
c.requestHistogram.With(prom.Labels{"name": c.Config.Name}).Observe(duration.Seconds())
c.responseCounter.With(prom.Labels{"status": status, "name": c.Config.Name}).Inc()
case <-c.t.Dying():
return nil
}
}
})
return nil
}
// Stop stops the HTTP discovery component
func (c *HTTPDiscovery) Stop() error {
c.Logger.Info("Stopping the http discovery")
c.tick.Stop()
c.t.Kill(nil)
err := c.t.Wait()
if err != nil {
return err
}
return nil
}