github.com/rudderlabs/rudder-go-kit@v0.30.0/stats/measurement.go (about)

     1  package stats
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  // Counter represents a counter metric
     9  type Counter interface {
    10  	Count(n int)
    11  	Increment()
    12  }
    13  
    14  // Gauge represents a gauge metric
    15  type Gauge interface {
    16  	Gauge(value interface{})
    17  }
    18  
    19  // Histogram represents a histogram metric
    20  type Histogram interface {
    21  	Observe(value float64)
    22  }
    23  
    24  // Timer represents a timer metric
    25  type Timer interface {
    26  	SendTiming(duration time.Duration)
    27  	Since(start time.Time)
    28  	RecordDuration() func()
    29  }
    30  
    31  // Measurement provides all stat measurement functions
    32  // TODO: the API should not return a union of measurement methods, but rather a distinct type for each measurement type
    33  type Measurement interface {
    34  	Counter
    35  	Gauge
    36  	Histogram
    37  	Timer
    38  }
    39  
    40  type genericMeasurement struct {
    41  	statType string
    42  }
    43  
    44  // Count default behavior is to panic as not supported operation
    45  func (m *genericMeasurement) Count(_ int) {
    46  	panic(fmt.Errorf("operation Count not supported for measurement type:%s", m.statType))
    47  }
    48  
    49  // Increment default behavior is to panic as not supported operation
    50  func (m *genericMeasurement) Increment() {
    51  	panic(fmt.Errorf("operation Increment not supported for measurement type:%s", m.statType))
    52  }
    53  
    54  // Gauge default behavior is to panic as not supported operation
    55  func (m *genericMeasurement) Gauge(_ interface{}) {
    56  	panic(fmt.Errorf("operation Gauge not supported for measurement type:%s", m.statType))
    57  }
    58  
    59  // Observe default behavior is to panic as not supported operation
    60  func (m *genericMeasurement) Observe(_ float64) {
    61  	panic(fmt.Errorf("operation Observe not supported for measurement type:%s", m.statType))
    62  }
    63  
    64  // Start default behavior is to panic as not supported operation
    65  func (m *genericMeasurement) Start() {
    66  	panic(fmt.Errorf("operation Start not supported for measurement type:%s", m.statType))
    67  }
    68  
    69  func (m *genericMeasurement) End() {
    70  	panic(fmt.Errorf("operation End not supported for measurement type:%s", m.statType))
    71  }
    72  
    73  // SendTiming default behavior is to panic as not supported operation
    74  func (m *genericMeasurement) SendTiming(_ time.Duration) {
    75  	panic(fmt.Errorf("operation SendTiming not supported for measurement type:%s", m.statType))
    76  }
    77  
    78  // Since default behavior is to panic as not supported operation
    79  func (m *genericMeasurement) Since(_ time.Time) {
    80  	panic(fmt.Errorf("operation Since not supported for measurement type:%s", m.statType))
    81  }
    82  
    83  // RecordDuration default behavior is to panic as not supported operation
    84  func (m *genericMeasurement) RecordDuration() func() {
    85  	panic(fmt.Errorf("operation RecordDuration not supported for measurement type:%s", m.statType))
    86  }