From f6c914c1e4f2b01082e2779bb84121ca1bf54141 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 15 Nov 2020 00:38:38 +0300 Subject: [PATCH] metrics: minor changes to interface and set default Signed-off-by: Vasiliy Tolstov --- metrics/noop.go | 37 +++++++++++++++++++++++++++++++++++ metrics/noop/reporter.go | 37 ----------------------------------- metrics/noop/reporter_test.go | 20 ------------------- metrics/reporter.go | 3 ++- metrics/reporter_test.go | 17 ++++++++++++++++ 5 files changed, 56 insertions(+), 58 deletions(-) create mode 100644 metrics/noop.go delete mode 100644 metrics/noop/reporter.go delete mode 100644 metrics/noop/reporter_test.go create mode 100644 metrics/reporter_test.go diff --git a/metrics/noop.go b/metrics/noop.go new file mode 100644 index 00000000..8f210b77 --- /dev/null +++ b/metrics/noop.go @@ -0,0 +1,37 @@ +package metrics + +import ( + "time" +) + +// NoopReporter is an noop implementation of Reporter: +type noopReporter struct { + opts Options +} + +// NewReporter returns a configured noop reporter: +func NewReporter(opts ...Option) Reporter { + return &noopReporter{ + opts: NewOptions(opts...), + } +} + +// Count implements the Reporter interface Count method: +func (r *noopReporter) Count(metricName string, value int64, tags Tags) error { + return nil +} + +// Gauge implements the Reporter interface Gauge method: +func (r *noopReporter) Gauge(metricName string, value float64, tags Tags) error { + return nil +} + +// Timing implements the Reporter interface Timing method: +func (r *noopReporter) Timing(metricName string, value time.Duration, tags Tags) error { + return nil +} + +// Options implements the Reporter interface Optios method: +func (r *noopReporter) Options() Options { + return r.opts +} diff --git a/metrics/noop/reporter.go b/metrics/noop/reporter.go deleted file mode 100644 index 9c5b4b97..00000000 --- a/metrics/noop/reporter.go +++ /dev/null @@ -1,37 +0,0 @@ -package noop - -import ( - "time" - - log "github.com/unistack-org/micro/v3/logger" - "github.com/unistack-org/micro/v3/metrics" -) - -// Reporter is an implementation of metrics.Reporter: -type Reporter struct { - options metrics.Options -} - -// New returns a configured noop reporter: -func New(opts ...metrics.Option) *Reporter { - log.Info("Metrics/NoOp - not doing anything") - - return &Reporter{ - options: metrics.NewOptions(opts...), - } -} - -// Count implements the metrics.Reporter interface Count method: -func (r *Reporter) Count(metricName string, value int64, tags metrics.Tags) error { - return nil -} - -// Gauge implements the metrics.Reporter interface Gauge method: -func (r *Reporter) Gauge(metricName string, value float64, tags metrics.Tags) error { - return nil -} - -// Timing implements the metrics.Reporter interface Timing method: -func (r *Reporter) Timing(metricName string, value time.Duration, tags metrics.Tags) error { - return nil -} diff --git a/metrics/noop/reporter_test.go b/metrics/noop/reporter_test.go deleted file mode 100644 index 08b92d6e..00000000 --- a/metrics/noop/reporter_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package noop - -import ( - "testing" - - "github.com/unistack-org/micro/v3/metrics" - - "github.com/stretchr/testify/assert" -) - -func TestNoopReporter(t *testing.T) { - - // Make a Reporter: - reporter := New(metrics.Path("/noop")) - assert.NotNil(t, reporter) - assert.Equal(t, "/noop", reporter.options.Path) - - // Check that our implementation is valid: - assert.Implements(t, new(metrics.Reporter), reporter) -} diff --git a/metrics/reporter.go b/metrics/reporter.go index 0a07ef43..84f998f6 100644 --- a/metrics/reporter.go +++ b/metrics/reporter.go @@ -7,7 +7,7 @@ import "time" type Tags map[string]string var ( - DefaultReporter Reporter + DefaultReporter Reporter = NewReporter() ) // Reporter is an interface for collecting and instrumenting metrics @@ -15,4 +15,5 @@ type Reporter interface { Count(id string, value int64, tags Tags) error Gauge(id string, value float64, tags Tags) error Timing(id string, value time.Duration, tags Tags) error + Options() Options } diff --git a/metrics/reporter_test.go b/metrics/reporter_test.go new file mode 100644 index 00000000..f528a90a --- /dev/null +++ b/metrics/reporter_test.go @@ -0,0 +1,17 @@ +package metrics + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNoopReporter(t *testing.T) { + // Make a Reporter: + reporter := NewReporter(Path("/noop")) + assert.NotNil(t, reporter) + assert.Equal(t, "/noop", reporter.Options().Path) + + // Check that our implementation is valid: + assert.Implements(t, new(Reporter), reporter) +}