-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validator): adding new metric for validator export (#8)
- Loading branch information
1 parent
b3b0479
commit f45ce70
Showing
9 changed files
with
495 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package collector | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"go.uber.org/zap" | ||
|
||
"github.com/warden-protocol/warden-exporter/pkg/config" | ||
"github.com/warden-protocol/warden-exporter/pkg/grpc" | ||
log "github.com/warden-protocol/warden-exporter/pkg/logger" | ||
types "github.com/warden-protocol/warden-exporter/pkg/types" | ||
) | ||
|
||
const ( | ||
missedBlocksMetricName = "cosmos_validator_missed_blocks" | ||
) | ||
|
||
//nolint:gochecknoglobals // this is needed as it's used in multiple places | ||
var missedBlocks = prometheus.NewDesc( | ||
missedBlocksMetricName, | ||
"Returns missed blocks for a validator.", | ||
[]string{ | ||
"chain_id", | ||
"valcons", | ||
"valoper", | ||
"moniker", | ||
"jailed", | ||
"tombstoned", | ||
"bond_status", | ||
}, | ||
nil, | ||
) | ||
|
||
type ValidatorsCollector struct { | ||
Cfg config.Config | ||
} | ||
|
||
func (vc ValidatorsCollector) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- missedBlocks | ||
} | ||
|
||
func (vc ValidatorsCollector) Collect(ch chan<- prometheus.Metric) { | ||
ctx, cancel := context.WithTimeout( | ||
context.Background(), | ||
time.Duration(vc.Cfg.Timeout)*time.Second, | ||
) | ||
|
||
defer cancel() | ||
|
||
vals, err := grpc.SigningValidators(ctx, vc.Cfg) | ||
if err != nil { | ||
log.Error(fmt.Sprintf("error getting signing validators: %s", err)) | ||
} else { | ||
log.Debug("Start collecting", zap.String("metric", missedBlocksMetricName)) | ||
|
||
for _, m := range vc.missedBlocksMetrics(vals) { | ||
ch <- m | ||
} | ||
|
||
log.Debug("Stop collecting", zap.String("metric", missedBlocksMetricName)) | ||
} | ||
} | ||
|
||
func (vc ValidatorsCollector) missedBlocksMetrics(vals []types.Validator) []prometheus.Metric { | ||
metrics := []prometheus.Metric{} | ||
|
||
for _, val := range vals { | ||
metrics = append( | ||
metrics, | ||
prometheus.MustNewConstMetric( | ||
missedBlocks, | ||
prometheus.GaugeValue, | ||
float64(val.MissedBlocks), | ||
[]string{ | ||
vc.Cfg.ChainID, | ||
val.ConsAddress, | ||
val.OperatorAddress, | ||
val.Moniker, | ||
strconv.FormatBool(val.Jailed), | ||
strconv.FormatBool(val.Tombstoned), | ||
val.BondStatus, | ||
}..., | ||
), | ||
) | ||
} | ||
|
||
return metrics | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cosmos/cosmos-sdk/types/query" | ||
auth "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
) | ||
|
||
// Accounts. | ||
func (c Client) Accounts(ctx context.Context) (uint64, error) { | ||
var key []byte | ||
|
||
client := auth.NewQueryClient(c.conn) | ||
|
||
req := auth.QueryAccountsRequest{Pagination: &query.PageRequest{Key: key}} | ||
|
||
accounts, err := client.Accounts(ctx, &req) | ||
if err != nil { | ||
return 0, endpointError(err.Error()) | ||
} | ||
|
||
return accounts.Pagination.Total, nil | ||
} |
Oops, something went wrong.