Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some improvements #29

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ linters-settings:
linters:
enable-all: true
disable:
- exhaustruct
- maintidx
- prealloc
- lll
- dupl
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ You can pass the artuments to the executable file to configure it. Here is the p
- `--denom-exponent` - the denom exponent, `6` for cosmos. Defaults to `0`. Can't provide along with `--denom-coefficient`
- `--listen-address` - the address with port the node would listen to. For example, you can use it to redefine port or to make the exporter accessible from the outside by listening on `127.0.0.1`. Defaults to `:9300` (so it's accessible from the outside on port 9300)
- `--node` - the gRPC node URL. Defaults to `localhost:9090`
- `--use-tls` - Use TLS (for https). Defaults to false
- `--tendermint-rpc` - Tendermint RPC URL to query node stats (specifically `chain-id`). Defaults to `http://localhost:26657`
- `--log-devel` - logger level. Defaults to `info`. You can set it to `debug` to make it more verbose.
- `--limit` - pagination limit for gRPC requests. Defaults to 1000.
Expand Down
4 changes: 2 additions & 2 deletions general.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"google.golang.org/grpc"
)

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

sublogger := log.With().
Expand Down Expand Up @@ -117,7 +117,7 @@ func GeneralHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Clien

distributionClient := distributiontypes.NewQueryClient(grpcConn)
response, err := distributionClient.CommunityPool(
context.Background(),
ctx,
&distributiontypes.QueryCommunityPoolRequest{},
)
if err != nil {
Expand Down
18 changes: 8 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ go 1.16

replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

replace google.golang.org/grpc => google.golang.org/grpc v1.33.2

require (
github.com/cosmos/cosmos-sdk v0.42.4
github.com/google/uuid v1.2.0
github.com/prometheus/client_golang v1.8.0
github.com/rs/zerolog v1.20.0
github.com/spf13/cobra v1.1.1
github.com/cosmos/cosmos-sdk v0.46.6
github.com/google/uuid v1.3.0
github.com/prometheus/client_golang v1.12.2
github.com/rs/zerolog v1.27.0
github.com/spf13/cobra v1.6.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
github.com/tendermint/tendermint v0.34.9
google.golang.org/grpc v1.35.0
github.com/spf13/viper v1.13.0
github.com/tendermint/tendermint v0.34.23
google.golang.org/grpc v1.50.1
)
2,961 changes: 2,843 additions & 118 deletions go.sum

Large diffs are not rendered by default.

56 changes: 37 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"context"
"crypto/tls"
"fmt"
"math"
"net/http"
"os"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand All @@ -15,6 +17,8 @@ import (
"github.com/spf13/viper"
tmrpc "github.com/tendermint/tendermint/rpc/client/http"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)

var (
Expand All @@ -23,9 +27,10 @@ var (
Denom string
ListenAddress string
NodeAddress string
UseTLS bool
TendermintRPC string
LogLevel string
JsonOutput bool
JSONOutput bool
Limit uint64

Prefix string
Expand Down Expand Up @@ -122,7 +127,7 @@ func Execute(cmd *cobra.Command, args []string) {
log.Fatal().Err(err).Msg("Could not parse log level")
}

if JsonOutput {
if JSONOutput {
log = zerolog.New(os.Stdout).With().Timestamp().Logger()
}

Expand All @@ -136,10 +141,13 @@ func Execute(cmd *cobra.Command, args []string) {
Str("--bech-consensus-node-prefix", ConsensusNodePrefix).
Str("--bech-consensus-node-pubkey-prefix", ConsensusNodePubkeyPrefix).
Str("--denom", Denom).
Str("--denom-cofficient", fmt.Sprintf("%f", DenomCoefficient)).
Str("--denom-coefficient", fmt.Sprintf("%f", DenomCoefficient)).
Str("--denom-exponent", fmt.Sprintf("%d", DenomExponent)).
Str("--listen-address", ListenAddress).
Str("--node", NodeAddress).
Str("--tendermint-rpc", TendermintRPC).
Str("--use-tls", fmt.Sprintf("%t", UseTLS)).
Str("--limit", fmt.Sprintf("%d", Limit)).
Str("--log-level", LogLevel).
Msg("Started with following parameters")

Expand All @@ -149,51 +157,62 @@ func Execute(cmd *cobra.Command, args []string) {
config.SetBech32PrefixForConsensusNode(ConsensusNodePrefix, ConsensusNodePubkeyPrefix)
config.Seal()

var dialOption grpc.DialOption
if UseTLS {
dialOption = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{MinVersion: tls.VersionTLS12}))
} else {
dialOption = grpc.WithTransportCredentials(insecure.NewCredentials())
}
grpcConn, err := grpc.Dial(
NodeAddress,
grpc.WithInsecure(),
dialOption,
)
if err != nil {
log.Fatal().Err(err).Msg("Could not connect to gRPC node")
}

setChainID()
setDenom(grpcConn)
ctx := context.Background()
setChainID(ctx)
setDenom(grpcConn, ctx)

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

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

http.HandleFunc("/metrics/validators", func(w http.ResponseWriter, r *http.Request) {
ValidatorsHandler(w, r, grpcConn)
ValidatorsHandler(w, r, grpcConn, ctx)
})

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

http.HandleFunc("/metrics/general", func(w http.ResponseWriter, r *http.Request) {
GeneralHandler(w, r, grpcConn)
GeneralHandler(w, r, grpcConn, ctx)
})

log.Info().Str("address", ListenAddress).Msg("Listening")
err = http.ListenAndServe(ListenAddress, nil)
server := &http.Server{
Addr: ListenAddress,
ReadHeaderTimeout: 3 * time.Second,
}
err = server.ListenAndServe()
if err != nil {
log.Fatal().Err(err).Msg("Could not start application")
}
}

func setChainID() {
func setChainID(ctx context.Context) {
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())
status, err := client.Status(ctx)
if err != nil {
log.Fatal().Err(err).Msg("Could not query Tendermint status")
}
Expand All @@ -205,7 +224,7 @@ func setChainID() {
}
}

func setDenom(grpcConn *grpc.ClientConn) {
func setDenom(grpcConn *grpc.ClientConn, ctx context.Context) {
// 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 {
Expand All @@ -214,7 +233,7 @@ func setDenom(grpcConn *grpc.ClientConn) {

bankClient := banktypes.NewQueryClient(grpcConn)
denoms, err := bankClient.DenomsMetadata(
context.Background(),
ctx,
&banktypes.QueryDenomsMetadataRequest{},
)
if err != nil {
Expand Down Expand Up @@ -249,7 +268,6 @@ func setDenom(grpcConn *grpc.ClientConn) {
}

func checkAndHandleDenomInfoProvidedByUser() bool {

if Denom != "" {
if DenomCoefficient != 1 && DenomExponent != 0 {
log.Fatal().Msg("denom-coefficient and denom-exponent are both provided. Must provide only one")
Expand Down Expand Up @@ -277,7 +295,6 @@ func checkAndHandleDenomInfoProvidedByUser() bool {
}

return false

}

func main() {
Expand All @@ -287,10 +304,11 @@ func main() {
rootCmd.PersistentFlags().Uint64Var(&DenomExponent, "denom-exponent", 0, "Denom exponent")
rootCmd.PersistentFlags().StringVar(&ListenAddress, "listen-address", ":9300", "The address this exporter would listen on")
rootCmd.PersistentFlags().StringVar(&NodeAddress, "node", "localhost:9090", "RPC node address")
rootCmd.PersistentFlags().BoolVar(&UseTLS, "use-tls", false, "Use TLS")
rootCmd.PersistentFlags().StringVar(&LogLevel, "log-level", "info", "Logging level")
rootCmd.PersistentFlags().Uint64Var(&Limit, "limit", 1000, "Pagination limit for gRPC requests")
rootCmd.PersistentFlags().StringVar(&TendermintRPC, "tendermint-rpc", "http://localhost:26657", "Tendermint RPC address")
rootCmd.PersistentFlags().BoolVar(&JsonOutput, "json", false, "Output logs as JSON")
rootCmd.PersistentFlags().BoolVar(&JSONOutput, "json", false, "Output logs as JSON")

// some networks, like Iris, have the different prefixes for address, validator and consensus node
rootCmd.PersistentFlags().StringVar(&Prefix, "bech-prefix", "persistence", "Bech32 global prefix")
Expand Down
4 changes: 2 additions & 2 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"google.golang.org/grpc"
)

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

sublogger := log.With().
Expand Down Expand Up @@ -168,7 +168,7 @@ func ParamsHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Client

stakingClient := stakingtypes.NewQueryClient(grpcConn)
paramsResponse, err := stakingClient.Params(
context.Background(),
ctx,
&stakingtypes.QueryParamsRequest{},
)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"google.golang.org/grpc"
)

func ValidatorHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.ClientConn) {
func ValidatorHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.ClientConn, ctx context.Context) {
requestStart := time.Now()
sublogger := log.With().
Str("request-id", uuid.New().String()).
Expand Down Expand Up @@ -175,7 +175,7 @@ func ValidatorHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cli

stakingClient := stakingtypes.NewQueryClient(grpcConn)
validator, err := stakingClient.Validator(
context.Background(),
ctx,
&stakingtypes.QueryValidatorRequest{ValidatorAddr: myAddress.String()},
)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"google.golang.org/grpc"
)

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

Expand Down Expand Up @@ -134,7 +134,7 @@ func ValidatorsHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Cl

stakingClient := stakingtypes.NewQueryClient(grpcConn)
validatorsResponse, err := stakingClient.Validators(
context.Background(),
ctx,
&stakingtypes.QueryValidatorsRequest{
Pagination: &querytypes.PageRequest{
Limit: Limit,
Expand Down
4 changes: 2 additions & 2 deletions wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"google.golang.org/grpc"
)

func WalletHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.ClientConn) {
func WalletHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.ClientConn, ctx context.Context) {
requestStart := time.Now()

sublogger := log.With().
Expand Down Expand Up @@ -98,7 +98,7 @@ func WalletHandler(w http.ResponseWriter, r *http.Request, grpcConn *grpc.Client

bankClient := banktypes.NewQueryClient(grpcConn)
bankRes, err := bankClient.AllBalances(
context.Background(),
ctx,
&banktypes.QueryAllBalancesRequest{Address: myAddress.String()},
)
if err != nil {
Expand Down