Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
48 changes: 27 additions & 21 deletions src/app/application_metrics.def
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,44 @@
*/

/**
* @file application_metrics.def
* @brief Application-level metrics definitions
*
* Format:
* METRIC_GAUGE(field_name,
* "prometheus_metric_name",
* "help_text")
*
* METRIC_GAUGE(field_name,
* "prometheus_metric_name",
* "help_text",
* "label1",
* "label2",
* ...)
* METRIC_GAUGE_LABELS(field_name,
* "prometheus_metric_name",
* "help_text",
* {"label1", "label2", ...})
*
* METRIC_COUNTER(field_name,
* "prometheus_metric_name",
* "help_text")
*
* METRIC_COUNTER_LABELS(field_name,
* "prometheus_metric_name",
* "help_text",
* {"label1", "label2", ...})
*
* Parameters:
* - field_name: Required - C++ member variable name
* - prometheus_metric_name: Required - Prometheus metric name
* - help_text: Required - Metric description
* - labels: Optional - Any number of label names (must be string literals)
* METRIC_HISTOGRAM(field_name,
* "prometheus_metric_name",
* "help_text",
* {bucket1, bucket2, ...})
*
* Notes:
* - All labels MUST be strings (enforced by static_assert at compile time)
* - If you pass a non-string type, you'll get a compile error
* - Usage: metrics_->metric_name({{"label1", value1}, {"label2", value2}})->set(...)
* METRIC_HISTOGRAM_LABELS(field_name,
* "prometheus_metric_name",
* "help_text",
* {bucket1, bucket2, ...},
* {"label1", "label2", ...})
*/

METRIC_GAUGE_LABELS(app_build_info,
"lean_build_info",
"A metric with a constant '1' value labeled by name, version",
"name",
"version")
METRIC_GAUGE_LABELS(
app_build_info,
"lean_build_info",
"A metric with a constant '1' value labeled by name, version",
{"name", "version"})

METRIC_GAUGE(app_process_start_time,
"lean_process_start_time_seconds",
Expand Down
16 changes: 14 additions & 2 deletions src/blockchain/fork_choice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,16 @@ namespace lean {
"Validating attestation for target {}, source {}",
signed_vote.data.target,
signed_vote.data.source);
auto timer = metrics_->fc_attestation_validation_time_seconds()->timer();
auto &vote = signed_vote.data;

// Validate vote targets exist in store
if (not blocks_.contains(vote.source.root)) {
metrics_->fc_attestations_invalid_total()->inc();
return Error::INVALID_ATTESTATION;
}
if (not blocks_.contains(vote.target.root)) {
metrics_->fc_attestations_invalid_total()->inc();
return Error::INVALID_ATTESTATION;
}

Expand All @@ -198,25 +201,31 @@ namespace lean {
auto &target_block = blocks_.at(vote.target.root);

if (source_block.slot > target_block.slot) {
metrics_->fc_attestations_invalid_total()->inc();
return Error::INVALID_ATTESTATION;
}
if (vote.source.slot > vote.target.slot) {
metrics_->fc_attestations_invalid_total()->inc();
return Error::INVALID_ATTESTATION;
}

// Validate checkpoint slots match block slots
if (source_block.slot != vote.source.slot) {
metrics_->fc_attestations_invalid_total()->inc();
return Error::INVALID_ATTESTATION;
}
if (target_block.slot != vote.target.slot) {
metrics_->fc_attestations_invalid_total()->inc();
return Error::INVALID_ATTESTATION;
}

// Validate attestation is not too far in the future
if (vote.slot > getCurrentSlot() + 1) {
metrics_->fc_attestations_invalid_total()->inc();
return Error::INVALID_ATTESTATION;
}

metrics_->fc_attestations_valid_total()->inc();
return outcome::success();
}

Expand Down Expand Up @@ -261,6 +270,7 @@ namespace lean {
}

outcome::result<void> ForkChoiceStore::onBlock(Block block) {
auto timer = metrics_->fc_block_processing_time_seconds()->timer();
block.setHash();
auto block_hash = block.hash();
// If the block is already known, ignore it
Expand Down Expand Up @@ -464,7 +474,8 @@ namespace lean {
qtils::SharedRef<log::LoggingSystem> logging_system,
qtils::SharedRef<metrics::Metrics> metrics,
qtils::SharedRef<ValidatorRegistry> validator_registry)
: validator_registry_(validator_registry),
: stf_(metrics),
validator_registry_(validator_registry),
logger_(
logging_system->getLogger("ForkChoiceStore", "fork_choice_store")),
metrics_(std::move(metrics)) {
Expand Down Expand Up @@ -507,7 +518,8 @@ namespace lean {
Votes latest_new_votes,
ValidatorIndex validator_index,
qtils::SharedRef<ValidatorRegistry> validator_registry)
: time_(now_sec / SECONDS_PER_INTERVAL),
: stf_(metrics),
time_(now_sec / SECONDS_PER_INTERVAL),
logger_(
logging_system->getLogger("ForkChoiceStore", "fork_choice_store")),
config_(config),
Expand Down
56 changes: 41 additions & 15 deletions src/blockchain/fork_choice_metrics.def
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,55 @@
*/

/**
* @file fork_choice_metrics.def
* @brief ForkChoiceStore metrics definitions
*
* Format:
* METRIC_GAUGE(field_name,
* "prometheus_metric_name",
* "help_text")
*
* METRIC_GAUGE(field_name,
* "prometheus_metric_name",
* "help_text",
* "label1",
* "label2",
* ...)
* METRIC_GAUGE_LABELS(field_name,
* "prometheus_metric_name",
* "help_text",
* {"label1", "label2", ...})
*
* METRIC_COUNTER(field_name,
* "prometheus_metric_name",
* "help_text")
*
* Parameters:
* - field_name: Required - C++ member variable name
* - prometheus_metric_name: Required - Prometheus metric name
* - help_text: Required - Metric description
* - labels: Optional - Any number of label names
* METRIC_COUNTER_LABELS(field_name,
* "prometheus_metric_name",
* "help_text",
* {"label1", "label2", ...})
*
* METRIC_HISTOGRAM(field_name,
* "prometheus_metric_name",
* "help_text",
* {bucket1, bucket2, ...})
*
* METRIC_HISTOGRAM_LABELS(field_name,
* "prometheus_metric_name",
* "help_text",
* {bucket1, bucket2, ...},
* {"label1", "label2", ...})
*/

METRIC_GAUGE(fc_head_slot,
"lean_head_slot",
"Latest slot of the beacon chain")
METRIC_GAUGE(fc_head_slot, "lean_head_slot", "Latest slot of the beacon chain")

// Attestation validation
METRIC_COUNTER(fc_attestations_valid_total,
"lean_attestations_valid_total",
"Total number of valid attestations")
METRIC_COUNTER(fc_attestations_invalid_total,
"lean_attestations_invalid_total",
"Total number of invalid attestations")
METRIC_HISTOGRAM(fc_attestation_validation_time_seconds,
"lean_attestation_validation_time_seconds",
"Time taken to validate attestation",
{0.005, 0.01, 0.025, 0.05, 0.1, 1})

// Block processing
METRIC_HISTOGRAM(fc_block_processing_time_seconds,
"lean_fork_choice_block_processing_time_seconds",
"Time taken to process block in fork choice",
{0.005, 0.01, 0.025, 0.05, 0.1, 1})
43 changes: 43 additions & 0 deletions src/blockchain/impl/validator_metrics.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @brief Validator metrics definitions
*
* Format:
* METRIC_GAUGE(field_name,
* "prometheus_metric_name",
* "help_text")
*
* METRIC_GAUGE_LABELS(field_name,
* "prometheus_metric_name",
* "help_text",
* {"label1", "label2", ...})
*
* METRIC_COUNTER(field_name,
* "prometheus_metric_name",
* "help_text")
*
* METRIC_COUNTER_LABELS(field_name,
* "prometheus_metric_name",
* "help_text",
* {"label1", "label2", ...})
*
* METRIC_HISTOGRAM(field_name,
* "prometheus_metric_name",
* "help_text",
* {bucket1, bucket2, ...})
*
* METRIC_HISTOGRAM_LABELS(field_name,
* "prometheus_metric_name",
* "help_text",
* {bucket1, bucket2, ...},
* {"label1", "label2", ...})
*/

METRIC_GAUGE(val_validators_count,
"lean_validators_count",
"Number of connected validators")
9 changes: 9 additions & 0 deletions src/blockchain/impl/validator_registry_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <yaml-cpp/yaml.h>

#include "app/configuration.hpp"
#include "metrics/metrics.hpp"

namespace lean {
enum class ValidatorRegistryError {
Expand Down Expand Up @@ -42,16 +43,20 @@ namespace lean {

ValidatorRegistryImpl::ValidatorRegistryImpl(
qtils::SharedRef<log::LoggingSystem> logging_system,
qtils::SharedRef<metrics::Metrics> metrics,
const app::Configuration &config)
: ValidatorRegistryImpl{std::move(logging_system),
std::move(metrics),
YAML::LoadFile(config.validatorRegistryPath()),
config.nodeId()} {}

ValidatorRegistryImpl::ValidatorRegistryImpl(
qtils::SharedRef<log::LoggingSystem> logging_system,
qtils::SharedRef<metrics::Metrics> metrics,
std::string yaml,
std::string current_node_id)
: ValidatorRegistryImpl{std::move(logging_system),
std::move(metrics),
YAML::Load(yaml),
std::move(current_node_id)} {}

Expand Down Expand Up @@ -83,10 +88,12 @@ namespace lean {

ValidatorRegistryImpl::ValidatorRegistryImpl(
qtils::SharedRef<log::LoggingSystem> logging_system,
qtils::SharedRef<metrics::Metrics> metrics,
YAML::Node root,
std::string current_node_id)
: logger_{logging_system->getLogger("ValidatorRegistry",
"validator_registry")},
metrics_{std::move(metrics)},
current_node_id_{std::move(current_node_id)} {
if (not root.IsDefined() or root.IsNull()) {
SL_WARN(logger_, "Validator registry YAML is empty");
Expand Down Expand Up @@ -161,5 +168,7 @@ namespace lean {
"Validator indices for node '{}' not found in registry YAML",
current_node_id_);
}

metrics_->val_validators_count()->set(current_validator_indices_.size());
}
} // namespace lean
10 changes: 10 additions & 0 deletions src/blockchain/impl/validator_registry_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <boost/di.hpp>
#include <log/logger.hpp>
#include <qtils/shared_ref.hpp>

#include "blockchain/validator_registry.hpp"

Expand All @@ -21,6 +22,10 @@ namespace lean::app {
class Configuration;
} // namespace lean::app

namespace lean::metrics {
class Metrics;
} // namespace lean::metrics

namespace lean {
/**
* ValidatorRegistry provides access to mapping between validator indices and
Expand All @@ -36,10 +41,13 @@ namespace lean {
class ValidatorRegistryImpl : public ValidatorRegistry {
public:
ValidatorRegistryImpl(qtils::SharedRef<log::LoggingSystem> logging_system,
qtils::SharedRef<metrics::Metrics> metrics,
const app::Configuration &config);
BOOST_DI_INJECT_TRAITS(qtils::SharedRef<lean::log::LoggingSystem>,
qtils::SharedRef<lean::metrics::Metrics>,
const lean::app::Configuration &);
ValidatorRegistryImpl(qtils::SharedRef<log::LoggingSystem> logging_system,
qtils::SharedRef<metrics::Metrics> metrics,
std::string yaml,
std::string current_node_id);

Expand All @@ -55,10 +63,12 @@ namespace lean {

private:
ValidatorRegistryImpl(qtils::SharedRef<log::LoggingSystem> logging_system,
qtils::SharedRef<metrics::Metrics> metrics,
YAML::Node root,
std::string current_node_id);

log::Logger logger_;
qtils::SharedRef<metrics::Metrics> metrics_;
std::string current_node_id_;
std::unordered_map<ValidatorIndex, std::string> index_to_node_;
std::unordered_map<std::string, std::vector<ValidatorIndex>>
Expand Down
Loading
Loading