Skip to content

Commit

Permalink
Merge pull request #21 from baskarap/register-hystrix-metrics
Browse files Browse the repository at this point in the history
Register statsd collector for hystrix metrics
  • Loading branch information
Albertus Angga Raharja committed Jul 30, 2019
2 parents ab40603 + f91bcfb commit 664d287
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
20 changes: 18 additions & 2 deletions pkg/metrics/statsd_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package metrics

import (
"fmt"
"github.com/cactus/go-statsd-client/statsd"
"github.com/gojek/darkroom/pkg/logger"
"strings"
"time"

"github.com/afex/hystrix-go/hystrix/metric_collector"
"github.com/afex/hystrix-go/plugins"
"github.com/cactus/go-statsd-client/statsd"
"github.com/gojek/darkroom/pkg/logger"
)

// https://github.com/etsy/statsd/blob/master/docs/metric_types.md#multi-metric-packets
Expand Down Expand Up @@ -56,6 +59,19 @@ func InitializeStatsdCollector(config *StatsdCollectorConfig) error {
return nil
}

func RegisterHystrixMetrics(config *StatsdCollectorConfig, prefix string) error {
c, err := plugins.InitializeStatsdCollector(&plugins.StatsdCollectorConfig{
StatsdAddr: config.StatsdAddr,
Prefix: prefix,
})
if err != nil {
logger.Errorf("failed to initialize statsd collector for hystrix metrics with error: %s", err.Error())
return err
}
metricCollector.Registry.Register(c.NewStatsdCollector)
return nil
}

func formatter(updateOption UpdateOption) string {
scope := strings.Trim(updateOption.Scope, ".")
if updateOption.Scope == "" {
Expand Down
15 changes: 13 additions & 2 deletions pkg/metrics/statsd_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package metrics
import (
"errors"
"fmt"
"testing"
"time"

"github.com/cactus/go-statsd-client/statsd"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
"time"
)

func TestInitializeStatsdCollector(t *testing.T) {
Expand All @@ -27,6 +28,16 @@ func TestInitializeStatsdCollector(t *testing.T) {
assert.Equal(t, float32(1), instance.sampleRate)
}

func TestRegisterHystrixMetrics(t *testing.T) {
err := RegisterHystrixMetrics(&StatsdCollectorConfig{}, "prefix")
assert.Nil(t, err)

err = RegisterHystrixMetrics(&StatsdCollectorConfig{
StatsdAddr: "foo:bar:foo",
}, "prefix")
assert.NotNil(t, err)
}

func TestUpdate(t *testing.T) {
// Test when instance is nil
instance = nil
Expand Down

0 comments on commit 664d287

Please sign in to comment.