Skip to content

Commit 9860b63

Browse files
committed
add labels for the results
Signed-off-by: Thomas Labarussias <[email protected]>
1 parent 834fea6 commit 9860b63

File tree

15 files changed

+66
-38
lines changed

15 files changed

+66
-38
lines changed

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func Main() {
9090
defer logger.Sync()
9191
daemonComponent, err := daemon.New(logger, &config)
9292
if err != nil {
93-
return errors.Wrapf(err, "Fail to creae the daemon")
93+
return errors.Wrapf(err, "Fail to create the daemon")
9494
}
9595
signals := make(chan os.Signal, 1)
9696
errChan := make(chan error)

healthcheck/command.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ func (h *CommandHealthcheck) LogInfo(message string) {
107107
}
108108

109109
// Execute executes an healthcheck on the given domain
110-
func (h *CommandHealthcheck) Execute(ctx context.Context) error {
110+
func (h *CommandHealthcheck) Execute(ctx *context.Context) error {
111111
h.LogDebug("start executing healthcheck")
112-
ctx, cancel := context.WithTimeout(ctx, time.Duration(h.Config.Timeout)*time.Second)
112+
ctxh, cancel := context.WithTimeout(*ctx, time.Duration(h.Config.Timeout)*time.Second)
113113
defer cancel()
114114
var stdErr bytes.Buffer
115-
cmd := exec.CommandContext(ctx, h.Config.Command, h.Config.Arguments...)
115+
cmd := exec.CommandContext(ctxh, h.Config.Command, h.Config.Arguments...)
116116
cmd.Stderr = &stdErr
117117
if err := cmd.Run(); err != nil {
118118
var errorMsg string

healthcheck/command_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ func TestCommandExecuteSuccess(t *testing.T) {
1616
Timeout: Duration(time.Second * 2),
1717
},
1818
}
19-
err := h.Execute(context.Background())
19+
ctx := context.Background()
20+
err := h.Execute(&ctx)
2021
if err != nil {
2122
t.Fatalf("healthcheck error :\n%v", err)
2223
}
@@ -31,7 +32,8 @@ func TestCommandExecuteFailure(t *testing.T) {
3132
Timeout: Duration(time.Second * 2),
3233
},
3334
}
34-
err := h.Execute(context.Background())
35+
ctx := context.Background()
36+
err := h.Execute(&ctx)
3537
if err == nil {
3638
t.Fatalf("healthcheck was expected to fail")
3739
}

healthcheck/dns.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ func verifyIPs(expectedIPs []IP, lookupIPs []net.IP) error {
135135
return nil
136136
}
137137

138-
func (h *DNSHealthcheck) lookupIP(ctx context.Context) ([]net.IP, error) {
139-
ctx, cancel := context.WithTimeout(ctx, time.Duration(h.Config.Timeout))
138+
func (h *DNSHealthcheck) lookupIP(ctx *context.Context) ([]net.IP, error) {
139+
ctxh, cancel := context.WithTimeout(*ctx, time.Duration(h.Config.Timeout))
140140
defer cancel()
141-
addrs, err := net.DefaultResolver.LookupIPAddr(ctx, h.Config.Domain)
141+
addrs, err := net.DefaultResolver.LookupIPAddr(ctxh, h.Config.Domain)
142142
if err != nil {
143143
return nil, err
144144
}
@@ -150,7 +150,7 @@ func (h *DNSHealthcheck) lookupIP(ctx context.Context) ([]net.IP, error) {
150150
}
151151

152152
// Execute executes an healthcheck on the given domain
153-
func (h *DNSHealthcheck) Execute(ctx context.Context) error {
153+
func (h *DNSHealthcheck) Execute(ctx *context.Context) error {
154154
h.LogDebug("start executing healthcheck")
155155
ips, err := h.lookupIP(ctx)
156156
if err != nil {

healthcheck/dns_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ func TestDNSExecuteSuccess(t *testing.T) {
2020
Timeout: Duration(time.Second * 2),
2121
},
2222
}
23-
24-
err := h.Execute(context.Background())
23+
ctx := context.Background()
24+
err := h.Execute(&ctx)
2525
if err != nil {
2626
t.Fatalf("healthcheck error :\n%v", err)
2727
}
@@ -35,8 +35,8 @@ func TestDNSExecuteFailure(t *testing.T) {
3535
Timeout: Duration(time.Second * 2),
3636
},
3737
}
38-
39-
err := h.Execute(context.Background())
38+
ctx := context.Background()
39+
err := h.Execute(&ctx)
4040
if err == nil {
4141
t.Fatalf("Was expecting an error: the domain does not exist")
4242
}

healthcheck/http.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (h *HTTPHealthcheck) LogInfo(message string) {
214214
}
215215

216216
// Execute executes an healthcheck on the given target
217-
func (h *HTTPHealthcheck) Execute(ctx context.Context) error {
217+
func (h *HTTPHealthcheck) Execute(ctx *context.Context) error {
218218
h.LogDebug("start executing healthcheck")
219219
body := bytes.NewBuffer([]byte(h.Config.Body))
220220
req, err := http.NewRequest(h.Config.Method, h.URL, body)
@@ -232,7 +232,7 @@ func (h *HTTPHealthcheck) Execute(ctx context.Context) error {
232232
req.Host = h.Config.Host
233233
}
234234
client := h.Client
235-
timeoutCtx, cancel := context.WithTimeout(ctx, time.Duration(h.Config.Timeout))
235+
timeoutCtx, cancel := context.WithTimeout(*ctx, time.Duration(h.Config.Timeout))
236236
defer cancel()
237237
req = req.WithContext(timeoutCtx)
238238
if len(h.Config.Query) != 0 {
@@ -257,6 +257,7 @@ func (h *HTTPHealthcheck) Execute(ctx context.Context) error {
257257
if len(responseBodyStr) > maxMessageSize {
258258
message = responseBodyStr[0:maxMessageSize]
259259
}
260+
*ctx = context.WithValue(*ctx, "labels", map[string]string{"HTTP Status Code": fmt.Sprintf("%v", response.StatusCode)})
260261
if !h.isSuccessful(response) {
261262
errorMsg := fmt.Sprintf("HTTP request failed: status %d. Body: '%s'", response.StatusCode, html.EscapeString(message))
262263
err = errors.New(errorMsg)

healthcheck/http_test.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ func TestHTTPExecuteGetSuccess(t *testing.T) {
105105
if err != nil {
106106
t.Fatalf("Initialization error :\n%v", err)
107107
}
108-
err = h.Execute(context.Background())
108+
ctx := context.Background()
109+
err = h.Execute(&ctx)
109110
if err != nil {
110111
t.Fatalf("healthcheck error :\n%v", err)
111112
}
@@ -155,7 +156,8 @@ func TestHTTPExecuteRegexpSuccess(t *testing.T) {
155156
if err != nil {
156157
t.Fatalf("Initialization error :\n%v", err)
157158
}
158-
err = h.Execute(context.Background())
159+
ctx := context.Background()
160+
err = h.Execute(&ctx)
159161
if err != nil {
160162
t.Fatalf("healthcheck error :\n%v", err)
161163
}
@@ -199,7 +201,8 @@ func TestHTTPExecuteRegexpFailure(t *testing.T) {
199201
if err != nil {
200202
t.Fatalf("Initialization error :\n%v", err)
201203
}
202-
err = h.Execute(context.Background())
204+
ctx := context.Background()
205+
err = h.Execute(&ctx)
203206
if err == nil {
204207
t.Fatalf("Was expecting an error")
205208
}
@@ -241,7 +244,8 @@ func TestHTTPv6ExecuteSuccess(t *testing.T) {
241244
if err != nil {
242245
t.Fatalf("Initialization error :\n%v", err)
243246
}
244-
err = h.Execute(context.Background())
247+
ctx := context.Background()
248+
err = h.Execute(&ctx)
245249
if err != nil {
246250
t.Fatalf("healthcheck error :\n%v", err)
247251
}
@@ -280,7 +284,8 @@ func TestHTTPExecuteFailure(t *testing.T) {
280284
if err != nil {
281285
t.Fatalf("Initialization error :\n%v", err)
282286
}
283-
err = h.Execute(context.Background())
287+
ctx := context.Background()
288+
err = h.Execute(&ctx)
284289
if err == nil {
285290
t.Fatalf("Was expecting an error")
286291
}
@@ -408,7 +413,8 @@ func TestHTTPExecuteSourceIP(t *testing.T) {
408413
if err != nil {
409414
t.Fatalf("Initialization error :\n%v", err)
410415
}
411-
err = h.Execute(context.Background())
416+
ctx := context.Background()
417+
err = h.Execute(&ctx)
412418
if err != nil {
413419
t.Fatalf("healthcheck error :\n%v", err)
414420
}
@@ -471,7 +477,8 @@ func TestHTTPExecutePostSuccess(t *testing.T) {
471477
if err != nil {
472478
t.Fatalf("Initialization error :\n%v", err)
473479
}
474-
err = h.Execute(context.Background())
480+
ctx := context.Background()
481+
err = h.Execute(&ctx)
475482
if err != nil {
476483
t.Fatalf("healthcheck error :\n%v", err)
477484
}
@@ -547,7 +554,8 @@ func TestHTTPExecuteQueryParam(t *testing.T) {
547554
if err != nil {
548555
t.Fatalf("Initialization error :\n%v", err)
549556
}
550-
err = h.Execute(context.Background())
557+
ctx := context.Background()
558+
err = h.Execute(&ctx)
551559
if err != nil {
552560
t.Fatalf("healthcheck error :\n%v", err)
553561
}

healthcheck/result.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Result struct {
1212
Success bool `json:"success"`
1313
HealthcheckTimestamp int64 `json:"healthcheck-timestamp"`
1414
Message string `json:"message"`
15+
MessageLabels map[string]string `json:"message_labels"`
1516
Duration int64 `json:"duration"`
1617
Source string `json:"source"`
1718
}
@@ -51,7 +52,7 @@ func (r Result) Equals(v Result) bool {
5152
}
5253

5354
// NewResult build a a new result for an healthcheck
54-
func NewResult(healthcheck Healthcheck, duration int64, err error) *Result {
55+
func NewResult(healthcheck Healthcheck, duration int64, labels map[string]string, err error) *Result {
5556
now := time.Now()
5657
source := "configuration"
5758
if healthcheck.Base().Source != "" {
@@ -64,6 +65,7 @@ func NewResult(healthcheck Healthcheck, duration int64, err error) *Result {
6465
HealthcheckTimestamp: now.Unix(),
6566
Duration: duration,
6667
Source: source,
68+
MessageLabels: labels,
6769
}
6870
if err != nil {
6971
result.Success = false

healthcheck/root.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Healthcheck interface {
2929
Initialize() error
3030
GetConfig() interface{}
3131
Summary() string
32-
Execute(ctx context.Context) error
32+
Execute(ctx *context.Context) error
3333
LogDebug(message string)
3434
LogInfo(message string)
3535
Base() Base
@@ -61,12 +61,16 @@ func (c *Component) startWrapper(w *Wrapper) {
6161
ctx, span := tracer.Start(context.Background(), "healthcheck.periodic")
6262
span.SetAttributes(attribute.String("cabourotte.healthcheck.name", w.healthcheck.Base().Name))
6363
start := time.Now()
64-
err := w.healthcheck.Execute(ctx)
64+
err := w.healthcheck.Execute(&ctx)
6565
duration := time.Since(start)
6666
result := NewResult(
6767
w.healthcheck,
6868
duration.Milliseconds(),
69+
map[string]string{},
6970
err)
71+
if ctx.Value("labels") != nil {
72+
result.MessageLabels = ctx.Value("labels").(map[string]string)
73+
}
7074
status := "failure"
7175
if result.Success {
7276
status = "success"

healthcheck/tcp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (h *TCPHealthcheck) LogInfo(message string) {
126126
}
127127

128128
// Execute executes an healthcheck on the given target
129-
func (h *TCPHealthcheck) Execute(ctx context.Context) error {
129+
func (h *TCPHealthcheck) Execute(ctx *context.Context) error {
130130
h.LogDebug("start executing healthcheck")
131131
dialer := net.Dialer{}
132132
if h.Config.SourceIP != nil {
@@ -139,7 +139,7 @@ func (h *TCPHealthcheck) Execute(ctx context.Context) error {
139139
LocalAddr: addr,
140140
}
141141
}
142-
timeoutCtx, cancel := context.WithTimeout(ctx, time.Duration(h.Config.Timeout))
142+
timeoutCtx, cancel := context.WithTimeout(*ctx, time.Duration(h.Config.Timeout))
143143
defer cancel()
144144
conn, err := dialer.DialContext(timeoutCtx, "tcp", h.URL)
145145
if h.Config.ShouldFail {

0 commit comments

Comments
 (0)