replace metrics interface
This commit is contained in:
@@ -1,22 +0,0 @@
|
|||||||
metrics
|
|
||||||
=======
|
|
||||||
|
|
||||||
The metrics package provides a simple metrics "Reporter" interface which allows the user to submit counters, gauges and timings (along with key/value tags).
|
|
||||||
|
|
||||||
Implementations
|
|
||||||
---------------
|
|
||||||
|
|
||||||
* Prometheus (pull): will be first
|
|
||||||
* Prometheus (push): certainly achievable
|
|
||||||
* InfluxDB: could quite easily be done
|
|
||||||
* Telegraf: almost identical to the InfluxDB implementation
|
|
||||||
* Micro: Could we provide metrics over Micro's server interface?
|
|
||||||
|
|
||||||
|
|
||||||
Todo
|
|
||||||
----
|
|
||||||
|
|
||||||
* Include a handler middleware which uses the Reporter interface to generate per-request level metrics
|
|
||||||
- Throughput
|
|
||||||
- Errors
|
|
||||||
- Duration
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
package logging
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/v3/logger"
|
|
||||||
"github.com/micro/go-micro/v3/metrics"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
defaultLoggingLevel = logger.TraceLevel
|
|
||||||
)
|
|
||||||
|
|
||||||
// Reporter is an implementation of metrics.Reporter:
|
|
||||||
type Reporter struct {
|
|
||||||
options metrics.Options
|
|
||||||
}
|
|
||||||
|
|
||||||
// New returns a configured logging reporter:
|
|
||||||
func New(opts ...metrics.Option) *Reporter {
|
|
||||||
logger.Logf(logger.InfoLevel, "Metrics/Logging - metrics will be logged (at %s level)", defaultLoggingLevel.String())
|
|
||||||
|
|
||||||
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 {
|
|
||||||
logger.Logf(defaultLoggingLevel, "Count metric: (%s: %d) %s", metricName, value, tags)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gauge implements the metrics.Reporter interface Gauge method:
|
|
||||||
func (r *Reporter) Gauge(metricName string, value float64, tags metrics.Tags) error {
|
|
||||||
logger.Logf(defaultLoggingLevel, "Gauge metric: (%s: %f) %s", metricName, value, tags)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Timing implements the metrics.Reporter interface Timing method:
|
|
||||||
func (r *Reporter) Timing(metricName string, value time.Duration, tags metrics.Tags) error {
|
|
||||||
logger.Logf(defaultLoggingLevel, "Timing metric: (%s: %s) %s", metricName, value.String(), 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
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package logging
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"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)
|
|
||||||
|
|
||||||
// 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))
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,45 @@
|
|||||||
// Package metrics is for instrumentation and debugging
|
// Package metrics is an interface for instrumentation.
|
||||||
package metrics
|
package metrics
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
// Tags is a map of fields to add to a metric:
|
type Fields map[string]string
|
||||||
type Tags map[string]string
|
|
||||||
|
|
||||||
// Reporter is an interface for collecting and instrumenting metrics
|
// Metrics provides a way to instrument application data
|
||||||
type Reporter interface {
|
type Metrics interface {
|
||||||
Count(id string, value int64, tags Tags) error
|
Counter(id string) Counter
|
||||||
Gauge(id string, value float64, tags Tags) error
|
Gauge(id string) Gauge
|
||||||
Timing(id string, value time.Duration, tags Tags) error
|
Histogram(id string) Histogram
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Counter interface {
|
||||||
|
// Increment by the given value
|
||||||
|
Incr(d uint64)
|
||||||
|
// Decrement by the given value
|
||||||
|
Decr(d uint64)
|
||||||
|
// Reset the counter
|
||||||
|
Reset()
|
||||||
|
// Label the counter
|
||||||
|
WithFields(f Fields) Counter
|
||||||
|
}
|
||||||
|
|
||||||
|
type Gauge interface {
|
||||||
|
// Set the gauge value
|
||||||
|
Set(d int64)
|
||||||
|
// Reset the gauge
|
||||||
|
Reset()
|
||||||
|
// Label the gauge
|
||||||
|
WithFields(f Fields) Gauge
|
||||||
|
}
|
||||||
|
|
||||||
|
type Histogram interface {
|
||||||
|
// Record a timing
|
||||||
|
Record(d int64)
|
||||||
|
// Reset the histogram
|
||||||
|
Reset()
|
||||||
|
// Label the histogram
|
||||||
|
WithFields(f Fields) Histogram
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
package noop
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/micro/go-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 {
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package noop
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/micro/go-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)
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
package metrics
|
|
||||||
|
|
||||||
var (
|
|
||||||
// The Prometheus metrics will be made available on this port:
|
|
||||||
defaultPrometheusListenAddress = ":9000"
|
|
||||||
// This is the endpoint where the Prometheus metrics will be made available ("/metrics" is the default with Prometheus):
|
|
||||||
defaultPath = "/metrics"
|
|
||||||
// defaultPercentiles is the default spread of percentiles/quantiles we maintain for timings / histogram metrics:
|
|
||||||
defaultPercentiles = []float64{0, 0.5, 0.75, 0.90, 0.95, 0.98, 0.99, 1}
|
|
||||||
)
|
|
||||||
|
|
||||||
// Option powers the configuration for metrics implementations:
|
|
||||||
type Option func(*Options)
|
|
||||||
|
|
||||||
// Options for metrics implementations:
|
|
||||||
type Options struct {
|
|
||||||
Address string
|
|
||||||
DefaultTags Tags
|
|
||||||
Path string
|
|
||||||
Percentiles []float64
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewOptions prepares a set of options:
|
|
||||||
func NewOptions(opt ...Option) Options {
|
|
||||||
opts := Options{
|
|
||||||
Address: defaultPrometheusListenAddress,
|
|
||||||
DefaultTags: make(Tags),
|
|
||||||
Path: defaultPath,
|
|
||||||
Percentiles: defaultPercentiles,
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, o := range opt {
|
|
||||||
o(&opts)
|
|
||||||
}
|
|
||||||
|
|
||||||
return opts
|
|
||||||
}
|
|
||||||
|
|
||||||
// Path used to serve metrics over HTTP:
|
|
||||||
func Path(value string) Option {
|
|
||||||
return func(o *Options) {
|
|
||||||
o.Path = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Address is the listen address to serve metrics on:
|
|
||||||
func Address(value string) Option {
|
|
||||||
return func(o *Options) {
|
|
||||||
o.Address = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// DefaultTags will be added to every metric:
|
|
||||||
func DefaultTags(value Tags) Option {
|
|
||||||
return func(o *Options) {
|
|
||||||
o.DefaultTags = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Percentiles defines the desired spread of statistics for histogram / timing metrics:
|
|
||||||
func Percentiles(value []float64) Option {
|
|
||||||
return func(o *Options) {
|
|
||||||
o.Percentiles = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package metrics
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestOptions(t *testing.T) {
|
|
||||||
|
|
||||||
// Make some new options:
|
|
||||||
options := NewOptions(
|
|
||||||
Address(":9999"),
|
|
||||||
DefaultTags(map[string]string{"service": "prometheus-test"}),
|
|
||||||
Path("/prometheus"),
|
|
||||||
Percentiles([]float64{0.11, 0.22, 0.33}),
|
|
||||||
)
|
|
||||||
|
|
||||||
// Check that the defaults and overrides were accepted:
|
|
||||||
assert.Equal(t, ":9999", options.Address)
|
|
||||||
assert.Equal(t, "prometheus-test", options.DefaultTags["service"])
|
|
||||||
assert.Equal(t, "/prometheus", options.Path)
|
|
||||||
assert.Equal(t, []float64{0.11, 0.22, 0.33}, options.Percentiles)
|
|
||||||
}
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
package wrapper
|
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/micro/go-micro/v3/metrics"
|
|
||||||
"github.com/micro/go-micro/v3/server"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Wrapper provides a HandlerFunc for metrics.Reporter implementations:
|
|
||||||
type Wrapper struct {
|
|
||||||
reporter metrics.Reporter
|
|
||||||
}
|
|
||||||
|
|
||||||
// New returns a *Wrapper configured with the given metrics.Reporter:
|
|
||||||
func New(reporter metrics.Reporter) *Wrapper {
|
|
||||||
return &Wrapper{
|
|
||||||
reporter: reporter,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandlerFunc instruments handlers registered to a service:
|
|
||||||
func (w *Wrapper) HandlerFunc(handlerFunction server.HandlerFunc) server.HandlerFunc {
|
|
||||||
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
||||||
|
|
||||||
// Build some tags to describe the call:
|
|
||||||
tags := metrics.Tags{
|
|
||||||
"method": req.Method(),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start the clock:
|
|
||||||
callTime := time.Now()
|
|
||||||
|
|
||||||
// Run the handlerFunction:
|
|
||||||
err := handlerFunction(ctx, req, rsp)
|
|
||||||
|
|
||||||
// Add a result tag:
|
|
||||||
if err != nil {
|
|
||||||
tags["result"] = "failure"
|
|
||||||
} else {
|
|
||||||
tags["result"] = "success"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Instrument the result (if the DefaultClient has been configured):
|
|
||||||
w.reporter.Timing("service.handler", time.Since(callTime), tags)
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user