Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,13 @@ 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
```

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(<statsd addr>, <metrics-prefix>)` 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:
Expand Down
1 change: 0 additions & 1 deletion examples/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 0 additions & 13 deletions hystrix/hystrix_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -36,8 +34,6 @@ type Client struct {
retryCount int
retryableCodes []int
retryErrorBudget *internal.ErrorBudget

statsD *plugins.StatsdCollectorConfig
}

const (
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 13 additions & 2 deletions hystrix/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
18 changes: 0 additions & 18 deletions hystrix/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func TestOptionsAreSet(t *testing.T) {
WithErrorPercentThreshold(30),
WithSleepWindow(5),
WithRequestVolumeThreshold(5),
WithStatsDCollector("localhost:8125", "myapp.hystrix"),
WithRetryableStatusCodes(400, 200, 424),
)

Expand All @@ -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)
}

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

Expand Down
Loading