Skip to content

Commit

Permalink
Merge pull request #2 from irisnet/feature/prometheus
Browse files Browse the repository at this point in the history
Feature/prometheus
  • Loading branch information
programokey authored Jul 9, 2018
2 parents 0c61031 + 3f7bcb3 commit 973d134
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 19 deletions.
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
13 changes: 13 additions & 0 deletions tools/prometheus/mempool/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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"
"time"
)

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

func (m *Metrics )Monitor(rpc tools.Context){
go func() {
for {
time.Sleep(1 * time.Second)
result := rpc.NumUnconfirmedTxs()
m.Size.Set(float64(result.N))
}
}()

}
12 changes: 12 additions & 0 deletions tools/prometheus/p2p/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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"
"time"
)

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

func (m *Metrics) Monitor(ctx tools.Context){
go func() {
for {
time.Sleep(1 * time.Second)
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
}

0 comments on commit 973d134

Please sign in to comment.