1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! [Prometheus] metrics.
//!
//! [Prometheus]: https://prometheus.io/
use prometrics::metrics::{Counter, Gauge, MetricBuilder};
/// [`ConnectionPool`] metrics.
///
/// [`ConnectionPool`]: ../connection/struct.ConnectionPool.html
#[derive(Debug, Clone)]
pub struct ConnectionPoolMetrics {
pub(crate) max_pool_size: Gauge,
// allocated
pub(crate) allocated_connections: Counter,
// released
pub(crate) closed_connections: Counter,
pub(crate) connect_failed_connections: Counter,
pub(crate) request_failed_connections: Counter,
pub(crate) expired_connections: Counter,
pub(crate) kicked_out_connections: Counter,
// lent
pub(crate) lent_connections: Counter,
// returned
pub(crate) returned_connections: Counter,
// error
pub(crate) no_available_connection_errors: Counter,
}
impl ConnectionPoolMetrics {
/// Maximum number of pooled connections.
///
/// Metric: `fibers_http_client_connection_pool_max_pool_size <GAUGE>`
pub fn max_pool_size(&self) -> usize {
self.max_pool_size.value() as usize
}
/// Current number of pooled connections.
///
/// This includes the connections that being used by clients.
///
/// Metric: `sum(fibers_http_client_connection_allocated_connections_total) - sum(fibers_http_client_connection_released_connections_total)`
pub fn pool_size(&self) -> usize {
let released = self.closed_connections()
+ self.connect_failed_connections()
+ self.request_failed_connections()
+ self.expired_connections()
+ self.kicked_out_connections();
let allocated = self.allocated_connections();
allocated.saturating_sub(released) as usize
}
/// Number of connections allocated by the pool.
///
/// Metric: `fibers_http_client_connection_allocated_connections_total <COUNTER>`
pub fn allocated_connections(&self) -> u64 {
self.allocated_connections.value() as u64
}
/// Number of connections released from the pool due to TCP closure.
///
/// Metric: `fibers_http_client_connection_released_connections_total { reason="closed" } <COUNTER>`
pub fn closed_connections(&self) -> u64 {
self.closed_connections.value() as u64
}
/// Number of connections released from the pool due to TCP connect error.
///
/// Metric: `fibers_http_client_connection_released_connections_total { reason="connect_failed" } <COUNTER>`
pub fn connect_failed_connections(&self) -> u64 {
self.connect_failed_connections.value() as u64
}
/// Number of connections released from the pool due to HTTP request error.
///
/// Metric: `fibers_http_client_connection_released_connections_total { reason="request_failed" } <COUNTER>`
pub fn request_failed_connections(&self) -> u64 {
self.request_failed_connections.value() as u64
}
/// Number of connections released from the pool due to keepalive expiration.
///
/// Metric: `fibers_http_client_connection_released_connections_total { reason="expired" } <COUNTER>`
pub fn expired_connections(&self) -> u64 {
self.expired_connections.value() as u64
}
/// Number of connections kicked out from the pool.
///
/// Metric: `fibers_http_client_connection_released_connections_total { reason="kicked_out" } <COUNTER>`
pub fn kicked_out_connections(&self) -> u64 {
self.kicked_out_connections.value() as u64
}
/// Number of connections lent to clients.
///
/// Metric: `fibers_http_client_connection_lent_connections_total <COUNTER>`
pub fn lent_connections(&self) -> u64 {
self.lent_connections.value() as u64
}
/// Number of connections returned from clients.
///
/// Metric: `fibers_http_client_connection_returned_connections_total <COUNTER>`
pub fn returned_connections(&self) -> u64 {
self.returned_connections.value() as u64
}
/// Currently used connections by clients.
///
/// Metric: `fibers_http_client_connection_lent_connections_total - fibers_http_client_connection_returned_connections_total`
pub fn in_use_connetions(&self) -> u64 {
self.lent_connections()
.saturating_sub(self.returned_connections())
}
/// Number of connection acquisition failures.
///
/// Metric: `fibers_http_client_connection_errors_total { reason="no_available_connection" } <COUNTER>`
pub fn no_available_connection_errors(&self) -> u64 {
self.no_available_connection_errors.value() as u64
}
pub(crate) fn new(mut builder: MetricBuilder) -> Self {
builder
.namespace("fibers_http_client")
.subsystem("connection_pool");
ConnectionPoolMetrics {
max_pool_size: builder
.gauge("max_pool_size")
.help("Maximum number of pooled connections")
.finish()
.expect("never fails"),
allocated_connections: builder
.counter("allocated_connections_total")
.help("Number of connections allocated by pools so far")
.finish()
.expect("never fails"),
closed_connections: builder
.counter("released_connections_total")
.help("Number of connections released from pools so far")
.label("reason", "closed")
.finish()
.expect("never fails"),
connect_failed_connections: builder
.counter("released_connections_total")
.help("Number of connections released from pools so far")
.label("reason", "connect_failed")
.finish()
.expect("never fails"),
request_failed_connections: builder
.counter("released_connections_total")
.help("Number of connections released from pools so far")
.label("reason", "request_failed")
.finish()
.expect("never fails"),
expired_connections: builder
.counter("released_connections_total")
.help("Number of connections released from pools so far")
.label("reason", "expired")
.finish()
.expect("never fails"),
kicked_out_connections: builder
.counter("released_connections_total")
.help("Number of connections released from pools so far")
.label("reason", "kicked_out")
.finish()
.expect("never fails"),
lent_connections: builder
.counter("lent_connections_total")
.help("Number of connections lent to clients so far")
.finish()
.expect("never fails"),
returned_connections: builder
.counter("returned_connections_total")
.help("Number of connections returned from clients so far")
.finish()
.expect("never fails"),
no_available_connection_errors: builder
.counter("no_available_connection_errors_total")
.help("Number of errors")
.label("reason", "no_available_connection")
.finish()
.expect("never fails"),
}
}
}