github.com/wfusion/gofusion@v1.1.14/metrics/types.go (about)

     1  package metrics
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"time"
     7  
     8  	"github.com/wfusion/gofusion/common/infra/metrics"
     9  	"github.com/wfusion/gofusion/common/utils"
    10  	"github.com/wfusion/gofusion/config"
    11  	"github.com/wfusion/gofusion/log"
    12  )
    13  
    14  const (
    15  	ErrDuplicatedName utils.Error = "duplicated metrics name"
    16  )
    17  
    18  var (
    19  	customLoggerType  = reflect.TypeOf((*customLogger)(nil)).Elem()
    20  	metricsLoggerType = reflect.TypeOf((*metrics.Logger)(nil)).Elem()
    21  )
    22  
    23  // The Sink interface is used to transmit metrics information
    24  // to an external system
    25  type Sink interface {
    26  	// SetGauge A Gauge should retain the last value it is set to
    27  	SetGauge(ctx context.Context, key []string, val float64, opts ...utils.OptionExtender)
    28  
    29  	// IncrCounter Counters should accumulate values
    30  	IncrCounter(ctx context.Context, key []string, val float64, opts ...utils.OptionExtender)
    31  
    32  	// AddSample Samples are for timing information, where quantiles are used
    33  	AddSample(ctx context.Context, key []string, val float64, opts ...utils.OptionExtender)
    34  
    35  	// MeasureSince A better way to add timer samples
    36  	MeasureSince(ctx context.Context, key []string, start time.Time, opts ...utils.OptionExtender)
    37  
    38  	// IsEnableServiceLabel check if enable service label
    39  	IsEnableServiceLabel() bool
    40  
    41  	getProxy() any
    42  	shutdown()
    43  }
    44  
    45  type Label struct {
    46  	Key, Value string
    47  }
    48  
    49  // Conf metrics conf
    50  //nolint: revive // struct tag too long issue
    51  type Conf struct {
    52  	Type                  metricsType       `yaml:"type" json:"type" toml:"type"`
    53  	Mode                  mode              `yaml:"mode" json:"mode" toml:"mode"`
    54  	Interval              string            `yaml:"interval" json:"interval" toml:"interval"`
    55  	Endpoint              *endpointConf     `yaml:"endpoint" json:"endpoint" toml:"endpoint"`
    56  	Labels                map[string]string `yaml:"labels" json:"labels" toml:"labels"`
    57  	EnableServiceLabel    bool              `yaml:"enable_service_label" json:"enable_service_label" toml:"enable_service_label"`
    58  	EnableInternalMetrics bool              `yaml:"enable_internal_metrics" json:"enable_internal_metrics" toml:"enable_internal_metrics"`
    59  	QueueLimit            int               `yaml:"queue_limit" json:"queue_limit" toml:"queue_limit" default:"16384"`
    60  	QueueConcurrency      int               `yaml:"queue_concurrency" json:"queue_concurrency" toml:"queue_concurrency"`
    61  	Logger                string            `yaml:"logger" json:"logger" toml:"logger" default:"github.com/wfusion/gofusion/log/customlogger.metricsLogger"`
    62  	LogInstance           string            `yaml:"log_instance" json:"log_instance" toml:"log_instance" default:"default"`
    63  	EnableLogger          bool              `yaml:"enable_logger" json:"enable_logger" toml:"enable_logger" default:"false"`
    64  }
    65  
    66  type endpointConf struct {
    67  	Addresses []string `yaml:"addresses" json:"addresses" toml:"addresses"`
    68  }
    69  
    70  type metricsType string
    71  
    72  const (
    73  	metricsTypeMock       metricsType = "mock"
    74  	metricsTypePrometheus metricsType = "prometheus"
    75  )
    76  
    77  type mode string
    78  
    79  const (
    80  	modePull mode = "pull"
    81  	modePush mode = "push"
    82  )
    83  
    84  type cfg struct {
    85  	c          *Conf
    86  	ctx        context.Context
    87  	name       string
    88  	appName    string
    89  	interval   time.Duration
    90  	initOption *config.InitOption
    91  	logger     metrics.Logger
    92  }
    93  
    94  type option struct {
    95  	prometheusBuckets []float64
    96  	precision         bool
    97  	timeout           time.Duration
    98  	labels            []Label
    99  }
   100  
   101  func PrometheusBuckets(buckets []float64) utils.OptionFunc[option] {
   102  	return func(o *option) {
   103  		o.prometheusBuckets = buckets
   104  	}
   105  }
   106  
   107  func Precision() utils.OptionFunc[option] {
   108  	return func(o *option) {
   109  		o.precision = true
   110  	}
   111  }
   112  
   113  func Timeout(timeout time.Duration) utils.OptionFunc[option] {
   114  	return func(o *option) {
   115  		o.timeout = timeout
   116  	}
   117  }
   118  
   119  func WithoutTimeout() utils.OptionFunc[option] {
   120  	return func(o *option) {
   121  		o.timeout = -1
   122  	}
   123  }
   124  
   125  func Labels(labels []Label) utils.OptionFunc[option] {
   126  	return func(o *option) {
   127  		o.labels = labels
   128  	}
   129  }
   130  
   131  type customLogger interface {
   132  	Init(log log.Loggable, appName, name string)
   133  }