From 78bb599c50ead6cc8597287fb1c9ab7baa1282a1 Mon Sep 17 00:00:00 2001 From: Alexander Yastrebov Date: Fri, 19 Apr 2024 12:18:50 +0200 Subject: [PATCH] proxy: support configurable metrics Use configed metrics and fallback to global for backwards compatibility. Updates #3026 Signed-off-by: Alexander Yastrebov --- proxy/metrics_test.go | 9 ++++----- proxy/proxy.go | 10 +++++++++- skipper.go | 11 ++++++----- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/proxy/metrics_test.go b/proxy/metrics_test.go index 5342f2adcf..f601c4e319 100644 --- a/proxy/metrics_test.go +++ b/proxy/metrics_test.go @@ -9,19 +9,15 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/zalando/skipper/filters/builtin" - "github.com/zalando/skipper/metrics" "github.com/zalando/skipper/metrics/metricstest" + "github.com/zalando/skipper/proxy" "github.com/zalando/skipper/proxy/proxytest" "github.com/zalando/skipper/routing" "github.com/zalando/skipper/routing/testdataclient" ) func TestMetricsUncompressed(t *testing.T) { - dm := metrics.Default - t.Cleanup(func() { metrics.Default = dm }) - m := &metricstest.MockMetrics{} - metrics.Default = m // will update routes after proxy address is known dc := testdataclient.New(nil) @@ -32,6 +28,9 @@ func TestMetricsUncompressed(t *testing.T) { FilterRegistry: builtin.MakeRegistry(), DataClients: []routing.DataClient{dc}, }, + ProxyParams: proxy.Params{ + Metrics: m, + }, }.Create() defer p.Close() diff --git a/proxy/proxy.go b/proxy/proxy.go index d2156d8f55..6e0616530b 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -217,6 +217,10 @@ type Params struct { // Control flags. See the Flags values. Flags Flags + // Metrics collector. + // If not specified proxy uses global metrics.Default. + Metrics metrics.Metrics + // And optional list of priority routes to be used for matching // before the general lookup tree. PriorityRoutes []PriorityRoute @@ -766,7 +770,11 @@ func WithParams(p Params) *Proxy { } } - m := metrics.Default + m := p.Metrics + if m == nil { + m = metrics.Default + } + if p.Flags.Debug() { m = metrics.Void } diff --git a/skipper.go b/skipper.go index ef179a5252..c7e9aa2d71 100644 --- a/skipper.go +++ b/skipper.go @@ -1309,9 +1309,8 @@ func listenAndServeQuit( } if o.EnableConnMetricsServer { - m := metrics.Default srv.ConnState = func(conn net.Conn, state http.ConnState) { - m.IncCounter(fmt.Sprintf("lb-conn-%s", state)) + mtr.IncCounter(fmt.Sprintf("lb-conn-%s", state)) } } @@ -1521,6 +1520,7 @@ func run(o Options, sig chan os.Signal, idleConnsCH chan struct{}) error { if mtr == nil { mtr = metrics.NewMetrics(mtrOpts) } + // set global instance for backwards compatibility metrics.Default = mtr // *DEPRECATED* client tracking parameter @@ -2010,10 +2010,8 @@ func run(o Options, sig chan os.Signal, idleConnsCH chan struct{}) error { proxyFlags := proxy.Flags(o.ProxyOptions) | o.ProxyFlags proxyParams := proxy.Params{ Routing: routing, - EndpointRegistry: endpointRegistry, - EnablePassiveHealthCheck: passiveHealthCheckEnabled, - PassiveHealthCheck: passiveHealthCheck, Flags: proxyFlags, + Metrics: mtr, PriorityRoutes: o.PriorityRoutes, IdleConnectionsPerHost: o.IdleConnectionsPerHost, CloseIdleConnsPeriod: o.CloseIdleConnsPeriod, @@ -2034,6 +2032,9 @@ func run(o Options, sig chan os.Signal, idleConnsCH chan struct{}) error { ClientTLS: o.ClientTLS, CustomHttpRoundTripperWrap: o.CustomHttpRoundTripperWrap, RateLimiters: ratelimitRegistry, + EndpointRegistry: endpointRegistry, + EnablePassiveHealthCheck: passiveHealthCheckEnabled, + PassiveHealthCheck: passiveHealthCheck, } if o.EnableBreakers || len(o.BreakerSettings) > 0 {