libp2p_metrics/
relay.rs

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
21use prometheus_client::{
22    encoding::{EncodeLabelSet, EncodeLabelValue},
23    metrics::{counter::Counter, family::Family},
24    registry::Registry,
25};
26
27pub(crate) struct Metrics {
28    events: Family<EventLabels, Counter>,
29}
30
31impl Metrics {
32    pub(crate) fn new(registry: &mut Registry) -> Self {
33        let sub_registry = registry.sub_registry_with_prefix("relay");
34
35        let events = Family::default();
36        sub_registry.register(
37            "events",
38            "Events emitted by the relay NetworkBehaviour",
39            events.clone(),
40        );
41
42        Self { events }
43    }
44}
45
46#[derive(Debug, Clone, Hash, PartialEq, Eq, EncodeLabelSet)]
47struct EventLabels {
48    event: EventType,
49}
50
51#[derive(Debug, Clone, Hash, PartialEq, Eq, EncodeLabelValue)]
52enum EventType {
53    ReservationReqAccepted,
54    ReservationReqAcceptFailed,
55    ReservationReqDenied,
56    ReservationReqDenyFailed,
57    ReservationClosed,
58    ReservationTimedOut,
59    CircuitReqDenied,
60    CircuitReqDenyFailed,
61    CircuitReqOutboundConnectFailed,
62    CircuitReqAccepted,
63    CircuitReqAcceptFailed,
64    CircuitClosed,
65}
66
67impl From<&libp2p_relay::Event> for EventType {
68    fn from(event: &libp2p_relay::Event) -> Self {
69        match event {
70            libp2p_relay::Event::ReservationReqAccepted { .. } => EventType::ReservationReqAccepted,
71            #[allow(deprecated)]
72            libp2p_relay::Event::ReservationReqAcceptFailed { .. } => {
73                EventType::ReservationReqAcceptFailed
74            }
75            libp2p_relay::Event::ReservationReqDenied { .. } => EventType::ReservationReqDenied,
76            #[allow(deprecated)]
77            libp2p_relay::Event::ReservationReqDenyFailed { .. } => {
78                EventType::ReservationReqDenyFailed
79            }
80            libp2p_relay::Event::ReservationClosed { .. } => EventType::ReservationClosed,
81            libp2p_relay::Event::ReservationTimedOut { .. } => EventType::ReservationTimedOut,
82            libp2p_relay::Event::CircuitReqDenied { .. } => EventType::CircuitReqDenied,
83            #[allow(deprecated)]
84            libp2p_relay::Event::CircuitReqOutboundConnectFailed { .. } => {
85                EventType::CircuitReqOutboundConnectFailed
86            }
87            #[allow(deprecated)]
88            libp2p_relay::Event::CircuitReqDenyFailed { .. } => EventType::CircuitReqDenyFailed,
89            libp2p_relay::Event::CircuitReqAccepted { .. } => EventType::CircuitReqAccepted,
90            #[allow(deprecated)]
91            libp2p_relay::Event::CircuitReqAcceptFailed { .. } => EventType::CircuitReqAcceptFailed,
92            libp2p_relay::Event::CircuitClosed { .. } => EventType::CircuitClosed,
93        }
94    }
95}
96
97impl super::Recorder<libp2p_relay::Event> for Metrics {
98    fn record(&self, event: &libp2p_relay::Event) {
99        self.events
100            .get_or_create(&EventLabels {
101                event: event.into(),
102            })
103            .inc();
104    }
105}