aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/system_metrics.rs
blob: fb3c983c90fd57f69be811830a9f7f7b5e644092 (plain) (blame)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant};

use opentelemetry::{global, metrics::*, KeyValue};

use crate::system::{ClusterHealthStatus, System};

/// TableMetrics reference all counter used for metrics
pub struct SystemMetrics {
	// Static values
	pub(crate) _garage_build_info: ValueObserver<u64>,
	pub(crate) _replication_factor: ValueObserver<u64>,

	// Disk space values from System::local_status
	pub(crate) _disk_avail: ValueObserver<u64>,
	pub(crate) _disk_total: ValueObserver<u64>,

	// Health report from System::health()
	pub(crate) _cluster_healthy: ValueObserver<u64>,
	pub(crate) _cluster_available: ValueObserver<u64>,
	pub(crate) _known_nodes: ValueObserver<u64>,
	pub(crate) _connected_nodes: ValueObserver<u64>,
	pub(crate) _storage_nodes: ValueObserver<u64>,
	pub(crate) _storage_nodes_ok: ValueObserver<u64>,
	pub(crate) _partitions: ValueObserver<u64>,
	pub(crate) _partitions_quorum: ValueObserver<u64>,
	pub(crate) _partitions_all_ok: ValueObserver<u64>,

	// Status report for individual cluster nodes
	pub(crate) _layout_node_connected: ValueObserver<u64>,
	pub(crate) _layout_node_disconnected_time: ValueObserver<u64>,
}

impl SystemMetrics {
	pub fn new(system: Arc<System>) -> Self {
		let meter = global::meter("garage_system");

		let health_cache = RwLock::new((Instant::now(), system.health()));
		let system2 = system.clone();
		let get_health = Arc::new(move || {
			{
				let cache = health_cache.read().unwrap();
				if cache.0 > Instant::now() - Duration::from_secs(1) {
					return cache.1;
				}
			}

			let health = system2.health();
			*health_cache.write().unwrap() = (Instant::now(), health);
			health
		});

		Self {
			// Static values
			_garage_build_info: meter
				.u64_value_observer("garage_build_info", move |observer| {
					observer.observe(
						1,
						&[
							KeyValue::new("rustversion", garage_util::version::rust_version()),
							KeyValue::new("version", garage_util::version::garage_version()),
						],
					)
				})
				.with_description("Garage build info")
				.init(),
			_replication_factor: {
				let replication_factor = system.replication_factor;
				meter
					.u64_value_observer("garage_replication_factor", move |observer| {
						observer.observe(replication_factor as u64, &[])
					})
					.with_description("Garage replication factor setting")
					.init()
			},

			// Disk space values from System::local_status
			_disk_avail: {
				let system = system.clone();
				meter
					.u64_value_observer("garage_local_disk_avail", move |observer| {
						let st = system.local_status.read().unwrap();
						if let Some((avail, _total)) = st.data_disk_avail {
							observer.observe(avail, &[KeyValue::new("volume", "data")]);
						}
						if let Some((avail, _total)) = st.meta_disk_avail {
							observer.observe(avail, &[KeyValue::new("volume", "metadata")]);
						}
					})
					.with_description("Garage available disk space on each node")
					.init()
			},
			_disk_total: {
				let system = system.clone();
				meter
					.u64_value_observer("garage_local_disk_total", move |observer| {
						let st = system.local_status.read().unwrap();
						if let Some((_avail, total)) = st.data_disk_avail {
							observer.observe(total, &[KeyValue::new("volume", "data")]);
						}
						if let Some((_avail, total)) = st.meta_disk_avail {
							observer.observe(total, &[KeyValue::new("volume", "metadata")]);
						}
					})
					.with_description("Garage total disk space on each node")
					.init()
			},

			// Health report from System::()
			_cluster_healthy: {
				let get_health = get_health.clone();
				meter
					.u64_value_observer("cluster_healthy", move |observer| {
						let h = get_health();
						if h.status == ClusterHealthStatus::Healthy {
							observer.observe(1, &[]);
						} else {
							observer.observe(0, &[]);
						}
					})
					.with_description("Whether all storage nodes are connected")
					.init()
			},
			_cluster_available: {
				let get_health = get_health.clone();
				meter.u64_value_observer("cluster_available", move |observer| {
					let h = get_health();
					if h.status != ClusterHealthStatus::Unavailable {
						observer.observe(1, &[]);
					} else {
						observer.observe(0, &[]);
					}
				})
				.with_description("Whether all requests can be served, even if some storage nodes are disconnected")
				.init()
			},
			_known_nodes: {
				let get_health = get_health.clone();
				meter
					.u64_value_observer("cluster_known_nodes", move |observer| {
						let h = get_health();
						observer.observe(h.known_nodes as u64, &[]);
					})
					.with_description("Number of nodes already seen once in the cluster")
					.init()
			},
			_connected_nodes: {
				let get_health = get_health.clone();
				meter
					.u64_value_observer("cluster_connected_nodes", move |observer| {
						let h = get_health();
						observer.observe(h.connected_nodes as u64, &[]);
					})
					.with_description("Number of nodes currently connected")
					.init()
			},
			_storage_nodes: {
				let get_health = get_health.clone();
				meter
					.u64_value_observer("cluster_storage_nodes", move |observer| {
						let h = get_health();
						observer.observe(h.storage_nodes as u64, &[]);
					})
					.with_description("Number of storage nodes declared in the current layout")
					.init()
			},
			_storage_nodes_ok: {
				let get_health = get_health.clone();
				meter
					.u64_value_observer("cluster_storage_nodes_ok", move |observer| {
						let h = get_health();
						observer.observe(h.storage_nodes_ok as u64, &[]);
					})
					.with_description("Number of storage nodes currently connected")
					.init()
			},
			_partitions: {
				let get_health = get_health.clone();
				meter
					.u64_value_observer("cluster_partitions", move |observer| {
						let h = get_health();
						observer.observe(h.partitions as u64, &[]);
					})
					.with_description("Number of partitions in the layout")
					.init()
			},
			_partitions_quorum: {
				let get_health = get_health.clone();
				meter
					.u64_value_observer("cluster_partitions_quorum", move |observer| {
						let h = get_health();
						observer.observe(h.partitions_quorum as u64, &[]);
					})
					.with_description(
						"Number of partitions for which we have a quorum of connected nodes",
					)
					.init()
			},
			_partitions_all_ok: {
				let get_health = get_health.clone();
				meter
					.u64_value_observer("cluster_partitions_all_ok", move |observer| {
						let h = get_health();
						observer.observe(h.partitions_all_ok as u64, &[]);
					})
					.with_description(
						"Number of partitions for which all storage nodes are connected",
					)
					.init()
			},

			// Status report for individual cluster nodes
			_layout_node_connected: {
				let system = system.clone();
				meter
					.u64_value_observer("cluster_layout_node_connected", move |observer| {
						let layout = system.cluster_layout();
						let nodes = system.get_known_nodes();
						for (id, _, config) in layout.current().roles.items().iter() {
							if let Some(role) = &config.0 {
								let mut kv = vec![
									KeyValue::new("id", format!("{:?}", id)),
									KeyValue::new("role_zone", role.zone.clone()),
								];
								match role.capacity {
									Some(cap) => {
										kv.push(KeyValue::new("role_capacity", cap as i64));
										kv.push(KeyValue::new("role_gateway", 0));
									}
									None => {
										kv.push(KeyValue::new("role_gateway", 1));
									}
								}

								let value;
								if let Some(node) = nodes.iter().find(|n| n.id == *id) {
									value = if node.is_up { 1 } else { 0 };
								// TODO: if we add address and hostname, and those change, we
								// get duplicate metrics, due to bad otel aggregation :(
								// Can probably be fixed when we upgrade opentelemetry
								// kv.push(KeyValue::new("address", node.addr.to_string()));
								// kv.push(KeyValue::new(
								// 	"hostname",
								// 	node.status.hostname.clone(),
								// ));
								} else {
									value = 0;
								}

								observer.observe(value, &kv);
							}
						}
					})
					.with_description("Connection status for nodes in the cluster layout")
					.init()
			},
			_layout_node_disconnected_time: {
				let system = system.clone();
				meter
					.u64_value_observer("cluster_layout_node_disconnected_time", move |observer| {
						let layout = system.cluster_layout();
						let nodes = system.get_known_nodes();
						for (id, _, config) in layout.current().roles.items().iter() {
							if let Some(role) = &config.0 {
								let mut kv = vec![
									KeyValue::new("id", format!("{:?}", id)),
									KeyValue::new("role_zone", role.zone.clone()),
								];
								match role.capacity {
									Some(cap) => {
										kv.push(KeyValue::new("role_capacity", cap as i64));
										kv.push(KeyValue::new("role_gateway", 0));
									}
									None => {
										kv.push(KeyValue::new("role_gateway", 1));
									}
								}

								if let Some(node) = nodes.iter().find(|n| n.id == *id) {
									// TODO: see comment above
									// kv.push(KeyValue::new("address", node.addr.to_string()));
									// kv.push(KeyValue::new(
									// 	"hostname",
									// 	node.status.hostname.clone(),
									// ));
									if node.is_up {
										observer.observe(0, &kv);
									} else if let Some(secs) = node.last_seen_secs_ago {
										observer.observe(secs, &kv);
									}
								}
							}
						}
					})
					.with_description(
						"Time (in seconds) since last connection to nodes in the cluster layout",
					)
					.init()
			},
		}
	}
}