Skip to content

Commit

Permalink
chore: refactoring and add latest_block_height
Browse files Browse the repository at this point in the history
  • Loading branch information
albttx committed Mar 3, 2023
1 parent f802b0f commit 2582466
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 65 deletions.
42 changes: 35 additions & 7 deletions general.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import (
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
)

func GeneralHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.ClientConn) {
func (s *service) GeneralHandler(w http.ResponseWriter, r *http.Request) {
requestStart := time.Now()

sublogger := log.With().
Expand Down Expand Up @@ -78,6 +77,14 @@ func GeneralHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Clien
[]string{"denom"},
)

generalLatestBlockHeight := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_latest_block_height",
Help: "Latest block height",
ConstLabels: ConstLabels,
},
)

generalTokenPrice := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_token_price",
Expand All @@ -93,6 +100,7 @@ func GeneralHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Clien
registry.MustRegister(generalSupplyTotalGauge)
registry.MustRegister(generalInflationGauge)
registry.MustRegister(generalAnnualProvisions)
registry.MustRegister(generalLatestBlockHeight)
registry.MustRegister(generalTokenPrice)

var wg sync.WaitGroup
Expand All @@ -110,13 +118,33 @@ func GeneralHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Clien
generalTokenPrice.Set(price)
}()

wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().Msg("Started querying base pool")

queryStart := time.Now()

status, err := s.tmRPC.Status(context.Background())
if err != nil {
sublogger.Error().Err(err).Msg("Could not status")
return
}

sublogger.Debug().
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying rpc status")

generalLatestBlockHeight.Set(float64(status.SyncInfo.LatestBlockHeight))
}()

wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().Msg("Started querying staking pool")
queryStart := time.Now()

stakingClient := stakingtypes.NewQueryClient(grpcConn)
stakingClient := stakingtypes.NewQueryClient(s.grpcConn)
response, err := stakingClient.Pool(
context.Background(),
&stakingtypes.QueryPoolRequest{},
Expand Down Expand Up @@ -149,7 +177,7 @@ func GeneralHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Clien
sublogger.Debug().Msg("Started querying distribution community pool")
queryStart := time.Now()

distributionClient := distributiontypes.NewQueryClient(grpcConn)
distributionClient := distributiontypes.NewQueryClient(s.grpcConn)
response, err := distributionClient.CommunityPool(
context.Background(),
&distributiontypes.QueryCommunityPoolRequest{},
Expand Down Expand Up @@ -182,7 +210,7 @@ func GeneralHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Clien
sublogger.Debug().Msg("Started querying bank total supply")
queryStart := time.Now()

bankClient := banktypes.NewQueryClient(grpcConn)
bankClient := banktypes.NewQueryClient(s.grpcConn)
response, err := bankClient.TotalSupply(
context.Background(),
&banktypes.QueryTotalSupplyRequest{},
Expand Down Expand Up @@ -215,7 +243,7 @@ func GeneralHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Clien
sublogger.Debug().Msg("Started querying inflation")
queryStart := time.Now()

mintClient := minttypes.NewQueryClient(grpcConn)
mintClient := minttypes.NewQueryClient(s.grpcConn)
response, err := mintClient.Inflation(
context.Background(),
&minttypes.QueryInflationRequest{},
Expand Down Expand Up @@ -244,7 +272,7 @@ func GeneralHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Clien
sublogger.Debug().Msg("Started querying annual provisions")
queryStart := time.Now()

mintClient := minttypes.NewQueryClient(grpcConn)
mintClient := minttypes.NewQueryClient(s.grpcConn)
response, err := mintClient.AnnualProvisions(
context.Background(),
&minttypes.QueryAnnualProvisionsRequest{},
Expand Down
56 changes: 26 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ var (
DenomExponent uint64
)

type service struct {
grpcConn *grpc.ClientConn
tmRPC *tmrpc.HTTP
}

var log = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger()

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -149,36 +154,32 @@ func Execute(cmd *cobra.Command, args []string) {
config.SetBech32PrefixForConsensusNode(ConsensusNodePrefix, ConsensusNodePubkeyPrefix)
config.Seal()

grpcConn, err := grpc.Dial(
s := &service{}

// Setup gRPC connection
s.grpcConn, err = grpc.Dial(
NodeAddress,
grpc.WithInsecure(),
)
if err != nil {
log.Fatal().Err(err).Msg("Could not connect to gRPC node")
}
defer s.grpcConn.Close()

setChainID()
setDenom(grpcConn)

http.HandleFunc("/metrics/wallet", func(w http.ResponseWriter, r *http.Request) {
WalletHandler(w, r, grpcConn)
})

http.HandleFunc("/metrics/validator", func(w http.ResponseWriter, r *http.Request) {
ValidatorHandler(w, r, grpcConn)
})

http.HandleFunc("/metrics/validators", func(w http.ResponseWriter, r *http.Request) {
ValidatorsHandler(w, r, grpcConn)
})
// Setup Tendermint RPC connection
s.tmRPC, err = tmrpc.New(TendermintRPC, "/websocket")
if err != nil {
log.Fatal().Err(err).Msg("Could not create Tendermint client")
}

http.HandleFunc("/metrics/params", func(w http.ResponseWriter, r *http.Request) {
ParamsHandler(w, r, grpcConn)
})
s.setChainID()
s.setDenom()

http.HandleFunc("/metrics/general", func(w http.ResponseWriter, r *http.Request) {
GeneralHandler(w, r, grpcConn)
})
http.HandleFunc("/metrics/wallet", s.WalletHandler)
http.HandleFunc("/metrics/validator", s.ValidatorHandler)
http.HandleFunc("/metrics/validators", s.ValidatorsHandler)
http.HandleFunc("/metrics/params", s.ParamsHandler)
http.HandleFunc("/metrics/general", s.GeneralHandler)

log.Info().Str("address", ListenAddress).Msg("Listening")
err = http.ListenAndServe(ListenAddress, nil)
Expand All @@ -187,13 +188,8 @@ func Execute(cmd *cobra.Command, args []string) {
}
}

func setChainID() {
client, err := tmrpc.New(TendermintRPC, "/websocket")
if err != nil {
log.Fatal().Err(err).Msg("Could not create Tendermint client")
}

status, err := client.Status(context.Background())
func (s *service) setChainID() {
status, err := s.tmRPC.Status(context.Background())
if err != nil {
log.Fatal().Err(err).Msg("Could not query Tendermint status")
}
Expand All @@ -205,14 +201,14 @@ func setChainID() {
}
}

func setDenom(grpcConn *grpc.ClientConn) {
func (s *service) setDenom() {
// if --denom and (--denom-coefficient or --denom-exponent) are provided, use them
// instead of fetching them via gRPC. Can be useful for networks like osmosis.
if isUserProvidedAndHandled := checkAndHandleDenomInfoProvidedByUser(); isUserProvidedAndHandled {
return
}

bankClient := banktypes.NewQueryClient(grpcConn)
bankClient := banktypes.NewQueryClient(s.grpcConn)
denoms, err := bankClient.DenomsMetadata(
context.Background(),
&banktypes.QueryDenomsMetadataRequest{},
Expand Down
11 changes: 5 additions & 6 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import (
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
)

func ParamsHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.ClientConn) {
func (s *service) ParamsHandler(w http.ResponseWriter, r *http.Request) {
requestStart := time.Now()

sublogger := log.With().
Expand Down Expand Up @@ -166,7 +165,7 @@ func ParamsHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Client
sublogger.Debug().Msg("Started querying global staking params")
queryStart := time.Now()

stakingClient := stakingtypes.NewQueryClient(grpcConn)
stakingClient := stakingtypes.NewQueryClient(s.grpcConn)
paramsResponse, err := stakingClient.Params(
context.Background(),
&stakingtypes.QueryParamsRequest{},
Expand All @@ -192,7 +191,7 @@ func ParamsHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Client
sublogger.Debug().Msg("Started querying global mint params")
queryStart := time.Now()

mintClient := minttypes.NewQueryClient(grpcConn)
mintClient := minttypes.NewQueryClient(s.grpcConn)
paramsResponse, err := mintClient.Params(
context.Background(),
&minttypes.QueryParamsRequest{},
Expand Down Expand Up @@ -250,7 +249,7 @@ func ParamsHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Client
sublogger.Debug().Msg("Started querying global slashing params")
queryStart := time.Now()

slashingClient := slashingtypes.NewQueryClient(grpcConn)
slashingClient := slashingtypes.NewQueryClient(s.grpcConn)
paramsResponse, err := slashingClient.Params(
context.Background(),
&slashingtypes.QueryParamsRequest{},
Expand Down Expand Up @@ -300,7 +299,7 @@ func ParamsHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Client
sublogger.Debug().Msg("Started querying global distribution params")
queryStart := time.Now()

distributionClient := distributiontypes.NewQueryClient(grpcConn)
distributionClient := distributiontypes.NewQueryClient(s.grpcConn)
paramsResponse, err := distributionClient.Params(
context.Background(),
&distributiontypes.QueryParamsRequest{},
Expand Down
19 changes: 9 additions & 10 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ import (
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
)

func ValidatorHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.ClientConn) {
func (s *service) ValidatorHandler(w http.ResponseWriter, r *http.Request) {
requestStart := time.Now()
sublogger := log.With().
Str("request-id", uuid.New().String()).
Expand Down Expand Up @@ -173,7 +172,7 @@ func ValidatorHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cli
Msg("Started querying validator")
validatorQueryStart := time.Now()

stakingClient := stakingtypes.NewQueryClient(grpcConn)
stakingClient := stakingtypes.NewQueryClient(s.grpcConn)
validator, err := stakingClient.Validator(
context.Background(),
&stakingtypes.QueryValidatorRequest{ValidatorAddr: myAddress.String()},
Expand Down Expand Up @@ -260,7 +259,7 @@ func ValidatorHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cli
Msg("Started querying validator delegations")
queryStart := time.Now()

stakingClient := stakingtypes.NewQueryClient(grpcConn)
stakingClient := stakingtypes.NewQueryClient(s.grpcConn)
stakingRes, err := stakingClient.ValidatorDelegations(
context.Background(),
&stakingtypes.QueryValidatorDelegationsRequest{ValidatorAddr: myAddress.String()},
Expand Down Expand Up @@ -305,7 +304,7 @@ func ValidatorHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cli
Msg("Started querying validator commission")
queryStart := time.Now()

distributionClient := distributiontypes.NewQueryClient(grpcConn)
distributionClient := distributiontypes.NewQueryClient(s.grpcConn)
distributionRes, err := distributionClient.ValidatorCommission(
context.Background(),
&distributiontypes.QueryValidatorCommissionRequest{ValidatorAddress: myAddress.String()},
Expand Down Expand Up @@ -350,7 +349,7 @@ func ValidatorHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cli
Msg("Started querying validator rewards")
queryStart := time.Now()

distributionClient := distributiontypes.NewQueryClient(grpcConn)
distributionClient := distributiontypes.NewQueryClient(s.grpcConn)
distributionRes, err := distributionClient.ValidatorOutstandingRewards(
context.Background(),
&distributiontypes.QueryValidatorOutstandingRewardsRequest{ValidatorAddress: myAddress.String()},
Expand Down Expand Up @@ -394,7 +393,7 @@ func ValidatorHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cli
Msg("Started querying validator unbonding delegations")
queryStart := time.Now()

stakingClient := stakingtypes.NewQueryClient(grpcConn)
stakingClient := stakingtypes.NewQueryClient(s.grpcConn)
stakingRes, err := stakingClient.ValidatorUnbondingDelegations(
context.Background(),
&stakingtypes.QueryValidatorUnbondingDelegationsRequest{ValidatorAddr: myAddress.String()},
Expand Down Expand Up @@ -444,7 +443,7 @@ func ValidatorHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cli
Msg("Started querying validator redelegations")
queryStart := time.Now()

stakingClient := stakingtypes.NewQueryClient(grpcConn)
stakingClient := stakingtypes.NewQueryClient(s.grpcConn)
stakingRes, err := stakingClient.Redelegations(
context.Background(),
&stakingtypes.QueryRedelegationsRequest{SrcValidatorAddr: myAddress.String()},
Expand Down Expand Up @@ -514,7 +513,7 @@ func ValidatorHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cli
Msg("Could not get validator pubkey")
}

slashingClient := slashingtypes.NewQueryClient(grpcConn)
slashingClient := slashingtypes.NewQueryClient(s.grpcConn)
slashingRes, err := slashingClient.SigningInfo(
context.Background(),
&slashingtypes.QuerySigningInfoRequest{ConsAddress: pubKey.String()},
Expand Down Expand Up @@ -552,7 +551,7 @@ func ValidatorHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cli
Msg("Started querying validator other validators")
queryStart := time.Now()

stakingClient := stakingtypes.NewQueryClient(grpcConn)
stakingClient := stakingtypes.NewQueryClient(s.grpcConn)
stakingRes, err := stakingClient.Validators(
context.Background(),
&stakingtypes.QueryValidatorsRequest{
Expand Down
9 changes: 4 additions & 5 deletions validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ import (
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
)

func ValidatorsHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.ClientConn) {
func (s *service) ValidatorsHandler(w http.ResponseWriter, r *http.Request) {
encCfg := simapp.MakeTestEncodingConfig()
interfaceRegistry := encCfg.InterfaceRegistry

Expand Down Expand Up @@ -132,7 +131,7 @@ func ValidatorsHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cl
sublogger.Debug().Msg("Started querying validators")
queryStart := time.Now()

stakingClient := stakingtypes.NewQueryClient(grpcConn)
stakingClient := stakingtypes.NewQueryClient(s.grpcConn)
validatorsResponse, err := stakingClient.Validators(
context.Background(),
&stakingtypes.QueryValidatorsRequest{
Expand Down Expand Up @@ -169,7 +168,7 @@ func ValidatorsHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cl
sublogger.Debug().Msg("Started querying validators signing infos")
queryStart := time.Now()

slashingClient := slashingtypes.NewQueryClient(grpcConn)
slashingClient := slashingtypes.NewQueryClient(s.grpcConn)
signingInfosResponse, err := slashingClient.SigningInfos(
context.Background(),
&slashingtypes.QuerySigningInfosRequest{
Expand Down Expand Up @@ -197,7 +196,7 @@ func ValidatorsHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cl
sublogger.Debug().Msg("Started querying staking params")
queryStart := time.Now()

stakingClient := stakingtypes.NewQueryClient(grpcConn)
stakingClient := stakingtypes.NewQueryClient(s.grpcConn)
paramsResponse, err := stakingClient.Params(
context.Background(),
&stakingtypes.QueryParamsRequest{},
Expand Down
Loading

0 comments on commit 2582466

Please sign in to comment.