Skip to content

Commit 03d41eb

Browse files
committed
Add MetricsReporter type for per-subnetwork metrics reporting
1 parent 58ad19a commit 03d41eb

File tree

5 files changed

+231
-259
lines changed

5 files changed

+231
-259
lines changed

portalnet/src/metrics/overlay.rs

Lines changed: 101 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
use crate::metrics::labels::{
2-
MessageDirectionLabel, MessageLabel, UtpDirectionLabel, UtpOutcomeLabel,
3-
};
4-
use crate::types::messages::{Request, Response};
51
use prometheus_exporter::{
62
self,
73
prometheus::{
@@ -10,6 +6,11 @@ use prometheus_exporter::{
106
},
117
};
128

9+
use crate::metrics::labels::{
10+
MessageDirectionLabel, MessageLabel, UtpDirectionLabel, UtpOutcomeLabel,
11+
};
12+
use crate::types::messages::{Request, Response};
13+
1314
/// Contains metrics reporters for use in the overlay network
1415
/// (eg. `portalnet/src/overlay.rs` & `portalnet/src/overlay_service.rs`).
1516
/// Metric types reported here include protocol messages, utp transfers,
@@ -63,9 +64,19 @@ impl OverlayMetrics {
6364
validation_count,
6465
})
6566
}
67+
}
6668

67-
pub fn message_total(&self, protocol: &str, direction: &str, message_type: &str) {
68-
self.message_count
69+
#[derive(Clone)]
70+
pub struct OverlayMetricsReporter {
71+
pub protocol: String,
72+
pub overlay_metrics: OverlayMetrics,
73+
}
74+
75+
impl OverlayMetricsReporter {
76+
pub fn message_total(&self, direction: &str, message_type: &str) {
77+
let protocol: &str = &self.protocol.to_string();
78+
self.overlay_metrics
79+
.message_count
6980
.with_label_values(&[protocol, direction, message_type])
7081
.inc();
7182
}
@@ -77,139 +88,139 @@ impl OverlayMetrics {
7788
/// Returns the value of the given metric with the specified labels.
7889
pub fn message_count_by_labels(
7990
&self,
80-
protocol: &str,
8191
direction: MessageDirectionLabel,
8292
message_name: MessageLabel,
8393
) -> u64 {
94+
let protocol: &str = &self.protocol.to_string();
8495
let labels: [&str; 3] = [protocol, direction.into(), message_name.into()];
85-
self.message_count.with_label_values(&labels).get()
96+
self.overlay_metrics
97+
.message_count
98+
.with_label_values(&labels)
99+
.get()
86100
}
87101

88-
pub fn report_outbound_request(&self, protocol: &str, request: &Request) {
89-
self.increment_message_count(protocol, MessageDirectionLabel::Sent, request.into());
102+
pub fn report_outbound_request(&self, request: &Request) {
103+
self.increment_message_count(MessageDirectionLabel::Sent, request.into());
90104
}
91105

92-
pub fn report_inbound_request(&self, protocol: &str, request: &Request) {
93-
self.increment_message_count(protocol, MessageDirectionLabel::Received, request.into());
106+
pub fn report_inbound_request(&self, request: &Request) {
107+
self.increment_message_count(MessageDirectionLabel::Received, request.into());
94108
}
95109

96-
pub fn report_outbound_response(&self, protocol: &str, response: &Response) {
97-
self.increment_message_count(protocol, MessageDirectionLabel::Sent, response.into());
110+
pub fn report_outbound_response(&self, response: &Response) {
111+
self.increment_message_count(MessageDirectionLabel::Sent, response.into());
98112
}
99113

100-
pub fn report_inbound_response(&self, protocol: &str, response: &Response) {
101-
self.increment_message_count(protocol, MessageDirectionLabel::Received, response.into());
114+
pub fn report_inbound_response(&self, response: &Response) {
115+
self.increment_message_count(MessageDirectionLabel::Received, response.into());
102116
}
103117

104-
fn increment_message_count(
105-
&self,
106-
protocol: &str,
107-
direction: MessageDirectionLabel,
108-
message: MessageLabel,
109-
) {
118+
fn increment_message_count(&self, direction: MessageDirectionLabel, message: MessageLabel) {
119+
let protocol: &str = &self.protocol.to_string();
110120
let labels: [&str; 3] = [protocol, direction.into(), message.into()];
111-
self.message_count.with_label_values(&labels).inc();
121+
self.overlay_metrics
122+
.message_count
123+
.with_label_values(&labels)
124+
.inc();
112125
}
113126

114127
//
115128
// uTP metrics
116129
//
117130

118-
fn utp_active_count(&self, protocol: &str, direction: UtpDirectionLabel) -> u64 {
131+
fn utp_active_count(&self, direction: UtpDirectionLabel) -> u64 {
132+
let protocol: &str = &self.protocol.to_string();
119133
let labels: [&str; 2] = [protocol, direction.into()];
120-
self.utp_active_gauge.with_label_values(&labels).get() as u64
134+
self.overlay_metrics
135+
.utp_active_gauge
136+
.with_label_values(&labels)
137+
.get() as u64
121138
}
122139

123-
fn utp_outcome_count(
124-
&self,
125-
protocol: &str,
126-
direction: UtpDirectionLabel,
127-
outcome: UtpOutcomeLabel,
128-
) -> u64 {
140+
fn utp_outcome_count(&self, direction: UtpDirectionLabel, outcome: UtpOutcomeLabel) -> u64 {
141+
let protocol: &str = &self.protocol.to_string();
129142
let labels: [&str; 3] = [protocol, direction.into(), outcome.into()];
130-
self.utp_outcome_count.with_label_values(&labels).get()
143+
self.overlay_metrics
144+
.utp_outcome_count
145+
.with_label_values(&labels)
146+
.get()
131147
}
132148

133-
pub fn report_utp_outcome(
134-
&self,
135-
protocol: &str,
136-
direction: UtpDirectionLabel,
137-
outcome: UtpOutcomeLabel,
138-
) {
149+
pub fn report_utp_outcome(&self, direction: UtpDirectionLabel, outcome: UtpOutcomeLabel) {
150+
let protocol: &str = &self.protocol.to_string();
139151
let labels: [&str; 3] = [protocol, direction.into(), outcome.into()];
140-
self.utp_outcome_count.with_label_values(&labels).inc();
141-
self.report_utp_active_dec(protocol, direction);
152+
self.overlay_metrics
153+
.utp_outcome_count
154+
.with_label_values(&labels)
155+
.inc();
156+
self.report_utp_active_dec(direction);
142157
}
143158

144-
pub fn report_utp_active_inc(&self, protocol: &str, direction: UtpDirectionLabel) {
159+
pub fn report_utp_active_inc(&self, direction: UtpDirectionLabel) {
160+
let protocol: &str = &self.protocol.to_string();
145161
let labels: [&str; 2] = [protocol, direction.into()];
146-
self.utp_active_gauge.with_label_values(&labels).inc();
162+
self.overlay_metrics
163+
.utp_active_gauge
164+
.with_label_values(&labels)
165+
.inc();
147166
}
148167

149-
pub fn report_utp_active_dec(&self, protocol: &str, direction: UtpDirectionLabel) {
168+
pub fn report_utp_active_dec(&self, direction: UtpDirectionLabel) {
169+
let protocol: &str = &self.protocol.to_string();
150170
let labels: [&str; 2] = [protocol, direction.into()];
151-
self.utp_active_gauge.with_label_values(&labels).dec();
171+
self.overlay_metrics
172+
.utp_active_gauge
173+
.with_label_values(&labels)
174+
.dec();
152175
}
153176

154177
//
155178
// Validations
156179
//
157180
/// Returns the value of the given metric with the specified labels.
158-
pub fn validation_count_by_outcome(&self, protocol: &str, outcome: bool) -> u64 {
181+
pub fn validation_count_by_outcome(&self, outcome: bool) -> u64 {
182+
let protocol: &str = &self.protocol.to_string();
159183
let outcome = outcome.to_string();
160184
let labels: [&str; 2] = [protocol, outcome.as_str()];
161-
self.validation_count.with_label_values(&labels).get()
185+
self.overlay_metrics
186+
.validation_count
187+
.with_label_values(&labels)
188+
.get()
162189
}
163190

164-
pub fn report_validation(&self, protocol: &str, success: bool) {
191+
pub fn report_validation(&self, success: bool) {
192+
let protocol: &str = &self.protocol.to_string();
165193
let success = success.to_string();
166194
let labels: [&str; 2] = [protocol, success.as_str()];
167-
self.validation_count.with_label_values(&labels).inc();
195+
self.overlay_metrics
196+
.validation_count
197+
.with_label_values(&labels)
198+
.inc();
168199
}
169200

170-
pub fn get_utp_summary(&self, protocol: &str) -> String {
171-
let inbound_success = self.utp_outcome_count(
172-
protocol,
173-
UtpDirectionLabel::Inbound,
174-
UtpOutcomeLabel::Success,
175-
);
201+
pub fn get_utp_summary(&self) -> String {
202+
let inbound_success =
203+
self.utp_outcome_count(UtpDirectionLabel::Inbound, UtpOutcomeLabel::Success);
176204
let inbound_failed_connection = self.utp_outcome_count(
177-
protocol,
178205
UtpDirectionLabel::Inbound,
179206
UtpOutcomeLabel::FailedConnection,
180207
);
181-
let inbound_failed_data_tx = self.utp_outcome_count(
182-
protocol,
183-
UtpDirectionLabel::Inbound,
184-
UtpOutcomeLabel::FailedDataTx,
185-
);
186-
let inbound_failed_shutdown = self.utp_outcome_count(
187-
protocol,
188-
UtpDirectionLabel::Inbound,
189-
UtpOutcomeLabel::FailedShutdown,
190-
);
191-
let outbound_success = self.utp_outcome_count(
192-
protocol,
193-
UtpDirectionLabel::Outbound,
194-
UtpOutcomeLabel::Success,
195-
);
208+
let inbound_failed_data_tx =
209+
self.utp_outcome_count(UtpDirectionLabel::Inbound, UtpOutcomeLabel::FailedDataTx);
210+
let inbound_failed_shutdown =
211+
self.utp_outcome_count(UtpDirectionLabel::Inbound, UtpOutcomeLabel::FailedShutdown);
212+
let outbound_success =
213+
self.utp_outcome_count(UtpDirectionLabel::Outbound, UtpOutcomeLabel::Success);
196214
let outbound_failed_connection = self.utp_outcome_count(
197-
protocol,
198215
UtpDirectionLabel::Outbound,
199216
UtpOutcomeLabel::FailedConnection,
200217
);
201-
let outbound_failed_data_tx = self.utp_outcome_count(
202-
protocol,
203-
UtpDirectionLabel::Outbound,
204-
UtpOutcomeLabel::FailedDataTx,
205-
);
206-
let outbound_failed_shutdown = self.utp_outcome_count(
207-
protocol,
208-
UtpDirectionLabel::Outbound,
209-
UtpOutcomeLabel::FailedShutdown,
210-
);
211-
let active_inbound = self.utp_active_count(protocol, UtpDirectionLabel::Inbound);
212-
let active_outbound = self.utp_active_count(protocol, UtpDirectionLabel::Outbound);
218+
let outbound_failed_data_tx =
219+
self.utp_outcome_count(UtpDirectionLabel::Outbound, UtpOutcomeLabel::FailedDataTx);
220+
let outbound_failed_shutdown =
221+
self.utp_outcome_count(UtpDirectionLabel::Outbound, UtpOutcomeLabel::FailedShutdown);
222+
let active_inbound = self.utp_active_count(UtpDirectionLabel::Inbound);
223+
let active_outbound = self.utp_active_count(UtpDirectionLabel::Outbound);
213224
format!(
214225
"(in/out): active={} ({}/{}), success={} ({}/{}), failed={} ({}/{}) \
215226
failed_connection={} ({}/{}), failed_data_tx={} ({}/{}), failed_shutdown={} ({}/{})",
@@ -239,33 +250,17 @@ impl OverlayMetrics {
239250
)
240251
}
241252

242-
pub fn get_message_summary(&self, protocol: &str) -> String {
253+
pub fn get_message_summary(&self) -> String {
243254
// for every offer you made, how many accepts did you receive
244255
// for every offer you received, how many accepts did you make
245-
let successful_validations = self.validation_count_by_outcome(protocol, true);
246-
let failed_validations = self.validation_count_by_outcome(protocol, false);
256+
let successful_validations = self.validation_count_by_outcome(true);
257+
let failed_validations = self.validation_count_by_outcome(false);
247258
format!(
248259
"offers={}/{}, accepts={}/{}, validations={}/{}",
249-
self.message_count_by_labels(
250-
protocol,
251-
MessageDirectionLabel::Received,
252-
MessageLabel::Accept
253-
),
254-
self.message_count_by_labels(
255-
protocol,
256-
MessageDirectionLabel::Sent,
257-
MessageLabel::Offer
258-
),
259-
self.message_count_by_labels(
260-
protocol,
261-
MessageDirectionLabel::Sent,
262-
MessageLabel::Accept
263-
),
264-
self.message_count_by_labels(
265-
protocol,
266-
MessageDirectionLabel::Received,
267-
MessageLabel::Offer
268-
),
260+
self.message_count_by_labels(MessageDirectionLabel::Received, MessageLabel::Accept),
261+
self.message_count_by_labels(MessageDirectionLabel::Sent, MessageLabel::Offer),
262+
self.message_count_by_labels(MessageDirectionLabel::Sent, MessageLabel::Accept),
263+
self.message_count_by_labels(MessageDirectionLabel::Received, MessageLabel::Offer),
269264
successful_validations,
270265
successful_validations + failed_validations,
271266
)

0 commit comments

Comments
 (0)