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

R4R: Refactor prometheus metrics #1971

Merged
merged 24 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e0f02bf
refactor gov metrics
Sep 9, 2019
a77752d
replace consAddr with operator address
Sep 10, 2019
01c9e44
replace consAddr with operator address
Sep 10, 2019
0c002bd
replace consAddr with operator address
Sep 10, 2019
51c5cb4
refactor code
Sep 10, 2019
5dc156a
Merge branch 'develop' of https://github.com/irisnet/irishub into pro…
Sep 10, 2019
5a4b8cd
refactor code
Sep 10, 2019
8be0d98
Merge branch 'develop' of https://github.com/irisnet/irishub into pro…
Sep 11, 2019
bb14895
refactor code
Sep 11, 2019
54b0e3d
refactor code
Sep 11, 2019
1646066
refactor code
Sep 11, 2019
4e8b588
Merge branch 'develop' of https://github.com/irisnet/irishub into pro…
Sep 12, 2019
06ddff4
remove useless code
Sep 12, 2019
35fd15d
refactor code
Sep 12, 2019
c531fe7
Merge branch 'develop' of https://github.com/irisnet/irishub into pro…
Sep 16, 2019
8eb741a
Merge branch 'develop' of https://github.com/irisnet/irishub into pro…
Sep 16, 2019
2381eff
apply github comments
Sep 16, 2019
411dd75
apply github comments
Sep 16, 2019
ee34e08
Merge branch 'develop' of https://github.com/irisnet/irishub into pro…
Sep 16, 2019
0694e70
Merge branch 'develop' of https://github.com/irisnet/irishub into pro…
Sep 16, 2019
267dd4c
modify code's comments
Sep 16, 2019
49c71b0
Merge branch 'develop' of https://github.com/irisnet/irishub into pro…
Sep 16, 2019
a2a733c
Merge branch 'develop' into prometheus
Sep 16, 2019
012ca50
Merge branch 'prometheus' of https://github.com/zhiqiang-bianjie/iris…
Sep 16, 2019
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
11 changes: 6 additions & 5 deletions app/v1/gov/abci.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package gov

import (
"strconv"

"github.com/irisnet/irishub/app/v1/gov/tags"
sdk "github.com/irisnet/irishub/types"
tmstate "github.com/tendermint/tendermint/state"
Expand All @@ -28,6 +26,7 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) {
keeper.SubProposalNum(ctx, inactiveProposal.GetProposalLevel())
keeper.DeleteDeposits(ctx, proposalID)
keeper.DeleteProposal(ctx, proposalID)
keeper.metrics.DeleteProposalStatus(proposalID)

resTags = resTags.AppendTag(tags.Action, tags.ActionProposalDropped)
resTags = resTags.AppendTag(tags.ProposalID, []byte(string(proposalID)))
Expand All @@ -49,18 +48,18 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) {

var action []byte
if result == PASS {
keeper.metrics.ProposalStatus.With(ProposalIDLabel, strconv.FormatUint(proposalID, 10)).Set(2)
keeper.metrics.SetProposalStatus(proposalID, StatusPassed)
keeper.RefundDeposits(ctx, activeProposal.GetProposalID())
activeProposal.SetStatus(StatusPassed)
action = tags.ActionProposalPassed
activeProposal.Execute(ctx, keeper)
} else if result == REJECT {
keeper.metrics.ProposalStatus.With(ProposalIDLabel, strconv.FormatUint(proposalID, 10)).Set(3)
keeper.metrics.SetProposalStatus(proposalID, StatusRejected)
keeper.RefundDeposits(ctx, activeProposal.GetProposalID())
activeProposal.SetStatus(StatusRejected)
action = tags.ActionProposalRejected
} else if result == REJECTVETO {
keeper.metrics.ProposalStatus.With(ProposalIDLabel, strconv.FormatUint(proposalID, 10)).Set(3)
keeper.metrics.SetProposalStatus(proposalID, StatusRejected)
keeper.DeleteDeposits(ctx, activeProposal.GetProposalID())
activeProposal.SetStatus(StatusRejected)
action = tags.ActionProposalRejected
Expand All @@ -83,10 +82,12 @@ func EndBlocker(ctx sdk.Context, keeper Keeper) (resTags sdk.Tags) {
keeper.GetTallyingProcedure(ctx, activeProposal.GetProposalLevel()).Penalty)
}
}
keeper.metrics.DeleteVote(valAddr.String(), proposalID)
}

keeper.SubProposalNum(ctx, activeProposal.GetProposalLevel())
keeper.DeleteValidatorSet(ctx, activeProposal.GetProposalID())
keeper.metrics.DeleteProposalStatus(proposalID)
}
return resTags
}
7 changes: 3 additions & 4 deletions app/v1/gov/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ func (keeper Keeper) SetProposal(ctx sdk.Context, proposal Proposal) {

// Implements sdk.AccountKeeper.
func (keeper Keeper) DeleteProposal(ctx sdk.Context, proposalID uint64) {
keeper.metrics.ProposalStatus.With(ProposalIDLabel, strconv.FormatUint(proposalID, 10)).Set(4)
store := ctx.KVStore(keeper.storeKey)
proposal := keeper.GetProposal(ctx, proposalID)
keeper.RemoveFromInactiveProposalQueue(ctx, proposal.GetDepositEndTime(), proposalID)
Expand Down Expand Up @@ -328,7 +327,7 @@ func (keeper Keeper) AddVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.A
}
keeper.setVote(ctx, proposalID, voterAddr, vote)
if validator != nil {
keeper.metrics.Vote.With(ValidatorLabel, validator.GetConsAddr().String(), ProposalIDLabel, strconv.FormatUint(proposalID, 10)).Set(float64(option))
keeper.metrics.AddVote(validator.GetOperator().String(), proposalID, option)
}
return nil
}
Expand Down Expand Up @@ -508,7 +507,7 @@ func (keeper Keeper) ActiveProposalQueueIterator(ctx sdk.Context, endTime time.T

// Inserts a ProposalID into the active proposal queue at endTime
func (keeper Keeper) InsertActiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID uint64) {
keeper.metrics.ProposalStatus.With(ProposalIDLabel, strconv.FormatUint(proposalID, 10)).Set(1)
keeper.metrics.SetProposalStatus(proposalID, StatusVotingPeriod)
store := ctx.KVStore(keeper.storeKey)
bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID)
store.Set(KeyActiveProposalQueueProposal(endTime, proposalID), bz)
Expand All @@ -528,7 +527,7 @@ func (keeper Keeper) InactiveProposalQueueIterator(ctx sdk.Context, endTime time

// Inserts a ProposalID into the inactive proposal queue at endTime
func (keeper Keeper) InsertInactiveProposalQueue(ctx sdk.Context, endTime time.Time, proposalID uint64) {
keeper.metrics.ProposalStatus.With(ProposalIDLabel, strconv.FormatUint(proposalID, 10)).Set(0)
keeper.metrics.SetProposalStatus(proposalID, StatusDepositPeriod)
store := ctx.KVStore(keeper.storeKey)
bz := keeper.cdc.MustMarshalBinaryLengthPrefixed(proposalID)
store.Set(KeyInactiveProposalQueueProposal(endTime, proposalID), bz)
Expand Down
74 changes: 54 additions & 20 deletions app/v1/gov/metrics.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package gov

import (
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/discard"
"github.com/go-kit/kit/metrics/prometheus"
distr "github.com/irisnet/irishub/app/v1/distribution/types"
"github.com/irisnet/irishub/app/v1/mint"
promutil "github.com/irisnet/irishub/tools/prometheus"
Expand All @@ -20,10 +17,12 @@ const (
ParamKeyLabel = "parameter_key"
)

type Label = stdprometheus.Labels

type Metrics struct {
ProposalStatus metrics.Gauge // 0:DepositPeriod 1:VotingPeriod 2:Pass 3:Reject 4:Other
Vote metrics.Gauge // 0:Yes 1:No 2:NoWithVeto 3:Abstain
Param metrics.Gauge
ProposalStatus *stdprometheus.GaugeVec // 1:DepositPeriod 2:VotingPeriod 3:Passed 4:Rejected
Vote *stdprometheus.GaugeVec // 0:Yes 1:No 2:NoWithVeto 3:Abstain
dreamer-zq marked this conversation as resolved.
Show resolved Hide resolved
Param *stdprometheus.GaugeVec
}

// PrometheusMetrics returns Metrics build using Prometheus client library.
Expand Down Expand Up @@ -56,27 +55,62 @@ func PrometheusMetrics(config *cfg.InstrumentationConfig) *Metrics {
promutil.RegisterMetrics(proposalStatusVec, voteVec, paramVec)

return &Metrics{
ProposalStatus: prometheus.NewGauge(proposalStatusVec),
Vote: prometheus.NewGauge(voteVec),
Param: prometheus.NewGauge(paramVec),
ProposalStatus: proposalStatusVec,
Vote: voteVec,
Param: paramVec,
}
}

func NopMetrics() *Metrics {
return &Metrics{
ProposalStatus: discard.NewGauge(),
Vote: discard.NewGauge(),
Param: discard.NewGauge(),
ProposalStatus: &stdprometheus.GaugeVec{},
Vote: &stdprometheus.GaugeVec{},
Param: &stdprometheus.GaugeVec{},
}
}

func SetParameterMetrics(metrics *Metrics, key string, value interface{}) {
switch key {
case string(mint.KeyInflation), string(distr.KeyBaseProposerReward), string(distr.KeyBonusProposerReward), string(distr.KeyCommunityTax):
valueFloat64, err := strconv.ParseFloat(value.(sdk.Dec).String(), 64)
if err == nil {
metrics.Param.With(ParamKeyLabel, key).Set(valueFloat64)
func (metrics *Metrics) SetProposalStatus(proposalID uint64, status ProposalStatus) {
promutil.SafeExec(func() {
s := float64(status)
metrics.ProposalStatus.WithLabelValues(strconv.FormatUint(proposalID, 10)).Set(s)
})
}

func (metrics *Metrics) DeleteProposalStatus(proposalID uint64) {
promutil.SafeExec(func() {
metrics.ProposalStatus.DeleteLabelValues(strconv.FormatUint(proposalID, 10))
})
}

func (metrics *Metrics) AddParameter(key string, value interface{}) {
promutil.SafeExec(func() {
switch key {
case string(mint.KeyInflation), string(distr.KeyBaseProposerReward), string(distr.KeyBonusProposerReward), string(distr.KeyCommunityTax):
valueFloat64, err := strconv.ParseFloat(value.(sdk.Dec).String(), 64)
if err == nil {
metrics.Param.WithLabelValues(key).Set(valueFloat64)
}
default:
}
default:
}
})
}
func (metrics *Metrics) AddVote(consAddr string, proposalID uint64, option VoteOption) {
promutil.SafeExec(func() {
labels := Label{
ValidatorLabel: consAddr,
ProposalIDLabel: strconv.FormatUint(proposalID, 10),
}
metrics.Vote.With(labels).Set(float64(option))
})

}

func (metrics *Metrics) DeleteVote(valAddr string, proposalID uint64) {
promutil.SafeExec(func() {
labels := Label{
ValidatorLabel: valAddr,
ProposalIDLabel: strconv.FormatUint(proposalID, 10),
}
metrics.Vote.Delete(labels)
})
}
2 changes: 1 addition & 1 deletion app/v1/gov/proposal_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (pp *ParameterProposal) Execute(ctx sdk.Context, k Keeper) sdk.Error {
value, _ := paramSet.Validate(param.Key, param.Value)
subspace, found := k.paramsKeeper.GetSubspace(param.Subspace)
if found {
SetParameterMetrics(k.metrics, param.Key, value)
k.metrics.AddParameter(param.Key, value)
subspace.Set(ctx, []byte(param.Key), value)
ctx.Logger().Info("Execute ParameterProposal Success", "key", param.Key, "value", param.Value)
} else {
Expand Down
5 changes: 4 additions & 1 deletion app/v1/slashing/signing_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ func (k Keeper) IterateValidatorSigningInfos(ctx sdk.Context, handler func(addre
func (k Keeper) SetValidatorSigningInfo(ctx sdk.Context, address sdk.ConsAddress, info ValidatorSigningInfo) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(info)
k.metrics.MissedBlocks.With("validator_address", address.String()).Set(float64(info.MissedBlocksCounter))
validator := k.validatorSet.ValidatorByConsAddr(ctx, address)
if validator != nil {
k.metrics.MissedBlocks.With("validator_address", validator.GetOperator().String()).Set(float64(info.MissedBlocksCounter))
}
store.Set(GetValidatorSigningInfoKey(address), bz)
}

Expand Down
4 changes: 2 additions & 2 deletions app/v1/stake/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,12 @@ func (k Keeper) UpdateMetrics(ctx sdk.Context) {
burnedCoins := k.bankKeeper.GetCoins(ctx, auth.BurnedCoinsAccAddr)
burnedAmount, err := strconv.ParseFloat(sdk.NewDecFromInt(burnedCoins.AmountOf(types.StakeDenom)).QuoInt(sdk.AttoScaleFactor).String(), 64)
if err == nil {
k.metrics.BurnedToken.Set(burnedAmount)
k.metrics.SetBurnedToken(burnedAmount)
}

looseCoins := k.bankKeeper.GetLoosenCoins(ctx)
looseAmount, err := strconv.ParseFloat(sdk.NewDecFromInt(looseCoins.AmountOf(types.StakeDenom)).QuoInt(sdk.AttoScaleFactor).String(), 64)
if err == nil {
k.metrics.LoosenToken.Set(looseAmount)
k.metrics.SetLoosenToken(looseAmount)
}
}
112 changes: 86 additions & 26 deletions app/v1/stake/keeper/metrics.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package keeper

import (
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/discard"
"github.com/go-kit/kit/metrics/prometheus"
promutil "github.com/irisnet/irishub/tools/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
cfg "github.com/tendermint/tendermint/config"
)

const MetricsSubsystem = "module_stake"
const (
MetricsSubsystem = "module_stake"
ValidatorLabel = "validator_address"
)

type Metrics struct {
BondedToken metrics.Gauge
LoosenToken metrics.Gauge
BurnedToken metrics.Gauge
SlashedToken metrics.Counter
Jailed metrics.Gauge
Power metrics.Gauge
BondedToken *stdprometheus.GaugeVec
LoosenToken *stdprometheus.GaugeVec
BurnedToken *stdprometheus.GaugeVec
SlashedToken *stdprometheus.CounterVec
Jailed *stdprometheus.GaugeVec
Power *stdprometheus.GaugeVec
}

// PrometheusMetrics returns Metrics build using Prometheus client library.
Expand All @@ -31,7 +31,7 @@ func PrometheusMetrics(config *cfg.InstrumentationConfig) *Metrics {
Subsystem: MetricsSubsystem,
Name: "bonded_token",
Help: "bonded token",
}, []string{"validator_address"})
}, []string{ValidatorLabel})

loosenTokenVec := stdprometheus.NewGaugeVec(stdprometheus.GaugeOpts{
Namespace: config.Namespace,
Expand All @@ -52,41 +52,101 @@ func PrometheusMetrics(config *cfg.InstrumentationConfig) *Metrics {
Subsystem: MetricsSubsystem,
Name: "slashed_token",
Help: "slashed token",
}, []string{"validator_address"})
}, []string{ValidatorLabel})

jailedVec := stdprometheus.NewGaugeVec(stdprometheus.GaugeOpts{
Namespace: config.Namespace,
Subsystem: MetricsSubsystem,
Name: "jailed",
Help: "jailed",
}, []string{"validator_address"})
}, []string{ValidatorLabel})

powerVec := stdprometheus.NewGaugeVec(stdprometheus.GaugeOpts{
Namespace: config.Namespace,
Subsystem: MetricsSubsystem,
Name: "power",
Help: "power",
}, []string{"validator_address"})
}, []string{ValidatorLabel})

promutil.RegisterMetrics(bondedTokenVec, loosenTokenVec, burnedTokenVec, slashedTokenVec, jailedVec, powerVec)

return &Metrics{
BondedToken: prometheus.NewGauge(bondedTokenVec),
LoosenToken: prometheus.NewGauge(loosenTokenVec),
BurnedToken: prometheus.NewGauge(burnedTokenVec),
SlashedToken: prometheus.NewCounter(slashedTokenVec),
Jailed: prometheus.NewGauge(jailedVec),
Power: prometheus.NewGauge(powerVec),
BondedToken: bondedTokenVec,
LoosenToken: loosenTokenVec,
BurnedToken: burnedTokenVec,
SlashedToken: slashedTokenVec,
Jailed: jailedVec,
Power: powerVec,
}
}

func (m *Metrics) SetBondedToken(valAddr string, bondedToken float64) {
promutil.SafeExec(func() {
m.BondedToken.With(stdprometheus.Labels{
ValidatorLabel: valAddr,
}).Set(bondedToken)
})
}

func (m *Metrics) DeleteBondedToken(valAddr string) {
promutil.SafeExec(func() {
m.BondedToken.Delete(stdprometheus.Labels{
ValidatorLabel: valAddr,
})
})
}

func (m *Metrics) SetLoosenToken(loosenToken float64) {
promutil.SafeExec(func() {
m.LoosenToken.WithLabelValues().Set(loosenToken)
})
}

func (m *Metrics) SetBurnedToken(burnedToken float64) {
promutil.SafeExec(func() {
m.BurnedToken.WithLabelValues().Set(burnedToken)
})
}

func (m *Metrics) SetSlashedToken(valAddr string, slashedToken float64) {
promutil.SafeExec(func() {
m.BondedToken.With(stdprometheus.Labels{
ValidatorLabel: valAddr,
}).Set(slashedToken)
})
}

func (m *Metrics) Jail(valAddr string) {
promutil.SafeExec(func() {
m.Jailed.With(stdprometheus.Labels{
ValidatorLabel: valAddr,
}).Set(1)
})
}

func (m *Metrics) Unjail(valAddr string) {
promutil.SafeExec(func() {
m.Jailed.Delete(stdprometheus.Labels{
ValidatorLabel: valAddr,
})
})
}

func (m *Metrics) SetVotingPower(valAddr string, power float64) {
promutil.SafeExec(func() {
m.BondedToken.With(stdprometheus.Labels{
ValidatorLabel: valAddr,
}).Set(power)
})
}

func NopMetrics() *Metrics {
return &Metrics{
BondedToken: discard.NewGauge(),
LoosenToken: discard.NewGauge(),
BurnedToken: discard.NewGauge(),
SlashedToken: discard.NewCounter(),
Jailed: discard.NewGauge(),
Power: discard.NewGauge(),
BondedToken: &stdprometheus.GaugeVec{},
LoosenToken: &stdprometheus.GaugeVec{},
BurnedToken: &stdprometheus.GaugeVec{},
SlashedToken: &stdprometheus.CounterVec{},
Jailed: &stdprometheus.GaugeVec{},
Power: &stdprometheus.GaugeVec{},
}
}
Loading