-
Notifications
You must be signed in to change notification settings - Fork 3
/
transitive.go
69 lines (61 loc) · 1.26 KB
/
transitive.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
package libhealth
import (
"context"
"net/http"
"time"
)
var (
sharedClient = http.Client{
Timeout: 10 * time.Second,
}
)
// TransitiveMonitor creates a Monitor that is a dependency on another service that responds to a healthcheck
func TransitiveMonitor(
url,
name,
description,
wikipage string,
urgency Urgency,
statusChan chan HealthStatus,
) *Monitor {
errorHealth := func(err error, start time.Time) Health {
msg := "error checking transitive monitor: " + err.Error()
return Health{
Status: OUTAGE,
Urgency: urgency,
Time: start,
Message: Message(msg),
Duration: time.Since(start),
}
}
return NewMonitor(
name,
description,
wikipage,
urgency,
func(ctx context.Context) Health {
start := time.Now()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
if err != nil {
return errorHealth(err, start)
}
resp, err := sharedClient.Do(req)
if err != nil {
return errorHealth(err, start)
}
defer resp.Body.Close()
state := OK
if resp.StatusCode != http.StatusOK {
state = OUTAGE
}
return Health{
Status: state,
Urgency: urgency,
Time: start,
Message: Message(resp.Status),
Duration: time.Since(start),
}
},
statusChan,
)
}