Skip to content

Commit

Permalink
proxy: support configurable metrics (#3027)
Browse files Browse the repository at this point in the history
Use configed metrics and fallback to global for backwards compatibility.

Updates #3026

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
  • Loading branch information
AlexanderYastrebov authored Apr 22, 2024
1 parent 2a5d037 commit 5dc8098
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
9 changes: 4 additions & 5 deletions proxy/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()

Expand Down
10 changes: 9 additions & 1 deletion proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 6 additions & 5 deletions skipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 5dc8098

Please sign in to comment.