forked from PFC-developer/cosmos-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'PFC-developer:master' into master
- Loading branch information
Showing
12 changed files
with
472 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/pfc-developer/cosmos-exporter/pkg/exporter" | ||
Check failure on line 15 in cmd/pryzm-cosmos-exporter/main.go GitHub Actions / test (1.21.x, ubuntu-latest)
Check failure on line 15 in cmd/pryzm-cosmos-exporter/main.go GitHub Actions / test (1.21.x, macos-latest)
|
||
) | ||
|
||
var ( | ||
config exporter.ServiceConfig | ||
log = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger() | ||
) | ||
|
||
var ( | ||
Oracle bool | ||
LCD string | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "cosmos-exporter", | ||
Long: "Scrape the data about the validators set, specific validators or wallets in the Cosmos network.", | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
if config.ConfigPath == "" { | ||
config.SetBechPrefixes(cmd) | ||
|
||
return nil | ||
} | ||
|
||
viper.SetConfigFile(config.ConfigPath) | ||
if err := viper.ReadInConfig(); err != nil { | ||
if _, ok := err.(viper.ConfigFileNotFoundError); !ok { | ||
log.Info().Err(err).Msg("Error reading config file") | ||
return err | ||
} | ||
} | ||
|
||
// Credits to https://carolynvanslyck.com/blog/2020/08/sting-of-the-viper/ | ||
cmd.Flags().VisitAll(func(f *pflag.Flag) { | ||
if !f.Changed && viper.IsSet(f.Name) { | ||
val := viper.Get(f.Name) | ||
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil { | ||
log.Fatal().Err(err).Msg("Could not set flag") | ||
} | ||
} | ||
}) | ||
config.SetBechPrefixes(cmd) | ||
|
||
return nil | ||
}, | ||
Run: Execute, | ||
} | ||
|
||
func Execute(_ *cobra.Command, _ []string) { | ||
logLevel, err := zerolog.ParseLevel(config.LogLevel) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Could not parse log level") | ||
} | ||
|
||
if config.JSONOutput { | ||
log = zerolog.New(os.Stdout).With().Timestamp().Logger() | ||
} | ||
|
||
zerolog.SetGlobalLevel(logLevel) | ||
|
||
config.LogConfig(log.Info()). | ||
Bool("oracle", Oracle). | ||
Str("lcd", LCD). | ||
Msg("Started with following parameters") | ||
|
||
sdkconfig := sdk.GetConfig() | ||
sdkconfig.SetBech32PrefixForAccount(config.AccountPrefix, config.AccountPubkeyPrefix) | ||
sdkconfig.SetBech32PrefixForValidator(config.ValidatorPrefix, config.ValidatorPubkeyPrefix) | ||
sdkconfig.SetBech32PrefixForConsensusNode(config.ConsensusNodePrefix, config.ConsensusNodePubkeyPrefix) | ||
sdkconfig.Seal() | ||
|
||
s := &exporter.Service{} | ||
|
||
s.Log = log | ||
err = s.Connect(&config) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Could not connect to service") | ||
} | ||
defer func(service *exporter.Service) { | ||
err := service.Close() | ||
if err != nil { | ||
s.Log.Fatal().Err(err).Msg("Could not close service client") | ||
} | ||
}(s) | ||
|
||
s.SetChainID(&config) | ||
s.SetDenom(&config) | ||
|
||
s.Params = config.Params | ||
s.Wallets = config.Wallets | ||
s.Validators = config.Validators | ||
s.Proposals = config.Proposals | ||
s.Oracle = config.Oracle | ||
s.Params = config.Params | ||
s.Upgrades = config.Upgrades | ||
s.Config = &config | ||
|
||
if config.SingleReq { | ||
log.Info().Msg("Starting Single Mode") | ||
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) { InjSingleHandler(w, r, s) }) | ||
} | ||
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) | ||
|
||
http.HandleFunc("/metrics/delegator", s.DelegatorHandler) | ||
http.HandleFunc("/metrics/proposals", s.ProposalsHandler) | ||
http.HandleFunc("/metrics/upgrade", s.UpgradeHandler) | ||
if config.Prefix == "pryzm" { | ||
http.HandleFunc("/metrics/pryzm", func(w http.ResponseWriter, r *http.Request) { PryzmMetricHandler(w, r, s) }) | ||
} | ||
|
||
log.Info().Str("address", config.ListenAddress).Msg("Listening") | ||
err = http.ListenAndServe(config.ListenAddress, nil) // #nosec | ||
if err != nil { | ||
log.Error().Err(err).Msg("could not start application") | ||
return | ||
} | ||
} | ||
|
||
func main() { | ||
config.SetCommonParameters(rootCmd) | ||
rootCmd.PersistentFlags().BoolVar(&Oracle, "oracle", false, "serve pryzm oracle info in the single call to /metrics") | ||
rootCmd.PersistentFlags().StringVar(&LCD, "lcd", "http://localhost:1317", "LCD endpoint") | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
log.Fatal().Err(err).Msg("Could not start application") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// original here - https://gist.github.com/jumanzii/031cfea1b2aa3c2a43b63aa62a919285 | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
"sync" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/rs/zerolog" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/pfc-developer/cosmos-exporter/pkg/exporter" | ||
) | ||
|
||
/* | ||
type voteMissCounter struct { | ||
MissCount string `json:"miss_count"` | ||
} | ||
*/ | ||
type PryzmMetrics struct { | ||
missCounter *prometheus.CounterVec | ||
} | ||
|
||
func NewPryzmMetrics(reg prometheus.Registerer, config *exporter.ServiceConfig) *PryzmMetrics { | ||
m := &PryzmMetrics{ | ||
missCounter: prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "cosmos_pryzm_feeder_miss_counter", | ||
Help: "miss counter", | ||
ConstLabels: config.ConstLabels, | ||
}, | ||
[]string{"type"}, | ||
), | ||
} | ||
|
||
reg.MustRegister(m.missCounter) | ||
|
||
return m | ||
} | ||
|
||
type MissCounterResponse struct { | ||
Miss MissCounterM `json:"miss_counter"` | ||
} | ||
|
||
type MissCounterM struct { | ||
Validator string `json:"validator"` | ||
Counter string `json:"counter"` | ||
} | ||
|
||
func doPryzmMetrics(wg *sync.WaitGroup, sublogger *zerolog.Logger, metrics *PryzmMetrics, _ *exporter.Service, _ *exporter.ServiceConfig, validatorAddress sdk.ValAddress) { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
sublogger.Debug().Msg("Started querying LCD peggy module state") | ||
queryStart := time.Now() | ||
|
||
requestURL := fmt.Sprintf("%s/refractedlabs/oracle/v1/miss_counter/%s", LCD, validatorAddress.String()) | ||
response, err := http.Get(requestURL) // #nosec | ||
if err != nil { | ||
sublogger.Error(). | ||
Err(err). | ||
Msg("Could not get peggy module state") | ||
return | ||
} | ||
moduleStateResponse := &MissCounterResponse{} | ||
err = json.NewDecoder(response.Body).Decode(moduleStateResponse) | ||
if err != nil { | ||
sublogger.Error(). | ||
Err(err). | ||
Msg("Could not decode LCD peggy module state") | ||
return | ||
} | ||
sublogger.Debug().Str("response", moduleStateResponse.Miss.Counter).Msg("Response") | ||
sublogger.Debug(). | ||
Float64("request-time", time.Since(queryStart).Seconds()). | ||
Msg("Finished querying oracle feeder metrics") | ||
|
||
missCount, err := strconv.ParseFloat(moduleStateResponse.Miss.Counter, 64) | ||
if err != nil { | ||
sublogger.Error(). | ||
Err(err). | ||
Msg("Could not get convert module state to float") | ||
return | ||
} | ||
metrics.missCounter.WithLabelValues("miss").Add(missCount) | ||
}() | ||
} | ||
|
||
func PryzmMetricHandler(w http.ResponseWriter, r *http.Request, s *exporter.Service) { | ||
requestStart := time.Now() | ||
|
||
sublogger := s.Log.With(). | ||
Str("request-id", uuid.New().String()). | ||
Logger() | ||
|
||
address := r.URL.Query().Get("validator") | ||
myAddress, err := sdk.ValAddressFromBech32(address) | ||
if err != nil { | ||
sublogger.Error(). | ||
Str("address", address). | ||
Err(err). | ||
Msg("Could not get address") | ||
return | ||
} | ||
registry := prometheus.NewRegistry() | ||
pryzmMetrics := NewPryzmMetrics(registry, s.Config) | ||
|
||
var wg sync.WaitGroup | ||
|
||
doPryzmMetrics(&wg, &sublogger, pryzmMetrics, s, s.Config, myAddress) | ||
|
||
wg.Wait() | ||
|
||
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{}) | ||
h.ServeHTTP(w, r) | ||
sublogger.Info(). | ||
Str("method", "GET"). | ||
Str("endpoint", "/metrics/pryzm"). | ||
Float64("request-time", time.Since(requestStart).Seconds()). | ||
Msg("Request processed") | ||
} |
Oops, something went wrong.