Skip to content

Commit

Permalink
feat: split /general up so bank supply is not served in /single mode
Browse files Browse the repository at this point in the history
  • Loading branch information
PFC-developer committed Mar 27, 2024
1 parent 3265030 commit 3b91dc1
Showing 1 changed file with 120 additions and 99 deletions.
219 changes: 120 additions & 99 deletions pkg/exporter/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
)

type GeneralMetrics struct {
bondedTokensGauge prometheus.Gauge
notBondedTokensGauge prometheus.Gauge
communityPoolGauge *prometheus.GaugeVec
supplyTotalGauge *prometheus.GaugeVec
bondedTokensGauge prometheus.Gauge
notBondedTokensGauge prometheus.Gauge
// communityPoolGauge *prometheus.GaugeVec
// supplyTotalGauge *prometheus.GaugeVec
latestBlockHeight prometheus.Gauge
syncing prometheus.Gauge
tokenPrice prometheus.Gauge
Expand All @@ -37,6 +37,10 @@ type GeneralMetrics struct {
applicationVersion *prometheus.GaugeVec
defaultNodeInfo *prometheus.GaugeVec
}
type GeneralExtendedMetrics struct {
communityPoolGauge *prometheus.GaugeVec
supplyTotalGauge *prometheus.GaugeVec
}

func NewGeneralMetrics(reg prometheus.Registerer, config *ServiceConfig) *GeneralMetrics {
m := &GeneralMetrics{
Expand All @@ -54,22 +58,7 @@ func NewGeneralMetrics(reg prometheus.Registerer, config *ServiceConfig) *Genera
ConstLabels: config.ConstLabels,
},
),
communityPoolGauge: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_general_community_pool",
Help: "Community pool",
ConstLabels: config.ConstLabels,
},
[]string{"denom"},
),
supplyTotalGauge: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_general_supply_total",
Help: "Total supply",
ConstLabels: config.ConstLabels,
},
[]string{"denom"},
),

latestBlockHeight: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_latest_block_height",
Expand Down Expand Up @@ -118,8 +107,6 @@ func NewGeneralMetrics(reg prometheus.Registerer, config *ServiceConfig) *Genera
}
reg.MustRegister(m.bondedTokensGauge)
reg.MustRegister(m.notBondedTokensGauge)
reg.MustRegister(m.communityPoolGauge)
reg.MustRegister(m.supplyTotalGauge)

// registry.MustRegister(generalInflationGauge)
// registry.MustRegister(generalAnnualProvisions)
Expand Down Expand Up @@ -158,6 +145,33 @@ func NewGeneralMetrics(reg prometheus.Registerer, config *ServiceConfig) *Genera
*/
}

func NewGeneralExtendedMetrics(reg prometheus.Registerer, config *ServiceConfig) *GeneralExtendedMetrics {
m := &GeneralExtendedMetrics{

Check failure on line 150 in pkg/exporter/general.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
communityPoolGauge: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_general_community_pool",
Help: "Community pool",
ConstLabels: config.ConstLabels,
},
[]string{"denom"},
),
supplyTotalGauge: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_general_supply_total",
Help: "Total supply",
ConstLabels: config.ConstLabels,
},
[]string{"denom"},
),
}
reg.MustRegister(m.communityPoolGauge)
reg.MustRegister(m.supplyTotalGauge)

return m

Check failure on line 172 in pkg/exporter/general.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
}

func GetGeneralMetrics(wg *sync.WaitGroup, sublogger *zerolog.Logger, metrics *GeneralMetrics, s *Service, config *ServiceConfig) {
if config.TokenPrice {
wg.Add(1)
Expand Down Expand Up @@ -255,38 +269,6 @@ func GetGeneralMetrics(wg *sync.WaitGroup, sublogger *zerolog.Logger, metrics *G
// generalNotBondedTokensGauge.Set(float64(response.Pool.NotBondedTokens.Int64()))
}()

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

distributionClient := distributiontypes.NewQueryClient(s.GrpcConn)
response, err := distributionClient.CommunityPool(
context.Background(),
&distributiontypes.QueryCommunityPoolRequest{},
)
if err != nil {
sublogger.Error().Err(err).Msg("Could not get distribution community pool")
return
}

sublogger.Debug().
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying distribution community pool")

for _, coin := range response.Pool {
if value, err := strconv.ParseFloat(coin.Amount.String(), 64); err != nil {
sublogger.Error().
Err(err).
Msg("Could not get community pool coin")
} else {
metrics.communityPoolGauge.With(prometheus.Labels{
"denom": config.Denom,
}).Set(value / config.DenomCoefficient)
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
Expand Down Expand Up @@ -324,51 +306,6 @@ func GetGeneralMetrics(wg *sync.WaitGroup, sublogger *zerolog.Logger, metrics *G
}).Set(float64(1))
}()

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

bankClient := banktypes.NewQueryClient(s.GrpcConn)
response, err := bankClient.TotalSupply(
context.Background(),
&banktypes.QueryTotalSupplyRequest{},
)
for {
if err != nil {
sublogger.Error().Err(err).Msg("Could not get bank total supply")
return
}

sublogger.Debug().
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying bank total supply")

for _, coin := range response.Supply {
if value, err := strconv.ParseFloat(coin.Amount.String(), 64); err != nil {
sublogger.Error().
Err(err).
Msg("Could not get total supply")
} else {
metrics.supplyTotalGauge.With(prometheus.Labels{
"denom": coin.GetDenom(),
}).Set(value)
}
}
if response.Pagination.NextKey == nil {
break
}
response, err = bankClient.TotalSupply(
context.Background(),
&banktypes.QueryTotalSupplyRequest{
Pagination: &query.PageRequest{
Key: response.Pagination.NextKey,
},
},
)
}
}()
/*
wg.Add(1)
go func() {
Expand Down Expand Up @@ -474,6 +411,88 @@ func GetGeneralMetrics(wg *sync.WaitGroup, sublogger *zerolog.Logger, metrics *G
}
}

func GetGeneralExtendedMetrics(wg *sync.WaitGroup, sublogger *zerolog.Logger, metrics *GeneralExtendedMetrics, s *Service, config *ServiceConfig) {

Check failure on line 415 in pkg/exporter/general.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
wg.Add(1)
go func() {
defer wg.Done()
sublogger.Debug().Msg("Started querying distribution community pool")
queryStart := time.Now()

distributionClient := distributiontypes.NewQueryClient(s.GrpcConn)
response, err := distributionClient.CommunityPool(
context.Background(),
&distributiontypes.QueryCommunityPoolRequest{},
)
if err != nil {
sublogger.Error().Err(err).Msg("Could not get distribution community pool")
return
}

sublogger.Debug().
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying distribution community pool")

for _, coin := range response.Pool {
if value, err := strconv.ParseFloat(coin.Amount.String(), 64); err != nil {
sublogger.Error().
Err(err).
Msg("Could not get community pool coin")
} else {
metrics.communityPoolGauge.With(prometheus.Labels{
"denom": config.Denom,
}).Set(value / config.DenomCoefficient)
}
}
}()

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

bankClient := banktypes.NewQueryClient(s.GrpcConn)
response, err := bankClient.TotalSupply(
context.Background(),
&banktypes.QueryTotalSupplyRequest{},
)
for {
if err != nil {
sublogger.Error().Err(err).Msg("Could not get bank total supply")
return
}

sublogger.Debug().
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying bank total supply")

for _, coin := range response.Supply {
if value, err := strconv.ParseFloat(coin.Amount.String(), 64); err != nil {
sublogger.Error().
Err(err).
Msg("Could not get total supply")
} else {
metrics.supplyTotalGauge.With(prometheus.Labels{
"denom": coin.GetDenom(),
}).Set(value)
}
}
if response.Pagination.NextKey == nil {
break
}
response, err = bankClient.TotalSupply(
context.Background(),
&banktypes.QueryTotalSupplyRequest{
Pagination: &query.PageRequest{
Key: response.Pagination.NextKey,
},
},
)
}
}()
}

func (s *Service) GeneralHandler(w http.ResponseWriter, r *http.Request) {
requestStart := time.Now()

Expand All @@ -483,10 +502,12 @@ func (s *Service) GeneralHandler(w http.ResponseWriter, r *http.Request) {

registry := prometheus.NewRegistry()
generalMetrics := NewGeneralMetrics(registry, s.Config)
generalExtendedMetrics := NewGeneralExtendedMetrics(registry, s.Config)

var wg sync.WaitGroup

GetGeneralMetrics(&wg, &sublogger, generalMetrics, s, s.Config)
GetGeneralExtendedMetrics(&wg, &sublogger, generalExtendedMetrics, s, s.Config)

wg.Wait()

Expand Down

0 comments on commit 3b91dc1

Please sign in to comment.