github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/metrics/metrics.go (about)

     1  package metrics
     2  
     3  import (
     4  	"github.com/prometheus/client_golang/prometheus"
     5  )
     6  
     7  const (
     8  	CntGatewayIncomingHttpMessage      = "gateway_incoming_http_message"
     9  	CntGatewayIncomingWebsocketMessage = "gateway_incoming_websocket_message"
    10  	CntGatewayOutgoingHttpMessage      = "gateway_outgoing_http_message"
    11  	CntGatewayOutgoingWebsocketMessage = "gateway_outgoing_websocket_message"
    12  	GaugeActiveWebsocketConnections    = "gateway_active_websocket_conns_total"
    13  	HistGatewayRequestTime             = "gateway_request_duration_milliseconds"
    14  
    15  	CntTunnelIncomingMessage = "tunnel_incoming_message"
    16  	CntTunnelOutgoingMessage = "tunnel_outgoing_message"
    17  	HistTunnelRequestTime    = "tunnel_request_duration_milliseconds"
    18  	HistTunnelRoundTripTime  = "tunnel_round_trip_duration_milliseconds"
    19  
    20  	CntStoreConflicts = "store_conflicts"
    21  	CntRPC            = "rpc_requests"
    22  )
    23  
    24  var (
    25  	_Prom            *Prometheus
    26  	SizeBucketKB     = []float64{1, 5, 10, 20, 40, 80, 160, 320, 640, 1280, 2560}
    27  	TimeBucketMS     = []float64{0.1, 0.5, 1, 10, 20, 50, 100, 500, 1000, 2000, 3000, 5000}
    28  	TimeBucketMicroS = []float64{1, 2, 5, 10, 20, 50, 100, 200, 300, 400, 500, 1000, 2000, 5000, 10000}
    29  )
    30  
    31  func Init(constLabels map[string]string) {
    32  	if _Prom != nil {
    33  		return
    34  	}
    35  	_Prom = NewPrometheus("rony", constLabels)
    36  	_Prom.RegisterCounter(CntGatewayIncomingHttpMessage, "number of incoming http messages", nil)
    37  	_Prom.RegisterCounter(CntGatewayOutgoingHttpMessage, "number of outgoing http messages", nil)
    38  	_Prom.RegisterCounter(CntGatewayIncomingWebsocketMessage, "number of incoming websocket messages", nil)
    39  	_Prom.RegisterCounter(CntGatewayOutgoingWebsocketMessage, "number of outgoing websocket messages", nil)
    40  	_Prom.RegisterCounter(CntTunnelIncomingMessage, "number of incoming messages", nil)
    41  	_Prom.RegisterCounter(CntTunnelOutgoingMessage, "number of outgoing messages", nil)
    42  	_Prom.RegisterCounter(CntStoreConflicts, "number of txn conflicts", nil)
    43  	_Prom.RegisterCounterVec(CntRPC, "number of rpc calls", nil, []string{"constructor"})
    44  
    45  	_Prom.RegisterGauge(GaugeActiveWebsocketConnections, "number of gateway active websocket connections", nil)
    46  
    47  	_Prom.RegisterHistogram(HistGatewayRequestTime, "the amount of process time for gateway requests", TimeBucketMS, nil)
    48  	_Prom.RegisterHistogram(HistTunnelRequestTime, "the amount of process time for tunnel requests", TimeBucketMS, nil)
    49  	_Prom.RegisterHistogram(HistTunnelRoundTripTime, "the roundtrip of a execute remote command", TimeBucketMS, nil)
    50  }
    51  
    52  func Register(registerer prometheus.Registerer) {
    53  	for _, m := range _Prom.counters {
    54  		registerer.MustRegister(m)
    55  	}
    56  	for _, m := range _Prom.counterVectors {
    57  		registerer.MustRegister(m)
    58  	}
    59  	for _, m := range _Prom.gauges {
    60  		registerer.MustRegister(m)
    61  	}
    62  	for _, m := range _Prom.gaugeVectors {
    63  		registerer.MustRegister(m)
    64  	}
    65  	for _, m := range _Prom.histograms {
    66  		registerer.MustRegister(m)
    67  	}
    68  }
    69  
    70  func IncCounter(name string) {
    71  	if _Prom == nil {
    72  		return
    73  	}
    74  	_Prom.Counter(name).Inc()
    75  }
    76  
    77  func IncCounterVec(name string, labelValues ...string) {
    78  	if _Prom == nil {
    79  		return
    80  	}
    81  	_Prom.CounterVec(name).WithLabelValues(labelValues...).Inc()
    82  }
    83  
    84  func AddCounter(name string, v float64) {
    85  	if _Prom == nil {
    86  		return
    87  	}
    88  	_Prom.Counter(name).Add(v)
    89  }
    90  
    91  func AddCounterVec(name string, v float64, labelValues ...string) {
    92  	if _Prom == nil {
    93  		return
    94  	}
    95  	_Prom.CounterVec(name).WithLabelValues(labelValues...).Add(v)
    96  }
    97  
    98  func SetGauge(name string, v float64) {
    99  	if _Prom == nil {
   100  		return
   101  	}
   102  	_Prom.Gauge(name).Set(v)
   103  }
   104  
   105  func ObserveHistogram(name string, v float64) {
   106  	if _Prom == nil {
   107  		return
   108  	}
   109  	_Prom.Histogram(name).Observe(v)
   110  }