From c67fe6f330fa26384981e7e08c0c39749f139308 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 22 Jan 2021 19:18:28 +0300 Subject: [PATCH] meter: add option helper and provide default metric name and label prefix Signed-off-by: Vasiliy Tolstov --- meter/context.go | 34 ++++++++++++++++++++++++++++++++++ meter/meter.go | 13 ++++++++++++- meter/options.go | 30 ++++++++++++------------------ 3 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 meter/context.go diff --git a/meter/context.go b/meter/context.go new file mode 100644 index 00000000..dff0bd2b --- /dev/null +++ b/meter/context.go @@ -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) + } +} diff --git a/meter/meter.go b/meter/meter.go index bf02e4e4..76bb8f10 100644 --- a/meter/meter.go +++ b/meter/meter.go @@ -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 diff --git a/meter/options.go b/meter/options.go index 015cd9c5..5866c130 100644 --- a/meter/options.go +++ b/meter/options.go @@ -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