Skip to content

Commit

Permalink
Make HTTP middleware optional (#194)
Browse files Browse the repository at this point in the history
* Make HTTP middleware optional
* Allow the Router to be set by caller

Signed-off-by: Michel Hollands <[email protected]>
  • Loading branch information
MichelHollands authored Sep 14, 2020
1 parent cef15bc commit 61ffdd4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
28 changes: 21 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ type Config struct {
HTTPServerWriteTimeout time.Duration `yaml:"http_server_write_timeout"`
HTTPServerIdleTimeout time.Duration `yaml:"http_server_idle_timeout"`

GRPCOptions []grpc.ServerOption `yaml:"-"`
GRPCMiddleware []grpc.UnaryServerInterceptor `yaml:"-"`
GRPCStreamMiddleware []grpc.StreamServerInterceptor `yaml:"-"`
HTTPMiddleware []middleware.Interface `yaml:"-"`
GRPCOptions []grpc.ServerOption `yaml:"-"`
GRPCMiddleware []grpc.UnaryServerInterceptor `yaml:"-"`
GRPCStreamMiddleware []grpc.StreamServerInterceptor `yaml:"-"`
HTTPMiddleware []middleware.Interface `yaml:"-"`
Router *mux.Router `yaml:"-"`
DoNotAddDefaultHTTPMiddleware bool `yaml:"-"`

GPRCServerMaxRecvMsgSize int `yaml:"grpc_server_max_recv_msg_size"`
GRPCServerMaxSendMsgSize int `yaml:"grpc_server_max_send_msg_size"`
Expand Down Expand Up @@ -270,7 +272,12 @@ func New(cfg Config) (*Server, error) {
grpcServer := grpc.NewServer(grpcOptions...)

// Setup HTTP server
router := mux.NewRouter()
var router *mux.Router
if cfg.Router != nil {
router = cfg.Router
} else {
router = mux.NewRouter()
}
if cfg.PathPrefix != "" {
// Expect metrics and pprof handlers to be prefixed with server's path prefix.
// e.g. /loki/metrics or /loki/debug/pprof
Expand All @@ -279,14 +286,16 @@ func New(cfg Config) (*Server, error) {
if cfg.RegisterInstrumentation {
RegisterInstrumentation(router)
}

var sourceIPs *middleware.SourceIPExtractor
if cfg.LogSourceIPs {
sourceIPs, err = middleware.NewSourceIPs(cfg.LogSourceIPsHeader, cfg.LogSourceIPsRegex)
if err != nil {
return nil, fmt.Errorf("error setting up source IP extraction: %v", err)
}
}
httpMiddleware := []middleware.Interface{

defaultHTTPMiddleware := []middleware.Interface{
middleware.Tracer{
RouteMatcher: router,
SourceIPs: sourceIPs,
Expand All @@ -303,8 +312,13 @@ func New(cfg Config) (*Server, error) {
InflightRequests: inflightRequests,
},
}
httpMiddleware := []middleware.Interface{}
if cfg.DoNotAddDefaultHTTPMiddleware {
httpMiddleware = cfg.HTTPMiddleware
} else {
httpMiddleware = append(defaultHTTPMiddleware, cfg.HTTPMiddleware...)
}

httpMiddleware = append(httpMiddleware, cfg.HTTPMiddleware...)
httpServer := &http.Server{
ReadTimeout: cfg.HTTPServerReadTimeout,
WriteTimeout: cfg.HTTPServerWriteTimeout,
Expand Down
15 changes: 9 additions & 6 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"google.golang.org/grpc/status"

google_protobuf "github.com/golang/protobuf/ptypes/empty"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
node_https "github.com/prometheus/node_exporter/https"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -425,12 +426,14 @@ func TestMiddlewareLogging(t *testing.T) {
var level logging.Level
level.Set("info")
cfg := Config{
HTTPListenAddress: "localhost",
HTTPListenPort: 9192,
GRPCListenAddress: "localhost",
HTTPMiddleware: []middleware.Interface{middleware.Logging},
MetricsNamespace: "testing_logging",
LogLevel: level,
HTTPListenAddress: "localhost",
HTTPListenPort: 9192,
GRPCListenAddress: "localhost",
HTTPMiddleware: []middleware.Interface{middleware.Logging},
MetricsNamespace: "testing_logging",
LogLevel: level,
DoNotAddDefaultHTTPMiddleware: true,
Router: &mux.Router{},
}
server, err := New(cfg)
require.NoError(t, err)
Expand Down

0 comments on commit 61ffdd4

Please sign in to comment.