2021-01-20 01:21:15 +03:00
|
|
|
package meter
|
2020-08-18 19:27:50 +12: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"
|
|
|
|
)
|
2020-08-29 17:44:49 +03:00
|
|
|
|
2020-08-18 19:27:50 +12:00
|
|
|
// Option powers the configuration for metrics implementations:
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// Options for metrics implementations:
|
|
|
|
type Options struct {
|
2021-01-29 13:17:32 +03:00
|
|
|
Name string
|
2021-01-27 00:54:19 +03:00
|
|
|
Address string
|
|
|
|
Path string
|
|
|
|
Labels Labels
|
2021-01-22 18:21:40 +03:00
|
|
|
//TimingObjectives map[float64]float64
|
2021-02-14 16:16:01 +03:00
|
|
|
Logger logger.Logger
|
|
|
|
Context context.Context
|
|
|
|
MetricPrefix string
|
|
|
|
LabelPrefix string
|
|
|
|
WriteProcessMetrics bool
|
|
|
|
WriteFDMetrics bool
|
2020-08-18 19:27:50 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOptions prepares a set of options:
|
|
|
|
func NewOptions(opt ...Option) Options {
|
|
|
|
opts := Options{
|
2021-01-22 19:18:28 +03:00
|
|
|
Address: DefaultAddress,
|
|
|
|
Path: DefaultPath,
|
|
|
|
Context: context.Background(),
|
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
MetricPrefix: DefaultMetricPrefix,
|
|
|
|
LabelPrefix: DefaultLabelPrefix,
|
2020-08-18 19:27:50 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opt {
|
|
|
|
o(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:18:28 +03:00
|
|
|
// Context sets the metrics context
|
2021-01-15 22:47:28 +03:00
|
|
|
func Context(ctx context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:21:40 +03:00
|
|
|
// Path used to serve metrics over HTTP
|
2020-08-18 19:27:50 +12:00
|
|
|
func Path(value string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Path = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:21:40 +03:00
|
|
|
// Address is the listen address to serve metrics
|
2020-08-18 19:27:50 +12:00
|
|
|
func Address(value string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Address = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:21:40 +03:00
|
|
|
/*
|
2020-08-18 19:27:50 +12: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
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 18:21:40 +03:00
|
|
|
*/
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 00:54:19 +03:00
|
|
|
|
|
|
|
// Label sets the label
|
|
|
|
func Label(key, val string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Labels.keys = append(o.Labels.keys, key)
|
|
|
|
o.Labels.vals = append(o.Labels.vals, val)
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 14:07:35 +03:00
|
|
|
|
|
|
|
// Name sets the name
|
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 16:16:01 +03:00
|
|
|
|
|
|
|
// WriteProcessMetrics enable process metrics output for write
|
|
|
|
func WriteProcessMetrics(b bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.WriteProcessMetrics = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteFDMetrics enable fd metrics output for write
|
|
|
|
func WriteFDMetrics(b bool) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.WriteFDMetrics = b
|
|
|
|
}
|
|
|
|
}
|