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"
|
2023-07-29 00:40:58 +03:00
|
|
|
"reflect"
|
2021-01-15 22:47:28 +03:00
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/logger"
|
2023-07-29 00:40:58 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
|
|
|
rutil "go.unistack.org/micro/v4/util/reflect"
|
2020-11-18 16:50:41 +03:00
|
|
|
)
|
2020-08-29 17:44:49 +03:00
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// Options for metrics implementations
|
2020-08-18 10:27:50 +03:00
|
|
|
type Options struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Logger used for logging
|
|
|
|
Logger logger.Logger
|
|
|
|
// Context holds external options
|
|
|
|
Context context.Context
|
|
|
|
// Name holds the meter name
|
|
|
|
Name string
|
|
|
|
// Address holds the address that serves metrics
|
2021-01-27 00:54:19 +03:00
|
|
|
Address string
|
2021-03-06 19:45:13 +03:00
|
|
|
// Path holds the path for metrics
|
|
|
|
Path string
|
|
|
|
// MetricPrefix holds the prefix for all metrics
|
|
|
|
MetricPrefix string
|
|
|
|
// LabelPrefix holds the prefix for all labels
|
|
|
|
LabelPrefix string
|
|
|
|
// Labels holds the default labels
|
2021-03-15 00:44:13 +03:00
|
|
|
Labels []string
|
2021-03-06 19:45:13 +03:00
|
|
|
// WriteProcessMetrics flag to write process metrics
|
2021-02-14 16:16:01 +03:00
|
|
|
WriteProcessMetrics bool
|
2021-03-06 19:45:13 +03:00
|
|
|
// WriteFDMetrics flag to write fd metrics
|
|
|
|
WriteFDMetrics bool
|
2020-08-18 10:27:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOptions prepares a set of options:
|
2023-07-29 00:40:58 +03:00
|
|
|
func NewOptions(opt ...options.Option) Options {
|
2020-08-18 10:27:50 +03:00
|
|
|
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 10:27:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range opt {
|
|
|
|
o(&opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2021-10-09 19:16:45 +03:00
|
|
|
// LabelPrefix sets the labels prefix
|
2023-07-29 00:40:58 +03:00
|
|
|
func LabelPrefix(pref string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, pref, ".LabelPrefix")
|
2021-10-09 19:16:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MetricPrefix sets the metric prefix
|
2023-07-29 00:40:58 +03:00
|
|
|
func MetricPrefix(pref string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, pref, ".MetricPrefix")
|
2021-01-15 22:47:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:21:40 +03:00
|
|
|
// Path used to serve metrics over HTTP
|
2023-07-29 00:40:58 +03:00
|
|
|
func Path(path string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, path, ".Path")
|
2020-08-18 10:27:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 18:21:40 +03:00
|
|
|
/*
|
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
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 18:21:40 +03:00
|
|
|
*/
|
2020-08-29 17:44:49 +03:00
|
|
|
|
2022-01-10 16:47:37 +03:00
|
|
|
// Labels sets the meter labels
|
2023-07-29 00:40:58 +03:00
|
|
|
func Labels(ls ...string) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
v, err := options.Get(src, ".Labels")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if rutil.IsZero(v) {
|
|
|
|
v = reflect.MakeSlice(reflect.TypeOf(v), 0, len(ls)).Interface()
|
|
|
|
}
|
|
|
|
cv := reflect.ValueOf(v)
|
|
|
|
for _, l := range ls {
|
|
|
|
reflect.Append(cv, reflect.ValueOf(l))
|
|
|
|
}
|
|
|
|
err = options.Set(src, cv, ".Labels")
|
|
|
|
return err
|
2021-01-29 14:07:35 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-14 16:16:01 +03:00
|
|
|
|
|
|
|
// WriteProcessMetrics enable process metrics output for write
|
2023-07-29 00:40:58 +03:00
|
|
|
func WriteProcessMetrics(b bool) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, b, ".WriteProcessMetrics")
|
2021-02-14 16:16:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteFDMetrics enable fd metrics output for write
|
2023-07-29 00:40:58 +03:00
|
|
|
func WriteFDMetrics(b bool) options.Option {
|
|
|
|
return func(src interface{}) error {
|
|
|
|
return options.Set(src, b, ".WriteFDMetrics")
|
2021-02-14 16:16:01 +03:00
|
|
|
}
|
|
|
|
}
|