Skip to content
Open
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
44 changes: 43 additions & 1 deletion pgdog/src/stats/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl Pools {
let mut maxwait = vec![];
let mut errors = vec![];
let mut out_of_sync = vec![];
let mut banned = vec![];
let mut total_xact_count = vec![];
let mut total_xact_2pc_count = vec![];
let mut avg_xact_count = vec![];
Expand Down Expand Up @@ -98,7 +99,7 @@ impl Pools {

for (user, cluster) in databases().all() {
for (shard_num, shard) in cluster.shards().iter().enumerate() {
for (role, pool) in shard.pools_with_roles() {
for (role, ban, pool) in shard.pools_with_roles_and_bans() {
let state = pool.state();
let labels = vec![
("user".into(), user.user.clone()),
Expand Down Expand Up @@ -149,6 +150,11 @@ impl Pools {
measurement: state.out_of_sync.into(),
});

banned.push(Measurement {
labels: labels.clone(),
measurement: (ban.banned() as i64).into(),
});

let stats = state.stats;
let totals = stats.counts;
let averages = stats.averages;
Expand Down Expand Up @@ -449,6 +455,14 @@ impl Pools {
metric_type: Some("counter".into()),
}));

metrics.push(Metric::new(PoolMetric {
name: "banned".into(),
measurements: banned,
help: "Whether the pool is currently banned from serving traffic (1 = banned, 0 = available).".into(),
unit: None,
metric_type: None,
}));

metrics.push(Metric::new(PoolMetric {
name: "total_xact_count".into(),
measurements: total_xact_count,
Expand Down Expand Up @@ -841,6 +855,34 @@ mod tests {
assert_eq!(lines[2], "# HELP sv_active Active servers per pool");
assert_eq!(lines[3], "sv_active{user=\"alice\",database=\"app\"} 5");
}

#[test]
fn banned_metric_renders_as_gauge() {
config::set(ConfigAndUsers::default()).unwrap();

let metric = PoolMetric {
name: "banned".into(),
measurements: vec![Measurement {
labels: vec![
("database".into(), "app".into()),
("role".into(), "replica".into()),
],
measurement: (true as i64).into(),
}],
help: "Whether the pool is currently banned from serving traffic (1 = banned, 0 = available).".into(),
unit: None,
metric_type: None,
};

let rendered = Metric::new(metric).to_string();
let lines: Vec<&str> = rendered.lines().collect();
assert_eq!(lines[0], "# TYPE banned gauge");
assert_eq!(
lines[1],
"# HELP banned Whether the pool is currently banned from serving traffic (1 = banned, 0 = available)."
);
assert_eq!(lines[2], "banned{database=\"app\",role=\"replica\"} 1");
}
}

impl std::fmt::Display for Pools {
Expand Down
Loading