Skip to content

Commit

Permalink
v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
wlsaud619 committed Jan 6, 2020
1 parent 52cfb17 commit 35180d8
Show file tree
Hide file tree
Showing 23 changed files with 1,467 additions and 57 deletions.
25 changes: 10 additions & 15 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
# TOML Document for Cosmos-Validator Exporter(Pometheus & Grafana)
# TOML Document for Terra-Validator Exporter(Pometheus & Grafana)

title = "Cosmos-Validator Exporter TOML"
network = "cosmos"
title = "TOML Document"

# RPC-Server
[rpc]
address = "localhost:26657"
[Servers]
[Servers.addr]
rpc = "localhost:26657"
rest = "localhost:1317"

# Rest-server
[rest_server]
address = "localhost:1317"
[Validator]
operatorAddr = "cosmosvaloper14l0fp639yudfl46zauvv8rkzjgd4u0zk2aseys"

[validator_info]
operatorAddress = "cosmosvaloper14l0fp639yudfl46zauvv8rkzjgd4u0zk2aseys"

[option]
exporterListenPort = "26661"
outputPrint = true
[Options]
listenPort = "26661"
68 changes: 68 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package config

import (
"log"
// "time"

"github.com/BurntSushi/toml"
rpc "github.com/node-a-team/cosmos-validator_exporter/getData/rpc"
rest "github.com/node-a-team/cosmos-validator_exporter/getData/rest"
// "github.com/spf13/viper"
)

const (
)

var (
ConfigPath string
Config configType
)


type configType struct {

Title string `json:"title"`

Servers struct {
Addr struct {
RPC string `json:"rpc"`
REST string `json:"rest"`
}
}

Validator struct {
OperatorAddr string `json:"operatorAddr"`
}

Options struct {
ListenPort string `json:"listenPort"`
}
}


func Init() {

Config = readConfig()

rpc.Addr = Config.Servers.Addr.RPC
rest.Addr = Config.Servers.Addr.REST

rest.OperAddr = Config.Validator.OperatorAddr

}

func readConfig() configType {

var config configType

// path := viper.GetString(ConfigPath)+"/config.toml"

// if _, err := toml.DecodeFile(path, &config); err != nil{
if _, err := toml.DecodeFile(ConfigPath +"/config.toml", &config); err != nil{

log.Fatal("Config file is missing: ", config)
}

return config

}
161 changes: 161 additions & 0 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package exporter

import (
"fmt"
"time"
"go.uber.org/zap"

rpc "github.com/node-a-team/cosmos-validator_exporter/getData/rpc"
rest "github.com/node-a-team/cosmos-validator_exporter/getData/rest"
metric "github.com/node-a-team/cosmos-validator_exporter/exporter/metric"
utils "github.com/node-a-team/cosmos-validator_exporter/utils"

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

var (
previousBlockHeight int64

)

func Start(log *zap.Logger) {

gaugesNamespaceList := metric.GaugesNamespaceList

var gauges []prometheus.Gauge = make([]prometheus.Gauge, len(gaugesNamespaceList))
var gaugesDenom []prometheus.Gauge = make([]prometheus.Gauge, len(metric.DenomList)*3) // wallet, rewards, commission


// nomal guages
for i := 0; i < len(gaugesNamespaceList); i++ {
gauges[i] = metric.NewGauge("exporter", gaugesNamespaceList[i], "")
prometheus.MustRegister(gauges[i])
}


// denom gagues
count := 0
for i := 0; i < len(metric.DenomList)*3; i += 3 {
gaugesDenom[i] = metric.NewGauge("exporter_balances", metric.DenomList[count], "")
gaugesDenom[i+1] = metric.NewGauge("exporter_commission", metric.DenomList[count], "")
gaugesDenom[i+2] = metric.NewGauge("exporter_rewards", metric.DenomList[count], "")
prometheus.MustRegister(gaugesDenom[i])
prometheus.MustRegister(gaugesDenom[i+1])
prometheus.MustRegister(gaugesDenom[i+2])

count++
}


// labels
labels := []string{"chainId", "moniker", "operatorAddress", "accountAddress", "consHexAddress"}
gaugesForLabel := metric.NewCounterVec("exporter", "labels", "", labels)

prometheus.MustRegister(gaugesForLabel)


for {
func() {
defer func() {

if r := recover(); r != nil {
//Error Log
}

time.Sleep(500 * time.Millisecond)

}()


currentBlockHeight := rpc.BlockHeight()

if previousBlockHeight != currentBlockHeight {

log.Info("RPC-Server", zap.Bool("Success", true), zap.String("err", "nil"), zap.String("Get Data", "Block Height: " +fmt.Sprint(currentBlockHeight)))


restData, consHexAddr := rest.GetData(currentBlockHeight, log)
rpcData := rpc.GetData(currentBlockHeight, consHexAddr, log)

metric.SetMetric(currentBlockHeight, restData, rpcData, log)

metricData := metric.GetMetric()
denomList := metric.GetDenomList()

count := 0
for i := 0; i < len(denomList); i++ {

for _, value := range metricData.Validator.Account.Balances {
if value.Denom == denomList[i] {
gaugesDenom[count].Set(utils.StringToFloat64(value.Amount))
count++
}
}
for _, value := range metricData.Validator.Account.Commission {
if value.Denom == denomList[i] {
gaugesDenom[count].Set(utils.StringToFloat64(value.Amount))
count++
}
}
for _, value := range metricData.Validator.Account.Rewards {
if value.Denom == denomList[i] {
gaugesDenom[count].Set(utils.StringToFloat64(value.Amount))
count++
}
}
}


gaugesValue := [...]float64{
float64(metricData.Network.BlockHeight),

metricData.Network.Staking.NotBondedTokens,
metricData.Network.Staking.BondedTokens,
metricData.Network.Staking.TotalSupply,
metricData.Network.Staking.BondedRatio,

metricData.Network.Gov.TotalProposalCount,
metricData.Network.Gov.VotingProposalCount,

metricData.Validator.VotingPower,
metricData.Validator.MinSelfDelegation,
metricData.Validator.JailStatus,

metricData.Validator.Proposer.Ranking,
metricData.Validator.Proposer.Status,

metricData.Validator.Delegation.Shares,
metricData.Validator.Delegation.Ratio,
metricData.Validator.Delegation.DelegatorCount,
metricData.Validator.Delegation.Self,

metricData.Validator.Commission.Rate,
metricData.Validator.Commission.MaxRate,
metricData.Validator.Commission.MaxChangeRate,
metricData.Validator.Commit.VoteType,
metricData.Validator.Commit.PrecommitStatus,

metricData.Network.Minting.Inflation,
metricData.Network.Minting.ActualInflation,
}

for i := 0; i < len(gaugesNamespaceList); i++ {
gauges[i].Set(gaugesValue[i])
}


gaugesForLabel.WithLabelValues(metricData.Network.ChainID,
metricData.Validator.Moniker,
metricData.Validator.Address.Operator,
metricData.Validator.Address.Account,
metricData.Validator.Address.ConsensusHex,
).Add(0)

}

previousBlockHeight = currentBlockHeight
}()
}
}


32 changes: 32 additions & 0 deletions exporter/metric/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package metric

import (

"fmt"
// "encoding/hex"
sdk "github.com/cosmos/cosmos-sdk/types"

// "github.com/node-a-team/terra-validator_exporter/getData"
// cfg "github.com/node-a-team/terra-validator_exporter/config"
// utils "github.com/node-a-team/terra-validator_exporter/utils"
)

var (

)

func GetAccAddrFromOperAddr(operAddr string) string {

// Get HexAddress
hexAddr, err := sdk.ValAddressFromBech32(operAddr)
if err != nil {
// Error
}

accAddr, err := sdk.AccAddressFromHex(fmt.Sprint(hexAddr))
if err != nil {
// Error
}

return accAddr.String()
}
Loading

0 comments on commit 35180d8

Please sign in to comment.