github.com/safing/portbase@v0.19.5/metrics/metric_counter.go (about)

     1  package metrics
     2  
     3  import (
     4  	vm "github.com/VictoriaMetrics/metrics"
     5  )
     6  
     7  // Counter is a counter metric.
     8  type Counter struct {
     9  	*metricBase
    10  	*vm.Counter
    11  }
    12  
    13  // NewCounter registers a new counter metric.
    14  func NewCounter(id string, labels map[string]string, opts *Options) (*Counter, error) {
    15  	// Ensure that there are options.
    16  	if opts == nil {
    17  		opts = &Options{}
    18  	}
    19  
    20  	// Make base.
    21  	base, err := newMetricBase(id, labels, *opts)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	// Create metric struct.
    27  	m := &Counter{
    28  		metricBase: base,
    29  	}
    30  
    31  	// Create metric in set
    32  	m.Counter = m.set.NewCounter(m.LabeledID())
    33  
    34  	// Register metric.
    35  	err = register(m)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	// Load state.
    41  	m.loadState()
    42  
    43  	return m, nil
    44  }
    45  
    46  // CurrentValue returns the current counter value.
    47  func (c *Counter) CurrentValue() uint64 {
    48  	return c.Get()
    49  }