/
monitor_http.go
114 lines (96 loc) · 2.75 KB
/
monitor_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
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
108
109
110
111
112
113
114
package server
import (
"encoding/json"
"fmt"
"net/http"
"os"
)
// Monitor Configuration
type MonitorConfig struct {
Bind string `xml:"Bind"`
TLS bool `xml:"TLS"`
TLSCertificate string `xml:"TLSCertificate"`
TLSKey string `xml:"TLSKey"`
Headers []Header `xml:"Header"`
}
// DefaultMonitorConfig returns default Monitor config
func DefaultMonitorConfig() MonitorConfig {
return MonitorConfig{
Bind: "127.0.0.1:9100",
TLS: false,
TLSCertificate: "",
TLSKey: "",
}
}
// MonitorConfig returns MonitorConfig.
func (s *Server) MonitorConfig() MonitorConfig {
return s.ServerConfig().Monitor
}
const (
LivezEndpoint = "/livez"
ReadyzEndpoint = "/readyz"
)
// ResponseHandler handles responses for monitor routes (JSONP and JSON).
func (s *Server) ResponseHandler(w http.ResponseWriter, r *http.Request, code int, data interface{}) {
for _, header := range s.MonitorConfig().Headers {
w.Header().Set(header.Name, header.Text)
}
b, err := json.Marshal(data)
if err != nil {
s.Errorf("Monitor: Error marshaling response to %s request: %v", r.URL, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(code)
if callback := r.URL.Query().Get("callback"); callback != "" {
w.Header().Set("Content-Type", "application/javascript")
fmt.Fprintf(w, "%s(%s)", callback, b)
} else {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "%s", b)
}
}
// HandleLivez returns liveness check.
func (s *Server) HandleLivez(w http.ResponseWriter, r *http.Request) {
if status := s.healthStatus(); status.Error == "" {
s.ResponseHandler(w, r, http.StatusOK, status)
} else {
s.ResponseHandler(w, r, http.StatusServiceUnavailable, status)
}
}
// HandleReadyz returns readiness check.
func (s *Server) HandleReadyz(w http.ResponseWriter, r *http.Request) {
v := &HealthStatus{Status: "ok"}
s.ResponseHandler(w, r, http.StatusOK, v)
}
// StartMonitor starts the HTTP or HTTPs server if needed.
func (s *Server) StartMonitor() {
cfg := s.MonitorConfig()
s.Noticef("Starting Monitor on %v tls: %v", cfg.Bind, cfg.TLS)
mux := http.NewServeMux()
mux.HandleFunc(LivezEndpoint, s.HandleLivez)
mux.HandleFunc(ReadyzEndpoint, s.HandleReadyz)
srv := &http.Server{
Addr: cfg.Bind,
Handler: mux,
}
s.mu.Lock()
s.monitorServer = srv
s.mu.Unlock()
go func() {
serve := func() error {
if cfg.TLS {
return srv.ListenAndServeTLS(cfg.TLSCertificate, cfg.TLSKey)
} else {
return srv.ListenAndServe()
}
}
if err := serve(); err != nil {
if !s.IsShutdown() {
s.Errorf("Monitor: error starting monitor (FATAL): %v", err)
// TODO (nusov): cancel Start() and close all open connections before exit
os.Exit(1)
}
}
}()
}