Skip to content

Commit b1d9d95

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
feat(relay): add OpenTelemetry tracing, keep Prometheus metrics (#1398)
Signed-off-by: Will Pfleger <pfleger.will@gmail.com> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
1 parent 9f2a11b commit b1d9d95

12 files changed

Lines changed: 479 additions & 32 deletions

File tree

Cargo.lock

Lines changed: 107 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ serde_yaml = "0.9"
6565
evalexpr = "11"
6666
cron = "0.16"
6767
# Observability
68-
tracing = "0.1"
69-
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
68+
tracing = "0.1"
69+
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
70+
tracing-opentelemetry = { version = "0.33" }
71+
opentelemetry = { version = "0.32", features = ["trace"] }
72+
opentelemetry_sdk = { version = "0.32", features = ["trace", "rt-tokio"] }
73+
opentelemetry-otlp = { version = "0.32", default-features = false, features = ["trace", "grpc-tonic", "tls-ring"] }
7074
metrics = "0.24"
7175
metrics-exporter-prometheus = "0.18"
7276

crates/buzz-db/src/api_token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ mod tests {
350350
let pool = PgPool::connect(TEST_DB_URL)
351351
.await
352352
.expect("connect to test DB");
353-
Db { pool }
353+
Db::from_pool(pool)
354354
}
355355

356356
async fn make_community(pool: &PgPool) -> Uuid {

crates/buzz-db/src/lib.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ pub async fn insert_mentions(
131131
#[derive(Clone, Debug)]
132132
pub struct Db {
133133
pub(crate) pool: PgPool,
134+
/// Maximum connections configured for this pool (from [`DbConfig::max_connections`]).
135+
pub(crate) max_connections: u32,
136+
}
137+
138+
/// Snapshot of Postgres connection pool utilisation.
139+
#[derive(Debug, Clone, Copy)]
140+
pub struct DbPoolStats {
141+
/// Total connections currently in the pool (idle + active).
142+
pub size: u32,
143+
/// Connections available for immediate reuse.
144+
pub idle: u32,
145+
/// Pool ceiling — the `max_connections` value set at construction.
146+
pub max: u32,
134147
}
135148

136149
/// Configuration for the Postgres connection pool.
@@ -203,12 +216,18 @@ impl Db {
203216
.idle_timeout(Duration::from_secs(config.idle_timeout_secs))
204217
.connect(&config.database_url)
205218
.await?;
206-
Ok(Self { pool })
219+
Ok(Self {
220+
pool,
221+
max_connections: config.max_connections,
222+
})
207223
}
208224

209225
/// Creates a `Db` from an existing `PgPool` (useful in tests).
210226
pub fn from_pool(pool: PgPool) -> Self {
211-
Self { pool }
227+
Self {
228+
max_connections: pool.options().get_max_connections(),
229+
pool,
230+
}
212231
}
213232

214233
/// Run pending database migrations.
@@ -221,6 +240,19 @@ impl Db {
221240
sqlx::query("SELECT 1").execute(&self.pool).await.is_ok()
222241
}
223242

243+
/// Returns pool utilisation stats for metrics emission.
244+
///
245+
/// `size` — total connections (idle + active)
246+
/// `idle` — connections available for immediate reuse
247+
/// `max` — pool ceiling set at construction
248+
pub fn pool_stats(&self) -> DbPoolStats {
249+
DbPoolStats {
250+
size: self.pool.size(),
251+
idle: self.pool.num_idle() as u32,
252+
max: self.max_connections,
253+
}
254+
}
255+
224256
/// Begin a database transaction for atomic multi-statement operations.
225257
///
226258
/// Returns a `'static` transaction because `PgPool` is `Arc`-backed internally.
@@ -2563,7 +2595,7 @@ mod tests {
25632595
let pool = PgPool::connect(TEST_DB_URL)
25642596
.await
25652597
.expect("connect to test DB");
2566-
Db { pool }
2598+
Db::from_pool(pool)
25672599
}
25682600

25692601
async fn make_community(pool: &PgPool) -> Uuid {

crates/buzz-relay/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ serde = { workspace = true }
3636
serde_json = { workspace = true }
3737
tracing = { workspace = true }
3838
tracing-subscriber = { workspace = true }
39+
tracing-opentelemetry = { workspace = true }
40+
opentelemetry = { workspace = true }
41+
opentelemetry_sdk = { workspace = true }
42+
opentelemetry-otlp = { workspace = true }
3943
thiserror = { workspace = true }
4044
anyhow = { workspace = true }
4145
uuid = { workspace = true }

0 commit comments

Comments
 (0)