diff --git a/api/grpcserver.go b/api/grpcserver.go index 1a7ca92703..ff242ec30f 100644 --- a/api/grpcserver.go +++ b/api/grpcserver.go @@ -26,7 +26,6 @@ import ( "github.com/iotexproject/iotex-proto/golang/iotexapi" "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/pkg/errors" - "github.com/prometheus/client_golang/prometheus" "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" "go.opentelemetry.io/otel/attribute" "go.uber.org/zap" @@ -35,7 +34,6 @@ import ( "google.golang.org/grpc/health" "google.golang.org/grpc/health/grpc_health_v1" "google.golang.org/grpc/keepalive" - "google.golang.org/grpc/peer" "google.golang.org/grpc/reflection" "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/timestamppb" @@ -72,28 +70,8 @@ var ( Time: 60 * time.Second, // Ping the client if it is idle for 60 seconds to ensure the connection is still active Timeout: 10 * time.Second, // Wait 10 seconds for the ping ack before assuming the connection is dead } - - _apiCallSourceWithChainIDMtc = prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "iotex_apicallsource_chainid_metrics", - Help: "API call Source ChainID Statistics", - }, - []string{"chain_id"}, - ) - _apiCallSourceWithOutChainIDMtc = prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "iotex_apicallsource_nochainid_metrics", - Help: "API call Source Without ChainID Statistics", - }, - []string{"client_ip", "sender"}, - ) ) -func init() { - prometheus.MustRegister(_apiCallSourceWithChainIDMtc) - prometheus.MustRegister(_apiCallSourceWithOutChainIDMtc) -} - // RecoveryInterceptor handles panic to a custom error func RecoveryInterceptor() grpc_recovery.Option { return grpc_recovery.WithRecoveryHandler(func(p interface{}) (err error) { @@ -342,22 +320,6 @@ func (svr *gRPCHandler) SendAction(ctx context.Context, in *iotexapi.SendActionR // tags output span.SetAttributes(attribute.String("actType", fmt.Sprintf("%T", in.GetAction().GetCore()))) defer span.End() - chainID := strconv.FormatUint(uint64(in.GetAction().GetCore().GetChainID()), 10) - if in.GetAction().GetCore().GetChainID() > 0 { - _apiCallSourceWithChainIDMtc.WithLabelValues(chainID).Inc() - } else { - selp, err := (&action.Deserializer{}).SetEvmNetworkID(svr.coreService.EVMNetworkID()).ActionToSealedEnvelope(in.GetAction()) - if err != nil { - return nil, err - } - var clientIP string - if p, ok := peer.FromContext(ctx); ok { - clientIP, _, _ = net.SplitHostPort(p.Addr.String()) - } else { - clientIP = "unknownIP" - } - _apiCallSourceWithOutChainIDMtc.WithLabelValues(clientIP, selp.SenderAddress().String()).Inc() - } actHash, err := svr.coreService.SendAction(ctx, in.GetAction()) if err != nil { return nil, err diff --git a/api/grpcserver_test.go b/api/grpcserver_test.go index 684304a456..5145aab049 100644 --- a/api/grpcserver_test.go +++ b/api/grpcserver_test.go @@ -86,7 +86,6 @@ func TestGrpcServer_SendAction(t *testing.T) { grpcSvr := newGRPCHandler(core) for _, test := range _sendActionTests { - core.EXPECT().EVMNetworkID().Return(uint32(1)) core.EXPECT().SendAction(context.Background(), test.actionPb).Return(test.actionHash, nil) request := &iotexapi.SendActionRequest{Action: test.actionPb} res, err := grpcSvr.SendAction(context.Background(), request) diff --git a/chainservice/chainservice.go b/chainservice/chainservice.go index 823c226156..515429d73f 100644 --- a/chainservice/chainservice.go +++ b/chainservice/chainservice.go @@ -8,11 +8,14 @@ package chainservice import ( "context" + "strconv" "github.com/libp2p/go-libp2p-core/peer" "github.com/pkg/errors" + "github.com/prometheus/client_golang/prometheus" "google.golang.org/protobuf/proto" + "github.com/iotexproject/iotex-address/address" "github.com/iotexproject/iotex-election/committee" "github.com/iotexproject/iotex-proto/golang/iotexrpc" "github.com/iotexproject/iotex-proto/golang/iotextypes" @@ -36,6 +39,28 @@ import ( "github.com/iotexproject/iotex-core/state/factory" ) +var ( + _apiCallWithChainIDMtc = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "iotex_apicall_chainid_metrics", + Help: "API call ChainID Statistics", + }, + []string{"chain_id"}, + ) + _apiCallWithOutChainIDMtc = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "iotex_apicall_nochainid_metrics", + Help: "API call Without ChainID Statistics", + }, + []string{"sender", "recipient"}, + ) +) + +func init() { + prometheus.MustRegister(_apiCallWithChainIDMtc) + prometheus.MustRegister(_apiCallWithOutChainIDMtc) +} + // ChainService is a blockchain service with all blockchain components. type ChainService struct { lifecycle lifecycle.Lifecycle @@ -80,9 +105,34 @@ func (cs *ChainService) HandleAction(ctx context.Context, actPb *iotextypes.Acti if err != nil { log.L().Debug(err.Error()) } + chainIDmetrics(act) return err } +func chainIDmetrics(act action.SealedEnvelope) { + chainID := strconv.FormatUint(uint64(act.ChainID()), 10) + if act.ChainID() > 0 { + _apiCallWithChainIDMtc.WithLabelValues(chainID).Inc() + } else { + recipient, _ := act.Destination() + //it will be empty for staking action, change string to staking in such case + if recipient == "" { + act, ok := act.Action().(action.EthCompatibleAction) + if ok { + if ethTx, err := act.ToEthTx(); err == nil && ethTx.To() != nil { + if add, err := address.FromHex(ethTx.To().Hex()); err == nil { + recipient = add.String() + } + } + } + if recipient == "" { + recipient = "staking" + } + } + _apiCallWithOutChainIDMtc.WithLabelValues(act.SenderAddress().String(), recipient).Inc() + } +} + // HandleBlock handles incoming block request. func (cs *ChainService) HandleBlock(ctx context.Context, peer string, pbBlock *iotextypes.Block) error { blk, err := block.NewDeserializer(cs.chain.EvmNetworkID()).FromBlockProto(pbBlock)