Skip to content

Commit

Permalink
feat: add a /metrics request handler
Browse files Browse the repository at this point in the history
  • Loading branch information
PFC-developer committed Oct 10, 2023
1 parent 82ea269 commit f218198
Show file tree
Hide file tree
Showing 9 changed files with 1,139 additions and 718 deletions.
195 changes: 106 additions & 89 deletions general.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,100 @@ package main

import (
"context"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/rs/zerolog"
"main/pkg/cosmosdirectory"
"math/big"
"net/http"
"strconv"
"sync"
"time"

"main/pkg/cosmosdirectory"

banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func (s *service) GeneralHandler(w http.ResponseWriter, r *http.Request) {
requestStart := time.Now()
type GeneralMetrics struct {
bondedTokensGauge prometheus.Gauge
notBondedTokensGauge prometheus.Gauge
communityPoolGauge *prometheus.GaugeVec
supplyTotalGauge *prometheus.GaugeVec
latestBlockHeight prometheus.Gauge
tokenPrice prometheus.Gauge
govVotingPeriodProposals prometheus.Gauge
}

sublogger := log.With().
Str("request-id", uuid.New().String()).
Logger()
func NewGeneralMetrics(reg prometheus.Registerer) *GeneralMetrics {
m := &GeneralMetrics{
bondedTokensGauge: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_general_bonded_tokens",
Help: "Bonded tokens",
ConstLabels: ConstLabels,
},
),
notBondedTokensGauge: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_general_not_bonded_tokens",
Help: "Not bonded tokens",
ConstLabels: ConstLabels,
},
),
communityPoolGauge: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_general_community_pool",
Help: "Community pool",
ConstLabels: ConstLabels,
},
[]string{"denom"},
),
supplyTotalGauge: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_general_supply_total",
Help: "Total supply",
ConstLabels: ConstLabels,
},
[]string{"denom"},
),
latestBlockHeight: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_latest_block_height",
Help: "Latest block height",
ConstLabels: ConstLabels,
},
),
tokenPrice: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_token_price",
Help: "Cosmos token price",
ConstLabels: ConstLabels,
},
),
govVotingPeriodProposals: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_gov_voting_period_proposals",
Help: "Voting period proposals",
ConstLabels: ConstLabels,
},
),
}
reg.MustRegister(m.bondedTokensGauge)
reg.MustRegister(m.notBondedTokensGauge)
reg.MustRegister(m.communityPoolGauge)
reg.MustRegister(m.supplyTotalGauge)

// registry.MustRegister(generalInflationGauge)
// registry.MustRegister(generalAnnualProvisions)

reg.MustRegister(m.latestBlockHeight)
reg.MustRegister(m.tokenPrice)
reg.MustRegister(m.govVotingPeriodProposals)
return m

generalBondedTokensGauge := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_general_bonded_tokens",
Help: "Bonded tokens",
ConstLabels: ConstLabels,
},
)

generalNotBondedTokensGauge := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_general_not_bonded_tokens",
Help: "Not bonded tokens",
ConstLabels: ConstLabels,
},
)

generalCommunityPoolGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_general_community_pool",
Help: "Community pool",
ConstLabels: ConstLabels,
},
[]string{"denom"},
)

generalSupplyTotalGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_general_supply_total",
Help: "Total supply",
ConstLabels: ConstLabels,
},
[]string{"denom"},
)
/*
generalInflationGauge := prometheus.NewGauge(
prometheus.GaugeOpts{
Expand All @@ -80,43 +117,8 @@ func (s *service) GeneralHandler(w http.ResponseWriter, r *http.Request) {
*/

generalLatestBlockHeight := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_latest_block_height",
Help: "Latest block height",
ConstLabels: ConstLabels,
},
)

generalTokenPrice := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_token_price",
Help: "Cosmos token price",
ConstLabels: ConstLabels,
},
)

paramsGovVotingPeriodProposals := prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "cosmos_gov_voting_period_proposals",
Help: "Voting period proposals",
ConstLabels: ConstLabels,
},
)

registry := prometheus.NewRegistry()
registry.MustRegister(generalBondedTokensGauge)
registry.MustRegister(generalNotBondedTokensGauge)
registry.MustRegister(generalCommunityPoolGauge)
registry.MustRegister(generalSupplyTotalGauge)
// registry.MustRegister(generalInflationGauge)
// registry.MustRegister(generalAnnualProvisions)
registry.MustRegister(generalLatestBlockHeight)
registry.MustRegister(generalTokenPrice)
registry.MustRegister(paramsGovVotingPeriodProposals)

var wg sync.WaitGroup

}
func getGeneralMetrics(wg *sync.WaitGroup, sublogger *zerolog.Logger, metrics *GeneralMetrics, s *service) {
wg.Add(1)
go func() {
defer wg.Done()
Expand All @@ -127,7 +129,7 @@ func (s *service) GeneralHandler(w http.ResponseWriter, r *http.Request) {
}

price := chain.GetPriceUSD()
generalTokenPrice.Set(price)
metrics.tokenPrice.Set(price)
}()

wg.Add(1)
Expand All @@ -147,9 +149,8 @@ func (s *service) GeneralHandler(w http.ResponseWriter, r *http.Request) {
Float64("request-time", time.Since(queryStart).Seconds()).
Msg("Finished querying rpc status")

generalLatestBlockHeight.Set(float64(status.SyncInfo.LatestBlockHeight))
metrics.latestBlockHeight.Set(float64(status.SyncInfo.LatestBlockHeight))
}()

wg.Add(1)
go func() {
defer wg.Done()
Expand All @@ -176,8 +177,8 @@ func (s *service) GeneralHandler(w http.ResponseWriter, r *http.Request) {
notBondedTokensBigInt := response.Pool.NotBondedTokens.BigInt()
notBondedTokens, _ := new(big.Float).SetInt(notBondedTokensBigInt).Float64()

generalBondedTokensGauge.Set(bondedTokens)
generalNotBondedTokensGauge.Set(notBondedTokens)
metrics.bondedTokensGauge.Set(bondedTokens)
metrics.notBondedTokensGauge.Set(notBondedTokens)
//fmt.Println("response: ", response.Pool.BondedTokens)
//generalBondedTokensGauge.Set(float64(response.Pool.BondedTokens.Int64()))
//generalNotBondedTokensGauge.Set(float64(response.Pool.NotBondedTokens.Int64()))
Expand Down Expand Up @@ -209,7 +210,7 @@ func (s *service) GeneralHandler(w http.ResponseWriter, r *http.Request) {
Err(err).
Msg("Could not get community pool coin")
} else {
generalCommunityPoolGauge.With(prometheus.Labels{
metrics.communityPoolGauge.With(prometheus.Labels{
"denom": Denom,
}).Set(value / DenomCoefficient)
}
Expand Down Expand Up @@ -242,7 +243,7 @@ func (s *service) GeneralHandler(w http.ResponseWriter, r *http.Request) {
Err(err).
Msg("Could not get total supply")
} else {
generalSupplyTotalGauge.With(prometheus.Labels{
metrics.supplyTotalGauge.With(prometheus.Labels{
"denom": Denom,
}).Set(value / DenomCoefficient)
}
Expand Down Expand Up @@ -325,9 +326,25 @@ func (s *service) GeneralHandler(w http.ResponseWriter, r *http.Request) {
}

proposalsCount := len(proposals.GetProposals())
paramsGovVotingPeriodProposals.Set(float64(proposalsCount))
metrics.govVotingPeriodProposals.Set(float64(proposalsCount))
}()

}

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

sublogger := log.With().
Str("request-id", uuid.New().String()).
Logger()

registry := prometheus.NewRegistry()
generalMetrics := NewGeneralMetrics(registry)

var wg sync.WaitGroup

getGeneralMetrics(&wg, &sublogger, generalMetrics, s)

wg.Wait()

h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
Expand Down
79 changes: 51 additions & 28 deletions kuji.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,44 @@ package main

import (
"context"
oracletypes "github.com/Team-Kujira/core/x/oracle/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/rs/zerolog"
"net/http"
"sync"
"time"

oracletypes "github.com/Team-Kujira/core/x/oracle/types"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

/*
type voteMissCounter struct {
MissCount string `json:"miss_count"`
}
type voteMissCounter struct {
MissCount string `json:"miss_count"`
}
*/
type KujiMetrics struct {
votePenaltyCount *prometheus.CounterVec
}

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

sublogger := log.With().
Str("request-id", uuid.New().String()).
Logger()

address := r.URL.Query().Get("address")

votePenaltyCount := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "cosmos_kujira_oracle_vote_miss_count",
Help: "Vote miss count",
ConstLabels: ConstLabels,
},
[]string{"type"},
)

registry := prometheus.NewRegistry()
registry.MustRegister(votePenaltyCount)

var wg sync.WaitGroup
func NewKujiMetrics(reg prometheus.Registerer) *KujiMetrics {
m := &KujiMetrics{
votePenaltyCount: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "cosmos_kujira_oracle_vote_miss_count",
Help: "Vote miss count",
ConstLabels: ConstLabels,
},
[]string{"type"},
),
}

reg.MustRegister(m.votePenaltyCount)

return m
}
func getKujiMetrics(wg *sync.WaitGroup, sublogger *zerolog.Logger, metrics *KujiMetrics, s *service, validatorAddress sdk.ValAddress) {
wg.Add(1)

go func() {
Expand All @@ -49,7 +49,7 @@ func (s *service) KujiraMetricHandler(w http.ResponseWriter, r *http.Request) {
queryStart := time.Now()

oracleClient := oracletypes.NewQueryClient(s.grpcConn)
response, err := oracleClient.MissCounter(context.Background(), &oracletypes.QueryMissCounterRequest{ValidatorAddr: address})
response, err := oracleClient.MissCounter(context.Background(), &oracletypes.QueryMissCounterRequest{ValidatorAddr: validatorAddress.String()})

if err != nil {
sublogger.Error().
Expand All @@ -64,9 +64,32 @@ func (s *service) KujiraMetricHandler(w http.ResponseWriter, r *http.Request) {

missCount := float64(response.MissCounter)

votePenaltyCount.WithLabelValues("miss").Add(missCount)
metrics.votePenaltyCount.WithLabelValues("miss").Add(missCount)

}()
}
func (s *service) KujiraMetricHandler(w http.ResponseWriter, r *http.Request) {
requestStart := time.Now()

sublogger := log.With().
Str("request-id", uuid.New().String()).
Logger()

address := r.URL.Query().Get("address")
myAddress, err := sdk.ValAddressFromBech32(address)
if err != nil {
sublogger.Error().
Str("address", address).
Err(err).
Msg("Could not get address")
return
}
registry := prometheus.NewRegistry()
kujiMetrics := NewKujiMetrics(registry)

var wg sync.WaitGroup
getKujiMetrics(&wg, &sublogger, kujiMetrics, s, myAddress)

wg.Wait()

h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
Expand Down
Loading

0 comments on commit f218198

Please sign in to comment.