meter: add option helper and provide default metric name and label prefix

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-01-22 19:18:28 +03:00
parent 8c3f0d2c64
commit c67fe6f330
3 changed files with 58 additions and 19 deletions

34
meter/context.go Normal file
View File

@ -0,0 +1,34 @@
package meter
import (
"context"
)
type meterKey struct{}
// FromContext get meter from context
func FromContext(ctx context.Context) (Meter, bool) {
if ctx == nil {
return nil, false
}
c, ok := ctx.Value(meterKey{}).(Meter)
return c, ok
}
// NewContext put meter in context
func NewContext(ctx context.Context, c Meter) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, meterKey{}, c)
}
// SetOption returns a function to setup a context with given value
func SetOption(k, v interface{}) Option {
return func(o *Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}

View File

@ -8,7 +8,18 @@ import (
)
var (
DefaultReporter Meter = NewMeter()
// DefaultMeter is the default meter
DefaultMeter Meter = NewMeter()
// DefaultAddress data will be made available on this host:port
DefaultAddress = ":9090"
// DefaultPath the meter endpoint where the Meter data will be made available
DefaultPath = "/metrics"
// timingObjectives is the default spread of stats we maintain for timings / histograms:
//defaultTimingObjectives = map[float64]float64{0.0: 0, 0.5: 0.05, 0.75: 0.04, 0.90: 0.03, 0.95: 0.02, 0.98: 0.001, 1: 0}
// default metric prefix
DefaultMetricPrefix = "micro_"
// default label prefix
DefaultLabelPrefix = "micro_"
)
// Meter is an interface for collecting and instrumenting metrics

View File

@ -7,15 +7,6 @@ import (
"github.com/unistack-org/micro/v3/metadata"
)
var (
// The Meter data will be made available on this port
DefaultAddress = ":9090"
// This is the endpoint where the Meter data will be made available ("/metrics" is the default)
DefaultPath = "/metrics"
// timingObjectives is the default spread of stats we maintain for timings / histograms:
//defaultTimingObjectives = map[float64]float64{0.0: 0, 0.5: 0.05, 0.75: 0.04, 0.90: 0.03, 0.95: 0.02, 0.98: 0.001, 1: 0}
)
// Option powers the configuration for metrics implementations:
type Option func(*Options)
@ -25,19 +16,22 @@ type Options struct {
Path string
Metadata metadata.Metadata
//TimingObjectives map[float64]float64
Logger logger.Logger
Context context.Context
Logger logger.Logger
Context context.Context
MetricPrefix string
LabelPrefix string
}
// NewOptions prepares a set of options:
func NewOptions(opt ...Option) Options {
opts := Options{
Address: DefaultAddress,
Metadata: metadata.New(3), // 3 elements contains service name, version and id
Path: DefaultPath,
// TimingObjectives: defaultTimingObjectives,
Context: context.Background(),
Logger: logger.DefaultLogger,
Address: DefaultAddress,
Metadata: metadata.New(3), // 3 elements contains service name, version and id
Path: DefaultPath,
Context: context.Background(),
Logger: logger.DefaultLogger,
MetricPrefix: DefaultMetricPrefix,
LabelPrefix: DefaultLabelPrefix,
}
for _, o := range opt {
@ -47,7 +41,7 @@ func NewOptions(opt ...Option) Options {
return opts
}
// Cntext sets the metrics context
// Context sets the metrics context
func Context(ctx context.Context) Option {
return func(o *Options) {
o.Context = ctx