-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonitoring.go
112 lines (88 loc) · 2.63 KB
/
monitoring.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
package tel
import (
"context"
"net/http"
"net/http/pprof"
"time"
"github.com/d7561985/tel/v2/monitoring/checkers"
health "github.com/d7561985/tel/v2/monitoring/heallth"
"github.com/pkg/errors"
)
const (
HealthEndpoint = "/health"
PprofIndexEndpoint = "/debug/pprof"
EchoShutdownTimeout = 5 * time.Second
)
type (
HealthChecker struct {
Name string
Handler health.Checker
}
Monitor interface {
AddHealthChecker(ctx context.Context, handlers ...HealthChecker)
}
monitor struct {
server *http.Server
health *HealthHandler
isDebug bool
}
)
func createMonitor(addr string, isDebug bool) *monitor {
return &monitor{isDebug: isDebug, server: &http.Server{Addr: addr}, health: NewHealthHandler()}
}
func createNilMonitor() Monitor {
return &monitor{health: NewHealthHandler()}
}
func (m *monitor) AddHealthChecker(ctx context.Context, handlers ...HealthChecker) {
if len(handlers) == 0 {
FromCtx(ctx).Warn("health checker is empty")
}
for _, c := range handlers {
if c.Handler == nil {
FromCtx(ctx).Fatal("add empty health handler",
String("name", c.Name))
}
m.health.CompositeChecker.AddChecker(c.Name, c.Handler)
}
}
func (m *monitor) route(ctx context.Context) {
mux := http.NewServeMux()
mux.Handle(HealthEndpoint, m.health)
if m.isDebug {
FromCtx(ctx).Info("monitor enable pprof endpoint", String("path", PprofIndexEndpoint))
mux.Handle(PprofIndexEndpoint+"/", http.HandlerFunc(pprof.Index))
mux.Handle(PprofIndexEndpoint+"/cmdline/", http.HandlerFunc(pprof.Cmdline))
mux.Handle(PprofIndexEndpoint+"/profile/", http.HandlerFunc(pprof.Profile))
mux.Handle(PprofIndexEndpoint+"/symbol/", http.HandlerFunc(pprof.Symbol))
mux.Handle(PprofIndexEndpoint+"/trace/", http.HandlerFunc(pprof.Trace))
}
m.server.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// create new copy of logger (aka session)
req := r.WithContext(FromCtx(ctx).Ctx())
mux.ServeHTTP(w, req)
})
}
// Start is blocking operation
func (m *monitor) Start(ctx context.Context) {
if m.server == nil {
return
}
m.route(ctx)
FromCtx(ctx).Info("start monitor", String("addr", m.server.Addr))
if err := m.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
FromCtx(ctx).Fatal("start monitor", Error(err))
}
}
func (m *monitor) GracefulStop(_ctx context.Context) {
if m.server == nil {
return
}
if m.health != nil {
m.health.AddChecker("ShutDown", checkers.ShutDown())
}
ctx, cancel := context.WithTimeout(_ctx, EchoShutdownTimeout)
defer cancel()
if err := m.server.Shutdown(ctx); err != nil {
FromCtx(ctx).Error("monitoring shutdown failed", Error(err))
}
}