prometheus: aggregate similar stats using labels

This commit is contained in:
Vincent Breitmoser 2019-07-20 17:00:02 +02:00
parent 88aff3d5c4
commit eec7765b54
No known key found for this signature in database
GPG Key ID: 7BD18320DEADFA11
4 changed files with 39 additions and 64 deletions

View File

@ -6,45 +6,47 @@ use anonymize_utils;
use database::types::Email;
lazy_static! {
pub static ref KEY_UPLOAD_NEW: Counter =
Counter::new("key_upload_new", "Uploaded keys (new)");
pub static ref KEY_UPLOAD_UPDATED: Counter =
Counter::new("key_upload_updated", "Uploaded keys (updated)");
pub static ref KEY_UPLOAD_UNCHANGED: Counter =
Counter::new("key_upload_unchanged", "Uploaded keys (unchanged)");
pub static ref KEY_UPLOAD_SECRET: Counter =
Counter::new("key_upload_secret", "Uploaded keys (secret)");
pub static ref KEY_UPLOAD_ERROR: Counter =
Counter::new("key_upload_error", "Uploaded keys (error)");
static ref KEY_UPLOAD: LabelCounter =
LabelCounter::new("key_upload", "Uploaded keys", &["result"]);
pub static ref MAIL_SEND_VERIFY: LabelCounter =
LabelCounter::new("mail_send_verify", "Sent verification mails", &["domain"]);
pub static ref MAIL_SEND_MANAGE: LabelCounter =
LabelCounter::new("mail_send_manage", "Sent manage mails", &["domain"]);
pub static ref MAIL_SEND_WELCOME: LabelCounter =
LabelCounter::new("mail_send_welcome", "Sent welcome mails", &["domain"]);
static ref MAIL_SENT: LabelCounter =
LabelCounter::new("mail_sent", "Sent verification mails", &["type", "domain"]);
pub static ref KEY_ADDRESS_PUBLISHED: LabelCounter =
static ref KEY_ADDRESS_PUBLISHED: LabelCounter =
LabelCounter::new("key_address_published", "Verified email addresses", &["domain"]);
pub static ref KEY_ADDRESS_UNPUBLISHED: LabelCounter =
static ref KEY_ADDRESS_UNPUBLISHED: LabelCounter =
LabelCounter::new("key_address_unpublished", "Unpublished email addresses", &["domain"]);
}
pub fn register_counters(registry: &prometheus::Registry) {
MAIL_SEND_VERIFY.register(registry);
MAIL_SEND_MANAGE.register(registry);
MAIL_SEND_WELCOME.register(registry);
KEY_UPLOAD.register(registry);
MAIL_SENT.register(registry);
KEY_UPLOAD_NEW.register(registry);
KEY_UPLOAD_UPDATED.register(registry);
KEY_UPLOAD_UNCHANGED.register(registry);
KEY_UPLOAD_SECRET.register(registry);
KEY_UPLOAD_ERROR.register(registry);
KEY_ADDRESS_PUBLISHED.register(registry);
KEY_ADDRESS_UNPUBLISHED.register(registry);
}
pub struct LabelCounter {
pub fn inc_key_upload(upload_result: &str) {
KEY_UPLOAD.inc(&[upload_result]);
}
pub fn inc_mail_sent(mail_type: &str, email: &Email) {
let anonymized_adddress = anonymize_utils::anonymize_address_fallback(email);
MAIL_SENT.inc(&[mail_type, &anonymized_adddress]);
}
pub fn inc_address_published(email: &Email) {
let anonymized_adddress = anonymize_utils::anonymize_address_fallback(email);
KEY_ADDRESS_PUBLISHED.inc(&[&anonymized_adddress]);
}
pub fn inc_address_unpublished(email: &Email) {
let anonymized_adddress = anonymize_utils::anonymize_address_fallback(email);
KEY_ADDRESS_UNPUBLISHED.inc(&[&anonymized_adddress]);
}
struct LabelCounter {
prometheus_counter: prometheus::IntCounterVec,
}
@ -62,31 +64,4 @@ impl LabelCounter {
fn inc(&self, values: &[&str]) {
self.prometheus_counter.with_label_values(values).inc();
}
pub fn inc_email(&self, email: &Email) {
let anonymized_adddress = anonymize_utils::anonymize_address_fallback(email);
self.inc(&[&anonymized_adddress]);
}
}
pub struct Counter {
prometheus_counter: prometheus::Counter,
}
impl Counter {
fn new(name: &str, help: &str) -> Self {
let opts = prometheus::Opts::new(name, help);
let prometheus_counter = prometheus::Counter::with_opts(opts).unwrap();
Self { prometheus_counter }
}
pub fn inc(&self) {
self.prometheus_counter.inc();
}
fn register(&self, registry: &prometheus::Registry) {
registry.register(Box::new(self.prometheus_counter.clone())).unwrap();
}
}

View File

@ -91,7 +91,7 @@ impl Service {
domain: self.domain.clone(),
};
counters::MAIL_SEND_VERIFY.inc_email(userid);
counters::inc_mail_sent("verify", userid);
self.send(
&vec![userid],
@ -110,7 +110,7 @@ impl Service {
domain: self.domain.clone(),
};
counters::MAIL_SEND_MANAGE.inc_email(recipient);
counters::inc_mail_sent("manage", recipient);
self.send(
&[recipient],
@ -130,7 +130,7 @@ impl Service {
domain: self.domain.clone(),
};
counters::MAIL_SEND_WELCOME.inc_email(userid);
counters::inc_mail_sent("welcome", userid);
self.send(
&vec![userid],

View File

@ -189,7 +189,7 @@ pub fn vks_manage_unpublish_or_fail(
let email = request.address.parse::<Email>()?;
db.set_email_unpublished(&verify_token.fpr, &email)?;
counters::KEY_ADDRESS_UNPUBLISHED.inc_email(&email);
counters::inc_address_unpublished(&email);
Ok(vks_manage_key(request_origin, db, request.token.to_owned(), token_service))
}

View File

@ -112,7 +112,7 @@ pub fn process_key(
tpks.push(match tpk {
Ok(t) => {
if t.is_tsk() {
counters::KEY_UPLOAD_SECRET.inc();
counters::inc_key_upload("secret");
return UploadResponse::err("Whoops, please don't upload secret keys!");
}
t
@ -130,10 +130,10 @@ pub fn process_key(
fn log_db_merge(import_result: Result<ImportResult>) -> Result<ImportResult> {
match import_result {
Ok(ImportResult::New(_)) => counters::KEY_UPLOAD_NEW.inc(),
Ok(ImportResult::Updated(_)) => counters::KEY_UPLOAD_UPDATED.inc(),
Ok(ImportResult::Unchanged(_)) => counters::KEY_UPLOAD_UNCHANGED.inc(),
Err(_) => counters::KEY_UPLOAD_ERROR.inc(),
Ok(ImportResult::New(_)) => counters::inc_key_upload("new"),
Ok(ImportResult::Updated(_)) => counters::inc_key_upload("updated"),
Ok(ImportResult::Unchanged(_)) => counters::inc_key_upload("unchanged"),
Err(_) => counters::inc_key_upload("error"),
};
import_result
@ -281,7 +281,7 @@ fn check_publish_token(
let (fingerprint, email) = serde_json::from_str(&payload)?;
db.set_email_published(&fingerprint, &email)?;
counters::KEY_ADDRESS_PUBLISHED.inc_email(&email);
counters::inc_address_published(&email);
Ok((fingerprint, email))
}