Observability/metrics update (#1962)

* Removing logging from the NOOP implementatino

* Simplifying the percentiles option

* Simple logging implementation

Co-authored-by: chris <chris@Profanity.local>
This commit is contained in:
Prawn
2020-08-21 20:57:10 +12:00
committed by GitHub
parent 1ae825032c
commit f9f61d29de
7 changed files with 142 additions and 20 deletions

View File

@@ -0,0 +1,53 @@
package logging
import (
"time"
log "github.com/micro/go-micro/v3/logger"
"github.com/micro/go-micro/v3/metrics"
)
// Reporter is an implementation of metrics.Reporter:
type Reporter struct {
logger log.Logger
options metrics.Options
}
// New returns a configured noop reporter:
func New(opts ...metrics.Option) *Reporter {
options := metrics.NewOptions(opts...)
logger := log.NewLogger(log.WithFields(convertTags(options.DefaultTags)))
logger.Log(log.InfoLevel, "Metrics/Logging - metrics will be logged (at TRACE level)")
return &Reporter{
logger: logger,
options: metrics.NewOptions(opts...),
}
}
// Count implements the metrics.Reporter interface Count method:
func (r *Reporter) Count(metricName string, value int64, tags metrics.Tags) error {
r.logger.Logf(log.TraceLevel, "Count metric: %s", tags)
return nil
}
// Gauge implements the metrics.Reporter interface Gauge method:
func (r *Reporter) Gauge(metricName string, value float64, tags metrics.Tags) error {
r.logger.Logf(log.TraceLevel, "Gauge metric: %s", tags)
return nil
}
// Timing implements the metrics.Reporter interface Timing method:
func (r *Reporter) Timing(metricName string, value time.Duration, tags metrics.Tags) error {
r.logger.Logf(log.TraceLevel, "Timing metric: %s", tags)
return nil
}
// convertTags turns Tags into prometheus labels:
func convertTags(tags metrics.Tags) map[string]interface{} {
labels := make(map[string]interface{})
for key, value := range tags {
labels[key] = value
}
return labels
}

View File

@@ -0,0 +1,51 @@
package logging
import (
"bytes"
"testing"
"time"
log "github.com/micro/go-micro/v3/logger"
"github.com/micro/go-micro/v3/metrics"
"github.com/stretchr/testify/assert"
)
func TestLoggingReporter(t *testing.T) {
// Make a Reporter:
reporter := New(metrics.Path("/prometheus"), metrics.DefaultTags(map[string]string{"service": "prometheus-test"}))
assert.NotNil(t, reporter)
assert.Equal(t, "prometheus-test", reporter.options.DefaultTags["service"])
assert.Equal(t, ":9000", reporter.options.Address)
assert.Equal(t, "/prometheus", reporter.options.Path)
// Make a log buffer and create a new logger to use it:
logBuffer := new(bytes.Buffer)
reporter.logger = log.NewLogger(log.WithLevel(log.TraceLevel), log.WithOutput(logBuffer), log.WithFields(convertTags(reporter.options.DefaultTags)))
// Check that our implementation is valid:
assert.Implements(t, new(metrics.Reporter), reporter)
// Test tag conversion:
tags := metrics.Tags{
"tag1": "false",
"tag2": "true",
}
convertedTags := convertTags(tags)
assert.Equal(t, "false", convertedTags["tag1"])
assert.Equal(t, "true", convertedTags["tag2"])
// Test submitting metrics through the interface methods:
assert.NoError(t, reporter.Count("test.counter.1", 6, tags))
assert.NoError(t, reporter.Count("test.counter.2", 19, tags))
assert.NoError(t, reporter.Count("test.counter.1", 5, tags))
assert.NoError(t, reporter.Gauge("test.gauge.1", 99, tags))
assert.NoError(t, reporter.Gauge("test.gauge.2", 55, tags))
assert.NoError(t, reporter.Gauge("test.gauge.1", 98, tags))
assert.NoError(t, reporter.Timing("test.timing.1", time.Second, tags))
assert.NoError(t, reporter.Timing("test.timing.2", time.Minute, tags))
// Test reading back the metrics from the logbuffer (doesn't seem to work because the output still goes to StdOut):
// assert.Contains(t, logBuffer.String(), "level=debug service=prometheus-test Count metric: map[tag1:false tag2:true]")
}