metrics: minor changes to interface and set default

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-11-15 00:38:38 +03:00
parent b38484d18e
commit f6c914c1e4
5 changed files with 56 additions and 58 deletions

37
metrics/noop.go Normal file
View File

@ -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
}

View File

@ -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
}

View File

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

View File

@ -7,7 +7,7 @@ import "time"
type Tags map[string]string type Tags map[string]string
var ( var (
DefaultReporter Reporter DefaultReporter Reporter = NewReporter()
) )
// Reporter is an interface for collecting and instrumenting metrics // Reporter is an interface for collecting and instrumenting metrics
@ -15,4 +15,5 @@ type Reporter interface {
Count(id string, value int64, tags Tags) error Count(id string, value int64, tags Tags) error
Gauge(id string, value float64, tags Tags) error Gauge(id string, value float64, tags Tags) error
Timing(id string, value time.Duration, tags Tags) error Timing(id string, value time.Duration, tags Tags) error
Options() Options
} }

17
metrics/reporter_test.go Normal file
View File

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