Skip to content

Commit

Permalink
Add sent block time metric (#5738)
Browse files Browse the repository at this point in the history
  • Loading branch information
terencechain authored May 5, 2020
1 parent 1fa495a commit 4958c03
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
23 changes: 23 additions & 0 deletions beacon-chain/blockchain/metrics.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package blockchain

import (
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/roughtime"
)

var (
Expand Down Expand Up @@ -83,6 +87,13 @@ var (
Name: "total_voted_target_balances",
Help: "The total amount of ether, in gwei, that is eligible for voting of previous epoch",
})
sentBlockPropagationHistogram = promauto.NewHistogram(
prometheus.HistogramOpts{
Name: "block_sent_latency_milliseconds",
Help: "Captures blocks broadcast time. Blocks sent in milliseconds distribution",
Buckets: []float64{1000, 2000, 3000, 4000, 5000, 6000},
},
)
)

// reportSlotMetrics reports slot related metrics.
Expand Down Expand Up @@ -183,3 +194,15 @@ func reportEpochMetrics(state *stateTrie.BeaconState) {
totalVotedTargetBalances.Set(float64(precompute.Balances.PrevEpochTargetAttested))
}
}

// This captures metrics for block sent time by subtracts slot start time.
func captureSentTimeMetric(genesisTime uint64, currentSlot uint64) error {
startTime, err := helpers.SlotToTime(genesisTime, currentSlot)
if err != nil {
return err
}
diffMs := roughtime.Now().Sub(startTime) / time.Millisecond
sentBlockPropagationHistogram.Observe(float64(diffMs))

return nil
}
5 changes: 5 additions & 0 deletions beacon-chain/blockchain/receive_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ func (s *Service) ReceiveBlock(ctx context.Context, block *ethpb.SignedBeaconBlo
"blockRoot": hex.EncodeToString(root[:]),
}).Debug("Broadcasting block")

if err := captureSentTimeMetric(uint64(s.genesisTime.Unix()), block.Block.Slot); err != nil {
// If a node fails to capture metric, this shouldn't cause the block processing to fail.
log.Warnf("Could not capture block sent time metric: %v", err)
}

if err := s.ReceiveBlockNoPubsub(ctx, block); err != nil {
return err
}
Expand Down

0 comments on commit 4958c03

Please sign in to comment.