Skip to content

Commit

Permalink
add option to expose hystrix metrics to statd collector
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanime authored and rShetty committed Jun 3, 2020
1 parent 2c43ae5 commit b1d6562
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ 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
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ module github.com/gojektech/heimdall/v6
go 1.14

require (
github.com/DataDog/datadog-go v3.7.1+incompatible // indirect
github.com/afex/hystrix-go v0.0.0-20180209013831-27fae8d30f1a
github.com/cactus/go-statsd-client/statsd v0.0.0-20200423205355-cb0885a1018c // indirect
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/gojektech/valkyrie v0.0.0-20180215180059-6aee720afcdf
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect
github.com/jtolds/gls v4.2.1+incompatible // indirect
github.com/mattn/goveralls v0.0.6 // indirect
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 // indirect
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c // indirect
github.com/stretchr/objx v0.1.1 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
github.com/DataDog/datadog-go v3.7.1+incompatible h1:HmA9qHVrHIAqpSvoCYJ+c6qst0lgqEhNW6/KwfkHbS8=
github.com/DataDog/datadog-go v3.7.1+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/afex/hystrix-go v0.0.0-20180209013831-27fae8d30f1a h1:kUr+IdWoKBJQ+e0LC/ysc1w5clvmxbvNNE+lK2yGPrQ=
github.com/afex/hystrix-go v0.0.0-20180209013831-27fae8d30f1a/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/cactus/go-statsd-client v3.2.0+incompatible h1:ZJpQV7zHnerDzsEQS1wnI38tpR7wX3QFmL7WzTerEmY=
github.com/cactus/go-statsd-client/statsd v0.0.0-20200423205355-cb0885a1018c h1:HIGF0r/56+7fuIZw2V4isE22MK6xpxWx7BbV8dJ290w=
github.com/cactus/go-statsd-client/statsd v0.0.0-20200423205355-cb0885a1018c/go.mod h1:l/bIBLeOl9eX+wxJAzxS4TveKRtAqlyDpHjhkfO0MEI=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gojektech/valkyrie v0.0.0-20180215180059-6aee720afcdf h1:WUa/Tvd+vZuW17gOND3CryHvG0yc2nhC1gr+H2F7bFM=
Expand All @@ -14,6 +19,8 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0 h1:MkV+77GLUNo5oJ0jf870itWm3D0Sjh7+Za9gazKc5LQ=
github.com/rcrowley/go-metrics v0.0.0-20200313005456-10cdbea86bc0/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c h1:Ho+uVpkel/udgjbwB5Lktg9BtvJSh2DT0Hi6LPSyI2w=
Expand Down
12 changes: 12 additions & 0 deletions hystrix/hystrix_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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/gojektech/heimdall/v6"
"github.com/gojektech/heimdall/v6/httpclient"
"github.com/pkg/errors"
Expand All @@ -29,6 +31,7 @@ type Client struct {
retryCount int
retrier heimdall.Retriable
fallbackFunc func(err error) error
statsD *plugins.StatsdCollectorConfig
}

const (
Expand Down Expand Up @@ -65,6 +68,15 @@ 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
8 changes: 8 additions & 0 deletions hystrix/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hystrix
import (
"time"

"github.com/afex/hystrix-go/plugins"
"github.com/gojektech/heimdall/v6"
"github.com/gojektech/heimdall/v6/httpclient"
)
Expand Down Expand Up @@ -87,3 +88,10 @@ func WithHTTPClient(client heimdall.Doer) Option {
opt(c.client)
}
}

// WithStatsDCollector exports hystrix metrics to a statsD backend
func WithStatsDCollector(addr, prefix string) Option {
return func(c *Client) {
c.statsD = &plugins.StatsdCollectorConfig{StatsdAddr: addr, Prefix: prefix}
}
}
18 changes: 18 additions & 0 deletions hystrix/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestOptionsAreSet(t *testing.T) {
WithErrorPercentThreshold(30),
WithSleepWindow(5),
WithRequestVolumeThreshold(5),
WithStatsDCollector("localhost:8125", "myapp.hystrix"),
)

assert.Equal(t, 10*time.Second, c.timeout)
Expand All @@ -27,6 +28,8 @@ 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)
}

func TestOptionsHaveDefaults(t *testing.T) {
Expand All @@ -39,6 +42,7 @@ 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 @@ -126,3 +130,17 @@ func ExampleWithRetrier() {
// retry attempt 3
// error
}

func ExampleWithStatsDCollector() {
c := NewClient(WithStatsDCollector("localhost:8125", "myapp.hystrix"))
req, err := http.NewRequest(http.MethodGet, "https://www.gojek.io/", 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
}

0 comments on commit b1d6562

Please sign in to comment.