|
| 1 | +// Copyright 2021 Protocol Labs. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the "Software"), |
| 5 | +// to deal in the Software without restriction, including without limitation |
| 6 | +// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 | +// and/or sell copies of the Software, and to permit persons to whom the |
| 8 | +// Software is furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 14 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 | +// DEALINGS IN THE SOFTWARE. |
| 20 | + |
| 21 | +use prometheus_client::encoding::text::Encode; |
| 22 | +use prometheus_client::metrics::counter::Counter; |
| 23 | +use prometheus_client::metrics::family::Family; |
| 24 | +use prometheus_client::registry::Registry; |
| 25 | + |
| 26 | +pub struct Metrics { |
| 27 | + events: Family<EventLabels, Counter>, |
| 28 | +} |
| 29 | + |
| 30 | +impl Metrics { |
| 31 | + pub fn new(registry: &mut Registry) -> Self { |
| 32 | + let sub_registry = registry.sub_registry_with_prefix("relay"); |
| 33 | + |
| 34 | + let events = Family::default(); |
| 35 | + sub_registry.register( |
| 36 | + "events", |
| 37 | + "Events emitted by the relay NetworkBehaviour", |
| 38 | + Box::new(events.clone()), |
| 39 | + ); |
| 40 | + |
| 41 | + Self { events } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Debug, Clone, Hash, PartialEq, Eq, Encode)] |
| 46 | +struct EventLabels { |
| 47 | + event: EventType, |
| 48 | +} |
| 49 | + |
| 50 | +#[derive(Debug, Clone, Hash, PartialEq, Eq, Encode)] |
| 51 | +enum EventType { |
| 52 | + InitiateDirectConnectionUpgrade, |
| 53 | + RemoteInitiatedDirectConnectionUpgrade, |
| 54 | + DirectConnectionUpgradeSucceeded, |
| 55 | + DirectConnectionUpgradeFailed, |
| 56 | +} |
| 57 | + |
| 58 | +impl From<&libp2p_dcutr::behaviour::Event> for EventType { |
| 59 | + fn from(event: &libp2p_dcutr::behaviour::Event) -> Self { |
| 60 | + match event { |
| 61 | + libp2p_dcutr::behaviour::Event::InitiatedDirectConnectionUpgrade { |
| 62 | + remote_peer_id: _, |
| 63 | + local_relayed_addr: _, |
| 64 | + } => EventType::InitiateDirectConnectionUpgrade, |
| 65 | + libp2p_dcutr::behaviour::Event::RemoteInitiatedDirectConnectionUpgrade { |
| 66 | + remote_peer_id: _, |
| 67 | + remote_relayed_addr: _, |
| 68 | + } => EventType::RemoteInitiatedDirectConnectionUpgrade, |
| 69 | + libp2p_dcutr::behaviour::Event::DirectConnectionUpgradeSucceeded { |
| 70 | + remote_peer_id: _, |
| 71 | + } => EventType::DirectConnectionUpgradeSucceeded, |
| 72 | + libp2p_dcutr::behaviour::Event::DirectConnectionUpgradeFailed { |
| 73 | + remote_peer_id: _, |
| 74 | + error: _, |
| 75 | + } => EventType::DirectConnectionUpgradeFailed, |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl super::Recorder<libp2p_dcutr::behaviour::Event> for super::Metrics { |
| 81 | + fn record(&self, event: &libp2p_dcutr::behaviour::Event) { |
| 82 | + self.dcutr |
| 83 | + .events |
| 84 | + .get_or_create(&EventLabels { |
| 85 | + event: event.into(), |
| 86 | + }) |
| 87 | + .inc(); |
| 88 | + } |
| 89 | +} |
0 commit comments