From b6f7b284613f4885008315fd3f707777c2f02a15 Mon Sep 17 00:00:00 2001 From: Janos Guljas Date: Sun, 1 May 2022 15:31:55 +0200 Subject: [PATCH] Rename internal http server to instrumentation http server --- server/instrumentation.go | 90 +++++++++++++++++++++++++++++++++++++++ server/internal.go | 90 --------------------------------------- server/server.go | 42 +++++++++--------- 3 files changed, 111 insertions(+), 111 deletions(-) create mode 100644 server/instrumentation.go delete mode 100644 server/internal.go diff --git a/server/instrumentation.go b/server/instrumentation.go new file mode 100644 index 0000000..733ca04 --- /dev/null +++ b/server/instrumentation.go @@ -0,0 +1,90 @@ +// Copyright (c) 2019, Janoš Guljaš +// All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package server + +import ( + "expvar" + "net/http" + "net/http/pprof" + + "github.com/gorilla/handlers" + "github.com/prometheus/client_golang/prometheus/promhttp" + "resenje.org/jsonhttp" + "resenje.org/x/datadump" + + "resenje.org/web" +) + +func newInstrumentationRouter(s *Server, setupFunc func(base, api *http.ServeMux)) http.Handler { + // + // Top level instrumentation router + // + baseRouter := http.NewServeMux() + + // + // Instrumentation router + // + instrumentationRouter := http.NewServeMux() + baseRouter.Handle("/", web.ChainHandlers( + handlers.CompressHandler, + s.textRecoveryHandler, + web.NoCacheHeadersHandler, + web.FinalHandler(instrumentationRouter), + )) + instrumentationRouter.Handle("/", http.HandlerFunc(textNotFoundHandler)) + instrumentationRouter.Handle("/status", http.HandlerFunc(s.statusHandler)) + instrumentationRouter.Handle("/data", datadump.Handler(s.dataDumpServices, s.name+"_"+s.Version(), s.logger)) + + instrumentationRouter.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index)) + instrumentationRouter.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline)) + instrumentationRouter.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile)) + instrumentationRouter.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol)) + instrumentationRouter.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace)) + + instrumentationRouter.Handle("/debug/vars", expvar.Handler()) + + // + // Instrumentation API router + // + instrumentationAPIRouter := http.NewServeMux() + baseRouter.Handle("/api/", web.ChainHandlers( + handlers.CompressHandler, + s.jsonRecoveryHandler, + web.NoCacheHeadersHandler, + web.FinalHandler(instrumentationAPIRouter), + )) + instrumentationAPIRouter.Handle("/api/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + jsonhttp.NotFound(w, nil) + })) + instrumentationAPIRouter.Handle("/api/status", http.HandlerFunc(s.statusAPIHandler)) + if s.maintenanceService != nil { + instrumentationAPIRouter.Handle("/api/maintenance", jsonMethodHandler{ + "GET": http.HandlerFunc(s.maintenanceService.StatusHandler), + "POST": http.HandlerFunc(s.maintenanceService.OnHandler), + "DELETE": http.HandlerFunc(s.maintenanceService.OffHandler), + }) + } + baseRouter.Handle("/metrics", promhttp.InstrumentMetricHandler( + s.metricsRegistry, + promhttp.HandlerFor(s.metricsRegistry, promhttp.HandlerOpts{}), + )) + + if setupFunc != nil { + setupFunc(baseRouter, instrumentationAPIRouter) + } + + // + // Final instrumentation handler + // + return web.ChainHandlers( + func(h http.Handler) http.Handler { + return web.NewSetHeadersHandler(h, map[string]string{ + "Server": s.name + "/" + s.Version(), + }) + }, + web.FinalHandler(baseRouter), + ) +} diff --git a/server/internal.go b/server/internal.go deleted file mode 100644 index b03a03a..0000000 --- a/server/internal.go +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright (c) 2019, Janoš Guljaš -// All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package server - -import ( - "expvar" - "net/http" - "net/http/pprof" - - "github.com/gorilla/handlers" - "github.com/prometheus/client_golang/prometheus/promhttp" - "resenje.org/jsonhttp" - "resenje.org/x/datadump" - - "resenje.org/web" -) - -func newInternalRouter(s *Server, setupFunc func(base, api *http.ServeMux)) http.Handler { - // - // Top level internal router - // - internalBaseRouter := http.NewServeMux() - - // - // Internal router - // - internalRouter := http.NewServeMux() - internalBaseRouter.Handle("/", web.ChainHandlers( - handlers.CompressHandler, - s.textRecoveryHandler, - web.NoCacheHeadersHandler, - web.FinalHandler(internalRouter), - )) - internalRouter.Handle("/", http.HandlerFunc(textNotFoundHandler)) - internalRouter.Handle("/status", http.HandlerFunc(s.statusHandler)) - internalRouter.Handle("/data", datadump.Handler(s.dataDumpServices, s.name+"_"+s.Version(), s.logger)) - - internalRouter.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index)) - internalRouter.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline)) - internalRouter.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile)) - internalRouter.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol)) - internalRouter.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace)) - - internalRouter.Handle("/debug/vars", expvar.Handler()) - - // - // Internal API router - // - internalAPIRouter := http.NewServeMux() - internalBaseRouter.Handle("/api/", web.ChainHandlers( - handlers.CompressHandler, - s.jsonRecoveryHandler, - web.NoCacheHeadersHandler, - web.FinalHandler(internalAPIRouter), - )) - internalAPIRouter.Handle("/api/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - jsonhttp.NotFound(w, nil) - })) - internalAPIRouter.Handle("/api/status", http.HandlerFunc(s.statusAPIHandler)) - if s.maintenanceService != nil { - internalAPIRouter.Handle("/api/maintenance", jsonMethodHandler{ - "GET": http.HandlerFunc(s.maintenanceService.StatusHandler), - "POST": http.HandlerFunc(s.maintenanceService.OnHandler), - "DELETE": http.HandlerFunc(s.maintenanceService.OffHandler), - }) - } - internalBaseRouter.Handle("/metrics", promhttp.InstrumentMetricHandler( - s.metricsRegistry, - promhttp.HandlerFor(s.metricsRegistry, promhttp.HandlerOpts{}), - )) - - if setupFunc != nil { - setupFunc(internalBaseRouter, internalAPIRouter) - } - - // - // Final internal handler - // - return web.ChainHandlers( - func(h http.Handler) http.Handler { - return web.NewSetHeadersHandler(h, map[string]string{ - "Server": s.name + "/" + s.Version(), - }) - }, - web.FinalHandler(internalBaseRouter), - ) -} diff --git a/server/server.go b/server/server.go index a591a6c..96dca9d 100644 --- a/server/server.go +++ b/server/server.go @@ -89,8 +89,8 @@ func New(o Options) (s *Server, err error) { ) var certificates []tls.Certificate - if o.InternalTLSKey != "" && o.InternalTLSCert != "" { - cert, err := tls.LoadX509KeyPair(o.InternalTLSCert, o.InternalTLSKey) + if o.InstrumentationTLSKey != "" && o.InstrumentationTLSCert != "" { + cert, err := tls.LoadX509KeyPair(o.InstrumentationTLSCert, o.InstrumentationTLSKey) if err != nil { return nil, fmt.Errorf("load certificate: %v", err) } @@ -103,15 +103,15 @@ func New(o Options) (s *Server, err error) { ClientSessionCache: tls.NewLRUClientSessionCache(-1), } - internalRouter := newInternalRouter(s, o.SetupInternalRouters) - if o.ListenInternal != "" { - s.servers.Add("internal HTTP", o.ListenInternal, httpServer.New( - internalRouter, + instrumentationRouter := newInstrumentationRouter(s, o.SetupInstrumentationRouters) + if o.ListenInstrumentation != "" { + s.servers.Add("instrumentation HTTP", o.ListenInstrumentation, httpServer.New( + instrumentationRouter, )) } - if o.ListenInternalTLS != "" { - s.servers.Add("internal TLS HTTP", o.ListenInternalTLS, httpServer.New( - internalRouter, + if o.ListenInstrumentationTLS != "" { + s.servers.Add("instrumentation TLS HTTP", o.ListenInstrumentationTLS, httpServer.New( + instrumentationRouter, httpServer.WithTLSConfig(tlsConfig), )) } @@ -120,16 +120,16 @@ func New(o Options) (s *Server, err error) { // Options structure contains optional properties for the Server. type Options struct { - Name string - Version string - BuildInfo string - ListenInternal string - ListenInternalTLS string - InternalTLSCert string - InternalTLSKey string - ACMECertsDir string - ACMECertsEmail string - SetupInternalRouters func(base, api *http.ServeMux) + Name string + Version string + BuildInfo string + ListenInstrumentation string + ListenInstrumentationTLS string + InstrumentationTLSCert string + InstrumentationTLSKey string + ACMECertsDir string + ACMECertsEmail string + SetupInstrumentationRouters func(base, api *http.ServeMux) Logger servers.Logger @@ -332,13 +332,13 @@ func (s *Server) Shutdown(ctx context.Context) { } // WithMetrics registers prometheus collector to be exposed -// on internal handler /metrics request. +// on instrumentation handler /metrics request. func (s *Server) WithMetrics(cs ...prometheus.Collector) { s.metricsRegistry.MustRegister(cs...) } // WithDataDumpService adds a service with data dump interface -// to be used on internal handler /data request. +// to be used on instrumentation handler /data request. func (s *Server) WithDataDumpService(name string, service datadump.Interface) { s.dataDumpServices[name] = service }