Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more consumer chains metrics #44

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func NewApp(configPath string, version string) *App {
queriersPkg.NewValidatorQuerier(logger, appConfig, tracer),
queriersPkg.NewDenomCoefficientsQuerier(logger, appConfig),
queriersPkg.NewSigningInfoQuerier(logger, appConfig, tracer),
queriersPkg.NewChainInfoQuerier(logger, appConfig, tracer),
queriersPkg.NewUptimeQuerier(),
}

Expand Down
103 changes: 103 additions & 0 deletions pkg/queriers/chain_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package queriers

import (
"context"
"main/pkg/config"
"main/pkg/tendermint"
"main/pkg/types"
"main/pkg/utils"
"sync"

"go.opentelemetry.io/otel/trace"

"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
)

type ChainInfoQuerier struct {
Logger zerolog.Logger
Config *config.Config
Tracer trace.Tracer
}

func NewChainInfoQuerier(
logger *zerolog.Logger,
config *config.Config,
tracer trace.Tracer,
) *ChainInfoQuerier {
return &ChainInfoQuerier{
Logger: logger.With().Str("component", "chain_info_querier").Logger(),
Config: config,
Tracer: tracer,
}
}

func (q *ChainInfoQuerier) GetMetrics(
ctx context.Context,
) ([]prometheus.Collector, []*types.QueryInfo) {
isConsumerGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validators_exporter_is_consumer",
Help: "Whether the chain is consumer (1 if yes, 0 if no)",
},
[]string{"chain"},
)

softOptOutThresholdGauge := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cosmos_validators_exporter_soft_opt_out_threshold",
Help: "Consumer chain's soft opt-out threshold",
},
[]string{"chain"},
)

queryInfos := make([]*types.QueryInfo, 0)

var wg sync.WaitGroup
var mutex sync.Mutex

for _, chain := range q.Config.Chains {
isConsumerGauge.With(prometheus.Labels{
"chain": chain.Name,
}).Set(utils.BoolToFloat64(chain.IsConsumer()))

if chain.IsConsumer() {
wg.Add(1)

go func(chain config.Chain) {
defer wg.Done()

rpc := tendermint.NewRPC(chain, q.Config.Timeout, q.Logger, q.Tracer)

threshold, query, err := rpc.GetConsumerSoftOutOutThreshold(ctx)

mutex.Lock()
defer mutex.Unlock()

if query != nil {
queryInfos = append(queryInfos, query)
}

if err != nil {
q.Logger.Error().
Err(err).
Str("chain", chain.Name).
Msg("Error querying soft opt-out threshold")
return
}

softOptOutThresholdGauge.With(prometheus.Labels{
"chain": chain.Name,
}).Set(threshold)
}(chain)
}
}

wg.Wait()

return []prometheus.Collector{isConsumerGauge, softOptOutThresholdGauge}, queryInfos
}

func (q *ChainInfoQuerier) Name() string {
return "chain-info-querier"
}
40 changes: 40 additions & 0 deletions pkg/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"main/pkg/http"
"main/pkg/types"
"main/pkg/utils"
"strconv"
"strings"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -415,6 +417,44 @@ func (rpc *RPC) GetSlashingParams(
return response, &info, nil
}

func (rpc *RPC) GetConsumerSoftOutOutThreshold(
ctx context.Context,
) (float64, *types.QueryInfo, error) {
if !rpc.Chain.QueryEnabled("params") {
return 0, nil, nil
}

childQuerierCtx, span := rpc.Tracer.Start(
ctx,
"Fetching soft opt-out threshold params",
)
defer span.End()

var response *types.ParamsResponse
info, err := rpc.Client.Get(
rpc.Chain.LCDEndpoint+"/cosmos/params/v1beta1/params?subspace=ccvconsumer&key=SoftOptOutThreshold",
&response,
childQuerierCtx,
)
if err != nil {
return 0, &info, err
}

if response.Code != 0 {
info.Success = false
return 0, &info, fmt.Errorf("expected code 0, but got %d", response.Code)
}

valueStripped := strings.ReplaceAll(response.Param.Value, "\"", "")
value, err := strconv.ParseFloat(valueStripped, 64)
if err != nil {
info.Success = false
return 0, &info, err
}

return value, &info, nil
}

func (rpc *RPC) GetStakingParams(
ctx context.Context,
) (*types.StakingParamsResponse, *types.QueryInfo, error) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/types/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,12 @@ type CommissionResponse struct {
Commission []ResponseAmount `json:"commission"`
} `json:"commission"`
}

type ParamsResponse struct {
Code int `json:"code"`
Param struct {
Subspace string `json:"subspace"`
Key string `json:"key"`
Value string `json:"value"`
} `json:"param"`
}
Loading