...

Source file src/github.com/penglongli/gin-metrics/ginmetrics/metric.go

Documentation: github.com/penglongli/gin-metrics/ginmetrics

     1  package ginmetrics
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	"github.com/prometheus/client_golang/prometheus"
     6  )
     7  
     8  // Metric defines a metric object. Users can use it to save
     9  // metric data. Every metric should be globally unique by name.
    10  type Metric struct {
    11  	Type        MetricType
    12  	Name        string
    13  	Description string
    14  	Labels      []string
    15  	Buckets     []float64
    16  	Objectives  map[float64]float64
    17  
    18  	vec prometheus.Collector
    19  }
    20  
    21  // SetGaugeValue set data for Gauge type Metric.
    22  func (m *Metric) SetGaugeValue(labelValues []string, value float64) error {
    23  	if m.Type == None {
    24  		return errors.Errorf("metric '%s' not existed.", m.Name)
    25  	}
    26  
    27  	if m.Type != Gauge {
    28  		return errors.Errorf("metric '%s' not Gauge type", m.Name)
    29  	}
    30  	m.vec.(*prometheus.GaugeVec).WithLabelValues(labelValues...).Set(value)
    31  	return nil
    32  }
    33  
    34  // Inc increases value for Counter/Gauge type metric, increments
    35  // the counter by 1
    36  func (m *Metric) Inc(labelValues []string) error {
    37  	if m.Type == None {
    38  		return errors.Errorf("metric '%s' not existed.", m.Name)
    39  	}
    40  
    41  	if m.Type != Gauge && m.Type != Counter {
    42  		return errors.Errorf("metric '%s' not Gauge or Counter type", m.Name)
    43  	}
    44  	switch m.Type {
    45  	case Counter:
    46  		m.vec.(*prometheus.CounterVec).WithLabelValues(labelValues...).Inc()
    47  		break
    48  	case Gauge:
    49  		m.vec.(*prometheus.GaugeVec).WithLabelValues(labelValues...).Inc()
    50  		break
    51  	}
    52  	return nil
    53  }
    54  
    55  // Add adds the given value to the Metric object. Only
    56  // for Counter/Gauge type metric.
    57  func (m *Metric) Add(labelValues []string, value float64) error {
    58  	if m.Type == None {
    59  		return errors.Errorf("metric '%s' not existed.", m.Name)
    60  	}
    61  
    62  	if m.Type != Gauge && m.Type != Counter {
    63  		return errors.Errorf("metric '%s' not Gauge or Counter type", m.Name)
    64  	}
    65  	switch m.Type {
    66  	case Counter:
    67  		m.vec.(*prometheus.CounterVec).WithLabelValues(labelValues...).Add(value)
    68  		break
    69  	case Gauge:
    70  		m.vec.(*prometheus.GaugeVec).WithLabelValues(labelValues...).Add(value)
    71  		break
    72  	}
    73  	return nil
    74  }
    75  
    76  // Observe is used by Histogram and Summary type metric to
    77  // add observations.
    78  func (m *Metric) Observe(labelValues []string, value float64) error {
    79  	if m.Type == 0 {
    80  		return errors.Errorf("metric '%s' not existed.", m.Name)
    81  	}
    82  	if m.Type != Histogram && m.Type != Summary {
    83  		return errors.Errorf("metric '%s' not Histogram or Summary type", m.Name)
    84  	}
    85  	switch m.Type {
    86  	case Histogram:
    87  		m.vec.(*prometheus.HistogramVec).WithLabelValues(labelValues...).Observe(value)
    88  		break
    89  	case Summary:
    90  		m.vec.(*prometheus.SummaryVec).WithLabelValues(labelValues...).Observe(value)
    91  		break
    92  	}
    93  	return nil
    94  }
    95  

View as plain text