1
- use crate :: metrics:: labels:: {
2
- MessageDirectionLabel , MessageLabel , UtpDirectionLabel , UtpOutcomeLabel ,
3
- } ;
4
- use crate :: types:: messages:: { Request , Response } ;
5
1
use prometheus_exporter:: {
6
2
self ,
7
3
prometheus:: {
@@ -10,6 +6,11 @@ use prometheus_exporter::{
10
6
} ,
11
7
} ;
12
8
9
+ use crate :: metrics:: labels:: {
10
+ MessageDirectionLabel , MessageLabel , UtpDirectionLabel , UtpOutcomeLabel ,
11
+ } ;
12
+ use crate :: types:: messages:: { Request , Response } ;
13
+
13
14
/// Contains metrics reporters for use in the overlay network
14
15
/// (eg. `portalnet/src/overlay.rs` & `portalnet/src/overlay_service.rs`).
15
16
/// Metric types reported here include protocol messages, utp transfers,
@@ -63,9 +64,19 @@ impl OverlayMetrics {
63
64
validation_count,
64
65
} )
65
66
}
67
+ }
66
68
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
69
80
. with_label_values ( & [ protocol, direction, message_type] )
70
81
. inc ( ) ;
71
82
}
@@ -77,139 +88,139 @@ impl OverlayMetrics {
77
88
/// Returns the value of the given metric with the specified labels.
78
89
pub fn message_count_by_labels (
79
90
& self ,
80
- protocol : & str ,
81
91
direction : MessageDirectionLabel ,
82
92
message_name : MessageLabel ,
83
93
) -> u64 {
94
+ let protocol: & str = & self . protocol . to_string ( ) ;
84
95
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 ( )
86
100
}
87
101
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 ( ) ) ;
90
104
}
91
105
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 ( ) ) ;
94
108
}
95
109
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 ( ) ) ;
98
112
}
99
113
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 ( ) ) ;
102
116
}
103
117
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 ( ) ;
110
120
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 ( ) ;
112
125
}
113
126
114
127
//
115
128
// uTP metrics
116
129
//
117
130
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 ( ) ;
119
133
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
121
138
}
122
139
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 ( ) ;
129
142
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 ( )
131
147
}
132
148
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 ( ) ;
139
151
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) ;
142
157
}
143
158
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 ( ) ;
145
161
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 ( ) ;
147
166
}
148
167
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 ( ) ;
150
170
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 ( ) ;
152
175
}
153
176
154
177
//
155
178
// Validations
156
179
//
157
180
/// 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 ( ) ;
159
183
let outcome = outcome. to_string ( ) ;
160
184
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 ( )
162
189
}
163
190
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 ( ) ;
165
193
let success = success. to_string ( ) ;
166
194
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 ( ) ;
168
199
}
169
200
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 ) ;
176
204
let inbound_failed_connection = self . utp_outcome_count (
177
- protocol,
178
205
UtpDirectionLabel :: Inbound ,
179
206
UtpOutcomeLabel :: FailedConnection ,
180
207
) ;
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 ) ;
196
214
let outbound_failed_connection = self . utp_outcome_count (
197
- protocol,
198
215
UtpDirectionLabel :: Outbound ,
199
216
UtpOutcomeLabel :: FailedConnection ,
200
217
) ;
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 ) ;
213
224
format ! (
214
225
"(in/out): active={} ({}/{}), success={} ({}/{}), failed={} ({}/{}) \
215
226
failed_connection={} ({}/{}), failed_data_tx={} ({}/{}), failed_shutdown={} ({}/{})",
@@ -239,33 +250,17 @@ impl OverlayMetrics {
239
250
)
240
251
}
241
252
242
- pub fn get_message_summary ( & self , protocol : & str ) -> String {
253
+ pub fn get_message_summary ( & self ) -> String {
243
254
// for every offer you made, how many accepts did you receive
244
255
// 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 ) ;
247
258
format ! (
248
259
"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 ) ,
269
264
successful_validations,
270
265
successful_validations + failed_validations,
271
266
)
0 commit comments