2021-11-16 08:59:39 -05:00
|
|
|
// Copyright 2021 Protocol Labs.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2022-02-03 11:31:41 +01:00
|
|
|
use prometheus_client::metrics::counter::Counter;
|
|
|
|
use prometheus_client::registry::Registry;
|
2021-11-16 08:59:39 -05:00
|
|
|
|
2023-04-26 09:31:56 +02:00
|
|
|
pub(crate) struct Metrics {
|
2021-11-16 08:59:39 -05:00
|
|
|
messages: Counter,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Metrics {
|
2023-04-26 09:31:56 +02:00
|
|
|
pub(crate) fn new(registry: &mut Registry) -> Self {
|
2021-11-16 08:59:39 -05:00
|
|
|
let sub_registry = registry.sub_registry_with_prefix("gossipsub");
|
|
|
|
|
|
|
|
let messages = Counter::default();
|
2023-01-03 20:42:32 +01:00
|
|
|
sub_registry.register("messages", "Number of messages received", messages.clone());
|
2021-11-16 08:59:39 -05:00
|
|
|
|
|
|
|
Self { messages }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 05:44:04 +01:00
|
|
|
impl super::Recorder<libp2p_gossipsub::Event> for Metrics {
|
|
|
|
fn record(&self, event: &libp2p_gossipsub::Event) {
|
|
|
|
if let libp2p_gossipsub::Event::Message { .. } = event {
|
2022-07-15 09:16:03 +02:00
|
|
|
self.messages.inc();
|
2021-11-16 08:59:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|