sieve: add metrics

This commit is contained in:
Astro 2023-10-15 23:30:37 +02:00
parent c26469b778
commit f4dbad281c
5 changed files with 44 additions and 10 deletions

3
Cargo.lock generated
View File

@ -461,6 +461,9 @@ dependencies = [
"cave", "cave",
"futures", "futures",
"http", "http",
"metrics",
"metrics-exporter-prometheus",
"metrics-util",
"redis", "redis",
"reqwest", "reqwest",
"serde", "serde",

View File

@ -16,3 +16,6 @@ redis = { version = "0.23", features = ["tokio-comp", "connection-manager"] }
reqwest = { version = "0.11" } reqwest = { version = "0.11" }
http = "*" http = "*"
sigh = "*" sigh = "*"
metrics = "0.20"
metrics-util = "0.14"
metrics-exporter-prometheus = "0.11"

View File

@ -2,3 +2,4 @@ redis: "redis://fedi.buzz:6379/"
redis_password_file: "redis_password.txt" redis_password_file: "redis_password.txt"
in_topic: "relay-in" in_topic: "relay-in"
priv_key_file: private-key.pem priv_key_file: private-key.pem
prometheus_port: 9102

View File

@ -8,6 +8,7 @@ pub struct Config {
pub redis_password_file: String, pub redis_password_file: String,
pub in_topic: String, pub in_topic: String,
priv_key_file: String, priv_key_file: String,
pub prometheus_port: u16,
} }
impl Config { impl Config {

View File

@ -13,6 +13,8 @@ use cave::{
config::LoadConfig, config::LoadConfig,
feed, feed,
}; };
use metrics_util::MetricKindMask;
use metrics_exporter_prometheus::PrometheusBuilder;
mod config; mod config;
@ -41,6 +43,14 @@ async fn main() {
cave::init::init_logger(5557); cave::init::init_logger(5557);
let config = config::Config::load(); let config = config::Config::load();
PrometheusBuilder::new()
.with_http_listener(([0; 8], config.prometheus_port))
.add_global_label("application", env!("CARGO_PKG_NAME"))
.idle_timeout(MetricKindMask::ALL, Some(Duration::from_secs(600)))
.install()
.unwrap();
let priv_key = Arc::new(config.priv_key()); let priv_key = Arc::new(config.priv_key());
let relay_in = connect_relay_in(config.redis_url(), &config.in_topic) let relay_in = connect_relay_in(config.redis_url(), &config.in_topic)
.await .await
@ -71,6 +81,7 @@ async fn main() {
relay_in.for_each(|action| async { relay_in.for_each(|action| async {
// Filter by action type // Filter by action type
if ! allowed_action_types.contains(&action.action_type) { if ! allowed_action_types.contains(&action.action_type) {
metrics::counter!("sieve_activity", 1, "type" => "type_ignored");
return; return;
} }
@ -100,10 +111,13 @@ async fn main() {
// fetch info for id // fetch info for id
tracing::debug!("GET {id}"); tracing::debug!("GET {id}");
match authorized_fetch(&client, id, KEY_ID, &priv_key).await { match authorized_fetch(&client, id, KEY_ID, &priv_key).await {
Ok(res) => Ok(res) => {
res, metrics::counter!("sieve_activity", 1, "type" => "fetch_object_ok");
res
}
Err(e) => { Err(e) => {
tracing::error!("{id} HTTP: {e:?}"); tracing::error!("{id} HTTP: {e:?}");
metrics::counter!("sieve_activity", 1, "type" => "fetch_object_error");
return; return;
} }
} }
@ -117,21 +131,26 @@ async fn main() {
post, post,
Err(e) => { Err(e) => {
tracing::error!("JSON of {id}: {e:?}"); tracing::error!("JSON of {id}: {e:?}");
metrics::counter!("sieve_activity", 1, "type" => "json_error");
return; return;
} }
}; };
let author: activitypub::Actor = if let Some(author_url) = &post.attributed_to { let author: activitypub::Actor = if let Some(author_url) = &post.attributed_to {
match authorized_fetch(&client, author_url, KEY_ID, &priv_key).await { match authorized_fetch(&client, author_url, KEY_ID, &priv_key).await {
Ok(author) => Ok(author) => {
author, metrics::counter!("sieve_activity", 1, "type" => "fetch_author_ok");
author
}
Err(e) => { Err(e) => {
tracing::error!("{author_url} HTTP: {e:?}"); tracing::error!("{author_url} HTTP: {e:?}");
metrics::counter!("sieve_activity", 1, "type" => "fetch_author_error");
return; return;
} }
} }
} else { } else {
tracing::error!("No attributedTo in {id}"); tracing::error!("No attributedTo in {id}");
metrics::counter!("sieve_activity", 1, "type" => "no_author");
return; return;
}; };
// Translate ActivityPub post to Mastodon client API post format // Translate ActivityPub post to Mastodon client API post format
@ -146,16 +165,23 @@ async fn main() {
post post
} else { } else {
tracing::error!("Cannot serialize post {id}"); tracing::error!("Cannot serialize post {id}");
metrics::counter!("sieve_activity", 1, "type" => "serialize_error");
return; return;
}; };
match store.save_post(encodable_post).await { match store.save_post(encodable_post).await {
Ok(true) => Ok(true) => {
tracing::info!("Post was new: {id}"), tracing::info!("Post was new: {id}");
Ok(false) => metrics::counter!("sieve_activity", 1, "type" => "post_new");
tracing::info!("Post was already known: {id}"), }
Err(e) => Ok(false) => {
tracing::error!("Error forwarding post {id}: {e:?}"), tracing::info!("Post was already known: {id}");
metrics::counter!("sieve_activity", 1, "type" => "post_known");
}
Err(e) => {
tracing::error!("Error forwarding post {id}: {e:?}");
metrics::counter!("sieve_activity", 1, "type" => "post_error");
}
} }
}); });
}).await; }).await;