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

chore(metrics): Upgrade to prometheus-client v0.19.0 #3207

Merged
merged 10 commits into from Jan 3, 2023
2 changes: 1 addition & 1 deletion misc/metrics/Cargo.toml
Expand Up @@ -26,7 +26,7 @@ libp2p-kad = { version = "0.43.0", path = "../../protocols/kad", optional = true
libp2p-ping = { version = "0.42.0", path = "../../protocols/ping", optional = true }
libp2p-relay = { version = "0.15.0", path = "../../protocols/relay", optional = true }
libp2p-swarm = { version = "0.42.0", path = "../../swarm" }
prometheus-client = "0.18.0"
prometheus-client = "0.19.0-alpha"

[target.'cfg(not(target_os = "unknown"))'.dependencies]
libp2p-gossipsub = { version = "0.44.0", path = "../../protocols/gossipsub", optional = true }
Expand Down
8 changes: 4 additions & 4 deletions misc/metrics/src/dcutr.rs
Expand Up @@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use prometheus_client::encoding::text::Encode;
use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue};
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::registry::Registry;
Expand All @@ -35,19 +35,19 @@ impl Metrics {
sub_registry.register(
"events",
"Events emitted by the relay NetworkBehaviour",
Box::new(events.clone()),
events.clone(),
);

Self { events }
}
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, Encode)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, EncodeLabelSet)]
struct EventLabels {
event: EventType,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, Encode)]
#[derive(Debug, Clone, Hash, PartialEq, Eq, EncodeLabelValue)]
enum EventType {
InitiateDirectConnectionUpgrade,
RemoteInitiatedDirectConnectionUpgrade,
Expand Down
6 changes: 1 addition & 5 deletions misc/metrics/src/gossipsub.rs
Expand Up @@ -30,11 +30,7 @@ impl Metrics {
let sub_registry = registry.sub_registry_with_prefix("gossipsub");

let messages = Counter::default();
sub_registry.register(
"messages",
"Number of messages received",
Box::new(messages.clone()),
);
sub_registry.register("messages", "Number of messages received", messages.clone());

Self { messages }
}
Expand Down
33 changes: 15 additions & 18 deletions misc/metrics/src/identify.rs
Expand Up @@ -20,7 +20,7 @@

use crate::protocol_stack;
use libp2p_core::PeerId;
use prometheus_client::encoding::text::{Encode, EncodeMetric, Encoder};
use prometheus_client::encoding::{EncodeLabelSet, EncodeMetric, MetricEncoder};
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
Expand Down Expand Up @@ -51,30 +51,30 @@ impl Metrics {
"Number of connected nodes supporting a specific protocol, with \
\"unrecognized\" for each peer supporting one or more unrecognized \
protocols",
Box::new(protocols.clone()),
protocols.clone(),
);

let error = Counter::default();
sub_registry.register(
"errors",
"Number of errors while attempting to identify the remote",
Box::new(error.clone()),
error.clone(),
);

let pushed = Counter::default();
sub_registry.register(
"pushed",
"Number of times identification information of the local node has \
been actively pushed to a peer.",
Box::new(pushed.clone()),
pushed.clone(),
);

let received = Counter::default();
sub_registry.register(
"received",
"Number of times identification information has been received from \
a peer",
Box::new(received.clone()),
received.clone(),
);

let received_info_listen_addrs =
Expand All @@ -83,7 +83,7 @@ impl Metrics {
"received_info_listen_addrs",
"Number of listen addresses for remote peer received in \
identification information",
Box::new(received_info_listen_addrs.clone()),
received_info_listen_addrs.clone(),
);

let received_info_protocols =
Expand All @@ -92,22 +92,22 @@ impl Metrics {
"received_info_protocols",
"Number of protocols supported by the remote peer received in \
identification information",
Box::new(received_info_protocols.clone()),
received_info_protocols.clone(),
);

let sent = Counter::default();
sub_registry.register(
"sent",
"Number of times identification information of the local node has \
been sent to a peer in response to an identification request",
Box::new(sent.clone()),
sent.clone(),
);

let listen_addresses = Family::default();
sub_registry.register(
"listen_addresses",
"Number of listen addresses for remote peer per protocol stack",
Box::new(listen_addresses.clone()),
listen_addresses.clone(),
);

Self {
Expand Down Expand Up @@ -208,12 +208,12 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
}
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
struct AddressLabels {
protocols: String,
}

#[derive(Default, Clone)]
#[derive(Default, Clone, Debug)]
struct Protocols {
peers: Arc<Mutex<HashMap<PeerId, Vec<String>>>>,
}
Expand All @@ -235,14 +235,14 @@ impl Protocols {
}

impl EncodeMetric for Protocols {
fn encode(&self, mut encoder: Encoder) -> Result<(), std::io::Error> {
fn encode(&self, mut encoder: MetricEncoder) -> Result<(), std::fmt::Error> {
let count_by_protocol = self
.peers
.lock()
.expect("Lock not to be poisoned")
.iter()
.fold(
HashMap::<String, u64>::default(),
HashMap::<String, i64>::default(),
|mut acc, (_, protocols)| {
for protocol in protocols {
let count = acc.entry(protocol.to_string()).or_default();
Expand All @@ -254,11 +254,8 @@ impl EncodeMetric for Protocols {

for (protocol, count) in count_by_protocol {
encoder
.with_label_set(&("protocol", protocol))
.no_suffix()?
.no_bucket()?
.encode_value(count)?
.no_exemplar()?;
.encode_family(&[("protocol", protocol)])?
.encode_gauge(&count)?;
}

Ok(())
Expand Down
50 changes: 25 additions & 25 deletions misc/metrics/src/kad.rs
Expand Up @@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use prometheus_client::encoding::text::Encode;
use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue};
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::histogram::{exponential_buckets, Histogram};
Expand Down Expand Up @@ -52,66 +52,66 @@ impl Metrics {
sub_registry.register(
"query_result_get_record_ok",
"Number of records returned by a successful Kademlia get record query.",
Box::new(query_result_get_record_ok.clone()),
query_result_get_record_ok.clone(),
);

let query_result_get_record_error = Family::default();
sub_registry.register(
"query_result_get_record_error",
"Number of failed Kademlia get record queries.",
Box::new(query_result_get_record_error.clone()),
query_result_get_record_error.clone(),
);

let query_result_get_closest_peers_ok = Histogram::new(exponential_buckets(1.0, 2.0, 10));
sub_registry.register(
"query_result_get_closest_peers_ok",
"Number of closest peers returned by a successful Kademlia get closest peers query.",
Box::new(query_result_get_closest_peers_ok.clone()),
query_result_get_closest_peers_ok.clone(),
);

let query_result_get_closest_peers_error = Family::default();
sub_registry.register(
"query_result_get_closest_peers_error",
"Number of failed Kademlia get closest peers queries.",
Box::new(query_result_get_closest_peers_error.clone()),
query_result_get_closest_peers_error.clone(),
);

let query_result_get_providers_ok = Histogram::new(exponential_buckets(1.0, 2.0, 10));
sub_registry.register(
"query_result_get_providers_ok",
"Number of providers returned by a successful Kademlia get providers query.",
Box::new(query_result_get_providers_ok.clone()),
query_result_get_providers_ok.clone(),
);

let query_result_get_providers_error = Family::default();
sub_registry.register(
"query_result_get_providers_error",
"Number of failed Kademlia get providers queries.",
Box::new(query_result_get_providers_error.clone()),
query_result_get_providers_error.clone(),
);

let query_result_num_requests: Family<_, _> =
Family::new_with_constructor(|| Histogram::new(exponential_buckets(1.0, 2.0, 10)));
sub_registry.register(
"query_result_num_requests",
"Number of requests started for a Kademlia query.",
Box::new(query_result_num_requests.clone()),
query_result_num_requests.clone(),
);

let query_result_num_success: Family<_, _> =
Family::new_with_constructor(|| Histogram::new(exponential_buckets(1.0, 2.0, 10)));
sub_registry.register(
"query_result_num_success",
"Number of successful requests of a Kademlia query.",
Box::new(query_result_num_success.clone()),
query_result_num_success.clone(),
);

let query_result_num_failure: Family<_, _> =
Family::new_with_constructor(|| Histogram::new(exponential_buckets(1.0, 2.0, 10)));
sub_registry.register(
"query_result_num_failure",
"Number of failed requests of a Kademlia query.",
Box::new(query_result_num_failure.clone()),
query_result_num_failure.clone(),
);

let query_result_duration: Family<_, _> =
Expand All @@ -120,21 +120,21 @@ impl Metrics {
"query_result_duration",
"Duration of a Kademlia query.",
Unit::Seconds,
Box::new(query_result_duration.clone()),
query_result_duration.clone(),
);

let routing_updated = Family::default();
sub_registry.register(
"routing_updated",
"Number of peers added, updated or evicted to, in or from a specific kbucket in the routing table",
Box::new(routing_updated.clone()),
routing_updated.clone(),
);

let inbound_requests = Family::default();
sub_registry.register(
"inbound_requests",
"Number of inbound requests",
Box::new(inbound_requests.clone()),
inbound_requests.clone(),
);

Self {
Expand Down Expand Up @@ -258,12 +258,12 @@ impl super::Recorder<libp2p_kad::KademliaEvent> for Metrics {
}
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
struct QueryResult {
r#type: QueryType,
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelValue, Hash, Clone, Eq, PartialEq, Debug)]
enum QueryType {
Bootstrap,
GetClosestPeers,
Expand Down Expand Up @@ -306,12 +306,12 @@ impl From<&libp2p_kad::QueryResult> for QueryResult {
}
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
struct GetRecordResult {
error: GetRecordError,
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelValue, Hash, Clone, Eq, PartialEq, Debug)]
enum GetRecordError {
NotFound,
QuorumFailed,
Expand All @@ -334,12 +334,12 @@ impl From<&libp2p_kad::GetRecordError> for GetRecordResult {
}
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
struct GetClosestPeersResult {
error: GetClosestPeersError,
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelValue, Hash, Clone, Eq, PartialEq, Debug)]
enum GetClosestPeersError {
Timeout,
}
Expand All @@ -354,12 +354,12 @@ impl From<&libp2p_kad::GetClosestPeersError> for GetClosestPeersResult {
}
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
struct GetProvidersResult {
error: GetProvidersError,
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelValue, Hash, Clone, Eq, PartialEq, Debug)]
enum GetProvidersError {
Timeout,
}
Expand All @@ -374,20 +374,20 @@ impl From<&libp2p_kad::GetProvidersError> for GetProvidersResult {
}
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
struct RoutingUpdated {
action: RoutingAction,
bucket: u32,
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelValue, Hash, Clone, Eq, PartialEq, Debug)]
enum RoutingAction {
Added,
Updated,
Evicted,
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
struct InboundRequest {
request: Request,
}
Expand All @@ -406,7 +406,7 @@ impl From<&libp2p_kad::InboundRequest> for InboundRequest {
}
}

#[derive(Encode, Hash, Clone, Eq, PartialEq)]
#[derive(EncodeLabelValue, Hash, Clone, Eq, PartialEq, Debug)]
enum Request {
FindNode,
GetProvider,
Expand Down