Skip to content

Commit

Permalink
move chanid metrics to chainservice (#3544)
Browse files Browse the repository at this point in the history
Co-authored-by: dustinxie <[email protected]>
  • Loading branch information
millken and dustinxie committed Jul 19, 2022
1 parent dced4ff commit 2048392
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 39 deletions.
38 changes: 0 additions & 38 deletions api/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion api/grpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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)
Expand Down
50 changes: 50 additions & 0 deletions chainservice/chainservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2048392

Please sign in to comment.