Skip to content

Commit

Permalink
[Telemetry] 1st Major Telemetry Milestone (#135) followsup - Attempt #2
Browse files Browse the repository at this point in the history
… (#159)

## Description
Minor code cleanup and footprint reduction post the first major telemetry milestone in #135.

## Type of change
Please mark the options that are relevant.

- [ ] New feature (non-breaking change which adds functionality)
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Documentation
- [X] Other: Code cleanup

## How Has This Been Tested?

I did a docker wipe to start on a clean slate.

- [X] `make test_all`
- [X] [LocalNet](https://github.com/pokt-network/pocket/blob/main/docs/development/README.md)

## Checklist
- [X] I have performed a self-review of my own code
- [X] I have commented my code, particularly in hard-to-understand areas
- [X] I have tested my changes using the available tooling
- [X] If applicable, I have made corresponding changes to related or global README
- [X] If applicable, I have added added new diagrams using [mermaid.js](https://mermaid-js.github.io)
- [X] If applicable, I have added tests that prove my fix is effective or that my feature works
  • Loading branch information
Olshansk authored Aug 11, 2022
1 parent 7a00c31 commit a5d2f26
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 125 deletions.
69 changes: 19 additions & 50 deletions consensus/hotstuff_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,7 @@ type HotstuffLeaderMessageHandler struct{}
/*** Prepare Step ***/

func (handler *HotstuffLeaderMessageHandler) HandleNewRoundMessage(m *consensusModule, msg *typesCons.HotstuffMessage) {
m.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
consensusTelemetry.CONSENSUS_EVENT_METRICS_NAMESPACE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_NAME,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT, m.CurrentHeight(),
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_NEW_ROUND,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_LEADER,
)
handler.emitTelemetryEvent(m, msg)

if err := handler.anteHandle(m, msg); err != nil {
m.nodeLogError(typesCons.ErrHotstuffValidation.Error(), err)
Expand Down Expand Up @@ -89,16 +80,7 @@ func (handler *HotstuffLeaderMessageHandler) HandleNewRoundMessage(m *consensusM
/*** PreCommit Step ***/

func (handler *HotstuffLeaderMessageHandler) HandlePrepareMessage(m *consensusModule, msg *typesCons.HotstuffMessage) {
m.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
consensusTelemetry.CONSENSUS_EVENT_METRICS_NAMESPACE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_NAME,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT, m.CurrentHeight(),
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_PREPARE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_LEADER,
)
handler.emitTelemetryEvent(m, msg)

if err := handler.anteHandle(m, msg); err != nil {
m.nodeLogError(typesCons.ErrHotstuffValidation.Error(), err)
Expand Down Expand Up @@ -142,16 +124,7 @@ func (handler *HotstuffLeaderMessageHandler) HandlePrepareMessage(m *consensusMo
/*** Commit Step ***/

func (handler *HotstuffLeaderMessageHandler) HandlePrecommitMessage(m *consensusModule, msg *typesCons.HotstuffMessage) {
m.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
consensusTelemetry.CONSENSUS_EVENT_METRICS_NAMESPACE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_NAME,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT, m.CurrentHeight(),
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_PRECOMMIT,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_LEADER,
)
handler.emitTelemetryEvent(m, msg)

if err := handler.anteHandle(m, msg); err != nil {
m.nodeLogError(typesCons.ErrHotstuffValidation.Error(), err)
Expand Down Expand Up @@ -195,16 +168,7 @@ func (handler *HotstuffLeaderMessageHandler) HandlePrecommitMessage(m *consensus
/*** Decide Step ***/

func (handler *HotstuffLeaderMessageHandler) HandleCommitMessage(m *consensusModule, msg *typesCons.HotstuffMessage) {
m.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
consensusTelemetry.CONSENSUS_EVENT_METRICS_NAMESPACE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_NAME,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT, m.CurrentHeight(),
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_COMMIT,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_LEADER,
)
handler.emitTelemetryEvent(m, msg)

if err := handler.anteHandle(m, msg); err != nil {
m.nodeLogError(typesCons.ErrHotstuffValidation.Error(), err)
Expand Down Expand Up @@ -253,16 +217,8 @@ func (handler *HotstuffLeaderMessageHandler) HandleCommitMessage(m *consensusMod
}

func (handler *HotstuffLeaderMessageHandler) HandleDecideMessage(m *consensusModule, msg *typesCons.HotstuffMessage) {
m.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
consensusTelemetry.CONSENSUS_EVENT_METRICS_NAMESPACE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_NAME,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT, m.CurrentHeight(),
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_DECIDE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_LEADER,
)
handler.emitTelemetryEvent(m, msg)

if err := handler.anteHandle(m, msg); err != nil {
m.nodeLogError(typesCons.ErrHotstuffValidation.Error(), err)
return
Expand All @@ -278,6 +234,19 @@ func (handler *HotstuffLeaderMessageHandler) anteHandle(m *consensusModule, msg
return nil
}

func (handler *HotstuffLeaderMessageHandler) emitTelemetryEvent(m *consensusModule, msg *typesCons.HotstuffMessage) {
m.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
consensusTelemetry.CONSENSUS_EVENT_METRICS_NAMESPACE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_NAME,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT, m.CurrentHeight(),
typesCons.StepToString[msg.GetStep()],
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_LEADER,
)
}

// ValidateBasic general validation checks that apply to every HotstuffLeaderMessage
func (handler *HotstuffLeaderMessageHandler) validateBasic(m *consensusModule, msg *typesCons.HotstuffMessage) error {
// Discard messages with invalid partial signatures before storing it in the leader's consensus mempool
Expand Down
70 changes: 20 additions & 50 deletions consensus/hotstuff_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package consensus

import (
"fmt"
"log"

consensusTelemetry "github.com/pokt-network/pocket/consensus/telemetry"
typesCons "github.com/pokt-network/pocket/consensus/types"
Expand All @@ -23,16 +24,7 @@ var (
/*** NewRound Step ***/

func (handler *HotstuffReplicaMessageHandler) HandleNewRoundMessage(m *consensusModule, msg *typesCons.HotstuffMessage) {
m.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
consensusTelemetry.CONSENSUS_EVENT_METRICS_NAMESPACE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_NAME,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT, m.CurrentHeight(),
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_NEW_ROUND,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_REPLICA,
)
handler.emitTelemetryEvent(m, msg)

if err := handler.anteHandle(m, msg); err != nil {
m.nodeLogError(typesCons.ErrHotstuffValidation.Error(), err)
Expand All @@ -46,16 +38,7 @@ func (handler *HotstuffReplicaMessageHandler) HandleNewRoundMessage(m *consensus
/*** Prepare Step ***/

func (handler *HotstuffReplicaMessageHandler) HandlePrepareMessage(m *consensusModule, msg *typesCons.HotstuffMessage) {
m.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
consensusTelemetry.CONSENSUS_EVENT_METRICS_NAMESPACE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_NAME,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT, m.CurrentHeight(),
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_PREPARE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_REPLICA,
)
handler.emitTelemetryEvent(m, msg)

if err := handler.anteHandle(m, msg); err != nil {
m.nodeLogError(typesCons.ErrHotstuffValidation.Error(), err)
Expand Down Expand Up @@ -88,16 +71,7 @@ func (handler *HotstuffReplicaMessageHandler) HandlePrepareMessage(m *consensusM
/*** PreCommit Step ***/

func (handler *HotstuffReplicaMessageHandler) HandlePrecommitMessage(m *consensusModule, msg *typesCons.HotstuffMessage) {
m.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
consensusTelemetry.CONSENSUS_EVENT_METRICS_NAMESPACE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_NAME,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT, m.CurrentHeight(),
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_PRECOMMIT,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_REPLICA,
)
handler.emitTelemetryEvent(m, msg)

if err := handler.anteHandle(m, msg); err != nil {
m.nodeLogError(typesCons.ErrHotstuffValidation.Error(), err)
Expand Down Expand Up @@ -125,16 +99,7 @@ func (handler *HotstuffReplicaMessageHandler) HandlePrecommitMessage(m *consensu
/*** Commit Step ***/

func (handler *HotstuffReplicaMessageHandler) HandleCommitMessage(m *consensusModule, msg *typesCons.HotstuffMessage) {
m.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
consensusTelemetry.CONSENSUS_EVENT_METRICS_NAMESPACE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_NAME,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT, m.CurrentHeight(),
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_COMMIT,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_REPLICA,
)
handler.emitTelemetryEvent(m, msg)

if err := handler.anteHandle(m, msg); err != nil {
m.nodeLogError(typesCons.ErrHotstuffValidation.Error(), err)
Expand Down Expand Up @@ -162,16 +127,7 @@ func (handler *HotstuffReplicaMessageHandler) HandleCommitMessage(m *consensusMo
/*** Decide Step ***/

func (handler *HotstuffReplicaMessageHandler) HandleDecideMessage(m *consensusModule, msg *typesCons.HotstuffMessage) {
m.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
consensusTelemetry.CONSENSUS_EVENT_METRICS_NAMESPACE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_NAME,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT, m.CurrentHeight(),
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_DECIDE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_REPLICA,
)
handler.emitTelemetryEvent(m, msg)

if err := handler.anteHandle(m, msg); err != nil {
m.nodeLogError(typesCons.ErrHotstuffValidation.Error(), err)
Expand All @@ -195,9 +151,23 @@ func (handler *HotstuffReplicaMessageHandler) HandleDecideMessage(m *consensusMo

// anteHandle is the handler called on every replica message before specific handler
func (handler *HotstuffReplicaMessageHandler) anteHandle(m *consensusModule, msg *typesCons.HotstuffMessage) error {
log.Println("TODO: Hotstuff replica ante handle not implemented yet")
return nil
}

func (handler *HotstuffReplicaMessageHandler) emitTelemetryEvent(m *consensusModule, msg *typesCons.HotstuffMessage) {
m.GetBus().
GetTelemetryModule().
GetEventMetricsAgent().
EmitEvent(
consensusTelemetry.CONSENSUS_EVENT_METRICS_NAMESPACE,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_NAME,
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT, m.CurrentHeight(),
typesCons.StepToString[msg.GetStep()],
consensusTelemetry.HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_REPLICA,
)
}

func (m *consensusModule) validateProposal(msg *typesCons.HotstuffMessage) error {
if !(msg.Type == Propose && msg.Step == Prepare) {
return typesCons.ErrProposalNotValidInPrepare
Expand Down
5 changes: 0 additions & 5 deletions consensus/telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ const (

HOTPOKT_MESSAGE_EVENT_METRIC_NAME = "hotpokt_message_event_metric"
HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_HEIGHT = "HEIGHT"
HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_NEW_ROUND = "STEP_NEW_ROUND"
HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_PREPARE = "STEP_PREPARE"
HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_PRECOMMIT = "STEP_PRECOMMIT"
HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_COMMIT = "STEP_COMMIT"
HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_STEP_DECIDE = "STEP_DECIDE"
HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_LEADER = "VALIDATOR_TYPE_LEADER"
HOTPOKT_MESSAGE_EVENT_METRIC_LABEL_VALIDATOR_TYPE_REPLICA = "VALIDATOR_TYPE_REPLICA"
)
8 changes: 6 additions & 2 deletions p2p/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func Create(cfg *config.Config) (m modules.P2PModule, err error) {
}

func (m *p2pModule) SetBus(bus modules.Bus) {
// INVESTIGATE: Can the code flow be modified to set the bus here?
// m.network.SetBus(m.GetBus())
m.bus = bus
}

Expand Down Expand Up @@ -85,6 +87,7 @@ func (m *p2pModule) Start() error {
} else {
m.network = stdnetwork.NewNetwork(addrBook)
}
m.network.SetBus(m.GetBus())

m.network.SetBus(m.GetBus())

Expand All @@ -98,11 +101,12 @@ func (m *p2pModule) Start() error {
go m.handleNetworkMessage(data)
}
}()
m.
GetBus().

m.GetBus().
GetTelemetryModule().
GetTimeSeriesAgent().
CounterIncrement(p2pTelemetry.P2P_NODE_STARTED_TIMESERIES_METRIC_NAME)

return nil
}

Expand Down
12 changes: 9 additions & 3 deletions p2p/raintree/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (n *rainTreeNetwork) HandleNetworkData(data []byte) ([]byte, error) {
EmitEvent(
p2pTelemetry.P2P_EVENT_METRICS_NAMESPACE,
p2pTelemetry.RAINTREE_MESSAGE_EVENT_METRIC_NAME,
"height", blockHeight,
p2pTelemetry.RAINTREE_MESSAGE_EVENT_METRIC_HEIGHT_LABEL, blockHeight,
)

var rainTreeMsg typesP2P.RainTreeMessage
Expand Down Expand Up @@ -188,8 +188,8 @@ func (n *rainTreeNetwork) HandleNetworkData(data []byte) ([]byte, error) {
EmitEvent(
p2pTelemetry.P2P_EVENT_METRICS_NAMESPACE,
p2pTelemetry.BROADCAST_MESSAGE_REDUNDANCY_PER_BLOCK_EVENT_METRIC_NAME,
"hash", hashString,
"height", blockHeight,
p2pTelemetry.RAINTREE_MESSAGE_EVENT_METRIC_HASH_LABEL, hashString,
p2pTelemetry.RAINTREE_MESSAGE_EVENT_METRIC_HEIGHT_LABEL, blockHeight,
)

return nil, nil
Expand Down Expand Up @@ -225,6 +225,11 @@ func (n *rainTreeNetwork) SetBus(bus modules.Bus) {
}

func (n *rainTreeNetwork) GetBus() modules.Bus {
// TODO: Do we need this if?
// if n.bus == nil {
// log.Printf("[WARN] PocketBus is not initialized in rainTreeNetwork")
// return nil
// }
return n.bus
}

Expand All @@ -234,6 +239,7 @@ func getNonce() uint64 {
}

// INVESTIGATE(olshansky): This did not generate a random nonce on every call

// func getNonce() uint64 {
// seed, err := cryptRand.Int(cryptRand.Reader, big.NewInt(math.MaxInt64))
// if err != nil {
Expand Down
1 change: 1 addition & 0 deletions p2p/raintree/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
)

// CLEANUP: Consolidate this with other similar functions in the codebase.
func GetHashStringFromBytes(b []byte) string {
hash := cryptoPocket.SHA3Hash(b)
return hex.EncodeToString(hash)
Expand Down
4 changes: 4 additions & 0 deletions p2p/telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ const (

BROADCAST_MESSAGE_REDUNDANCY_PER_BLOCK_EVENT_METRIC_NAME = "broadcast_message_redundancy_per_block_event_metric"
RAINTREE_MESSAGE_EVENT_METRIC_NAME = "raintree_message_event_metric"

// Attributes
RAINTREE_MESSAGE_EVENT_METRIC_HEIGHT_LABEL = "height"
RAINTREE_MESSAGE_EVENT_METRIC_HASH_LABEL = "hash"
)
1 change: 1 addition & 0 deletions p2p/types/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type AddrBookMap map[string]*NetworkPeer
// can be simplified greatly.
type Network interface {
modules.IntegratableModule

NetworkBroadcast(data []byte) error
NetworkSend(data []byte, address cryptoPocket.Address) error

Expand Down
2 changes: 1 addition & 1 deletion shared/modules/telemetry_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type TimeSeriesAgent interface {
// Register a gauge by name
GaugeRegister(name string, description string)

// Sets the Gauge to an arbitrary value.
// Sets the Gauge to an arbitrary value
GaugeSet(name string, value float64) (prometheus.Gauge, error)

// Increments the Gauge by 1. Use Add to increment it by arbitrary values.
Expand Down
10 changes: 1 addition & 9 deletions shared/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,7 @@ func Create(cfg *config.Config) (n *Node, err error) {
return nil, err
}

bus, err := CreateBus(
persistenceMod,
p2pMod,
utilityMod,
consensusMod,
telemetryMod,
cfg,
)

bus, err := CreateBus(persistenceMod, p2pMod, utilityMod, consensusMod, telemetryMod, cfg)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions shared/telemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,21 @@ _NOTE: Make sure you use `http` and not `https` when developing locally._

![](./docs/browsing-existing-dashboards.gif)


# Defining your own metrics

We follow a specific pattern to define our metrics to guarantee consistency and ease of use.
Metric definitions for each module/domain/service are stored under a new folder called `telemetry` in the module's folder, in a file named `metrics.go`, and they respect the following rules:
Metric definitions for each module/domain/service are stored under a new folder called `telemetry` in the module's folder, in a file named `metrics.go`, and they respect the following rules:

* Every metric's name and description and any additional information about the metric should be defined as a constant.
* Constants relative to a metric's definition follow a naming pattern: `<metric_name>_<metric_type>_<metric_attribute>`
* We keep the actual metric name value open for definition however the developer sees fit.
- Every metric's name and description and any additional information about the metric should be defined as a constant.
- Constants relative to a metric's definition follow a naming pattern: `<metric_name>_<metric_type>_<metric_attribute>`
- We keep the actual metric name value open for definition however the developer sees fit.

For example:

We want to define a metric of type: timeseries, with a name: `nodes_alive_counter`,

The constants definition will be as follows:

```go
// metric_name=NODES_ALIVE_COUNTER
// metric_type=TIME_SERIES
Expand Down

0 comments on commit a5d2f26

Please sign in to comment.