Skip to content

Commit 97d5748

Browse files
authored
send backend metrics to their own postgres channel (#843)
<!-- ELLIPSIS_HIDDEN --> > [!IMPORTANT] > Separate backend metrics into their own PostgreSQL channel using `emit_backend_metrics` in `subscribe.rs`. > > - **Behavior**: > - `publish_metrics` in `backend.rs` now uses `emit_backend_metrics` to send metrics to `plane_backend_metrics` channel. > - Removes `emit_ephemeral_with_key` and `emit_ephemeral_impl` from `subscribe.rs`. > - **Functions**: > - Adds `emit_backend_metrics` in `subscribe.rs` to handle backend metrics separately. > - Modifies `emit_impl` to support new channel logic. > - **Constants**: > - Adds `BACKEND_METRICS_EVENT_CHANNEL` in `subscribe.rs` for backend metrics. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=jamsocket%2Fplane&utm_source=github&utm_medium=referral)<sup> for b6ec34a. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent 72e20d6 commit 97d5748

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

plane/src/database/backend.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{
2-
subscribe::{emit_ephemeral_with_key, emit_with_key},
2+
subscribe::{emit_backend_metrics, emit_with_key},
33
PlaneDatabase,
44
};
55
use crate::{
@@ -455,7 +455,7 @@ impl<'a> BackendDatabase<'a> {
455455

456456
pub async fn publish_metrics(&self, metrics: BackendMetricsMessage) -> sqlx::Result<()> {
457457
let mut txn = self.db.pool.begin().await?;
458-
emit_ephemeral_with_key(&mut txn, &metrics.backend_id.to_string(), &metrics).await?;
458+
emit_backend_metrics(&mut txn, &metrics.backend_id.to_string(), &metrics).await?;
459459
txn.commit().await?;
460460
Ok(())
461461
}

plane/src/database/subscribe.rs

+22-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::database::util::MapSqlxError;
1+
use crate::database::{backend::BackendMetricsMessage, util::MapSqlxError};
22
use crate::util::ExponentialBackoff;
33
use chrono::{DateTime, Utc};
44
use serde::{de::DeserializeOwned, Deserialize, Serialize};
@@ -17,7 +17,8 @@ use tokio::{
1717

1818
type ListenerMap = Arc<RwLock<HashMap<(String, Option<String>), Box<dyn TypedSender>>>>;
1919

20-
const EVENT_CHANNEL: &str = "plane_events";
20+
pub const EVENT_CHANNEL: &str = "plane_events";
21+
pub const BACKEND_METRICS_EVENT_CHANNEL: &str = "plane_backend_metrics";
2122

2223
pub trait NotificationPayload:
2324
Serialize + DeserializeOwned + Debug + Send + Sync + Clone + 'static
@@ -382,12 +383,26 @@ pub async fn emit_impl<T: NotificationPayload>(
382383
Ok(())
383384
}
384385

385-
pub async fn emit_ephemeral_impl<T: NotificationPayload>(
386+
pub async fn emit<T: NotificationPayload>(
386387
db: &mut PgConnection,
387-
key: Option<&str>,
388388
payload: &T,
389389
) -> Result<(), sqlx::Error> {
390-
let kind = T::kind().to_string();
390+
emit_impl(db, None, payload).await
391+
}
392+
393+
pub async fn emit_with_key<T: NotificationPayload>(
394+
db: &mut PgConnection,
395+
key: &str,
396+
payload: &T,
397+
) -> Result<(), sqlx::Error> {
398+
emit_impl(db, Some(key), payload).await
399+
}
400+
401+
pub async fn emit_backend_metrics(
402+
db: &mut PgConnection,
403+
key: &str,
404+
payload: &BackendMetricsMessage,
405+
) -> Result<(), sqlx::Error> {
391406
sqlx::query!(
392407
r#"
393408
select pg_notify(
@@ -399,43 +414,13 @@ pub async fn emit_ephemeral_impl<T: NotificationPayload>(
399414
'key', $2::text
400415
)::text
401416
)"#,
402-
kind,
417+
BackendMetricsMessage::kind().to_string(),
403418
key,
404419
serde_json::to_value(&payload).map_sqlx_error()?,
405-
EVENT_CHANNEL,
420+
BACKEND_METRICS_EVENT_CHANNEL,
406421
)
407422
.execute(db)
408423
.await?;
409424

410425
Ok(())
411426
}
412-
413-
pub async fn emit<T: NotificationPayload>(
414-
db: &mut PgConnection,
415-
payload: &T,
416-
) -> Result<(), sqlx::Error> {
417-
emit_impl(db, None, payload).await
418-
}
419-
420-
pub async fn emit_with_key<T: NotificationPayload>(
421-
db: &mut PgConnection,
422-
key: &str,
423-
payload: &T,
424-
) -> Result<(), sqlx::Error> {
425-
emit_impl(db, Some(key), payload).await
426-
}
427-
428-
pub async fn emit_ephemeral<T: NotificationPayload>(
429-
db: &mut PgConnection,
430-
payload: &T,
431-
) -> Result<(), sqlx::Error> {
432-
emit_ephemeral_impl(db, None, payload).await
433-
}
434-
435-
pub async fn emit_ephemeral_with_key<T: NotificationPayload>(
436-
db: &mut PgConnection,
437-
key: &str,
438-
payload: &T,
439-
) -> Result<(), sqlx::Error> {
440-
emit_ephemeral_impl(db, Some(key), payload).await
441-
}

0 commit comments

Comments
 (0)