Skip to content

Commit

Permalink
Candidate metrics added, but it does not build
Browse files Browse the repository at this point in the history
  • Loading branch information
ErakhtinB committed Oct 31, 2024
1 parent fbbb302 commit 2ae7fa4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 8 deletions.
73 changes: 69 additions & 4 deletions core/consensus/babe/impl/babe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ namespace {
constexpr const char *kIsRelayChainValidator =
"kagome_node_is_active_validator";

constexpr const char *kBackedCandidatesInBlock =
"kagome_node_backed_candidates_in_block";

constexpr const char *kIncludeCandidatesInBlock =
"kagome_node_include_candidates_in_block";

constexpr const char *kNoBackedCandidatesInBlock =
"kagome_node_no_backed_candidates_in_block";

constexpr const char *kNoIncludeCandidatesInBlock =
"kagome_node_no_include_candidates_in_block";

constexpr const char *kNoBackedNoIncludeCandidatesInBlock =
"kagome_node_no_backed_no_include_candidates_in_block";

constexpr const char *kTotalBlocks = "kagome_node_total_blocks";

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
kagome::metrics::HistogramTimer metric_block_proposal_time{
"kagome_proposer_block_constructed",
Expand Down Expand Up @@ -149,6 +166,32 @@ namespace kagome::consensus::babe {
metric_is_relaychain_validator_ =
metrics_registry_->registerGaugeMetric(kIsRelayChainValidator);
metric_is_relaychain_validator_->set(false);

metrics_registry_->registerGaugeFamily(
kBackedCandidatesInBlock, "Number of backed candidates in the block");
metric_backed_candidates_in_block_ =
metrics_registry_->registerGaugeMetric(kBackedCandidatesInBlock);
metric_backed_candidates_in_block_->set(0);

metrics_registry_->registerGaugeFamily(
kIncludeCandidatesInBlock,
"Number of candidates included in the block");
metric_include_candidates_in_block_ =
metrics_registry_->registerGaugeMetric(kIncludeCandidatesInBlock);
metric_include_candidates_in_block_->set(0);

metric_no_backed_candidates_in_block_ =
metrics_registry_->registerCounterMetric(kNoBackedCandidatesInBlock);

metric_no_include_candidates_in_block_ =
metrics_registry_->registerCounterMetric(kNoIncludeCandidatesInBlock);

metric_no_backed_no_include_candidates_in_block_ =
metrics_registry_->registerCounterMetric(
kNoBackedNoIncludeCandidatesInBlock);

metric_total_blocks_ =
metrics_registry_->registerCounterMetric(kTotalBlocks);
}

bool Babe::isGenesisConsensus() const {
Expand Down Expand Up @@ -495,6 +538,11 @@ namespace kagome::consensus::babe {
return BlockProductionError::CAN_NOT_PREPARE_BLOCK;
}

CandidatesMetricInfo candidates{
.backed = parachain_inherent_data.backed_candidates.size(),
.included = parachain_inherent_data.bitfields.size(),
};

auto proposal_start = std::chrono::steady_clock::now();

auto pre_digest_res = makePreDigest();
Expand All @@ -512,7 +560,8 @@ namespace kagome::consensus::babe {
proposal_start,
pre_digest{std::move(pre_digest)},
slot = slot_,
parent = parent_]() mutable {
parent = parent_,
candidates]() mutable {
auto self = wp.lock();
if (not self) {
return;
Expand All @@ -538,12 +587,14 @@ namespace kagome::consensus::babe {
now,
proposal_start,
changes_tracker{std::move(changes_tracker)},
unsealed_block{std::move(unsealed_block)}]() mutable {
unsealed_block{std::move(unsealed_block)},
candidates]() mutable {
auto res =
self->processSlotLeadershipProposed(now,
proposal_start,
std::move(changes_tracker),
std::move(unsealed_block));
std::move(unsealed_block),
candidates);
if (res.has_error()) {
SL_ERROR(self->log_, "Cannot propose a block: {}", res.error());
return;
Expand All @@ -561,7 +612,8 @@ namespace kagome::consensus::babe {
clock::SteadyClock::TimePoint proposal_start,
std::shared_ptr<storage::changes_trie::StorageChangesTrackerImpl>
&&changes_tracker,
primitives::Block &&block) {
primitives::Block &&block,
std::optional<CandidatesMetricInfo> candidates_metrics) {
auto duration_ms =
metric_block_proposal_time.observe(proposal_start).count();
SL_DEBUG(log_, "Block has been built in {} ms", duration_ms);
Expand Down Expand Up @@ -648,6 +700,19 @@ namespace kagome::consensus::babe {
}
}

if (candidates_metrics) {
if (candidates_metrics->backed == 0 and candidates_metrics->included == 0) {
metric_no_backed_no_include_candidates_in_block_->inc();
} else if (candidates_metrics->backed == 0) {
metric_no_backed_candidates_in_block_->inc();
} else if (candidates_metrics->included == 0) {
metric_no_include_candidates_in_block_->inc();
}
metric_backed_candidates_in_block_->set(candidates_metrics->backed);
metric_include_candidates_in_block_->set(candidates_metrics->included);
}
metric_total_blocks_->inc();

return outcome::success();
}

Expand Down
15 changes: 14 additions & 1 deletion core/consensus/babe/impl/babe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ namespace kagome::consensus::babe {
std::shared_ptr<crypto::Sr25519Keypair> keypair;
};

struct CandidatesMetricInfo {
size_t backed;
size_t included;
};

Babe(
application::AppStateManager &app_state_manager,
const application::AppConfiguration &app_config,
Expand Down Expand Up @@ -167,7 +172,9 @@ namespace kagome::consensus::babe {
clock::SteadyClock::TimePoint proposal_start,
std::shared_ptr<storage::changes_trie::StorageChangesTrackerImpl>
&&changes_tracker,
primitives::Block &&block);
primitives::Block &&block,
std::optional<CandidatesMetricInfo> candidates_metrics =
std::nullopt) = 0;

private:
log::Logger log_;
Expand Down Expand Up @@ -211,6 +218,12 @@ namespace kagome::consensus::babe {
// Metrics
metrics::RegistryPtr metrics_registry_ = metrics::createRegistry();
metrics::Gauge *metric_is_relaychain_validator_;
metrics::Gauge *metric_backed_candidates_in_block_;
metrics::Gauge *metric_include_candidates_in_block_;
metrics::Counter *metric_no_backed_candidates_in_block_;
metrics::Counter *metric_no_include_candidates_in_block_;
metrics::Counter *metric_no_backed_no_include_candidates_in_block_;
metrics::Counter *metric_total_blocks_;

telemetry::Telemetry telemetry_; // telemetry
};
Expand Down
10 changes: 7 additions & 3 deletions test/core/consensus/babe/babe_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,13 @@ class BabeWrapper : public Babe {
kagome::clock::SteadyClock::TimePoint proposal_start,
std::shared_ptr<kagome::storage::changes_trie::StorageChangesTrackerImpl>
&&changes_tracker,
kagome::primitives::Block &&block) override {
auto res = Babe::processSlotLeadershipProposed(
now, proposal_start, std::move(changes_tracker), std::move(block));
kagome::primitives::Block &&block,
std::optional<CandidatesMetricInfo> candidates_metrics) override {
auto res = Babe::processSlotLeadershipProposed(now,
proposal_start,
std::move(changes_tracker),
std::move(block),
candidates_metrics);
if (on_proposed) {
on_proposed();
}
Expand Down

0 comments on commit 2ae7fa4

Please sign in to comment.