Skip to content

Commit

Permalink
Merge branch 'feature/monitor' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
programokey authored Jul 31, 2018
2 parents 2ee64c2 + abc7b26 commit 195a56f
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 8 deletions.
117 changes: 115 additions & 2 deletions tools/prometheus/consensus/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,58 @@ import (
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/tendermint/tendermint/types"
"github.com/tendermint/tendermint/consensus"
//"github.com/irisnet/irishub/tools"
sdk "github.com/cosmos/cosmos-sdk/types"
"log"
"strings"
"time"
"container/list"
"bytes"
"github.com/spf13/viper"
)

// TODO
const keyStoreStake = "stake"
type BlockInfo struct {
Height int64
Time time.Time
signed int // whether the given address signed the block, 1 if signed, 0 else.
}

// Metrics contains metrics exposed by this package.

type IrisMetrics struct {
Candidates metrics.Gauge
// Total power of all validators.
ValidatorsPower metrics.Gauge
// Number of validators who did not sign.
MissingValidators metrics.Gauge
// Total power of the missing validators.
MissingValidatorsPower metrics.Gauge
// Number of validators who tried to double sign.
ByzantineValidators metrics.Gauge
// Total power of the byzantine validators.
ByzantineValidatorsPower metrics.Gauge

// Time between this and the last block.
BlockIntervalSeconds metrics.Histogram
//average block interval in last 100 blocks (in seconds)
AvgBlockIntervalSeconds metrics.Gauge
//block info
blockInfo *list.List// queue of BlockInfo
// missed prevote ratio in last 100 blocks (in seconds)
MissedPrevotesRatio metrics.Gauge
// given address
Address types.Address
SignedCount int


// Number of transactions.
NumTxs metrics.Gauge
// Size of the block.
BlockSizeBytes metrics.Gauge
// Total number of transactions.
TotalTxs metrics.Gauge
}

func NewIrisMetrics() IrisMetrics{
Expand All @@ -48,13 +89,50 @@ func PrometheusMetrics() *Metrics {
TmMetrics : tmMetrics,
IrisMetrics : irisMetrics,

/*
BlockIntervalSeconds: prometheus.NewHistogramFrom(stdprometheus.HistogramOpts{
Subsystem: "consensus",
Name: "block_interval_seconds",
Help: "Time between this and the last block.",
Buckets: []float64{1, 2.5, 5, 10, 60},
}, []string{}),
AvgBlockIntervalSeconds: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Name: "avg_block_interval_seconds",
Help: "average block interval of last 100 blocks (in seconds).",
}, []string{}),
blockInfo:list.New(),
MissedPrevotesRatio:prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Name: "missed_precommits_ratio",
Help: "missed precommits ratio of last 100 blocks.",
}, []string{}),
Address:make([]byte, 0),
SignedCount:0,
*/
}
}

func (cs *Metrics) SetAddress(addr_str string){
if addr, err := sdk.GetValAddressHex(addr_str);err != nil{
log.Println("parse address falid ", err)
}else {
cs.Address = addr
}
}

func (cs *Metrics) Start(ctx app.Context) {
context, _ := cctx.WithTimeout(cctx.Background(), 10*time.Second)

var client = ctx.Ctx.Client
/*
func (cs *Metrics) Start(ctx tools.Context) {
context, _ := cctx.WithTimeout(cctx.Background(), 10*time.Second)
validaor_addr := viper.GetString("address")
cs.SetAddress(validaor_addr)
var client = ctx.Client
*/

//开启监听事件
client.Start()

Expand All @@ -79,6 +157,19 @@ func (cs *Metrics) RecordMetrics(ctx app.Context, cdc *wire.Codec, block *types.

cs.TmMetrics.Height.Set(float64(block.Height))
cs.TmMetrics.ByzantineValidators.Set(float64(len(block.Evidence.Evidence)))
/*
func (cs *Metrics) RecordMetrics(ctx tools.Context, cdc *wire.Codec, block *types.Block, storeName string) {
cs.Height.Set(float64(block.Height))
if len(block.Evidence.Evidence) != 0{
for _, evidence := range block.Evidence.Evidence{
fmt.Println()
fmt.Println(evidence)
fmt.Println()
}
}
cs.ByzantineValidators.Set(float64(len(block.Evidence.Evidence)))
*/

missingValidators := 0
missingValidatorsPower := int64(0)
Expand Down Expand Up @@ -124,9 +215,31 @@ func (cs *Metrics) RecordMetrics(ctx app.Context, cdc *wire.Codec, block *types.
cs.TmMetrics.BlockIntervalSeconds.Observe(interval)
}

cs.TmMetrics.NumTxs.Set(float64(block.NumTxs))
cs.TmMetrics.TotalTxs.Set(float64(block.TotalTxs))
cs.TmMetrics.NumTxs.Set(float64(block.NumTxs))
cs.TmMetrics.TotalTxs.Set(float64(block.TotalTxs))
/*
if block.Height > 0{
signed := 0
for _, vote := range block.LastCommit.Precommits{
if bytes.Equal(vote.ValidatorAddress.Bytes(), cs.Address.Bytes()){
signed = 1
break
}
}
cs.blockInfo.PushBack(BlockInfo{Height:block.Height, Time:block.Time, signed:signed})
firstBlock := cs.blockInfo.Front().Value.(BlockInfo)
if cs.blockInfo.Len() > 100{
cs.blockInfo.Remove(cs.blockInfo.Front())
}
cs.SignedCount += firstBlock.signed
avgInterval := time.Now().Sub(firstBlock.Time).Seconds()/float64(cs.blockInfo.Len())
cs.AvgBlockIntervalSeconds.Set(avgInterval)
cs.MissedPrevotesRatio.Set(1 - float64(cs.SignedCount)/float64(cs.blockInfo.Len()))
}
cs.NumTxs.Set(float64(block.NumTxs))
cs.TotalTxs.Set(float64(block.TotalTxs))
*/
bz, _ := cdc.MarshalBinaryBare(block)
cs.TmMetrics.BlockSizeBytes.Set(float64(len(bz)))
}
Expand Down
92 changes: 89 additions & 3 deletions tools/prometheus/p2p/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,33 @@ package p2p
import (
"github.com/irisnet/irishub/app"
"github.com/tendermint/tendermint/p2p"
/*
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/prometheus"
"github.com/irisnet/irishub/tools"
"github.com/pelletier/go-toml"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"io/ioutil"
"log"
"path/filepath"
"strings"
*/
"time"
"github.com/spf13/viper"
)

// Metrics contains metrics exposed by this package.
type Metrics struct {
// Number of peers.
TmMetrics p2p.Metrics
TmMetrics p2p.Metrics
/*
// Number of peers.
Peers metrics.Gauge
// Number of connected persistent peers.
ConnectedPersistentPeers metrics.Gauge
// Number of unconnected persistent peers.
UnonnectedPersistentPeers metrics.Gauge
persistent_peers map[string]string
*/
}

// PrometheusMetrics returns Metrics build using Prometheus client library.
Expand All @@ -21,11 +41,77 @@ func PrometheusMetrics() *Metrics {
}

func (m *Metrics) Start(ctx app.Context) {
/*
Peers: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "p2p",
Name: "peers",
Help: "Number of peers.",
}, []string{}),
ConnectedPersistentPeers: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "p2p",
Name: "connected_persistent_peers",
Help: "Number of connected persistent peers.",
}, []string{}),
UnonnectedPersistentPeers: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "p2p",
Name: "unconnected_persistent_peers",
Help: "Number of unconnected persistent peers.",
}, []string{}),
persistent_peers: make(map[string]string),
}
}
func (m *Metrics) Start(ctx tools.Context) {
m.setP2PPersistentPeers(viper.GetString("home"))
*/
go func() {
for {
time.Sleep(1 * time.Second)
result := ctx.NetInfo()
m.TmMetrics.Peers.Set(float64(result.NPeers))
m.TmMetrics.Peers.Set(float64(result.NPeers))
/*
connected := 0
for _, peer := range result.Peers {
if listenAddr, exist := m.persistent_peers[string(peer.ID)]; exist && listenAddr == peer.ListenAddr {
connected += 1
}
}
m.Peers.Set(float64(result.NPeers))
m.ConnectedPersistentPeers.Set(float64(connected))
m.UnonnectedPersistentPeers.Set(float64(len(m.persistent_peers) - connected))
*/
}
}()
}


//set the p2p persistent peers by given home dir of iris config file
func (m *Metrics) setP2PPersistentPeers(homeDir string) {
if !filepath.IsAbs(homeDir) {
absHomeDir, err := filepath.Abs(homeDir)
if err != nil {
log.Println("cannot find the file ", err)
return
}
homeDir = absHomeDir
}
configFilePath := filepath.Join(homeDir, "config/config.toml")
//fmt.Printf("configFilePath: %s\n", configFilePath)
if data, err := ioutil.ReadFile(configFilePath); err != nil {
log.Println("cannot open the file ", err)
return
} else {
if config, err := toml.LoadBytes(data); err != nil {
log.Println("parse config file failed: ", err)
return
} else {
persistent_peers := config.Get("p2p.persistent_peers").(string)
for _, peer := range strings.Split(persistent_peers, ",") {
splited := strings.Split(peer, "@")
m.persistent_peers[splited[0]] = splited[1]
}
}
}
}
13 changes: 10 additions & 3 deletions tools/prometheus/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package prometheus
import (
"github.com/cosmos/cosmos-sdk/wire"
"github.com/irisnet/irishub/app"
//"github.com/irisnet/irishub/tools"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
cmn "github.com/tendermint/tendermint/libs/common"
"log"
"net/http"
"github.com/spf13/viper"
"github.com/spf13/viper"
"fmt"
)

Expand All @@ -17,8 +18,8 @@ func MonitorCommand(cdc *wire.Codec) *cobra.Command {
Use: "monitor",
Short: "irishub monitor",
RunE: func(cmd *cobra.Command, args []string) error {

ctx := app.NewContext().WithCodeC(cdc)
//ctx := tools.NewContext(storeName, cdc)
monitor := DefaultMonitor(ctx)
monitor.Start()

Expand All @@ -43,14 +44,20 @@ func MonitorCommand(cdc *wire.Codec) *cobra.Command {
cmd.Flags().Int("port", 36660, "port to connect to")
cmd.Flags().StringP("node", "n", "tcp://localhost:46657", "Node to connect to")
cmd.Flags().String("chain-id", "fuxi", "Chain ID of tendermint node")
cmd.Flags().StringP("address", "a", "", `hex address of the validator that you want to
monitor`)

cmd.Flags().String("home", app.DefaultNodeHome, fmt.Sprintf("your iris home, %s by default",
app.DefaultNodeHome))

cmd.Flags().StringP("commands", "c", "iris start", `the processes you want to monitor that started
by these commands, separated by semicolons ';'.
eg: --commands="command 0;command 1;command 2", --commands=iris by default`)
cmd.Flags().StringP("disks", "d", "/", `mounted paths of storage devices, separated by semicolons ';'.
eg: --disks="/;/mnt1;/mnt2"`)
cmd.Flags().StringP("paths", "p", app.DefaultNodeHome, `path to config and data files/directories, separated by semicolons ';'.
cannot use ~ and environment variables. eg: --paths="/etc;/home;
size of files in sub-directories is excluded. to compute the size recursively, you can use --recursively=true"`)
size of files in sub-directories is excluded. to compute the size recursively, you can use --recursively=true`)
cmd.Flags().BoolP("recursively", "r", false, `specify whether the files in sub-directories is included,
excluded by default. If there are many files & sub-directory in given directories, this program may be very slow!`)
return cmd
Expand Down

0 comments on commit 195a56f

Please sign in to comment.