Skip to content

Commit

Permalink
Rework archive metrics according to best practices (#1071)
Browse files Browse the repository at this point in the history
* Rework archive metrics according to best practices

This PR improves the archive metrics to better follow
Prometheus best practices.

* Fix test

* Use updated metrics encoder

Also incorporate more recommendations from
platform ops.
  • Loading branch information
Frederik Rothenberger authored Dec 5, 2022
1 parent 5aec2e5 commit 58a6336
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 77 deletions.
73 changes: 33 additions & 40 deletions src/archive/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use metrics_encoder::MetricsEncoder;
use serde_bytes::ByteBuf;
use std::borrow::Cow;
use std::cell::RefCell;
use std::time::Duration;

#[cfg(test)]
mod anchor_index_key_tests;
Expand Down Expand Up @@ -421,56 +422,48 @@ fn http_request(req: HttpRequest) -> HttpResponse {
fn encode_metrics(w: &mut MetricsEncoder<Vec<u8>>) -> std::io::Result<()> {
with_config(|config| {
w.encode_gauge(
"ii_archive_last_upgrade_timestamp",
config.last_upgrade_timestamp as f64,
"ii_archive_last_upgrade_timestamp_seconds",
Duration::from_nanos(config.last_upgrade_timestamp).as_secs_f64(),
"Timestamp of the last upgrade of this canister.",
)
})?;
with_log(|log| {
w.encode_gauge(
"ii_archive_log_entries_count",
log.len() as f64,
"Number of log entries stored in this canister.",
)?;
w.encode_gauge(
"ii_archive_log_entries_size",
log.log_size_bytes() as f64,
"Total size of all logged entries in bytes, not counting data structure overhead.",
)?;
w.encode_gauge(
"ii_archive_log_index_memory_size",
log.index_size_bytes() as f64,
"Total size of the log index in bytes.",
)?;
w.encode_gauge(
"ii_archive_log_data_memory_size",
log.data_size_bytes() as f64,
"Total size of the log data in bytes.",
)
})?;
with_anchor_index_mut(|index| {
w.encode_gauge(
"ii_archive_anchor_index_entries_count",
index.len() as f64,
"Number of entries in the anchor index.",
)
with_anchor_index_mut(|index| {
w.gauge_vec(
"ii_archive_entries_count",
"Number of log entries stored in this canister.",
)
.unwrap()
.value(&[("source", "log")], log.len() as f64)
.unwrap()
.value(&[("source", "anchor_index")], index.len() as f64)
})?;
w.gauge_vec("ii_archive_log_bytes", "Size of log data in bytes.")
.unwrap()
.value(&[("type", "entries")], log.data_size_bytes() as f64)
.unwrap()
.value(&[("type", "index")], log.index_size_bytes() as f64)
})?;
MEMORY_MANAGER.with(|cell| {
let manager = cell.borrow();
w.encode_gauge(
"ii_archive_log_index_virtual_memory_size",
w.gauge_vec(
"ii_archive_virtual_memory_pages",
"Number of allocated virtual memory pages.",
)
.unwrap()
.value(
&[("kind", "log_index")],
manager.get(LOG_INDEX_MEMORY_ID).size() as f64,
"Number of stable memory pages allocated to the log index virtual memory.",
)?;
w.encode_gauge(
"ii_archive_log_data_virtual_memory_size",
)
.unwrap()
.value(
&[("kind", "log_data")],
manager.get(LOG_DATA_MEMORY_ID).size() as f64,
"Number of stable memory pages allocated to the log data virtual memory.",
)?;
w.encode_gauge(
"ii_archive_anchor_index_virtual_memory_size",
)
.unwrap()
.value(
&[("kind", "anchor_index")],
manager.get(ANCHOR_ACCESS_INDEX_MEMORY_ID).size() as f64,
"Number of stable memory pages allocated to the anchor index virtual memory.",
)
})?;
w.encode_gauge(
Expand Down
62 changes: 25 additions & 37 deletions src/archive/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ mod write_tests {
// assert logs have been written without decoding entries
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_log_entries_count",
"ii_archive_entries_count{source=\"log\"}",
2,
);
Ok(())
Expand Down Expand Up @@ -504,15 +504,14 @@ mod metrics_tests {
#[test]
fn should_return_metrics() -> Result<(), CallError> {
let metrics = vec![
"ii_archive_last_upgrade_timestamp",
"ii_archive_log_entries_count",
"ii_archive_log_entries_size",
"ii_archive_log_index_memory_size",
"ii_archive_log_data_memory_size",
"ii_archive_anchor_index_entries_count",
"ii_archive_log_index_virtual_memory_size",
"ii_archive_log_data_virtual_memory_size",
"ii_archive_anchor_index_virtual_memory_size",
"ii_archive_last_upgrade_timestamp_seconds",
"ii_archive_entries_count{source=\"log\"}",
"ii_archive_entries_count{source=\"anchor_index\"}",
"ii_archive_log_bytes{type=\"entries\"}",
"ii_archive_log_bytes{type=\"index\"}",
"ii_archive_virtual_memory_pages{kind=\"log_index\"}",
"ii_archive_virtual_memory_pages{kind=\"log_data\"}",
"ii_archive_virtual_memory_pages{kind=\"anchor_index\"}",
"ii_archive_stable_memory_pages",
];
let env = StateMachine::new();
Expand All @@ -539,11 +538,11 @@ mod metrics_tests {
let canister_id = install_archive_canister(&env, ARCHIVE_WASM.clone());
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_last_upgrade_timestamp",
"ii_archive_last_upgrade_timestamp_seconds",
env.time()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos() as u64,
.as_secs() as u64,
);
println!("{}", get_metrics(&env, canister_id));

Expand All @@ -553,11 +552,11 @@ mod metrics_tests {
println!("{}", get_metrics(&env, canister_id));
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_last_upgrade_timestamp",
"ii_archive_last_upgrade_timestamp_seconds",
env.time()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_nanos() as u64,
.as_secs() as u64,
);
Ok(())
}
Expand All @@ -566,8 +565,8 @@ mod metrics_tests {
#[test]
fn should_update_log_entries_count() -> Result<(), CallError> {
let metrics = vec![
"ii_archive_log_entries_count",
"ii_archive_anchor_index_entries_count",
"ii_archive_entries_count{source=\"log\"}",
"ii_archive_entries_count{source=\"anchor_index\"}",
];

let env = StateMachine::new();
Expand Down Expand Up @@ -602,19 +601,14 @@ mod metrics_tests {

assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_log_index_memory_size",
"ii_archive_log_bytes{type=\"index\"}",
INDEX_OVERHEAD, // empty index
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_log_data_memory_size",
"ii_archive_log_bytes{type=\"entries\"}",
DATA_OVERHEAD, // empty log
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_log_entries_size",
0, // size of actual entries
);

let entry = candid::encode_one(log_entry_1()).expect("failed to encode entry");
let entry_size = entry.len() as u64;
Expand All @@ -629,20 +623,14 @@ mod metrics_tests {

assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_log_index_memory_size",
"ii_archive_log_bytes{type=\"index\"}",
INDEX_OVERHEAD + 8, // 8 bytes per entry
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_log_data_memory_size",
"ii_archive_log_bytes{type=\"entries\"}",
DATA_OVERHEAD + entry_size,
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_log_entries_size",
entry_size, // size of actual entries
);

Ok(())
}

Expand All @@ -657,17 +645,17 @@ mod metrics_tests {

assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_log_index_virtual_memory_size",
"ii_archive_virtual_memory_pages{kind=\"log_index\"}",
1,
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_log_data_virtual_memory_size",
"ii_archive_virtual_memory_pages{kind=\"log_data\"}",
1,
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_anchor_index_virtual_memory_size",
"ii_archive_virtual_memory_pages{kind=\"anchor_index\"}",
1,
);
assert_metric(
Expand All @@ -687,17 +675,17 @@ mod metrics_tests {

assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_log_index_virtual_memory_size",
"ii_archive_virtual_memory_pages{kind=\"log_index\"}",
1, // does not change because the index additions are small
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_log_data_virtual_memory_size",
"ii_archive_virtual_memory_pages{kind=\"log_data\"}",
2,
);
assert_metric(
&get_metrics(&env, canister_id),
"ii_archive_anchor_index_virtual_memory_size",
"ii_archive_virtual_memory_pages{kind=\"anchor_index\"}",
1, // does not change because the index additions are small
);
assert_metric(
Expand Down
1 change: 1 addition & 0 deletions src/canister_tests/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ frame-ancestors 'none';$"
}

pub fn parse_metric(body: &str, metric: &str) -> (u64, SystemTime) {
let metric = metric.replace("{", "\\{").replace("}", "\\}");
let metric_capture = Regex::new(&format!("(?m)^{} (\\d+) (\\d+)$", metric))
.unwrap()
.captures(body)
Expand Down

0 comments on commit 58a6336

Please sign in to comment.