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

IRISHUB-34:irishub添加prometheus监控功能(添加p2p,memo监控,系统监控) #24

Merged
merged 2 commits into from
Jul 6, 2018
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
48 changes: 46 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions tools/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package tools

import (
"github.com/cosmos/cosmos-sdk/client/context"
"fmt"
"io/ioutil"
"encoding/json"
"strings"
"net/http"
ctypes "github.com/tendermint/tendermint/rpc/core/types"

)

type Context struct {
context.CoreContext
}

func NewContext() Context {
ctx := context.NewCoreContextFromViper()
return Context{
ctx,
}
}

type JsonRpc interface {
NetInfo() *ctypes.ResultNetInfo
NumUnconfirmedTxs() *ctypes.ResultUnconfirmedTxs
}

func (rpc Context) NetInfo() *ctypes.ResultNetInfo{
client := &http.Client{}

reqUri := tcpToHttpUrl(rpc.NodeURI) + "/net_info"

resp, err := client.Get(reqUri)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}

var res = struct {
JsonRpc string `json:"jsonrpc"`
Id string `json:"id"`
Result ctypes.ResultNetInfo `json:"result"`
}{}
if err := json.Unmarshal(body,&res); err != nil {
fmt.Println(err)
}

return &res.Result
}

func (rpc Context) NumUnconfirmedTxs() *ctypes.ResultUnconfirmedTxs{
client := &http.Client{}
reqUri := tcpToHttpUrl(rpc.NodeURI) + "/num_unconfirmed_txs"

resp, err := client.Get(reqUri)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}

var res = struct {
JsonRpc string `json:"jsonrpc"`
Id string `json:"id"`
Result ctypes.ResultUnconfirmedTxs `json:"result"`
}{}
if err := json.Unmarshal(body,&res); err != nil {
fmt.Println(err)
}

return &res.Result
}

func tcpToHttpUrl(url string) string{
urls := strings.Replace(url,"tcp","http",1)
return urls
}


18 changes: 10 additions & 8 deletions tools/prometheus/consensus/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/cosmos/cosmos-sdk/wire" // XXX fix
"fmt"
Expand All @@ -14,6 +13,7 @@ import (
"time"
"log"
cctx "context"
tools "github.com/irisnet/irishub/tools"
)

// Metrics contains metrics exposed by this package.
Expand Down Expand Up @@ -118,8 +118,7 @@ func PrometheusMetrics() *Metrics {
}
}


func Monitor(ctx context.CoreContext,csMetrics Metrics,cdc *wire.Codec,storeName string){
func (cs *Metrics)Monitor(ctx tools.Context,cdc *wire.Codec,storeName string){
context, _ := cctx.WithTimeout(cctx.Background(), 10*time.Second)

var client = ctx.Client
Expand All @@ -137,17 +136,18 @@ func Monitor(ctx context.CoreContext,csMetrics Metrics,cdc *wire.Codec,storeName
go func() {
for e := range blockC {
block := e.(types.TMEventData).(types.EventDataNewBlock)
csMetrics.RecordMetrics(ctx,cdc,block.Block,storeName)
cs.RecordMetrics(ctx,cdc,block.Block,storeName)
}
}()
}

func (cs Metrics) RecordMetrics(ctx context.CoreContext,cdc *wire.Codec,block *types.Block,storeName string) {
func (cs Metrics) RecordMetrics(ctx tools.Context,cdc *wire.Codec,block *types.Block,storeName string) {
cs.Height.Set(float64(block.Height))
cs.ByzantineValidators.Set(float64(len(block.Evidence.Evidence)))

missingValidators := 0
missingValidatorsPower := int64(0)
validatorsPower := int64(0)
validators := getValidators(cdc,ctx,storeName)

valMap := make(map[string]stake.Validator,len(validators))
Expand All @@ -162,10 +162,12 @@ func (cs Metrics) RecordMetrics(ctx context.CoreContext,cdc *wire.Codec,block *t
}

valMap[val.Owner.String()] = val
validatorsPower += val.GetPower().Evaluate()
}

cs.MissingValidators.Set(float64(missingValidators))
cs.MissingValidatorsPower.Set(float64(missingValidatorsPower))
cs.ValidatorsPower.Set(float64(validatorsPower))

byzantineValidatorsPower := int64(0)
for _, ev := range block.Evidence.Evidence {
Expand All @@ -178,9 +180,9 @@ func (cs Metrics) RecordMetrics(ctx context.CoreContext,cdc *wire.Codec,block *t

if block.Height > 1 {
lastBlockHight := block.Height -1
resultBlock,_ := ctx.Client.Block(&lastBlockHight)
lastBlock,_ := ctx.Client.Block(&lastBlockHight)
cs.BlockIntervalSeconds.Observe(
block.Time.Sub(resultBlock.BlockMeta.Header.Time).Seconds(),
block.Time.Sub(lastBlock.BlockMeta.Header.Time).Seconds(),
)
}

Expand All @@ -192,7 +194,7 @@ func (cs Metrics) RecordMetrics(ctx context.CoreContext,cdc *wire.Codec,block *t
}


func getValidators(cdc *wire.Codec,ctx context.CoreContext,storeName string) (validators []stake.Validator){
func getValidators(cdc *wire.Codec,ctx tools.Context,storeName string) (validators []stake.Validator){
key := stake.ValidatorsKey
resKVs, err := ctx.QuerySubspace(cdc, key, storeName)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions tools/prometheus/mempool/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
tools "github.com/irisnet/irishub/tools"
)

// Metrics contains metrics exposed by this package.
Expand All @@ -23,3 +24,8 @@ func PrometheusMetrics() *Metrics {
}, []string{}),
}
}

func (m *Metrics )Monitor(rpc tools.Context){
result := rpc.NumUnconfirmedTxs()
m.Size.Set(float64(result.N))
}
6 changes: 6 additions & 0 deletions tools/prometheus/p2p/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
tools "github.com/irisnet/irishub/tools"
)

// Metrics contains metrics exposed by this package.
Expand All @@ -22,3 +23,8 @@ func PrometheusMetrics() *Metrics {
}, []string{}),
}
}

func (m *Metrics) Monitor(ctx tools.Context){
result := ctx.NetInfo()
m.Peers.Set(float64(result.NPeers))
}
23 changes: 14 additions & 9 deletions tools/prometheus/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
cmn "github.com/tendermint/tmlibs/common"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/wire" // XXX fix
"github.com/irisnet/irishub/tools/prometheus/consensus"
sys "github.com/programokey/irishub/tools/prometheus/system"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/irisnet/irishub/tools"
"github.com/spf13/viper"
)


Expand All @@ -19,15 +18,20 @@ func MonitorCommand(storeName string, cdc *wire.Codec) *cobra.Command {
Short: "irishub monitor",
RunE: func(cmd *cobra.Command, args []string) error {
//TODO
csMetrics,_,_ , sysMertrics:= DefaultMetricsProvider()
ctx := context.NewCoreContextFromViper()
csMetrics,p2pMetrics,memMetrics, sysMertrics:= DefaultMetricsProvider()
ctx := tools.NewContext()

//监控共识参数
consensus.Monitor(ctx,*csMetrics,cdc,storeName)
csMetrics.Monitor(ctx,cdc,storeName)
//监控p2p参数
p2pMetrics.Monitor(ctx)
//监控mempool参数
memMetrics.Monitor(ctx)

//monitor system info, first parameter is the command of the process to be monitor
// and the second parameter is the directory that you want to get total size of its' files
sys.Monitor("irishub", "/", sysMertrics)
path := viper.GetString("home")
sysMertrics.Monitor("iris", path)

srv := &http.Server{
Addr: ":26660",
Expand All @@ -48,6 +52,7 @@ func MonitorCommand(storeName string, cdc *wire.Codec) *cobra.Command {
},
}
cmd.Flags().StringP("node", "n", "tcp://localhost:46657", "Node to connect to")
cmd.Flags().String("chain-id", "", "Chain ID of tendermint node")
cmd.Flags().String("chain-id", "fuxi", "Chain ID of tendermint node")
cmd.Flags().StringP("home", "", "", "directory for config and data")
return cmd
}