Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: have Bigtable match DynamoDB's metrics #627

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions autopush-common/src/db/bigtable/bigtable_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::Arc;
use std::time::{Duration, SystemTime};

use async_trait::async_trait;
use cadence::StatsdClient;
use cadence::{CountedExt, StatsdClient};
use futures_util::StreamExt;
use google_cloud_rust_raw::bigtable::admin::v2::bigtable_table_admin::DropRowRangeRequest;
use google_cloud_rust_raw::bigtable::admin::v2::bigtable_table_admin_grpc::BigtableTableAdminClient;
Expand Down Expand Up @@ -72,7 +72,7 @@ impl From<Uaid> for String {
pub struct BigTableClientImpl {
pub(crate) settings: BigTableDbSettings,
/// Metrics client
_metrics: Arc<StatsdClient>,
metrics: Arc<StatsdClient>,
/// Connection Channel (used for alternate calls)
pool: BigTablePool,
metadata: Metadata,
Expand Down Expand Up @@ -234,7 +234,7 @@ impl BigTableClientImpl {
let admin_metadata = db_settings.admin_metadata()?;
Ok(Self {
settings: db_settings,
_metrics: metrics,
metrics,
metadata,
admin_metadata,
pool,
Expand Down Expand Up @@ -928,7 +928,8 @@ impl DbClient for BigTableClientImpl {

let mut cells: Vec<cell::Cell> = Vec::new();

let family = if message.topic.is_some() {
let is_topic = message.topic.is_some();
let family = if is_topic {
MESSAGE_TOPIC_FAMILY
} else {
MESSAGE_FAMILY
Expand Down Expand Up @@ -973,7 +974,14 @@ impl DbClient for BigTableClientImpl {
}
row.add_cells(family, cells);
trace!("🉑 Adding row");
self.write_row(row).await.map_err(|e| e.into())
self.write_row(row).await?;

self.metrics
.incr_with_tags("notification.message.stored")
.with_tag("topic", &is_topic.to_string())
.with_tag("database", &self.name())
.send();
Ok(())
}

/// Save a batch of messages to the database.
Expand Down Expand Up @@ -1022,7 +1030,8 @@ impl DbClient for BigTableClientImpl {
new_version_cell(expiry),
],
);
self.write_row(row).await.map_err(|e| e.into())
self.write_row(row).await?;
Ok(())
}

/// Delete the notification from storage.
Expand All @@ -1034,7 +1043,12 @@ impl DbClient for BigTableClientImpl {
);
let row_key = format!("{}#{}", uaid.simple(), chidmessageid);
debug!("🉑🔥 Deleting message {}", &row_key);
self.delete_row(&row_key).await.map_err(|e| e.into())
self.delete_row(&row_key).await?;
self.metrics
.incr_with_tags("notification.message.deleted")
.with_tag("database", &self.name())
.send();
Ok(())
}

/// Return `limit` pending messages from storage. `limit=0` for all messages.
Expand Down
14 changes: 8 additions & 6 deletions autopush-common/src/db/dual/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ impl DualClientImpl {
let primary = BigTableClientImpl::new(metrics.clone(), &db_settings.primary)?;
let secondary = DdbClientImpl::new(metrics.clone(), &db_settings.secondary)?;
debug!("⚖ Got primary and secondary");
metrics
.incr_with_tags("database.dual.allot")
.with_tag(
"median",
&median.map_or_else(|| "None".to_owned(), |m| m.to_string()),
)
.send();
Ok(Self {
primary,
secondary: secondary.clone(),
Expand All @@ -119,19 +126,13 @@ impl DualClientImpl {
let target: (Box<&'a dyn DbClient>, bool) = if let Some(median) = self.median {
if uaid.as_bytes()[0] <= median {
debug!("⚖ Routing user to Bigtable");
// These are migrations so the metrics should appear as
// `auto[endpoint|connect].migrate`.
(Box::new(&self.primary), true)
} else {
(Box::new(&self.secondary), false)
}
} else {
(Box::new(&self.primary), true)
};
self.metrics
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was using the wrong metric label but it seemed too chatty to emit it for every single allot call, so I've moved this to emit its configured median only once at startup.

I think the combination of the added metrics/tags in this PR and the db's own metrics should give us a sense of what allot is doing anyway.

.incr_with_tags("database.dual.error")
.with_tag("target", &target.0.name())
.send();
debug!("⚖ alloting to {}", target.0.name());
Ok(target)
}
Expand Down Expand Up @@ -189,6 +190,7 @@ impl DbClient for DualClientImpl {
debug_assert!(user.version.is_none());
user.version = Some(Uuid::new_v4());
self.primary.add_user(&user).await?;
self.metrics.incr_with_tags("database.migrate").send();
let channels = self.secondary.get_channels(uaid).await?;
// NOTE: add_channels doesn't write a new version:
// user.version is still valid
Expand Down
3 changes: 3 additions & 0 deletions autopush-common/src/db/dynamodb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ impl DbClient for DdbClientImpl {
self.metrics
.incr_with_tags("notification.message.stored")
.with_tag("topic", &topic)
.with_tag("database", &self.name())
.send();
Ok(())
}
Expand All @@ -559,6 +560,7 @@ impl DbClient for DdbClientImpl {
self.metrics
.incr_with_tags("notification.message.stored")
.with_tag("topic", &n.topic.is_some().to_string())
.with_tag("database", &self.name())
.send();
serde_dynamodb::to_hashmap(&NotificationRecord::from_notif(uaid, n))
.ok()
Expand Down Expand Up @@ -600,6 +602,7 @@ impl DbClient for DdbClientImpl {
.await?;
self.metrics
.incr_with_tags("notification.message.deleted")
.with_tag("database", &self.name())
.send();
Ok(())
}
Expand Down