-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.go
107 lines (97 loc) · 2.78 KB
/
report.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package healthz
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"time"
)
var (
htmlTemplate *template.Template
)
func init() {
var err error
t := template.New("webpage").Funcs(map[string]interface{}{
"HealthTitle": func(h Health) string {
return healthToTitle[h]
},
"SeverityTitle": func(s Severity) string {
return severityToTitle[s]
},
})
htmlTemplate, err = t.Parse(tpl)
if err != nil {
panic(err)
}
}
func (rc *GroupReport) Len() int {
return len(rc.Subcomponents)
}
func (rc *GroupReport) Swap(i, j int) {
rc.Subcomponents[i], rc.Subcomponents[j] = rc.Subcomponents[j], rc.Subcomponents[i]
}
func (rc *GroupReport) Less(i, j int) bool {
if rc.Subcomponents[i].OverallHealth == rc.Subcomponents[j].OverallHealth {
return rc.Subcomponents[i].Severity > rc.Subcomponents[j].Severity
}
return rc.Subcomponents[i].OverallHealth < rc.Subcomponents[j].OverallHealth
}
type report struct {
ServiceSignature string
OverallHealth string
OverallHealthCode Health
Uptime time.Duration `json:",omitempty"`
Hostname string `json:",omitempty"`
Root *GroupReport `json:",omitempty"`
}
func (h *Handler) report() *report {
root := h.GroupReport()
rpt := &report{
ServiceSignature: h.serviceSignature,
OverallHealth: healthToTitle[root.OverallHealth],
OverallHealthCode: root.OverallHealth,
}
if h.details {
uptime := time.Since(h.startTime)
rpt.Uptime = uptime
rpt.Hostname = h.hostname
rpt.Root = root
}
return rpt
}
func (h *Handler) reportLiveness(w http.ResponseWriter, r *http.Request) {
overallHealth := h.OverallHealth()
if overallHealth > Error {
w.Write([]byte("OK"))
} else {
http.Error(w, healthToTitle[overallHealth], http.StatusServiceUnavailable)
}
}
func (h *Handler) reportReadiness(w http.ResponseWriter, r *http.Request) {
overallHealth := h.OverallHealth()
if overallHealth >= Normal {
w.Write([]byte("OK"))
} else {
http.Error(w, healthToTitle[overallHealth], http.StatusServiceUnavailable)
}
}
func (h *Handler) reportJSON(w http.ResponseWriter, r *http.Request) {
rpt := h.report()
jData, _ := json.Marshal(rpt)
w.Header().Set("Overall-Health", rpt.OverallHealth)
w.Header().Set("Overall-Health-Code", fmt.Sprintf("%d", rpt.OverallHealthCode))
w.Header().Set("Content-Type", "application/json")
w.Write(jData)
}
func (h *Handler) reportHTML(w http.ResponseWriter, r *http.Request) {
rpt := h.report()
w.Header().Set("Overall-Health", rpt.OverallHealth)
w.Header().Set("Overall-Health-Code", fmt.Sprintf("%d", rpt.OverallHealthCode))
w.Header().Set("Content-Type", "text/html; charset=utf-8")
err := htmlTemplate.Execute(w, rpt)
if err != nil {
w.Write([]byte("\n\n<h1>error while rendering the report:\n"))
w.Write([]byte(err.Error()))
w.Write([]byte("\n</h1>"))
}
}