Skip to content

metrics: Add additional metrics to p2p and consensus - #2425

Merged
xla merged 13 commits into
tendermint:developfrom
mslipper:issue-2169
Sep 25, 2018
Merged

xla merged 13 commits into
tendermint:developfrom
mslipper:issue-2169

Conversation

@mslipper

@mslipper mslipper commented Sep 18, 2018 •

Copy link
Copy Markdown
Contributor

Partially addresses cosmos/cosmos-sdk#2169.

  • Updated all relevant documentation in docs
  • Updated all code comments where relevant
  • Wrote tests
  • Updated CHANGELOG_PENDING.md

@mslipper

mslipper commented Sep 18, 2018 •

Copy link
Copy Markdown
Contributor Author

Two things I'd like direct feedback on:

  1. It seems like the RPC API uses the value of FastSync to determine if the node is syncing or not. I've followed that model for the metrics. Let me know if this isn't correct.
  2. I'm using a goroutine to poll the Status() method on MConnection to get the state of the send queue, since it seemed expensive to calculate all the time and I didn't want to inject metrics into the MConnection struct.

I've also deliberately avoided implementing the configurable namespacing with this PR - will do that separately so as to ease review.

@xla xla self-assigned this Sep 18, 2018
@xla xla added C:p2p Component: P2P pkg C:consensus Component: Consensus T:observability Type: Observability labels Sep 18, 2018

@melekes melekes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start! I've left a few comments that needs to be addressed though

Comment thread .gitignore Outdated
*.iml

libs/pubsub/query/fuzz_test/output
libs/db/test*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry about this. it should be fixed on develop as of now. please rebase and remove

Comment thread config/metrics.go Outdated
@@ -0,0 +1,3 @@
package config

const MetricsNamespace = "tendermint"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a variable to allow modifications?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was thinking I'd make it configurable in a separate PR that would add support for passed-in namespaces.

Comment thread consensus/metrics.go Outdated
// Total number of transactions.
TotalTxs metrics.Gauge
// The latest block height.
LatestBlockHeight metrics.Gauge

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There will be a confusion with Height. Maybe we should rename it to CommittedHeight?

Comment thread consensus/reactor.go

"github.com/pkg/errors"

amino "github.com/tendermint/go-amino"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do no remove prefix

Comment thread consensus/reactor.go

func (conR *ConsensusReactor) setCatchingUp() {
var catchingUp float64
if conR.fastSync {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Whether or not a node is synced. 0 if syncing, 1 if synced." 1 and 0 mixed up

if you're adding this condition, you need to modify metric name and description to be

"Whether or not a node is fast synced. 1 if fast syncing, 0 if synced or syncing without fast sync."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nice catch.

Comment thread consensus/reactor.go Outdated
// NewConsensusReactor returns a new ConsensusReactor with the given
// consensusState.
func NewConsensusReactor(consensusState *ConsensusState, fastSync bool) *ConsensusReactor {
func NewConsensusReactor(consensusState *ConsensusState, fastSync bool, csMetrics *Metrics) *ConsensusReactor {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metrics should be an option. see consensus/state.go for example


// WithMetrics sets the metrics.
func WithMetrics(metrics *Metrics) CSOption {
	return func(cs *ConsensusState) { cs.metrics = metrics }
}

Comment thread mempool/metrics.go Outdated
prometheus "github.com/go-kit/kit/metrics/prometheus"
"github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/tendermint/tendermint/config"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tmcfg to be consistent or better just cfg

Comment thread p2p/peer.go Outdated
}

p.metrics.PeerPendingSendBytes.With("peer-id", string(p.ID())).Set(sendQueueSize)
case <-p.quitChan:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

peer already has a quit channel: p.Quit()

Comment thread p2p/peer.go Outdated
return err
}

go func() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please extract to a separate function

Comment thread p2p/peer.go Outdated
reactorsByCh map[byte]Reactor,
chDescs []*tmconn.ChannelDescriptor,
onPeerError func(Peer, interface{}),
metrics *Metrics,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, should be an option, not a requirement

@xla xla left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up, definitely a good set of important datapoints for increased introspectability.

While most of the new metrics seem in the right place, the changes to the Peer are awfully intrusive and have time sensistivity and own stateful setup cost. I don't think we are well served adding more stop-gap solutions to that part of the codebase. We would be better served with the conn being an interface and have the actual implementation wrapped with an instrumenting one to keep concerns isolated. Another red flag is the peer-id label, which we have to investigate will not lead to a cardinality explosion as this can be the cause for degraded performance of the monitoring pipeline to the point of it being downright unusable if the values in that label exceed the 10s/100s.

We seem to have introduced a data race as well with this change.

Comment thread CHANGELOG_PENDING.md Outdated

IMPROVEMENTS:

- Added additional metrics to p2p and consensus

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our CHANGELOG format and tense for entries looks like this: `[component] effect in present tense (#issure reference, @contributor)

So I'd propose:

[consensus] Add additional metrics
[p2p] Add addtional metrics

Alternatively expanding what metrics have been added and a reference to all issues concerning those changes.

Comment thread consensus/metrics.go Outdated
prometheus "github.com/go-kit/kit/metrics/prometheus"
"github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
tmcfg "github.com/tendermint/tendermint/config"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect bundling of imports, we follwo this convention:

import (
	// stdlib packages

	// external packages

	// tendermint packages
)

Comment thread consensus/metrics.go Outdated
return &Metrics{
Height: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "consensus",
Namespace: tmcfg.MetricsNamespace,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are trying to move away from our package having a dependency on config, while it's tempting it leaks to much and leads to hard to reason about states. A better approach here would be to have namespace be a param fro the function.

Comment thread consensus/reactor.go Outdated
// NewConsensusReactor returns a new ConsensusReactor with the given
// consensusState.
func NewConsensusReactor(consensusState *ConsensusState, fastSync bool) *ConsensusReactor {
func NewConsensusReactor(consensusState *ConsensusState, fastSync bool, csMetrics *Metrics) *ConsensusReactor {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of requiring it to be passed this would be better served as an option. We employ functional options in a couple of places for this use-case. The default would be the NopMetrics() and only overriden if passed in. For reference:

// SocketPVOption sets an optional parameter on the SocketPV.
type SocketPVOption func(*SocketPV)
// SocketPVAcceptDeadline sets the deadline for the SocketPV listener.
// A zero time value disables the deadline.
func SocketPVAcceptDeadline(deadline time.Duration) SocketPVOption {
return func(sc *SocketPV) { sc.acceptDeadline = deadline }
}

Comment thread mempool/metrics.go Outdated
prometheus "github.com/go-kit/kit/metrics/prometheus"
"github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/tendermint/tendermint/config"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment about import order.

Comment thread mempool/metrics.go Outdated
func PrometheusMetrics() *Metrics {
return &Metrics{
Size: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Namespace: config.MetricsNamespace,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment about dependency on config and passing in of the namespace.

Comment thread node/node.go Outdated
bcReactor := bc.NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync)
bcReactor.SetLogger(logger.With("module", "blockchain"))

csm := cs.WithMetrics(csMetrics)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason to break it out into its own variable?

Comment thread p2p/metrics.go Outdated
prometheus "github.com/go-kit/kit/metrics/prometheus"
"github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"github.com/tendermint/tendermint/config"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment about import order.

Comment thread p2p/metrics.go Outdated
return &Metrics{
Peers: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Subsystem: "p2p",
Namespace: config.MetricsNamespace,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment about depending on config and passing of namespace.

@mslipper
mslipper changed the base branch from master to develop September 19, 2018 03:42
@mslipper

Copy link
Copy Markdown
Contributor Author

@xla @melekes updates in.

@xla - the data race is related to the Status() method. It already has a TODO indicating that atomic should be used: https://github.com/tendermint/tendermint/blob/master/p2p/conn/connection.go#L588. Would you like me to refactor that code to use atomic?

Re: separating the metrics code into an instrumenting interface - I'm happy to do this as part of this PR, but I'm not sure that the overhead of adding metrics directly to the peer is very high. From the discussion that spawned this issue (cosmos/cosmos-sdk#2169), my understanding is that Prometheus metrics are very lightweight and there isn't a common pattern in the codebase yet to expose metrics. The only 'heavy' piece of this implementation is the goroutine that polls a peer's status, but I don't think running a fairly tight loop once every 10 seconds will be a huge performance hit.

Comment thread config/config.go Resolved
Comment thread consensus/metrics.go Outdated
TotalTxs metrics.Gauge
// The latest block height.
CommittedHeight metrics.Gauge
// Whether or not a node is fast synced. 1 if yes, 0 if no.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

synced -> syncing

Comment thread consensus/metrics.go Outdated
FastSyncing: prometheus.NewGaugeFrom(stdprometheus.GaugeOpts{
Namespace: namespace,
Subsystem: MetricsSubsystem,
Name: "catching_up",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast_syncing

Comment thread consensus/metrics.go Outdated
Namespace: namespace,
Subsystem: MetricsSubsystem,
Name: "catching_up",
Help: "Whether or not a node is synced. 0 if syncing, 1 if synced.",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether or not a node is fast syncing. 1 if yes, 0 if no.

Comment thread consensus/reactor.go Outdated
metrics *Metrics
}

type ROption func(*ConsensusReactor)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactorOption is fine too

Comment thread consensus/state.go Outdated
// WithMetrics sets the metrics.
func WithMetrics(metrics *Metrics) CSOption {
// StateWithMetrics sets the metrics.
func StateWithMetrics(metrics *Metrics) CSOption {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StateMetrics

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CSOption -> StateOption

Comment thread p2p/peer.go Outdated
tmconn "github.com/tendermint/tendermint/p2p/conn"
)

const MetricsTickerDuration = 10 * time.Second

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to expose it?

Comment thread p2p/peer.go Outdated
return err
}

go p.metricsCollector()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename to metricsReporter()

Comment thread p2p/peer.go Outdated
return fmt.Sprintf("Peer{%v %v in}", p.mconn, p.ID())
}

func PeerWithMetrics(metrics *Metrics) PeerOption {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PeerMetrics

Comment thread p2p/peer.go Outdated

func PeerWithMetrics(metrics *Metrics) PeerOption {
return func(p *peer) {
if metrics != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why compare?

@melekes melekes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks

@mslipper

Copy link
Copy Markdown
Contributor Author

Cool. I have another PR with a bunch more metrics once this guy is merged :).

@mslipper
mslipper requested a review from zramsay as a code owner September 20, 2018 07:43

@xla xla left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the follow-up!

@xla - the data race is related to the Status() method. It already has a TODO indicating that atomic should be used: https://github.com/tendermint/tendermint/blob/master/p2p/conn/connection.go#L588. Would you like me to refactor that code to use atomic?

That would be really helpful, it seems that the recent changes surfaced it more prominently.

Re: separating the metrics code into an instrumenting interface - I'm happy to do this as part of this PR, but I'm not sure that the overhead of adding metrics directly to the peer is very high. From the discussion that spawned this issue (cosmos/cosmos-sdk#2169), my understanding is that Prometheus metrics are very lightweight and there isn't a common pattern in the codebase yet to expose metrics. The only 'heavy' piece of this implementation is the goroutine that polls a peer's status, but I don't think running a fairly tight loop once every 10 seconds will be a huge performance hit.

You are right, this is outside of the scope of the changes you are working on and better suited for the ongoing p2p refactor, see #2067. My original comment was not in regards to the time complexity of gathering the metrics, that's not a concern. I was referring to the added complexity and interleaving of concerns in our code, instead of aiming for clear separation.

Comment thread consensus/metrics.go
import (
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/metrics/discard"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This newline is meaningful, we group by: stdlib, external packages, internal packages.

Comment thread config/toml.go Outdated
max_open_connections = {{ .Instrumentation.MaxOpenConnections }}

# Instrumentation namespace
namespace = {{ .Instrumentation.Namespace }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to break the integration tests, check circle.

Comment thread p2p/peer.go Outdated

err := p.mconn.Start()
return err
if err != nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be inlined: if err := p.mconn.Start(); err != nil {

Comment thread p2p/peer.go Outdated
ourNodeInfo NodeInfo,
timeout time.Duration,
ourNodeInfo NodeInfo,
timeout time.Duration,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non meaningful whitespace.

Comment thread p2p/peer.go Outdated
reactorsByCh map[byte]Reactor,
chDescs []*tmconn.ChannelDescriptor,
onPeerError func(Peer, interface{}),
config tmconn.MConnConfig,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non meaningful whitespace.

Comment thread p2p/peer.go Outdated
reactorsByCh map[byte]Reactor,
chDescs []*tmconn.ChannelDescriptor,
onPeerError func(Peer, interface{}),
options ...PeerOption,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non meaningful whitespace.

Comment thread p2p/peer.go Outdated
Data *cmn.CMap

metrics *Metrics

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary newline.

@xla
xla removed request for ebuchman and zramsay September 20, 2018 09:11
@xla xla added this to the launch milestone Sep 20, 2018
@codecov-io

Copy link
Copy Markdown

Codecov Report

Merging #2425 into develop will decrease coverage by 0.15%.
The diff coverage is 39.84%.

@@            Coverage Diff             @@
##           develop   #2425      +/-   ##
==========================================
- Coverage    61.56%   61.4%   -0.16%     
==========================================
  Files          197     197              
  Lines        16291   16388      +97     
==========================================
+ Hits         10029   10063      +34     
- Misses        5430    5489      +59     
- Partials       832     836       +4
Impacted Files Coverage Δ
config/toml.go 53.19% <ø> (ø) ⬆️
mempool/metrics.go 27.27% <0%> (-2.73%) ⬇️
p2p/test_util.go 60.15% <0%> (-0.46%) ⬇️
config/config.go 82.67% <100%> (+0.08%) ⬆️
p2p/switch.go 52.49% <100%> (+0.31%) ⬆️
p2p/transport.go 80.37% <100%> (+0.09%) ⬆️
consensus/metrics.go 15.68% <11.9%> (-2.74%) ⬇️
p2p/metrics.go 18.75% <12.5%> (-11.25%) ⬇️
consensus/state.go 77.06% <50%> (+0.02%) ⬆️
p2p/peer.go 59% <66.66%> (+0.35%) ⬆️
... and 6 more

@ebuchman ebuchman modified the milestones: launch, v1.0 Sep 21, 2018
@xla xla changed the title Add additional metrics to p2p and consensus metrics: Add additional metrics to p2p and consensus Sep 25, 2018
@xla

xla commented Sep 25, 2018

Copy link
Copy Markdown
Contributor

@mslipper Thanks for all the ammendments. Could you resolve the CHANGELOG conflict? After that the PR shouldbe good to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C:consensus Component: Consensus C:p2p Component: P2P pkg T:observability Type: Observability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants