From 8fd8e179290e04e9532e705371bf06fba7dade17 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Sun, 9 May 2021 18:56:38 +0200 Subject: [PATCH] Add websocket subscription metrics --- common/metrics.go | 10 +++++----- server/websocket.go | 8 ++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/common/metrics.go b/common/metrics.go index 1171c7ca..10d1abb7 100644 --- a/common/metrics.go +++ b/common/metrics.go @@ -13,7 +13,7 @@ type Metrics struct { SocketIOClients prometheus.Gauge SocketIOReqDuration *prometheus.HistogramVec WebsocketRequests *prometheus.CounterVec - WebsocketSubscribes *prometheus.CounterVec + WebsocketSubscribes *prometheus.GaugeVec WebsocketClients prometheus.Gauge WebsocketReqDuration *prometheus.HistogramVec IndexResyncDuration prometheus.Histogram @@ -82,13 +82,13 @@ func GetMetrics(coin string) (*Metrics, error) { }, []string{"method", "status"}, ) - metrics.WebsocketSubscribes = prometheus.NewCounterVec( - prometheus.CounterOpts{ + metrics.WebsocketSubscribes = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ 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}, }, - []string{"channel", "status"}, + []string{"method"}, ) metrics.WebsocketClients = prometheus.NewGauge( prometheus.GaugeOpts{ diff --git a/server/websocket.go b/server/websocket.go index 64ec64c8..a8ead8b1 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -688,6 +688,7 @@ func (s *WebsocketServer) subscribeNewBlock(c *websocketChannel, req *websocketR s.newBlockSubscriptionsLock.Lock() defer s.newBlockSubscriptionsLock.Unlock() s.newBlockSubscriptions[c] = req.ID + s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeNewBlock"})).Set(float64(len(s.newBlockSubscriptions))) return &subscriptionResponse{true}, nil } @@ -695,6 +696,7 @@ func (s *WebsocketServer) unsubscribeNewBlock(c *websocketChannel) (res interfac s.newBlockSubscriptionsLock.Lock() defer s.newBlockSubscriptionsLock.Unlock() delete(s.newBlockSubscriptions, c) + s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeNewBlock"})).Set(float64(len(s.newBlockSubscriptions))) 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 } s.newTransactionSubscriptions[c] = req.ID + s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeNewTransaction"})).Set(float64(len(s.newTransactionSubscriptions))) 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 } delete(s.newTransactionSubscriptions, c) + s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeNewTransaction"})).Set(float64(len(s.newTransactionSubscriptions))) return &subscriptionResponse{false}, nil } @@ -765,6 +769,7 @@ func (s *WebsocketServer) subscribeAddresses(c *websocketChannel, addrDesc []bch } as[c] = req.ID } + s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeAddresses"})).Set(float64(len(s.addressSubscriptions))) return &subscriptionResponse{true}, nil } @@ -773,6 +778,7 @@ func (s *WebsocketServer) unsubscribeAddresses(c *websocketChannel) (res interfa s.addressSubscriptionsLock.Lock() defer s.addressSubscriptionsLock.Unlock() s.doUnsubscribeAddresses(c) + s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeAddresses"})).Set(float64(len(s.addressSubscriptions))) return &subscriptionResponse{false}, nil } @@ -805,6 +811,7 @@ func (s *WebsocketServer) subscribeFiatRates(c *websocketChannel, currency strin s.fiatRatesSubscriptions[currency] = as } as[c] = req.ID + s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeFiatRates"})).Set(float64(len(s.fiatRatesSubscriptions))) return &subscriptionResponse{true}, nil } @@ -813,6 +820,7 @@ func (s *WebsocketServer) unsubscribeFiatRates(c *websocketChannel) (res interfa s.fiatRatesSubscriptionsLock.Lock() defer s.fiatRatesSubscriptionsLock.Unlock() s.doUnsubscribeFiatRates(c) + s.metrics.WebsocketSubscribes.With((common.Labels{"method": "subscribeFiatRates"})).Set(float64(len(s.fiatRatesSubscriptions))) return &subscriptionResponse{false}, nil }