-
Notifications
You must be signed in to change notification settings - Fork 25
/
server.go
272 lines (242 loc) · 8.58 KB
/
server.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// +kubebuilder:validation:Optional
package http
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"time"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/fx"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/log"
"github.com/fluxninja/aperture/v2/pkg/metrics"
"github.com/fluxninja/aperture/v2/pkg/net/listener"
panichandler "github.com/fluxninja/aperture/v2/pkg/panic-handler"
"github.com/fluxninja/aperture/v2/pkg/utils"
)
const (
defaultServerKey = "server.http"
defaultHandlerName = "default"
)
type monitoringContext struct {
context.Context
handlerName string
}
// ServerModule is an fx module that provides annotated HTTP Server using the default listener and registers its metrics with the prometheus registry.
func ServerModule() fx.Option {
return fx.Options(
ServerConstructor{}.Annotate(),
)
}
// HTTPServerConfig holds configuration for HTTP Server.
// swagger:model
// +kubebuilder:object:generate=true
type HTTPServerConfig struct {
// Idle timeout
IdleTimeout config.Duration `json:"idle_timeout" validate:"gte=0s" default:"30s"`
// Read header timeout
ReadHeaderTimeout config.Duration `json:"read_header_timeout" validate:"gte=0s" default:"10s"`
// Read timeout
ReadTimeout config.Duration `json:"read_timeout" validate:"gte=0s" default:"10s"`
// Write timeout
WriteTimeout config.Duration `json:"write_timeout" validate:"gte=0s" default:"45s"`
// Max header size in bytes
MaxHeaderBytes int `json:"max_header_bytes" validate:"gte=0" default:"1048576"`
// Buckets specification in latency histogram
LatencyBucketsMS []float64 `json:"latency_buckets_ms" validate:"gte=0" default:"[10.0,25.0,100.0,250.0,1000.0]"`
// Disable HTTP Keepalive
DisableHTTPKeepAlives bool `json:"disable_http_keep_alives" default:"false"`
}
// ServerConstructor holds fields to create an annotated HTTP Server.
type ServerConstructor struct {
// Name of http server instance -- empty for main server
Name string
// Name of listener instance
ListenerName string
// Name of tls config instance
TLSConfigName string
// Viper config key/server name
ConfigKey string
// Default Server Config
DefaultConfig HTTPServerConfig
}
// Annotate creates an annotated instance of HTTP Server.
func (constructor ServerConstructor) Annotate() fx.Option {
if constructor.ConfigKey == "" {
constructor.ConfigKey = defaultServerKey
}
tlsName := config.NameTag(constructor.TLSConfigName) + ` optional:"true"`
name := config.NameTag(constructor.Name)
return fx.Options(
fx.Provide(
fx.Annotate(
constructor.provideServer,
fx.ParamTags(config.NameTag(constructor.ListenerName), tlsName),
fx.ResultTags(name, name, name),
),
),
)
}
// Server holds fields for custom HTTP server.
type Server struct {
Server *http.Server
Mux *mux.Router
Listener *listener.Listener
TLSConfig *tls.Config
// As we are using Gorilla Mux, root handler is registered the last as a catch all
RootHandler http.Handler
RequestCounters *prometheus.CounterVec
ErrorCounters *prometheus.CounterVec
Latencies *prometheus.HistogramVec
}
func (constructor ServerConstructor) provideServer(
listener *listener.Listener,
tlsConfig *tls.Config,
unmarshaller config.Unmarshaller,
lifecycle fx.Lifecycle,
shutdowner fx.Shutdowner,
pr *prometheus.Registry,
) (*mux.Router, *http.Server, *Server, error) {
config := constructor.DefaultConfig
if err := unmarshaller.UnmarshalKey(constructor.ConfigKey, &config); err != nil {
log.Error().Err(err).Msg("Unable to deserialize httpserver configuration!")
return nil, nil, nil, err
}
// Register metrics
defaultLabels := []string{metrics.MethodLabel, metrics.StatusCodeLabel, metrics.HandlerName}
errorCounters := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: metrics.HTTPErrorMetricName,
Help: "The total number of errors that occurred",
}, defaultLabels)
requestCounters := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: metrics.HTTPRequestMetricName,
Help: "The total number of requests that occurred",
}, defaultLabels)
// We record latency milliseconds
latencyHistograms := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: metrics.HTTPRequestLatencyMetricName,
Help: "Latency of the requests processed by the server",
Buckets: config.LatencyBucketsMS,
}, defaultLabels)
for _, metric := range []prometheus.Collector{errorCounters, requestCounters, latencyHistograms} {
err := pr.Register(metric)
if err != nil {
// Ignore already registered error, as this is not harmful. Metrics may
// be registered by other running server.
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
return nil, nil, nil, fmt.Errorf("could not register prometheus metrics: %w", err)
}
}
}
router := mux.NewRouter()
server := &http.Server{
Handler: router,
MaxHeaderBytes: config.MaxHeaderBytes,
IdleTimeout: config.IdleTimeout.AsDuration(),
ReadHeaderTimeout: config.ReadHeaderTimeout.AsDuration(),
ReadTimeout: config.ReadTimeout.AsDuration(),
WriteTimeout: config.WriteTimeout.AsDuration(),
TLSConfig: tlsConfig,
}
httpServer := &Server{
Server: server,
Mux: router,
Listener: listener,
TLSConfig: tlsConfig,
RequestCounters: requestCounters,
ErrorCounters: errorCounters,
Latencies: latencyHistograms,
}
router.Use(httpServer.monitoringMiddleware)
lifecycle.Append(fx.Hook{
OnStart: func(_ context.Context) error {
panichandler.Go(func() {
// request shutdown if this server exits
defer func() {
utils.Shutdown(shutdowner)
}()
listener := listener.GetListener()
if tlsConfig != nil {
listener = tls.NewListener(listener, tlsConfig)
}
log.Info().Str("constructor", constructor.ConfigKey).Str("addr", listener.Addr().String()).Msg("Starting HTTP server")
// check if RootHandler is set
if httpServer.RootHandler != nil {
log.Info().Msg("Registering RootHandlerFunc!")
router.PathPrefix("/").Handler(httpServer.RootHandler)
}
if err := server.Serve(listener); err != http.ErrServerClosed {
log.Error().Err(err).Msg("Unable to start HTTP server!")
}
})
return nil
},
OnStop: func(ctx context.Context) error {
listener := listener.GetListener()
log.Info().Str("constructor", constructor.ConfigKey).Str("addr", listener.Addr().String()).Msg("Stopping HTTP server")
return server.Shutdown(ctx)
},
})
return router, server, httpServer, nil
}
func (s *Server) monitoringMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := &monitoringContext{}
ctx.Context = r.Context()
ctx.handlerName = defaultHandlerName
startTime := time.Now()
rec := newStatusRecorder(w)
// Call the next handler, which can be another middleware in the chain, or the final handler.
next.ServeHTTP(rec, r.WithContext(ctx))
duration := time.Since(startTime)
labels := map[string]string{
metrics.MethodLabel: r.Method,
metrics.StatusCodeLabel: fmt.Sprintf("%d", rec.statusCode),
metrics.HandlerName: ctx.handlerName,
}
requestCounter, err := s.RequestCounters.GetMetricWith(labels)
if err != nil {
log.Debug().Msgf("Could not extract request counter metric from registry: %v", err)
} else {
requestCounter.Inc()
}
errorCounter, err := s.ErrorCounters.GetMetricWith(labels)
if err != nil {
log.Debug().Msgf("Could not extract error counter metric from registry: %v", err)
} else if rec.statusCode >= http.StatusBadRequest {
errorCounter.Inc()
}
latencyHistogram, err := s.Latencies.GetMetricWith(labels)
if err != nil {
log.Debug().Msgf("Could not extract latency histogram metric from registry: %v", err)
} else {
latencyHistogram.Observe(float64(duration.Milliseconds()))
}
})
}
// HandlerNameMiddleware sets handler name in monitoring context.
func HandlerNameMiddleware(handlerName string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if mCtx, ok := ctx.(*monitoringContext); ok {
mCtx.handlerName = handlerName
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
func newStatusRecorder(w http.ResponseWriter) *statusRecorder {
return &statusRecorder{ResponseWriter: w}
}
type statusRecorder struct {
http.ResponseWriter
statusCode int
}
// WriteHeader records statusCode and calls wrapped WriteHeader method.
func (sr *statusRecorder) WriteHeader(statusCode int) {
sr.statusCode = statusCode
sr.ResponseWriter.WriteHeader(statusCode)
}