-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
73 lines (63 loc) · 1.7 KB
/
http.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
package healthindicator
import (
"context"
"net/http"
"time"
actuator "github.com/megaease/actuator-go/actuator"
)
// HTTPHealthIndicator checks the health of a remote HTTP endpoint.
type HTTPHealthIndicator struct {
httpClient *http.Client
url string
}
// NewHTTPHealthIndicator creates a new HTTPHealthIndicator with the provided HTTP client and endpoint URL.
func NewHTTPHealthIndicator(httpClient *http.Client, url string) *HTTPHealthIndicator {
return &HTTPHealthIndicator{
httpClient: httpClient,
url: url,
}
}
// Health checks the health of the remote HTTP endpoint and returns the health status.
func (h *HTTPHealthIndicator) Health(withDetails bool) actuator.Health {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
// Send a GET request to the endpoint
requestTime := time.Now()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, h.url, nil)
if err != nil {
return actuator.Health{
Status: "DOWN",
Details: &actuator.HealthDetails{
"error": err.Error(),
},
}
}
resp, err := h.httpClient.Do(req)
if err != nil {
return actuator.Health{
Status: "DOWN",
Details: &actuator.HealthDetails{
"error": err.Error(),
},
}
}
defer resp.Body.Close()
// Determine the status based on the HTTP response
health := actuator.Health{
Status: "UP",
Details: nil,
}
if resp.StatusCode != http.StatusOK {
health.Status = "DOWN"
}
// Include detailed information if requested
if withDetails {
health.Details = &actuator.HealthDetails{
"statusCode": resp.StatusCode,
"status": resp.Status,
"url": h.url,
"responseTime": time.Since(requestTime).Milliseconds(),
}
}
return health
}