Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audi metrics #2333

Merged
merged 6 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions core/authority_discovery/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_library(address_publisher
target_link_libraries(address_publisher
authority_discovery_proto
logger
metrics
p2p::p2p_kademlia
scale_libp2p_types
sha
Expand Down
41 changes: 41 additions & 0 deletions core/authority_discovery/metrics.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/

#pragma once

#include "metrics/metrics.hpp"

namespace kagome::authority_discovery {
struct MetricDhtEventReceived {
static auto &get() {
static MetricDhtEventReceived self;
return self;
}

static metrics::Counter *make(const std::string &label) {
auto name = "kagome_authority_discovery_dht_event_received";
auto help = "Number of dht events received by authority discovery.";
static auto registry = [&] {
auto registry = metrics::createRegistry();
registry->registerCounterFamily(name, help);
return registry;
}();
return registry->registerCounterMetric(name, {{"name", label}});
}

void getResult(bool found) {
(found ? value_found : value_not_found)->inc();
}
void putResult(bool ok) {
(ok ? value_put : value_put_failed)->inc();
}

metrics::Counter *value_found = make("value_found");
metrics::Counter *value_not_found = make("value_not_found");
metrics::Counter *value_put = make("value_put");
metrics::Counter *value_put_failed = make("value_put_failed");
};
} // namespace kagome::authority_discovery
13 changes: 12 additions & 1 deletion core/authority_discovery/publisher/address_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

#include "authority_discovery/protobuf/authority_discovery.v2.pb.h"

#include "authority_discovery/metrics.hpp"
#include "authority_discovery/timestamp.hpp"
#include "crypto/sha/sha256.hpp"
#include "metrics/histogram_timer.hpp"

#define _PB_SPAN(f) \
[&](::kagome::common::BufferView a) { (f)(a.data(), a.size()); }
Expand All @@ -27,6 +29,12 @@ namespace kagome::authority_discovery {
constexpr std::chrono::seconds kIntervalInitial{2};
constexpr std::chrono::hours kIntervalMax{1};

static const metrics::GaugeHelper metric_amount_addresses_last_published{
"kagome_authority_discovery_amount_external_addresses_last_published",
"Number of external addresses published when authority discovery last "
"published addresses.",
};

AddressPublisher::AddressPublisher(
std::shared_ptr<runtime::AuthorityDiscoveryApi> authority_discovery_api,
network::Roles roles,
Expand Down Expand Up @@ -126,7 +134,9 @@ namespace kagome::authority_discovery {
peer_info,
*audi_key,
std::chrono::system_clock::now().time_since_epoch()));
return kademlia_->putValue(std::move(raw.first), std::move(raw.second));
auto r = kademlia_->putValue(std::move(raw.first), std::move(raw.second));
MetricDhtEventReceived::get().putResult(r.has_value());
return r;
}

outcome::result<std::pair<Buffer, Buffer>> audiEncode(
Expand All @@ -149,6 +159,7 @@ namespace kagome::authority_discovery {
OUTCOME_TRY(address2, libp2p::multi::Multiaddress::create(s));
addresses.emplace(std::move(address2));
}
metric_amount_addresses_last_published->set(addresses.size());
::authority_discovery_v3::AuthorityRecord record;
for (const auto &address : addresses) {
PB_SPAN_ADD(record, addresses, address.getBytesAddress());
Expand Down
25 changes: 25 additions & 0 deletions core/authority_discovery/query/query_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

#include "authority_discovery/query/query_impl.hpp"

#include "authority_discovery/metrics.hpp"
#include "authority_discovery/protobuf/authority_discovery.v2.pb.h"
#include "common/buffer_view.hpp"
#include "common/bytestr.hpp"
#include "crypto/sha/sha256.hpp"
#include "metrics/histogram_timer.hpp"
#include "network/impl/protocols/parachain.hpp"
#include "utils/retain_if.hpp"

Expand All @@ -35,6 +37,19 @@ namespace kagome::authority_discovery {
constexpr std::chrono::seconds kIntervalInitial{2};
constexpr std::chrono::minutes kIntervalMax{10};

static const metrics::GaugeHelper metric_requests_pending{
"kagome_authority_discovery_authority_address_requests_pending",
"Number of pending authority address requests.",
};
static metrics::CounterHelper metric_handle_value_found_event_failure{
"kagome_authority_discovery_handle_value_found_event_failure",
"Number of times handling a dht value found event failed.",
};
static const metrics::GaugeHelper metric_known_authorities_count{
"kagome_authority_discovery_known_authorities_count",
"Number of authorities known by authority discovery.",
};

QueryImpl::QueryImpl(
std::shared_ptr<application::AppStateManager> app_state_manager,
std::shared_ptr<blockchain::BlockTree> block_tree,
Expand Down Expand Up @@ -122,6 +137,7 @@ namespace kagome::authority_discovery {
auto r = add(*id, value);
if (not r) {
SL_DEBUG(log_, "Can't add: {}", r.error());
metric_handle_value_found_event_failure->inc();
}
return r;
}
Expand Down Expand Up @@ -165,15 +181,18 @@ namespace kagome::authority_discovery {
retain_if(authorities, [&](const primitives::AuthorityDiscoveryId &id) {
return not has(local_keys, id);
});
size_t known_authorities_count = 0;
// remove outdated authorities
audi_store_->retainIf([&](const primitives::AuthorityDiscoveryId &id,
const AuthorityPeerInfo &info) {
if (has(authorities, id)) {
++known_authorities_count;
return true;
}
validation_protocol_.get()->reserve(info.peer.id, false);
return false;
});
metric_known_authorities_count->set(known_authorities_count);
for (auto it = peer_to_auth_cache_.begin();
it != peer_to_auth_cache_.end();) {
if (has(authorities, it->second)) {
Expand All @@ -196,6 +215,7 @@ namespace kagome::authority_discovery {
}
}
}
metric_requests_pending->set(queue_.size());

pop();
return outcome::success();
Expand All @@ -220,6 +240,7 @@ namespace kagome::authority_discovery {
++active_;
auto authority = queue_.back();
queue_.pop_back();
metric_requests_pending->set(queue_.size());

scheduler_->schedule([wp{weak_from_this()},
hash = common::Buffer{crypto::sha256(authority)},
Expand All @@ -230,6 +251,7 @@ namespace kagome::authority_discovery {
std::ignore = self->kademlia_.get()->getValue(
hash, [=](const outcome::result<std::vector<uint8_t>> &res) {
if (auto self = wp.lock()) {
MetricDhtEventReceived::get().getResult(res.has_value());
std::unique_lock lock{self->mutex_};
--self->active_;
self->pop();
Expand Down Expand Up @@ -340,6 +362,9 @@ namespace kagome::authority_discovery {
peer.id, peer.addresses, libp2p::peer::ttl::kDay);

peer_to_auth_cache_.insert_or_assign(peer.id, authority);
if (not audi_store_->contains(authority)) {
metric_known_authorities_count->inc();
}
audi_store_->store(
authority,
AuthorityPeerInfo{
Expand Down
14 changes: 14 additions & 0 deletions core/metrics/histogram_timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ namespace kagome::metrics {
metrics::Gauge *metric_;
};

struct CounterHelper {
CounterHelper(const std::string &name, const std::string &help) {
registry_->registerCounterFamily(name, help);
metric_ = registry_->registerCounterMetric(name);
}

auto *operator->() const {
return metric_;
}

metrics::RegistryPtr registry_ = metrics::createRegistry();
metrics::Counter *metric_;
};

struct HistogramHelper {
HistogramHelper(const std::string &name,
const std::string &help,
Expand Down
Loading