Commit 9401cf02 authored by wesleyac's avatar wesleyac 💬
Browse files

tor-chanmgr: Add metrics.

This adds a structure to initialize metrics when the `ChanMgr` is
created, and adds a counter for the total number of channels built,
broken down by success and failure.
parent 2a502a1d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7012,6 +7012,7 @@ dependencies = [
 "hex-literal",
 "httparse",
 "itertools 0.14.0",
 "metrics",
 "oneshot-fused-workaround",
 "percent-encoding",
 "postage",
+6 −1
Original line number Diff line number Diff line
@@ -42,7 +42,12 @@ full = [
    "web-time-compat/full",
]

metrics = ["dep:metrics-exporter-prometheus"]
# This feature flag enables experimental features that are not supported. Turning it on may
# void your API.
experimental = ["metrics"]

metrics = ["tor-chanmgr/metrics", "dep:metrics-exporter-prometheus", "__is_experimental"]
__is_experimental = []

[dependencies]
anyhow = "1.0.23"
+2 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@ repository = "https://gitlab.torproject.org/tpo/core/arti.git/"

[features]
default = []
experimental = ["experimental-api", "relay", "testing"]
experimental = ["experimental-api", "relay", "metrics", "testing"]
experimental-api = ["__is_experimental"]
full = [
    "pt-client",
@@ -53,6 +53,7 @@ derive_more = { version = "2.0.1", features = ["full"] }
educe = "0.4.22"
futures = "0.3.14"
httparse = "1.10"
metrics = { version = "0.24.1", optional = true }
oneshot-fused-workaround = { path = "../oneshot-fused-workaround", version = "0.6.0" }
percent-encoding = "2.3.1"
postage = { version = "0.5.0", default-features = false, features = ["futures-traits"] }
+10 −1
Original line number Diff line number Diff line
@@ -319,7 +319,16 @@ impl<R: Runtime> ChanMgr<R> {
        src: Sensitive<std::net::SocketAddr>,
        stream: <R as tor_rtcompat::NetStreamProvider>::Stream,
    ) -> Result<Arc<Channel>> {
        self.mgr.handle_incoming(src, stream).await
        let result = self.mgr.handle_incoming(src, stream).await;

        #[cfg(feature = "metrics")]
        if result.is_ok() {
            self.mgr.metrics.channels_built_success.increment(1);
        } else {
            self.mgr.metrics.channels_built_failure.increment(1);
        }

        result
    }

    /// Try to get a suitable channel to the provided `target`,
+31 −0
Original line number Diff line number Diff line
@@ -164,6 +164,22 @@ pub(crate) struct AbstractChanMgr<CF: AbstractChannelFactory> {

    /// The memory quota account that every channel will be a child of
    pub(crate) memquota: ToplevelAccount,

    /// Metrics counters / gauges / histograms.
    #[cfg(feature = "metrics")]
    pub(crate) metrics: ChanMgrMetrics,
}

/// Struct to hold all the metrics counters / gauges / histograms we use.
///
/// We create these and store them in the [`AbstractChanMgr`] in order to avoid
/// the performance hit associated with re-registering counters.
#[cfg(feature = "metrics")]
pub(crate) struct ChanMgrMetrics {
    /// Number of channels successfully built.
    pub(crate) channels_built_success: metrics::Counter,
    /// Number of channels that we tried to build but had an error.
    pub(crate) channels_built_failure: metrics::Counter,
}

/// Type alias for a future that we wait on to see when a pending
@@ -254,6 +270,21 @@ impl<CF: AbstractChannelFactory + Clone> AbstractChanMgr<CF> {
            channels: state::MgrState::new(connector, config, dormancy, netparams),
            reporter,
            memquota,
            #[cfg(feature = "metrics")]
            metrics: ChanMgrMetrics {
                channels_built_success: metrics::counter!(
                    description: "Total number of channels built",
                    unit: metrics::Unit::Count,
                    "arti_chanmgr_channels_built",
                    "result" => "success",
                ),
                channels_built_failure: metrics::counter!(
                    description: "Total number of channels built",
                    unit: metrics::Unit::Count,
                    "arti_chanmgr_channels_built",
                    "result" => "failure",
                ),
            },
        }
    }