github.com/safedep/dry@v0.0.0-20241016050132-a15651f0548b/obs/metrics.go (about)

     1  package obs
     2  
     3  // Counter is a metric that represents a single numerical value
     4  // that only ever goes up.
     5  type Counter interface {
     6  	Inc()
     7  	Add(float64)
     8  }
     9  
    10  // Gauge is a metric that represents a single numerical value that can
    11  // arbitrarily go up and down.
    12  type Gauge interface {
    13  	Set(float64)
    14  	Add(float64)
    15  	Sub(float64)
    16  }
    17  
    18  // Histogram is a metric that samples observations (usually things like
    19  // request durations or response sizes) and counts them in configurable
    20  // buckets. It also provides a sum of all observed values.
    21  type Histogram interface {
    22  	Observe(float64)
    23  }
    24  
    25  type ProviderSpecificOpts interface{}
    26  type ProviderSpecificOptsEditor func(ProviderSpecificOpts)
    27  
    28  // Provider is an interface that provides a way to create metrics.
    29  // An implementation using Prometheus Go SDK is an example of a Provider.
    30  type Provider interface {
    31  	NewCounter(name, desc string) Counter
    32  	NewGauge(name, desc string) Gauge
    33  	NewHistogram(name, desc string, opts ...ProviderSpecificOptsEditor) Histogram
    34  }
    35  
    36  type dummyReceiver struct{}
    37  
    38  func (d *dummyReceiver) Inc()            {}
    39  func (d *dummyReceiver) Set(float64)     {}
    40  func (d *dummyReceiver) Add(float64)     {}
    41  func (d *dummyReceiver) Sub(float64)     {}
    42  func (d *dummyReceiver) Observe(float64) {}
    43  
    44  type dummyProvider struct{}
    45  
    46  // NewCounter creates a new Counter.
    47  func (d *dummyProvider) NewCounter(_, _ string) Counter {
    48  	return &dummyReceiver{}
    49  }
    50  
    51  // NewGauge creates a new Gauge.
    52  func (d *dummyProvider) NewGauge(_, _ string) Gauge {
    53  	return &dummyReceiver{}
    54  }
    55  
    56  // NewHistogram creates a new Histogram.
    57  func (d *dummyProvider) NewHistogram(_, _ string, _ ...ProviderSpecificOptsEditor) Histogram {
    58  	return &dummyReceiver{}
    59  }
    60  
    61  var (
    62  	__provider Provider = &dummyProvider{}
    63  )
    64  
    65  func NewCounter(name, desc string) Counter {
    66  	return __provider.NewCounter(name, desc)
    67  }
    68  
    69  func NewGauge(name, desc string) Gauge {
    70  	return __provider.NewGauge(name, desc)
    71  }
    72  
    73  func NewHistogram(name, desc string) Histogram {
    74  	return __provider.NewHistogram(name, desc)
    75  }
    76  
    77  // InitPrometheusMetricsProvider initializes the default metrics provider to
    78  // use Prometheus Go SDK. This function is not thread-safe and should be called
    79  // before any other function in this package.
    80  func InitPrometheusMetricsProvider(namespace, subsystem string) {
    81  	__provider = NewPrometheusMetricsProvider(namespace, subsystem)
    82  }