diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-06-25 23:18:42 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-06-25 23:18:42 +0800 |
commit | b0a5be4495962c291a25cbea793e43bad0781510 (patch) | |
tree | e058201e7b29c3cb8efd710e20ec1e1f97f64368 /rpc | |
parent | e64625aa8215985c85f97f914a98db081e07714f (diff) | |
parent | e9c0b5431cbd7430ddec9fd17983241018fd8a55 (diff) | |
download | dexon-b0a5be4495962c291a25cbea793e43bad0781510.tar.gz dexon-b0a5be4495962c291a25cbea793e43bad0781510.tar.zst dexon-b0a5be4495962c291a25cbea793e43bad0781510.zip |
Merge pull request #1321 from karalabe/cut-it-open-3000
Metrics collecting and reporting support
Diffstat (limited to 'rpc')
-rw-r--r-- | rpc/api/debug.go | 100 | ||||
-rw-r--r-- | rpc/api/debug_args.go | 24 | ||||
-rw-r--r-- | rpc/api/debug_js.go | 7 | ||||
-rw-r--r-- | rpc/xeth.go | 52 |
4 files changed, 183 insertions, 0 deletions
diff --git a/rpc/api/debug.go b/rpc/api/debug.go index b451d8662..f16f62d2e 100644 --- a/rpc/api/debug.go +++ b/rpc/api/debug.go @@ -2,6 +2,8 @@ package api import ( "fmt" + "strings" + "time" "github.com/ethereum/ethash" "github.com/ethereum/go-ethereum/core/state" @@ -11,6 +13,7 @@ import ( "github.com/ethereum/go-ethereum/rpc/codec" "github.com/ethereum/go-ethereum/rpc/shared" "github.com/ethereum/go-ethereum/xeth" + "github.com/rcrowley/go-metrics" ) const ( @@ -26,6 +29,7 @@ var ( "debug_processBlock": (*debugApi).ProcessBlock, "debug_seedHash": (*debugApi).SeedHash, "debug_setHead": (*debugApi).SetHead, + "debug_metrics": (*debugApi).Metrics, } ) @@ -171,3 +175,99 @@ func (self *debugApi) SeedHash(req *shared.Request) (interface{}, error) { return nil, err } } + +func (self *debugApi) Metrics(req *shared.Request) (interface{}, error) { + args := new(MetricsArgs) + if err := self.codec.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + // Create a rate formatter + units := []string{"", "K", "M", "G", "T", "E", "P"} + round := func(value float64, prec int) string { + unit := 0 + for value >= 1000 { + unit, value, prec = unit+1, value/1000, 2 + } + return fmt.Sprintf(fmt.Sprintf("%%.%df%s", prec, units[unit]), value) + } + format := func(total float64, rate float64) string { + return fmt.Sprintf("%s (%s/s)", round(total, 0), round(rate, 2)) + } + // Iterate over all the metrics, and just dump for now + counters := make(map[string]interface{}) + metrics.DefaultRegistry.Each(func(name string, metric interface{}) { + // Create or retrieve the counter hierarchy for this metric + root, parts := counters, strings.Split(name, "/") + for _, part := range parts[:len(parts)-1] { + if _, ok := root[part]; !ok { + root[part] = make(map[string]interface{}) + } + root = root[part].(map[string]interface{}) + } + name = parts[len(parts)-1] + + // Fill the counter with the metric details, formatting if requested + if args.Raw { + switch metric := metric.(type) { + case metrics.Meter: + root[name] = map[string]interface{}{ + "AvgRate01Min": metric.Rate1(), + "AvgRate05Min": metric.Rate5(), + "AvgRate15Min": metric.Rate15(), + "MeanRate": metric.RateMean(), + "Overall": float64(metric.Count()), + } + + case metrics.Timer: + root[name] = map[string]interface{}{ + "AvgRate01Min": metric.Rate1(), + "AvgRate05Min": metric.Rate5(), + "AvgRate15Min": metric.Rate15(), + "MeanRate": metric.RateMean(), + "Overall": float64(metric.Count()), + "Percentiles": map[string]interface{}{ + "5": metric.Percentile(0.05), + "20": metric.Percentile(0.2), + "50": metric.Percentile(0.5), + "80": metric.Percentile(0.8), + "95": metric.Percentile(0.95), + }, + } + + default: + root[name] = "Unknown metric type" + } + } else { + switch metric := metric.(type) { + case metrics.Meter: + root[name] = map[string]interface{}{ + "Avg01Min": format(metric.Rate1()*60, metric.Rate1()), + "Avg05Min": format(metric.Rate5()*300, metric.Rate5()), + "Avg15Min": format(metric.Rate15()*900, metric.Rate15()), + "Overall": format(float64(metric.Count()), metric.RateMean()), + } + + case metrics.Timer: + root[name] = map[string]interface{}{ + "Avg01Min": format(metric.Rate1()*60, metric.Rate1()), + "Avg05Min": format(metric.Rate5()*300, metric.Rate5()), + "Avg15Min": format(metric.Rate15()*900, metric.Rate15()), + "Overall": format(float64(metric.Count()), metric.RateMean()), + "Maximum": time.Duration(metric.Max()).String(), + "Minimum": time.Duration(metric.Min()).String(), + "Percentiles": map[string]interface{}{ + "5": time.Duration(metric.Percentile(0.05)).String(), + "20": time.Duration(metric.Percentile(0.2)).String(), + "50": time.Duration(metric.Percentile(0.5)).String(), + "80": time.Duration(metric.Percentile(0.8)).String(), + "95": time.Duration(metric.Percentile(0.95)).String(), + }, + } + + default: + root[name] = "Unknown metric type" + } + } + }) + return counters, nil +} diff --git a/rpc/api/debug_args.go b/rpc/api/debug_args.go index b9b5aa27e..b72fb03ae 100644 --- a/rpc/api/debug_args.go +++ b/rpc/api/debug_args.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "math/big" + "reflect" "github.com/ethereum/go-ethereum/rpc/shared" ) @@ -45,3 +46,26 @@ func (args *WaitForBlockArgs) UnmarshalJSON(b []byte) (err error) { return nil } + +type MetricsArgs struct { + Raw bool +} + +func (args *MetricsArgs) UnmarshalJSON(b []byte) (err error) { + var obj []interface{} + if err := json.Unmarshal(b, &obj); err != nil { + return shared.NewDecodeParamError(err.Error()) + } + if len(obj) > 1 { + return fmt.Errorf("metricsArgs needs 0, 1 arguments") + } + // default values when not provided + if len(obj) >= 1 && obj[0] != nil { + if value, ok := obj[0].(bool); !ok { + return fmt.Errorf("invalid argument %v", reflect.TypeOf(obj[0])) + } else { + args.Raw = value + } + } + return nil +} diff --git a/rpc/api/debug_js.go b/rpc/api/debug_js.go index 35fecb75f..bd3a6dfb2 100644 --- a/rpc/api/debug_js.go +++ b/rpc/api/debug_js.go @@ -46,6 +46,13 @@ web3._extend({ params: 1, inputFormatter: [web3._extend.formatters.formatInputInt], outputFormatter: function(obj) { return obj; } + }), + new web3._extend.Method({ + name: 'metrics', + call: 'debug_metrics', + params: 1, + inputFormatter: [web3._extend.formatters.formatInputBool], + outputFormatter: function(obj) { return obj; } }) ], properties: diff --git a/rpc/xeth.go b/rpc/xeth.go new file mode 100644 index 000000000..b3e844380 --- /dev/null +++ b/rpc/xeth.go @@ -0,0 +1,52 @@ +package rpc + +import ( + "encoding/json" + "fmt" + "reflect" + "sync/atomic" + + "github.com/ethereum/go-ethereum/rpc/comms" + "github.com/ethereum/go-ethereum/rpc/shared" +) + +// Xeth is a native API interface to a remote node. +type Xeth struct { + client comms.EthereumClient + reqId uint32 +} + +// NewXeth constructs a new native API interface to a remote node. +func NewXeth(client comms.EthereumClient) *Xeth { + return &Xeth{ + client: client, + } +} + +// Call invokes a method with the given parameters are the remote node. +func (self *Xeth) Call(method string, params []interface{}) (map[string]interface{}, error) { + // Assemble the json RPC request + data, err := json.Marshal(params) + if err != nil { + return nil, err + } + req := &shared.Request{ + Id: atomic.AddUint32(&self.reqId, 1), + Jsonrpc: "2.0", + Method: method, + Params: data, + } + // Send the request over and process the response + if err := self.client.Send(req); err != nil { + return nil, err + } + res, err := self.client.Recv() + if err != nil { + return nil, err + } + value, ok := res.(map[string]interface{}) + if !ok { + return nil, fmt.Errorf("Invalid response type: have %v, want %v", reflect.TypeOf(res), reflect.TypeOf(make(map[string]interface{}))) + } + return value, nil +} |