Skip to content

Commit

Permalink
Merge PR: Release/v0.18.7.x (#896)
Browse files Browse the repository at this point in the history
* Merge PR :fix function error in keywords of block.number/hash/timestamp (#879)

* fix error in keywords of block.number/hash/timestamp

* optimize code

* Merge PR:fix invalid state set when the value is zero (#881)

* Merge PR:open bloom filter default (#886)

* Merge PR: compatible with etherclient (#888)

* Merge PR :rpc monitor: register metrics to prometheus (#884)

* optimize rpc eth_getBalance method (#891)

* add mutex in websocket connection, in case of concurrent write (#887)

* add mutex in websocket connection, in case of concurrent write

* adpat the header format

* inital refactor

* add mutex in EventSystem

Co-authored-by: KamiD <[email protected]>

* Merge PR :fix rpc monitor error (#892)

* fix rpc monitor error

* fix rpc monitor elapse time

Co-authored-by: KamiD <[email protected]>

* Add LRU cache for GetCodeByHash (#893)

* delete json parse in function getCodeByHash

* add LRU cache

* change lru to lru.cache to make sure thread safe (#894)

* optimize code to avoid reinit cdc (#895)

* Merge PR:concurrent safe cdc&config singleton (#897)

* Merge PR :add state lru cache to optimize eth_call (#898)

* add 1KW LRU cache to optimize eth_call

* optimize code

* Merge PR :optimize viper to avoid recall (#899)

Co-authored-by: KamiD <[email protected]>
Co-authored-by: MengXiangJian <[email protected]>
Co-authored-by: Evan Han <[email protected]>
Co-authored-by: Ray Green <[email protected]>
  • Loading branch information
5 people authored Jun 7, 2021
1 parent df1f078 commit 2cc33d4
Show file tree
Hide file tree
Showing 23 changed files with 677 additions and 257 deletions.
68 changes: 65 additions & 3 deletions app/rpc/apis.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package rpc

import (
"fmt"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/server"
"github.com/ethereum/go-ethereum/rpc"
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/prometheus"
evmtypes "github.com/okex/exchain/x/evm/types"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/log"
"golang.org/x/time/rate"
"reflect"
"strings"
"unicode"

"github.com/okex/exchain/app/crypto/ethsecp256k1"
"github.com/okex/exchain/app/rpc/backend"
Expand Down Expand Up @@ -49,7 +55,7 @@ func GetAPIs(clientCtx context.CLIContext, log log.Logger, keys ...ethsecp256k1.
{
Namespace: Web3Namespace,
Version: apiVersion,
Service: web3.NewAPI(),
Service: web3.NewAPI(log),
Public: true,
},
{
Expand All @@ -61,13 +67,13 @@ func GetAPIs(clientCtx context.CLIContext, log log.Logger, keys ...ethsecp256k1.
{
Namespace: EthNamespace,
Version: apiVersion,
Service: filters.NewAPI(clientCtx, ethBackend),
Service: filters.NewAPI(clientCtx, log, ethBackend),
Public: true,
},
{
Namespace: NetNamespace,
Version: apiVersion,
Service: net.NewAPI(clientCtx),
Service: net.NewAPI(clientCtx, log),
Public: true,
},
}
Expand All @@ -80,6 +86,12 @@ func GetAPIs(clientCtx context.CLIContext, log log.Logger, keys ...ethsecp256k1.
Public: false,
})
}

if viper.GetBool(FlagEnableMonitor) {
for _, api := range apis {
makeMonitorMetrics(api.Namespace, api.Service)
}
}
return apis
}

Expand All @@ -97,3 +109,53 @@ func getRateLimiter() map[string]*rate.Limiter {
}
return rateLimiters
}

func makeMonitorMetrics(namespace string, service interface{}) {
receiver := reflect.ValueOf(service)
if !hasMetricsField(receiver.Elem()) {
return
}
metricsVal := receiver.Elem().FieldByName(MetricsFieldName)

monitorMetrics := make(map[string]metrics.Counter)
typ := receiver.Type()
for m := 0; m < typ.NumMethod(); m++ {
method := typ.Method(m)
if method.PkgPath != "" {
continue // method not exported
}
methodName := formatMethodName(method.Name)
name := fmt.Sprintf("%s_%s", namespace, methodName)
monitorMetrics[name] = prometheus.NewCounterFrom(stdprometheus.CounterOpts{
Namespace: MetricsNamespace,
Subsystem: MetricsSubsystem,
Name: name,
Help: fmt.Sprintf("Number of %s method.", name),
}, nil)
}

if metricsVal.CanSet() && metricsVal.Type() == reflect.ValueOf(monitorMetrics).Type() {
metricsVal.Set(reflect.ValueOf(monitorMetrics))
}
}

// formatMethodName converts to first character of name to lowercase.
func formatMethodName(name string) string {
ret := []rune(name)
if len(ret) > 0 {
ret[0] = unicode.ToLower(ret[0])
}
return string(ret)
}

func hasMetricsField(receiver reflect.Value) bool {
if receiver.Kind() != reflect.Struct {
return false
}
for i := 0; i < receiver.NumField(); i++ {
if receiver.Type().Field(i).Name == MetricsFieldName {
return true
}
}
return false
}
7 changes: 7 additions & 0 deletions app/rpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ const (
FlagRateLimitApi = "rpc.rate-limit-api"
FlagRateLimitCount = "rpc.rate-limit-count"
FlagRateLimitBurst = "rpc.rate-limit-burst"
FlagEnableMonitor = "rpc.enable-monitor"

MetricsNamespace = "x"
// MetricsSubsystem is a subsystem shared by all metrics exposed by this package.
MetricsSubsystem = "rpc"

MetricsFieldName = "Metrics"
)

// RegisterRoutes creates a new server and registers the `/rpc` endpoint.
Expand Down
38 changes: 38 additions & 0 deletions app/rpc/monitor/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package monitor

import (
"fmt"
"github.com/go-kit/kit/metrics"
"github.com/tendermint/tendermint/libs/log"
"time"
)

type Monitor struct {
method string
logger log.Logger
lastTimestamp int64
}

func GetMonitor(method string, logger log.Logger) *Monitor {
return &Monitor{
method: method,
logger: logger,
}
}

func (m *Monitor) OnBegin(metrics map[string]metrics.Counter) {
m.lastTimestamp = time.Now().UnixNano()

if metrics == nil {
return
}
if _, ok := metrics[m.method]; ok {
metrics[m.method].Add(1)
}
}

func (m *Monitor) OnEnd(args ...interface{}) {
now := time.Now().UnixNano()
unit := int64(1e6)
m.logger.Debug(fmt.Sprintf("RPC: Method<%s>, Interval<%dms>, Params<%v>", m.method, (now-m.lastTimestamp)/unit, args))
}
Loading

0 comments on commit 2cc33d4

Please sign in to comment.