From a7a546c21a6b614d94d720390db833028a5b9c46 Mon Sep 17 00:00:00 2001 From: Anmol Chopra Date: Fri, 26 Jun 2026 16:30:45 +0530 Subject: [PATCH] Deprecate statsd option --- README.md | 3 --- examples/client.go | 1 - hystrix/hystrix_client.go | 13 ------------- hystrix/options.go | 15 +++++++++++++-- hystrix/options_test.go | 18 ------------------ 5 files changed, 13 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 865fb4f..9b62f9b 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,6 @@ client := hystrix.NewClient( hystrix.WithHystrixTimeout(1000 * time.Millisecond), hystrix.WithMaxConcurrentRequests(30), hystrix.WithErrorPercentThreshold(20), - hystrix.WithStatsDCollector("localhost:8125", "myapp.hystrix"), ) // The rest is the same as the previous example @@ -108,8 +107,6 @@ client := hystrix.NewClient( In the above example, there are two timeout values used: one for the hystrix configuration, and one for the HTTP client configuration. The former determines the time at which hystrix should register an error, while the latter determines when the client itself should return a timeout error. Unless you have any special requirements, both of these would have the same values. -You can choose to export hystrix metrics to a statsD collector with the `hystrix.WithStatsDCollector(, )` option when initializing the client as shown above. - ### Creating a hystrix-like circuit breaker with fallbacks You can use the `hystrix.NewClient` function to create a client wrapped in a hystrix-like circuit breaker by passing in your own custom fallbacks: diff --git a/examples/client.go b/examples/client.go index 6f15545..8d0f436 100644 --- a/examples/client.go +++ b/examples/client.go @@ -53,7 +53,6 @@ func hystrixClientUsage() error { hystrix.WithErrorPercentThreshold(25), hystrix.WithSleepWindow(10), hystrix.WithRequestVolumeThreshold(10), - hystrix.WithStatsDCollector("localhost:8125", "myapp.hystrix"), ) headers := http.Header{} response, err := hystrixClient.Get(baseURL, headers) diff --git a/hystrix/hystrix_client.go b/hystrix/hystrix_client.go index f57ab2c..15ef8fd 100644 --- a/hystrix/hystrix_client.go +++ b/hystrix/hystrix_client.go @@ -9,8 +9,6 @@ import ( "time" "github.com/afex/hystrix-go/hystrix" - metricCollector "github.com/afex/hystrix-go/hystrix/metric_collector" - "github.com/afex/hystrix-go/plugins" "github.com/gojek/heimdall/v7" "github.com/gojek/heimdall/v7/httpclient" "github.com/gojek/heimdall/v7/internal" @@ -36,8 +34,6 @@ type Client struct { retryCount int retryableCodes []int retryErrorBudget *internal.ErrorBudget - - statsD *plugins.StatsdCollectorConfig } const ( @@ -72,15 +68,6 @@ func NewClient(opts ...Option) *Client { opt(&client) } - if client.statsD != nil { - c, err := plugins.InitializeStatsdCollector(client.statsD) - if err != nil { - panic(err) - } - - metricCollector.Registry.Register(c.NewStatsdCollector) - } - hystrix.ConfigureCommand(client.hystrixCommandName, hystrix.CommandConfig{ Timeout: durationToInt(client.hystrixTimeout, time.Millisecond), MaxConcurrentRequests: client.maxConcurrentRequests, diff --git a/hystrix/options.go b/hystrix/options.go index ac03c73..c12c460 100644 --- a/hystrix/options.go +++ b/hystrix/options.go @@ -5,6 +5,7 @@ import ( "slices" "time" + metricCollector "github.com/afex/hystrix-go/hystrix/metric_collector" "github.com/afex/hystrix-go/plugins" "github.com/gojek/heimdall/v7" "github.com/gojek/heimdall/v7/httpclient" @@ -103,10 +104,20 @@ func WithHTTPClient(client heimdall.Doer) Option { } // WithStatsDCollector exports hystrix metrics to a statsD backend +// +// Deprecated: hystrix plugin should only be registered once before any hystrix command is executed. +// Register plugins.InitializeStatsdCollector directly to continue using the functionality func WithStatsDCollector(addr, prefix string) Option { - return func(c *Client) { - c.statsD = &plugins.StatsdCollectorConfig{StatsdAddr: addr, Prefix: prefix} + collector, err := plugins.InitializeStatsdCollector(&plugins.StatsdCollectorConfig{ + StatsdAddr: addr, + Prefix: prefix, + }) + if err != nil { + panic(err) } + metricCollector.Registry.Register(collector.NewStatsdCollector) + + return func(_ *Client) {} } // WithRetryableStatusCodes sets status codes to be retried diff --git a/hystrix/options_test.go b/hystrix/options_test.go index 771675d..8d790e0 100644 --- a/hystrix/options_test.go +++ b/hystrix/options_test.go @@ -20,7 +20,6 @@ func TestOptionsAreSet(t *testing.T) { WithErrorPercentThreshold(30), WithSleepWindow(5), WithRequestVolumeThreshold(5), - WithStatsDCollector("localhost:8125", "myapp.hystrix"), WithRetryableStatusCodes(400, 200, 424), ) @@ -30,8 +29,6 @@ func TestOptionsAreSet(t *testing.T) { assert.Equal(t, 30, c.errorPercentThreshold) assert.Equal(t, 5, c.sleepWindow) assert.Equal(t, 5, c.requestVolumeThreshold) - assert.Equal(t, "localhost:8125", c.statsD.StatsdAddr) - assert.Equal(t, "myapp.hystrix", c.statsD.Prefix) assert.Equal(t, []int{200, 400, 424}, c.retryableCodes) } @@ -46,7 +43,6 @@ func TestOptionsHaveDefaults(t *testing.T) { assert.Equal(t, 25, c.errorPercentThreshold) assert.Equal(t, 10, c.sleepWindow) assert.Equal(t, 10, c.requestVolumeThreshold) - assert.Nil(t, c.statsD) } func ExampleWithHTTPTimeout() { @@ -134,20 +130,6 @@ func ExampleWithRetrier() { // error } -func ExampleWithStatsDCollector() { - c := NewClient(WithStatsDCollector("localhost:8125", "myapp.hystrix")) - req, err := http.NewRequest(http.MethodGet, "https://gojek.com/", nil) - if err != nil { - panic(err) - } - res, err := c.Do(req) - if err != nil { - panic(err) - } - fmt.Println("Response status : ", res.StatusCode) - // Output: Response status : 200 -} - func TestWithHTTPTimeoutOverridesUserHTTPClientTimeout(t *testing.T) { t.Parallel()