Skip to content

Commit

Permalink
Rename internal http server to instrumentation http server
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed May 1, 2022
1 parent be3afa5 commit b6f7b28
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 111 deletions.
90 changes: 90 additions & 0 deletions server/instrumentation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2019, Janoš Guljaš <[email protected]>
// 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),
)
}
90 changes: 0 additions & 90 deletions server/internal.go

This file was deleted.

42 changes: 21 additions & 21 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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),
))
}
Expand All @@ -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

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit b6f7b28

Please sign in to comment.