Skip to content

Commit

Permalink
feat: add price metric (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno authored Nov 6, 2022
1 parent 55f383f commit ddc8f7c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
17 changes: 16 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ func Handler(w http.ResponseWriter, r *http.Request, manager *Manager, log *zero
[]string{"chain"},
)

tokenPriceGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validators_exporter_price",
Help: "Price of 1 token in display denom in USD",
},
[]string{"chain"},
)

registry := prometheus.NewRegistry()
registry.MustRegister(queriesCountGauge)
registry.MustRegister(queriesSuccessfulGauge)
Expand Down Expand Up @@ -312,8 +320,9 @@ func Handler(w http.ResponseWriter, r *http.Request, manager *Manager, log *zero
registry.MustRegister(denomCoefficientGauge)
registry.MustRegister(activeSetSizeGauge)
registry.MustRegister(activeSetTokensGauge)
registry.MustRegister(tokenPriceGauge)

validators := manager.GetAllValidators()
validators, currencies := manager.GetAllValidators()
for _, validator := range validators {
queriesCountGauge.With(prometheus.Labels{
"chain": validator.Chain,
Expand Down Expand Up @@ -531,6 +540,12 @@ func Handler(w http.ResponseWriter, r *http.Request, manager *Manager, log *zero
}).Set(float64(chain.DenomCoefficient))
}

for chain, price := range currencies {
tokenPriceGauge.With(prometheus.Labels{
"chain": chain,
}).Set(price)
}

h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)

Expand Down
11 changes: 9 additions & 2 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ func NewManager(config Config, logger *zerolog.Logger) *Manager {
}
}

func (m *Manager) GetAllValidators() []ValidatorQuery {
func (m *Manager) GetAllValidators() ([]ValidatorQuery, map[string]float64) {
currenciesList := m.Config.GetCoingeckoCurrencies()
currenciesRates := m.Coingecko.FetchPrices(currenciesList)

currenciesRatesToChains := map[string]float64{}
for _, chain := range m.Config.Chains {
if rate, ok := currenciesRates[chain.CoingeckoCurrency]; ok {
currenciesRatesToChains[chain.Name] = rate
}
}

length := 0
for _, chain := range m.Config.Chains {
for range chain.Validators {
Expand Down Expand Up @@ -352,7 +359,7 @@ func (m *Manager) GetAllValidators() []ValidatorQuery {

wg.Wait()

return validators
return validators, currenciesRatesToChains
}

func (m *Manager) GetSelfDelegationsBalance(chain Chain, address string, rpc *RPC) (Balance, *QueryInfo, error) {
Expand Down

0 comments on commit ddc8f7c

Please sign in to comment.