@@ -18,8 +18,11 @@ package rpc
1818
1919import (
2020 "fmt"
21+ "reflect"
2122 "time"
2223
24+ "github.com/XinFinOrg/XDPoSChain/common"
25+ "github.com/XinFinOrg/XDPoSChain/log"
2326 "github.com/XinFinOrg/XDPoSChain/metrics"
2427)
2528
3538)
3639
3740// updateServeTimeHistogram tracks the serving time of a remote RPC call.
38- func updateServeTimeHistogram (method string , success bool , elapsed time.Duration ) {
41+ func updateServeTimeHistogram (method string , success bool , elapsed time.Duration , params ... interface {} ) {
3942 note := "success"
4043 if ! success {
4144 note = "failure"
@@ -47,4 +50,41 @@ func updateServeTimeHistogram(method string, success bool, elapsed time.Duration
4750 )
4851 }
4952 metrics .GetOrRegisterHistogramLazy (h , nil , sampler ).Update (elapsed .Nanoseconds ())
53+
54+ // Add metrics for eth_call with contract/caller info
55+ if method == "eth_call" && len (params ) > 0 {
56+ log .Debug ("eth_call paramsvvvvvvvvvvvv" , "params" , params )
57+ log .Debug ("eth_call params[0] type" , "type" , fmt .Sprintf ("%T" , params [0 ]))
58+
59+ // Use reflection to access the From and To fields
60+ v := reflect .ValueOf (params [0 ])
61+ if v .Kind () == reflect .Struct || (v .Kind () == reflect .Ptr && v .Elem ().Kind () == reflect .Struct ) {
62+ // If it's a pointer, get the struct it points to
63+ if v .Kind () == reflect .Ptr {
64+ v = v .Elem ()
65+ }
66+
67+ baseMetric := fmt .Sprintf ("%s/eth_call" , h )
68+
69+ // Get To field
70+ if toField := v .FieldByName ("To" ); toField .IsValid () && ! toField .IsNil () {
71+ to := toField .Interface ().(* common.Address )
72+ log .Debug ("eth_call contract addressvvvvvvvvvvvv" , "to" , to .Hex ())
73+ // Record contract address calls
74+ contractMetric := fmt .Sprintf ("%s/contract/%s" , baseMetric , to .Hex ())
75+ metrics .GetOrRegisterMeter (contractMetric , nil ).Mark (1 )
76+ }
77+
78+ // Get From field
79+ // if fromField := v.FieldByName("From"); fromField.IsValid() && !fromField.IsNil() {
80+ // from := fromField.Interface().(*common.Address)
81+ // log.Debug("eth_call caller addressvvvvvvvvvvvv", "from", from.Hex())
82+ // // Record caller address calls
83+ // callerMetric := fmt.Sprintf("%s/caller/%s", baseMetric, from.Hex())
84+ // metrics.GetOrRegisterMeter(callerMetric, nil).Mark(1)
85+ // }
86+ } else {
87+ log .Debug ("eth_call paramsvvvvvvvvvvvv,no ok!" , "params" , params )
88+ }
89+ }
5090}
0 commit comments