-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthz.go
78 lines (66 loc) · 2 KB
/
healthz.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
package healthz
import (
"encoding/json"
"fmt"
"net/http"
"sync"
)
var (
fatalErrors []HealthError
nonFatalErrors []HealthError
mutex sync.Mutex
fatalmutex sync.Mutex
)
type response struct {
FatalErrors []HealthError `json:"fatal_errors"`
NonFatalErrors []HealthError `json:"non_fatal_errors"`
}
// HealthError represents a single error, fatal or non, to be created by the package consumer and submitted through one of the two setter functions
type HealthError struct {
Description string `json:"description"`
Error string `json:"error"`
Metadata map[string]string `json:"metadata"`
Type string `json:"type"`
}
// NewFatalError appends a fatal error to the global fatal error slice
func NewFatalError(err HealthError) {
fatalmutex.Lock()
fatalErrors = append(fatalErrors, err)
fatalmutex.Unlock()
return
}
// NewNonFatalError appends a non fatal error to the global non fatal error slice
func NewNonFatalError(err HealthError) {
mutex.Lock()
nonFatalErrors = append(nonFatalErrors, err)
mutex.Unlock()
return
}
func healthzHandler(w http.ResponseWriter, r *http.Request) {
var statusCode int
if len(fatalErrors) > 0 {
statusCode = http.StatusInternalServerError
} else {
statusCode = http.StatusOK
}
response := response{FatalErrors: fatalErrors, NonFatalErrors: nonFatalErrors}
w.WriteHeader(statusCode)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
return
}
// Serve serves the healthz endpoint on the specified endpoint and url, defaults to localhost:8080/healthz
func Serve(port string, endpoint string) {
if endpoint == "" {
fmt.Printf("Endpoint not specified, defaulting to /healthz")
http.HandleFunc("/healthz", healthzHandler)
} else {
http.HandleFunc(endpoint, healthzHandler)
}
if port == "" {
fmt.Printf("Url/Port not specified, defaulting to localhost:8080")
go http.ListenAndServe("localhost:8080", nil)
} else {
go http.ListenAndServe("localhost:"+port, nil)
}
}