2021-01-20 01:21:15 +03:00
|
|
|
package meter
|
2020-08-18 10:27:50 +03:00
|
|
|
|
2020-11-18 16:50:41 +03:00
|
|
|
import (
|
2021-01-15 22:47:28 +03:00
|
|
|
"context"
|
|
|
|
|
2020-11-18 16:50:41 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
|
|
|
)
|
2020-08-29 17:44:49 +03:00
|
|
|
|
2020-08-18 10:27:50 +03:00
|
|
|
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"
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
// Options for metrics implementations:
|
|
|
|
type Options struct {
|
|
|
|
Address string
|
|
|
|
Path string
|
2020-11-18 16:50:41 +03:00
|
|
|
DefaultTags metadata.Metadata
|
2020-08-18 10:27:50 +03:00
|
|
|
TimingObjectives map[float64]float64
|
2020-08-29 17:44:49 +03:00
|
|
|
Logger logger.Logger
|
2021-01-15 22:47:28 +03:00
|
|
|
Context context.Context
|
2020-08-18 10:27:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOptions prepares a set of options:
|
|
|
|
func NewOptions(opt ...Option) Options {
|
|
|
|
opts := Options{
|
|
|
|
Address: defaultPrometheusListenAddress,
|
2020-11-18 16:50:41 +03:00
|
|
|
DefaultTags: metadata.New(2),
|
2020-08-18 10:27:50 +03:00
|
|
|
Path: defaultPath,
|
|
|
|
TimingObjectives: defaultTimingObjectives,
|
2021-01-15 22:47:28 +03:00
|
|
|
Context: context.Background(),
|
2020-08-18 10:27:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opt {
|
|
|
|
o(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2021-01-15 22:47:28 +03:00
|
|
|
// Cntext sets the metrics context
|
|
|
|
func Context(ctx context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 10:27:50 +03:00
|
|
|
// 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:
|
2020-11-18 16:50:41 +03:00
|
|
|
func DefaultTags(md metadata.Metadata) Option {
|
2020-08-18 10:27:50 +03:00
|
|
|
return func(o *Options) {
|
2020-11-18 16:50:41 +03:00
|
|
|
o.DefaultTags = metadata.Copy(md)
|
2020-08-18 10:27:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TimingObjectives defines the desired spread of statistics for histogram / timing metrics:
|
|
|
|
func TimingObjectives(value map[float64]float64) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.TimingObjectives = value
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 17:44:49 +03:00
|
|
|
|
|
|
|
// Logger sets the logger
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|