Add websocket subscription metrics

This commit is contained in:
Martin Boehm 2021-05-09 18:56:38 +02:00
parent 1482b51503
commit 8fd8e17929
2 changed files with 13 additions and 5 deletions

View File

@ -13,7 +13,7 @@ type Metrics struct {
SocketIOClients prometheus.Gauge SocketIOClients prometheus.Gauge
SocketIOReqDuration *prometheus.HistogramVec SocketIOReqDuration *prometheus.HistogramVec
WebsocketRequests *prometheus.CounterVec WebsocketRequests *prometheus.CounterVec
WebsocketSubscribes *prometheus.CounterVec WebsocketSubscribes *prometheus.GaugeVec
WebsocketClients prometheus.Gauge WebsocketClients prometheus.Gauge
WebsocketReqDuration *prometheus.HistogramVec WebsocketReqDuration *prometheus.HistogramVec
IndexResyncDuration prometheus.Histogram IndexResyncDuration prometheus.Histogram
@ -82,13 +82,13 @@ func GetMetrics(coin string) (*Metrics, error) {
}, },
[]string{"method", "status"}, []string{"method", "status"},
) )
metrics.WebsocketSubscribes = prometheus.NewCounterVec( metrics.WebsocketSubscribes = prometheus.NewGaugeVec(
prometheus.CounterOpts{ prometheus.GaugeOpts{
Name: "blockbook_websocket_subscribes", Name: "blockbook_websocket_subscribes",
Help: "Total number of websocket subscribes by channel and status", Help: "Number of websocket subscriptions by method",
ConstLabels: Labels{"coin": coin}, ConstLabels: Labels{"coin": coin},
}, },
[]string{"channel", "status"}, []string{"method"},
) )
metrics.WebsocketClients = prometheus.NewGauge( metrics.WebsocketClients = prometheus.NewGauge(
prometheus.GaugeOpts{ prometheus.GaugeOpts{

View File

@ -688,6 +688,7 @@ func (s *WebsocketServer) subscribeNewBlock(c *websocketChannel, req *websocketR
s.newBlockSubscriptionsLock.Lock() s.newBlockSubscriptionsLock.Lock()
defer s.newBlockSubscriptionsLock.Unlock() defer s.newBlockSubscriptionsLock.Unlock()
s.newBlockSubscriptions[c] = req.ID s.newBlockSubscriptions[c] = req.ID
s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeNewBlock"})).Set(float64(len(s.newBlockSubscriptions)))
return &subscriptionResponse{true}, nil return &subscriptionResponse{true}, nil
} }
@ -695,6 +696,7 @@ func (s *WebsocketServer) unsubscribeNewBlock(c *websocketChannel) (res interfac
s.newBlockSubscriptionsLock.Lock() s.newBlockSubscriptionsLock.Lock()
defer s.newBlockSubscriptionsLock.Unlock() defer s.newBlockSubscriptionsLock.Unlock()
delete(s.newBlockSubscriptions, c) delete(s.newBlockSubscriptions, c)
s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeNewBlock"})).Set(float64(len(s.newBlockSubscriptions)))
return &subscriptionResponse{false}, nil return &subscriptionResponse{false}, nil
} }
@ -705,6 +707,7 @@ func (s *WebsocketServer) subscribeNewTransaction(c *websocketChannel, req *webs
return &subscriptionResponseMessage{false, "subscribeNewTransaction not enabled, use -enablesubnewtx flag to enable."}, nil return &subscriptionResponseMessage{false, "subscribeNewTransaction not enabled, use -enablesubnewtx flag to enable."}, nil
} }
s.newTransactionSubscriptions[c] = req.ID s.newTransactionSubscriptions[c] = req.ID
s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeNewTransaction"})).Set(float64(len(s.newTransactionSubscriptions)))
return &subscriptionResponse{true}, nil return &subscriptionResponse{true}, nil
} }
@ -715,6 +718,7 @@ func (s *WebsocketServer) unsubscribeNewTransaction(c *websocketChannel) (res in
return &subscriptionResponseMessage{false, "unsubscribeNewTransaction not enabled, use -enablesubnewtx flag to enable."}, nil return &subscriptionResponseMessage{false, "unsubscribeNewTransaction not enabled, use -enablesubnewtx flag to enable."}, nil
} }
delete(s.newTransactionSubscriptions, c) delete(s.newTransactionSubscriptions, c)
s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeNewTransaction"})).Set(float64(len(s.newTransactionSubscriptions)))
return &subscriptionResponse{false}, nil return &subscriptionResponse{false}, nil
} }
@ -765,6 +769,7 @@ func (s *WebsocketServer) subscribeAddresses(c *websocketChannel, addrDesc []bch
} }
as[c] = req.ID as[c] = req.ID
} }
s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeAddresses"})).Set(float64(len(s.addressSubscriptions)))
return &subscriptionResponse{true}, nil return &subscriptionResponse{true}, nil
} }
@ -773,6 +778,7 @@ func (s *WebsocketServer) unsubscribeAddresses(c *websocketChannel) (res interfa
s.addressSubscriptionsLock.Lock() s.addressSubscriptionsLock.Lock()
defer s.addressSubscriptionsLock.Unlock() defer s.addressSubscriptionsLock.Unlock()
s.doUnsubscribeAddresses(c) s.doUnsubscribeAddresses(c)
s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeAddresses"})).Set(float64(len(s.addressSubscriptions)))
return &subscriptionResponse{false}, nil return &subscriptionResponse{false}, nil
} }
@ -805,6 +811,7 @@ func (s *WebsocketServer) subscribeFiatRates(c *websocketChannel, currency strin
s.fiatRatesSubscriptions[currency] = as s.fiatRatesSubscriptions[currency] = as
} }
as[c] = req.ID as[c] = req.ID
s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeFiatRates"})).Set(float64(len(s.fiatRatesSubscriptions)))
return &subscriptionResponse{true}, nil return &subscriptionResponse{true}, nil
} }
@ -813,6 +820,7 @@ func (s *WebsocketServer) unsubscribeFiatRates(c *websocketChannel) (res interfa
s.fiatRatesSubscriptionsLock.Lock() s.fiatRatesSubscriptionsLock.Lock()
defer s.fiatRatesSubscriptionsLock.Unlock() defer s.fiatRatesSubscriptionsLock.Unlock()
s.doUnsubscribeFiatRates(c) s.doUnsubscribeFiatRates(c)
s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeFiatRates"})).Set(float64(len(s.fiatRatesSubscriptions)))
return &subscriptionResponse{false}, nil return &subscriptionResponse{false}, nil
} }