mirror of
https://github.com/umputun/reproxy.git
synced 2024-09-11 08:55:25 +03:00
eliminate metrics middleware hit if mgmt disabled
This commit is contained in:
parent
f043a91dca
commit
971e77b5d3
9
Makefile
9
Makefile
@ -20,18 +20,13 @@ race_test:
|
||||
build: info
|
||||
- cd app && GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "-X main.revision=$(REV) -s -w" -o ../dist/reproxy
|
||||
|
||||
site: build_site deploy_site
|
||||
|
||||
build_site:
|
||||
site:
|
||||
@rm -f site/public/*
|
||||
docker build -f Dockerfile.site -t reproxy.site .
|
||||
docker run -d --name=reproxy.site reproxy.site
|
||||
docker cp reproxy.site:/build/public site/
|
||||
docker rm -f reproxy.site
|
||||
|
||||
deploy_site:
|
||||
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress \
|
||||
./site/public/ reproxy.io:/srv/www/reproxy.io
|
||||
rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress ./site/public/ reproxy.io:/srv/www/reproxy.io
|
||||
|
||||
info:
|
||||
- @echo "revision $(REV)"
|
||||
|
32
app/main.go
32
app/main.go
@ -188,22 +188,24 @@ func run() error {
|
||||
}
|
||||
}()
|
||||
|
||||
metrics := mgmt.NewMetrics()
|
||||
go func() {
|
||||
mgSrv := mgmt.Server{
|
||||
Listen: opts.Management.Listen,
|
||||
Informer: svc,
|
||||
AssetsLocation: opts.Assets.Location,
|
||||
AssetsWebRoot: opts.Assets.WebRoot,
|
||||
Version: revision,
|
||||
Metrics: metrics,
|
||||
}
|
||||
if opts.Management.Enabled {
|
||||
if mgErr := mgSrv.Run(ctx); err != nil {
|
||||
log.Printf("[WARN] management service failed, %v", mgErr)
|
||||
var metrics *mgmt.Metrics // disabled by default
|
||||
if opts.Management.Enabled {
|
||||
metrics = mgmt.NewMetrics()
|
||||
go func() {
|
||||
mgSrv := mgmt.Server{
|
||||
Listen: opts.Management.Listen,
|
||||
Informer: svc,
|
||||
AssetsLocation: opts.Assets.Location,
|
||||
AssetsWebRoot: opts.Assets.WebRoot,
|
||||
Version: revision,
|
||||
}
|
||||
}
|
||||
}()
|
||||
if opts.Management.Enabled {
|
||||
if mgErr := mgSrv.Run(ctx); err != nil {
|
||||
log.Printf("[WARN] management service failed, %v", mgErr)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
cacheControl, err := proxy.MakeCacheControl(opts.Assets.CacheControl)
|
||||
if err != nil {
|
||||
|
@ -25,7 +25,7 @@ func Test_Main(t *testing.T) {
|
||||
"--static.rule=*,/svc2/(.*), https://echo.umputun.com/$1,https://feedmaster.umputun.com/ping",
|
||||
"--file.enabled", "--file.name=discovery/provider/testdata/config.yml",
|
||||
"--dbg", "--logger.enabled", "--logger.stdout", "--logger.file=/tmp/reproxy.log",
|
||||
"--listen=127.0.0.1:" + strconv.Itoa(port), "--signature",
|
||||
"--listen=127.0.0.1:" + strconv.Itoa(port), "--signature", "--mgmt.enabled",
|
||||
"--error.enabled", "--error.template=proxy/testdata/errtmpl.html",
|
||||
}
|
||||
defer os.Remove("/tmp/reproxy.log")
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
|
||||
log "github.com/go-pkgz/lgr"
|
||||
"github.com/go-pkgz/rest"
|
||||
|
||||
"github.com/umputun/reproxy/app/discovery"
|
||||
)
|
||||
|
||||
|
@ -20,6 +20,7 @@ import (
|
||||
"github.com/gorilla/handlers"
|
||||
|
||||
"github.com/umputun/reproxy/app/discovery"
|
||||
"github.com/umputun/reproxy/app/mgmt"
|
||||
)
|
||||
|
||||
// Http is a proxy server for both http and https
|
||||
@ -104,7 +105,7 @@ func (h *Http) Run(ctx context.Context) error {
|
||||
h.signatureHandler(),
|
||||
h.pingHandler,
|
||||
h.healthMiddleware,
|
||||
h.Metrics.Middleware,
|
||||
h.mgmtHandler(),
|
||||
h.headersHandler(h.ProxyHeaders),
|
||||
h.accessLogHandler(h.AccessLog),
|
||||
h.stdoutLogHandler(h.StdOutEnabled, logger.New(logger.Log(log.Default()), logger.Prefix("[INFO]")).Handler),
|
||||
@ -394,7 +395,18 @@ func (h *Http) maxReqSizeHandler(maxSize int64) func(next http.Handler) http.Han
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Http) mgmtHandler() func(next http.Handler) http.Handler {
|
||||
if h.Metrics.(*mgmt.Metrics) != nil { // type assertion needed because we compare interface to nil
|
||||
log.Printf("[DEBUG] metrics enabled")
|
||||
return h.Metrics.Middleware
|
||||
}
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Http) makeHTTPServer(addr string, router http.Handler) *http.Server {
|
||||
|
Loading…
Reference in New Issue
Block a user