-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
149 lines (120 loc) · 3.18 KB
/
info.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package infrainfoserver
import (
"context"
"encoding/json"
"net/http"
"net/http/pprof"
"sync"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/pushwoosh/infra/log"
"github.com/pushwoosh/infra/operator"
"go.uber.org/zap"
)
// Info server is an internal http server that is used by k8s and prometheus to
// get live probes and metrics from the service.
type Info struct {
cfg *Config
op *infraoperator.Operator
mu sync.Mutex
srv *http.Server
bp BuildParams
}
type BuildParams struct {
AppName string `json:"application"`
AppVersion string `json:"version"`
GitRev string `json:"git_revision"`
BuildDate string `json:"build_date"`
}
var (
_ infraoperator.Starter = (*Info)(nil)
_ infraoperator.Stopper = (*Info)(nil)
_ infraoperator.Checker = (*Info)(nil)
)
func NewInfo(
cfg *Config,
op *infraoperator.Operator,
bp BuildParams,
) *Info {
return &Info{
cfg: cfg,
op: op,
bp: bp,
}
}
// Start starts listening info http server
func (s *Info) Start(_ context.Context) error {
infralog.Info("Starting Info http server")
mux := http.NewServeMux()
mux.Handle("/healthz", s.HealthzHandler())
mux.Handle("/readyz", s.ReadyzHandler())
mux.Handle("/release", s.ReleaseHandler())
mux.Handle("/metrics", promhttp.Handler())
// pprof handlers
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
s.srv = &http.Server{
Addr: s.cfg.Listen,
Handler: mux,
}
go func() {
if err := s.srv.ListenAndServe(); err != nil {
if errors.Is(err, http.ErrServerClosed) {
return
}
infralog.Fatal("info server error", zap.Error(err))
}
}()
return nil
}
func (s *Info) Stop(ctx context.Context) error {
s.mu.Lock()
defer s.mu.Unlock()
return s.srv.Shutdown(ctx)
}
func (s *Info) Check(_ context.Context) error {
return nil
}
func (s *Info) HealthzHandler() http.Handler {
return s.commonHealthzAndReadyzHandler()
}
func (s *Info) ReadyzHandler() http.Handler {
return s.commonHealthzAndReadyzHandler()
}
func (s *Info) commonHealthzAndReadyzHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
if s.cfg.ChecksTimeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, s.cfg.ChecksTimeout)
defer cancel()
}
checkErrors := s.op.Check(ctx)
if len(checkErrors) == 0 {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
return
}
for _, serviceError := range checkErrors {
infralog.Error("Health check error", zap.Error(serviceError))
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(503)
})
}
func (s *Info) ReleaseHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, err := json.Marshal(s.bp)
if err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(500)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write(data)
})
}