/
system.go
208 lines (174 loc) · 4.73 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package operations
import (
"context"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"strings"
"time"
"github.com/cloudflare/cfssl/log"
kitstatsd "github.com/go-kit/kit/metrics/statsd"
"github.com/gorilla/mux"
"github.com/hyperledger/fabric-lib-go/healthz"
"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/kfsoftware/hlf-operator/internal/github.com/hyperledger/fabric-ca/lib/metadata"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// System is an operations server that is responsible for metrics and health checks
type System struct {
metrics.Provider
healthHandler *healthz.HealthHandler
options Options
statsd *kitstatsd.Statsd
sendTicker *time.Ticker
httpServer *http.Server
mux *mux.Router
addr string
}
// Options contains configuration for the operations system
type Options struct {
ListenAddress string
Metrics MetricsOptions
TLS TLS
}
// MetricsOptions contains the information on providers
type MetricsOptions struct {
Provider string
Statsd *Statsd
}
// Statsd contains configuration of statsd
type Statsd struct {
Network string
Address string
WriteInterval time.Duration
Prefix string
}
// NewSystem creates a System struct
func NewSystem(o Options) *System {
system := &System{
options: o,
}
system.initializeServer()
system.initializeHealthCheckHandler()
system.initializeMetricsProvider()
system.initializeVersionInfoHandler()
return system
}
// Start starts the operations system server
func (s *System) Start() error {
err := s.startMetricsTickers()
if err != nil {
return err
}
listener, err := s.listen()
if err != nil {
return err
}
s.addr = listener.Addr().String()
log.Infof("Operation Server Listening on %s", listener.Addr())
go s.httpServer.Serve(listener)
return nil
}
// Stop stop the operations system server
func (s *System) Stop() error {
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) initializeServer() {
s.mux = mux.NewRouter()
s.httpServer = &http.Server{
Addr: s.options.ListenAddress,
Handler: s.mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 2 * time.Minute,
}
}
func (s *System) initializeMetricsProvider() {
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
case "prometheus":
s.Provider = &prometheus.Provider{}
s.mux.Handle("/metrics", promhttp.Handler())
default:
if providerType != "disabled" {
log.Warningf("Unknown provider type: %s; metrics disabled", providerType)
}
s.Provider = &disabled.Provider{}
}
}
func (s *System) initializeHealthCheckHandler() {
s.healthHandler = healthz.NewHealthHandler()
s.mux.Handle("/healthz", s.healthHandler)
}
func (s *System) initializeVersionInfoHandler() {
version := fmt.Sprintf(`{"Version":"%s"}`, metadata.Version)
s.mux.HandleFunc("/version", func(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, version)
})
}
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()
writeInterval := s.options.Metrics.Statsd.WriteInterval
s.sendTicker = time.NewTicker(writeInterval)
go s.statsd.SendLoop(context.Background(), s.sendTicker.C, network, address)
}
return nil
}
// Log is a function required to meet the interface required by statsd
func (s *System) Log(keyvals ...interface{}) error {
log.Warning(keyvals...)
return nil
}
// RegisterChecker registers the HealthCheck with Healthchecker server
func (s *System) RegisterChecker(component string, checker healthz.HealthChecker) error {
return s.healthHandler.RegisterChecker(component, checker)
}
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
}
// Addr returns the address of the listener
func (s *System) Addr() string {
return s.addr
}