Skip to content

Commit eceebeb

Browse files
committed
Enhance eth_call metrics tracking by including parameters in updateServeTimeHistogram
1 parent c40f50e commit eceebeb

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

rpc/handler.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,18 @@ func (h *handler) handleCall(cp *callProc, msg *jsonrpcMessage) *jsonrpcMessage
524524
successfulRequestGauge.Inc(1)
525525
}
526526
rpcServingTimer.UpdateSince(start)
527-
updateServeTimeHistogram(msg.Method, answer.Error == nil, time.Since(start))
527+
528+
// Pass args to updateServeTimeHistogram if it's eth_call
529+
if msg.Method == "eth_call" {
530+
log.Debug("eth_call raw params", "params", msg.Params)
531+
if len(args) > 0 {
532+
updateServeTimeHistogram(msg.Method, answer.Error == nil, time.Since(start), args[0].Interface())
533+
} else {
534+
updateServeTimeHistogram(msg.Method, answer.Error == nil, time.Since(start))
535+
}
536+
} else {
537+
updateServeTimeHistogram(msg.Method, answer.Error == nil, time.Since(start))
538+
}
528539
}
529540

530541
return answer

rpc/metrics.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ package rpc
1818

1919
import (
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

@@ -35,7 +38,7 @@ var (
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

Comments
 (0)