Skip to content

Commit

Permalink
Merge branch 'PFC-developer:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanschultzie authored Jul 3, 2024
2 parents 33f9f2a + 7669b84 commit c03774b
Show file tree
Hide file tree
Showing 12 changed files with 472 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
- run: go build ./cmd/kuji-cosmos-exporter
- run: go build ./cmd/sei-cosmos-exporter
- run: go build ./cmd/inj-cosmos-exporter
- run: go build ./cmd/pryzm-cosmos-exporter
go-vet:
runs-on: ubuntu-latest
steps:
Expand Down
12 changes: 12 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ builds:
- linux
- windows
- darwin

- main: ./cmd/kuji-cosmos-exporter
id: "kuji-cosmos-exporter"
binary: "kuji-cosmos-exporter"
Expand All @@ -42,6 +43,7 @@ builds:
- linux
- windows
- darwin

- main: ./cmd/inj-cosmos-exporter
id: "inj-cosmos-exporter"
binary: "inj-cosmos-exporter"
Expand All @@ -52,6 +54,16 @@ builds:
- windows
- darwin

- main: ./cmd/pryzm-cosmos-exporter
id: "pryzm-cosmos-exporter"
binary: "pryzm-cosmos-exporter"
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of `uname`.
Expand Down
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RUN go build ./cmd/cosmos-exporter
RUN go build ./cmd/kuji-cosmos-exporter
RUN go build ./cmd/sei-cosmos-exporter
RUN go build ./cmd/inj-cosmos-exporter
RUN go build ./cmd/pryzm-cosmos-exporter


FROM alpine
Expand All @@ -16,5 +17,6 @@ COPY --from=builder /app/cosmos-exporter /usr/local/bin/cosmos-exporter
COPY --from=builder /app/kuji-cosmos-exporter /usr/local/bin/kuji-cosmos-exporter
COPY --from=builder /app/sei-cosmos-exporter /usr/local/bin/sei-cosmos-exporter
COPY --from=builder /app/inj-cosmos-exporter /usr/local/bin/inj-cosmos-exporter
COPY --from=builder /app/pryzm-cosmos-exporter /usr/local/bin/pryzm-cosmos-exporter

ENTRYPOINT [ "/usr/local/bin/cosmos-exporter" ]
1 change: 0 additions & 1 deletion cmd/cosmos-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func Execute(_ *cobra.Command, _ []string) {

s.Log = log
err = s.Connect(&config)

if err != nil {
log.Fatal().Err(err).Msg("Could not connect to service")
}
Expand Down
1 change: 0 additions & 1 deletion cmd/inj-cosmos-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func Execute(_ *cobra.Command, _ []string) {

s.Log = log
err = s.Connect(&config)

if err != nil {
log.Fatal().Err(err).Msg("Could not connect to service")
}
Expand Down
1 change: 0 additions & 1 deletion cmd/kuji-cosmos-exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func Execute(_ *cobra.Command, _ []string) {
s.Log = log
// Setup gRPC connection
err = s.Connect(&config)

if err != nil {
log.Fatal().Err(err).Msg("Could not connect to service")
}
Expand Down
144 changes: 144 additions & 0 deletions cmd/pryzm-cosmos-exporter/main.go
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

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

no required module provides package github.com/pfc-developer/cosmos-exporter/pkg/exporter; to add it:

Check failure on line 15 in cmd/pryzm-cosmos-exporter/main.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

no required module provides package github.com/pfc-developer/cosmos-exporter/pkg/exporter; to add it:

Check failure on line 15 in cmd/pryzm-cosmos-exporter/main.go

View workflow job for this annotation

GitHub Actions / go-build

no required module provides package github.com/pfc-developer/cosmos-exporter/pkg/exporter; to add it:
)

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")
}
}
128 changes: 128 additions & 0 deletions cmd/pryzm-cosmos-exporter/pryzm.go
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")
}
Loading

0 comments on commit c03774b

Please sign in to comment.