[RPC] General solution of prometheus metrics for all RPC methods (#3919)

* [rpc] add method wise prometheus metrics

* [misc] remove method APIs for all services (not used anyway)

* [misc] remove use of service.APIs

* [rpc] fix goimport error, renamed new prometheus rpc
pull/3916/head
Jacky Wang 3 years ago committed by GitHub
parent 791c9d2018
commit 4bf9c3e527
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      api/service/blockproposal/service.go
  2. 6
      api/service/consensus/service.go
  3. 6
      api/service/explorer/service.go
  4. 2
      api/service/manager.go
  5. 6
      api/service/pprof/service.go
  6. 6
      api/service/prometheus/service.go
  7. 6
      api/service/synchronize/service.go
  8. 2
      consensus/votepower/roster.go
  9. 4
      eth/rpc/handler.go
  10. 68
      eth/rpc/metrics.go
  11. 4
      node/api.go

@ -3,7 +3,6 @@ package blockproposal
import ( import (
msg_pb "github.com/harmony-one/harmony/api/proto/message" msg_pb "github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/consensus" "github.com/harmony-one/harmony/consensus"
"github.com/harmony-one/harmony/eth/rpc"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
) )
@ -43,8 +42,3 @@ func (s *Service) Stop() error {
utils.Logger().Info().Msg("Role conversion stopped.") utils.Logger().Info().Msg("Role conversion stopped.")
return nil return nil
} }
// APIs for the services.
func (s *Service) APIs() []rpc.API {
return nil
}

@ -4,7 +4,6 @@ import (
msg_pb "github.com/harmony-one/harmony/api/proto/message" msg_pb "github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/consensus" "github.com/harmony-one/harmony/consensus"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/eth/rpc"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
) )
@ -41,8 +40,3 @@ func (s *Service) Stop() error {
utils.Logger().Info().Msg("Consensus service stopped.") utils.Logger().Info().Msg("Consensus service stopped.")
return s.consensus.Close() return s.consensus.Close()
} }
// APIs for the services.
func (s *Service) APIs() []rpc.API {
return nil
}

@ -17,7 +17,6 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
msg_pb "github.com/harmony-one/harmony/api/proto/message" msg_pb "github.com/harmony-one/harmony/api/proto/message"
"github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/eth/rpc"
"github.com/harmony-one/harmony/hmy" "github.com/harmony-one/harmony/hmy"
"github.com/harmony-one/harmony/internal/chain" "github.com/harmony-one/harmony/internal/chain"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node" nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
@ -294,11 +293,6 @@ func (s *Service) SetMessageChan(messageChan chan *msg_pb.Message) {
s.messageChan = messageChan s.messageChan = messageChan
} }
// APIs for the services.
func (s *Service) APIs() []rpc.API {
return nil
}
func defaultDBPath(ip, port string) string { func defaultDBPath(ip, port string) string {
return path.Join(nodeconfig.GetDefaultConfig().DBDir, "explorer_storage_"+ip+"_"+port) return path.Join(nodeconfig.GetDefaultConfig().DBDir, "explorer_storage_"+ip+"_"+port)
} }

@ -3,7 +3,6 @@ package service
import ( import (
"fmt" "fmt"
"github.com/harmony-one/harmony/eth/rpc"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -52,7 +51,6 @@ func (t Type) String() string {
type Service interface { type Service interface {
Start() error Start() error
Stop() error Stop() error
APIs() []rpc.API // the list of RPC descriptors the service provides
} }
// Manager stores all services for service manager. // Manager stores all services for service manager.

@ -10,7 +10,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/harmony-one/harmony/eth/rpc"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
) )
@ -127,11 +126,6 @@ func (s *Service) Stop() error {
return nil return nil
} }
// APIs return all APIs of the service
func (s *Service) APIs() []rpc.API {
return nil
}
// scheduleProfile schedules the provided profile based on the specified interval (e.g. saves the profile every x seconds) // scheduleProfile schedules the provided profile based on the specified interval (e.g. saves the profile every x seconds)
func scheduleProfile(profile Profile, dir string) { func scheduleProfile(profile Profile, dir string) {
go func() { go func() {

@ -11,7 +11,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/harmony-one/harmony/eth/rpc"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
@ -181,11 +180,6 @@ func (s *Service) Status() error {
return nil return nil
} }
// APIs returns the RPC apis of the prometheus service
func (s *Service) APIs() []rpc.API {
return nil
}
func (s *Service) getRegistry() *prometheus.Registry { func (s *Service) getRegistry() *prometheus.Registry {
s.registryOnce.Do(func() { s.registryOnce.Do(func() {
if svc.registry == nil { if svc.registry == nil {

@ -2,7 +2,6 @@ package synchronize
import ( import (
"github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/core"
"github.com/harmony-one/harmony/eth/rpc"
"github.com/harmony-one/harmony/hmy/downloader" "github.com/harmony-one/harmony/hmy/downloader"
"github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p"
) )
@ -30,8 +29,3 @@ func (s *Service) Stop() error {
s.Downloaders.Close() s.Downloaders.Close()
return nil return nil
} }
// APIs return all APIs of the service
func (s *Service) APIs() []rpc.API {
return nil
}

@ -103,7 +103,7 @@ type topLevelRegistry struct {
type Roster struct { type Roster struct {
Voters map[bls.SerializedPublicKey]*AccommodateHarmonyVote Voters map[bls.SerializedPublicKey]*AccommodateHarmonyVote
topLevelRegistry topLevelRegistry
ShardID uint32 ShardID uint32
OrderedSlots []bls.SerializedPublicKey OrderedSlots []bls.SerializedPublicKey
} }

@ -367,8 +367,12 @@ func (h *handler) handleSubscribe(cp *callProc, msg *jsonrpcMessage) *jsonrpcMes
// runMethod runs the Go callback for an RPC method. // runMethod runs the Go callback for an RPC method.
func (h *handler) runMethod(ctx context.Context, msg *jsonrpcMessage, callb *callback, args []reflect.Value) *jsonrpcMessage { func (h *handler) runMethod(ctx context.Context, msg *jsonrpcMessage, callb *callback, args []reflect.Value) *jsonrpcMessage {
timer := doMetricRequest(msg.Method)
defer doMetricDelayHist(timer)
result, err := callb.call(ctx, msg.Method, args) result, err := callb.call(ctx, msg.Method, args)
if err != nil { if err != nil {
doMetricErroredRequest(msg.Method)
return msg.errorResponse(err) return msg.errorResponse(err)
} }
return msg.response(result) return msg.response(result)

@ -0,0 +1,68 @@
package rpc
import (
prom "github.com/harmony-one/harmony/api/service/prometheus"
"github.com/prometheus/client_golang/prometheus"
)
func init() {
prom.PromRegistry().MustRegister(
requestCounterVec,
requestErroredCounterVec,
requestDurationHistVec,
)
}
var (
requestCounterVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "hmy",
Subsystem: "rpc2",
Name: "request_count",
Help: "counters for each RPC method",
},
[]string{"method"},
)
requestErroredCounterVec = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "hmy",
Subsystem: "rpc2",
Name: "err_count",
Help: "counters of errored RPC method",
},
[]string{"method"},
)
requestDurationHistVec = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "hmy",
Subsystem: "rpc2",
Name: "delay_histogram",
Help: "delays histogram in seconds",
// buckets: 50ms, 100ms, 200ms, 400ms, 800ms, 1600ms, 3200ms, +INF
Buckets: prometheus.ExponentialBuckets(0.05, 2, 8),
},
[]string{"method"},
)
)
func doMetricRequest(method string) *prometheus.Timer {
pLabel := prometheus.Labels{
"method": method,
}
requestCounterVec.With(pLabel).Inc()
timer := prometheus.NewTimer(requestDurationHistVec.With(pLabel))
return timer
}
func doMetricErroredRequest(method string) {
pLabel := prometheus.Labels{
"method": method,
}
requestErroredCounterVec.With(pLabel).Inc()
}
func doMetricDelayHist(timer *prometheus.Timer) {
timer.ObserveDuration()
}

@ -70,10 +70,6 @@ func (node *Node) StartRPC() error {
// Gather all the possible APIs to surface // Gather all the possible APIs to surface
apis := node.APIs(harmony) apis := node.APIs(harmony)
for _, service := range node.serviceManager.GetServices() {
apis = append(apis, service.APIs()...)
}
return hmy_rpc.StartServers(harmony, apis, node.NodeConfig.RPCServer) return hmy_rpc.StartServers(harmony, apis, node.NodeConfig.RPCServer)
} }

Loading…
Cancel
Save