-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
251 lines (208 loc) · 5.6 KB
/
system.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright IBM Corp All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package operations
import (
"context"
"crypto/tls"
"net"
"net/http"
"os"
"strings"
"time"
kitstatsd "github.com/go-kit/kit/metrics/statsd"
"github.com/hyperledger/fabric-lib-go/healthz"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/flogging/httpadmin"
"github.com/hyperledger/fabric/common/metrics"
"github.com/hyperledger/fabric/common/metrics/disabled"
"github.com/hyperledger/fabric/common/metrics/prometheus"
"github.com/hyperledger/fabric/common/metrics/statsd"
"github.com/hyperledger/fabric/common/metrics/statsd/goruntime"
"github.com/hyperledger/fabric/common/util"
"github.com/hyperledger/fabric/core/middleware"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
//go:generate counterfeiter -o fakes/logger.go -fake-name Logger . Logger
type Logger interface {
Warn(args ...interface{})
Warnf(template string, args ...interface{})
}
type Statsd struct {
Network string
Address string
WriteInterval time.Duration
Prefix string
}
type MetricsOptions struct {
Provider string
Statsd *Statsd
}
type Options struct {
Logger Logger
ListenAddress string
Metrics MetricsOptions
TLS TLS
Version string
}
type System struct {
metrics.Provider
logger Logger
healthHandler *healthz.HealthHandler
options Options
statsd *kitstatsd.Statsd
collectorTicker *time.Ticker
sendTicker *time.Ticker
httpServer *http.Server
mux *http.ServeMux
addr string
versionGauge metrics.Gauge
}
func NewSystem(o Options) *System {
logger := o.Logger
if logger == nil {
logger = flogging.MustGetLogger("operations.runner")
}
system := &System{
logger: logger,
options: o,
}
system.initializeServer()
system.initializeHealthCheckHandler()
system.initializeLoggingHandler()
system.initializeMetricsProvider()
return system
}
func (s *System) Run(signals <-chan os.Signal, ready chan<- struct{}) error {
err := s.Start()
if err != nil {
return err
}
close(ready)
select {
case <-signals:
return s.Stop()
}
}
func (s *System) Start() error {
err := s.startMetricsTickers()
if err != nil {
return err
}
s.versionGauge.With("version", s.options.Version).Set(1)
listener, err := s.listen()
if err != nil {
return err
}
s.addr = listener.Addr().String()
go s.httpServer.Serve(listener)
return nil
}
func (s *System) Stop() error {
if s.collectorTicker != nil {
s.collectorTicker.Stop()
s.collectorTicker = nil
}
if s.sendTicker != nil {
s.sendTicker.Stop()
s.sendTicker = nil
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return s.httpServer.Shutdown(ctx)
}
func (s *System) RegisterChecker(component string, checker healthz.HealthChecker) error {
return s.healthHandler.RegisterChecker(component, checker)
}
func (s *System) initializeServer() {
s.mux = http.NewServeMux()
s.httpServer = &http.Server{
Addr: s.options.ListenAddress,
Handler: s.mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 2 * time.Minute,
}
}
func (s *System) handlerChain(h http.Handler, secure bool) http.Handler {
if secure {
return middleware.NewChain(middleware.RequireCert(), middleware.WithRequestID(util.GenerateUUID)).Handler(h)
}
return middleware.NewChain(middleware.WithRequestID(util.GenerateUUID)).Handler(h)
}
func (s *System) initializeMetricsProvider() error {
m := s.options.Metrics
providerType := m.Provider
switch providerType {
case "statsd":
prefix := m.Statsd.Prefix
if prefix != "" && !strings.HasSuffix(prefix, ".") {
prefix = prefix + "."
}
ks := kitstatsd.New(prefix, s)
s.Provider = &statsd.Provider{Statsd: ks}
s.statsd = ks
s.versionGauge = versionGauge(s.Provider)
return nil
case "prometheus":
s.Provider = &prometheus.Provider{}
s.versionGauge = versionGauge(s.Provider)
s.mux.Handle("/metrics", s.handlerChain(promhttp.Handler(), s.options.TLS.Enabled))
return nil
default:
if providerType != "disabled" {
s.logger.Warnf("Unknown provider type: %s; metrics disabled", providerType)
}
s.Provider = &disabled.Provider{}
s.versionGauge = versionGauge(s.Provider)
return nil
}
}
func (s *System) initializeLoggingHandler() {
s.mux.Handle("/logspec", s.handlerChain(httpadmin.NewSpecHandler(), s.options.TLS.Enabled))
}
func (s *System) initializeHealthCheckHandler() {
s.healthHandler = healthz.NewHealthHandler()
s.mux.Handle("/healthz", s.handlerChain(s.healthHandler, false))
}
func (s *System) startMetricsTickers() error {
m := s.options.Metrics
if s.statsd != nil {
network := m.Statsd.Network
address := m.Statsd.Address
c, err := net.Dial(network, address)
if err != nil {
return err
}
c.Close()
opts := s.options.Metrics.Statsd
writeInterval := opts.WriteInterval
s.collectorTicker = time.NewTicker(writeInterval / 2)
goCollector := goruntime.NewCollector(s.Provider)
go goCollector.CollectAndPublish(s.collectorTicker.C)
s.sendTicker = time.NewTicker(writeInterval)
go s.statsd.SendLoop(s.sendTicker.C, network, address)
}
return nil
}
func (s *System) listen() (net.Listener, error) {
listener, err := net.Listen("tcp", s.options.ListenAddress)
if err != nil {
return nil, err
}
tlsConfig, err := s.options.TLS.Config()
if err != nil {
return nil, err
}
if tlsConfig != nil {
listener = tls.NewListener(listener, tlsConfig)
}
return listener, nil
}
func (s *System) Addr() string {
return s.addr
}
func (s *System) Log(keyvals ...interface{}) error {
s.logger.Warn(keyvals...)
return nil
}