-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
84 lines (70 loc) · 2.57 KB
/
handler.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
package healthz
import (
"net/http"
"os"
"time"
)
// Handler is a http handler which is notified about health level of the
// different components in the system, and reports them through http
type Handler struct {
details bool
serviceSignature string
startTime time.Time
hostname string
rootComponent *componentGroup
mux *http.ServeMux
}
// RegisterSubcomponent creates a subcomponent if it wasn't registered before,
// and sets the severity level of the component to the given value
func (h *Handler) RegisterSubcomponent(name string, severity Severity) ComponentGroup {
return h.rootComponent.RegisterSubcomponent(name, severity)
}
// UnregisterSubcomponent removes the subcomponent from the group, the reports,
// and the calculation of the `OverallHealth`
func (h *Handler) UnregisterSubcomponent(name string) {
h.rootComponent.UnregisterSubcomponent(name)
}
// SetGroupHealth sets the health level of the root component.
// Note that you can't mix SetGroupHealth and RegisterSubcomponent
func (h *Handler) SetGroupHealth(health Health) {
h.rootComponent.SetGroupHealth(health)
}
// OverallHealth is the overall health level of the handler. Its calculation
// is described in the doc of `ComponentGroup.OverallHealth`
func (h *Handler) OverallHealth() Health {
return h.rootComponent.OverallHealth()
}
// ServeHTTP is implemented to response to the report requests
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.mux.ServeHTTP(w, r)
}
// GroupReport copies the current status of the root and its subcomponents,
// and returns the copied object
func (h *Handler) GroupReport() *GroupReport {
return h.rootComponent.GroupReport()
}
// NewHandler creates a Handler and returns it. if details is true, these
// values are also included in the applicable reports:
//
// * Uptime: since call of this function
// * Service Signature: which is passed to this function
// * Hostname: which is extracted using `os.Hostname()`
// * Health level and Severity level for all the registered components
func NewHandler(serviceSignature string, details bool) *Handler {
mux := http.NewServeMux()
hostname, _ := os.Hostname()
h := &Handler{
details: details,
serviceSignature: serviceSignature,
startTime: time.Now(),
hostname: hostname,
rootComponent: newComponentGroup(Major, Unknown),
mux: mux,
}
mux.HandleFunc("/", h.reportHTML)
mux.HandleFunc("/liveness", h.reportLiveness)
mux.HandleFunc("/readiness", h.reportReadiness)
mux.HandleFunc("/min.css", h.reportMinCSS)
mux.HandleFunc("/json", h.reportJSON)
return h
}