Skip to content

Commit 2682864

Browse files
goffrieConvex, Inc.
authored andcommitted
Log mysql pool metrics (#36313)
GitOrigin-RevId: 09e75bbfdfa17899cb737ba859e444e9c9753ccd
1 parent 240c5d9 commit 2682864

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

crates/mysql/src/connection.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,13 @@ impl<RT: Runtime> ConvexMySqlPool<RT> {
510510
&self.cluster_name
511511
}
512512

513+
/// Report gauges with information about the MySQL pool.
514+
/// Note that this only makes sense if there is a single pool for this
515+
/// cluster in this process.
516+
pub fn log_pool_metrics(&self) {
517+
crate::metrics::log_pool_metrics(&self.cluster_name, &self.pool.metrics());
518+
}
519+
513520
pub async fn shutdown(&self) -> anyhow::Result<()> {
514521
tracing::info!("Shutting down ConvexMySqlPool");
515522
Ok(self.pool.clone().disconnect().await?)

crates/mysql/src/metrics.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use metrics::{
33
log_counter_with_labels,
44
log_distribution,
55
log_distribution_with_labels,
6+
log_gauge_with_labels,
67
register_convex_counter,
78
register_convex_gauge,
89
register_convex_histogram,
@@ -491,3 +492,103 @@ register_convex_counter!(
491492
pub fn log_large_statement(labels: Vec<StaticMetricLabel>) {
492493
log_counter_with_labels(&MYSQL_LARGE_STATEMENT_TOTAL, 1, labels)
493494
}
495+
496+
register_convex_gauge!(
497+
MYSQL_POOL_CONNECTION_COUNT_INFO,
498+
"Gauge of active connections to the database server, this includes both connections that have \
499+
belong
500+
to the pool, and connections currently owned by the application.",
501+
&["cluster_name"],
502+
);
503+
register_convex_gauge!(
504+
MYSQL_POOL_CONNECTIONS_IN_POOL_INFO,
505+
"Gauge of active connections that currently belong to the pool.",
506+
&["cluster_name"],
507+
);
508+
register_convex_gauge!(
509+
MYSQL_POOL_ACTIVE_WAIT_REQUESTS_INFO,
510+
"Gauge of GetConn requests that are currently active.",
511+
&["cluster_name"],
512+
);
513+
register_convex_gauge!(
514+
MYSQL_POOL_CREATE_FAILED_TOTAL,
515+
"Counter of connections that failed to be created.",
516+
&["cluster_name"],
517+
);
518+
register_convex_gauge!(
519+
MYSQL_POOL_DISCARDED_SUPERFLUOUS_CONNECTION_TOTAL,
520+
"Counter of connections discarded due to pool constraints.",
521+
&["cluster_name"],
522+
);
523+
register_convex_gauge!(
524+
MYSQL_POOL_DISCARDED_UNESTABLISHED_CONNECTION_TOTAL,
525+
"Counter of connections discarded due to being closed upon return to the pool.",
526+
&["cluster_name"],
527+
);
528+
register_convex_gauge!(
529+
MYSQL_POOL_DIRTY_CONNECTION_RETURN_TOTAL,
530+
"Counter of connections that have been returned to the pool dirty that needed to be cleaned
531+
(ie. open transactions, pending queries, etc).",
532+
&["cluster_name"],
533+
);
534+
register_convex_gauge!(
535+
MYSQL_POOL_DISCARDED_EXPIRED_CONNECTION_TOTAL,
536+
"Counter of connections that have been discarded as they were expired by the pool constraints.",
537+
&["cluster_name"],
538+
);
539+
register_convex_gauge!(
540+
MYSQL_POOL_RESETTING_CONNECTION_TOTAL,
541+
"Counter of connections that have been reset.",
542+
&["cluster_name"],
543+
);
544+
register_convex_gauge!(
545+
MYSQL_POOL_DISCARDED_ERROR_DURING_CLEANUP_TOTAL,
546+
"Counter of connections that have been discarded as they returned an error during cleanup.",
547+
&["cluster_name"],
548+
);
549+
register_convex_gauge!(
550+
MYSQL_POOL_CONNECTION_RETURNED_TO_POOL_TOTAL,
551+
"Counter of connections that have been returned to the pool.",
552+
&["cluster_name"],
553+
);
554+
pub fn log_pool_metrics(cluster_name: &str, metrics: &mysql_async::Metrics) {
555+
use std::sync::atomic::Ordering;
556+
macro_rules! mysql_metric {
557+
($name:ident, $gauge:ident) => {
558+
log_gauge_with_labels(
559+
&$gauge,
560+
metrics.$name.load(Ordering::Relaxed) as f64,
561+
vec![cluster_name_label(cluster_name)],
562+
)
563+
};
564+
}
565+
mysql_metric!(connection_count, MYSQL_POOL_CONNECTION_COUNT_INFO);
566+
mysql_metric!(connections_in_pool, MYSQL_POOL_CONNECTIONS_IN_POOL_INFO);
567+
mysql_metric!(active_wait_requests, MYSQL_POOL_ACTIVE_WAIT_REQUESTS_INFO);
568+
mysql_metric!(create_failed, MYSQL_POOL_CREATE_FAILED_TOTAL);
569+
mysql_metric!(
570+
discarded_superfluous_connection,
571+
MYSQL_POOL_DISCARDED_SUPERFLUOUS_CONNECTION_TOTAL
572+
);
573+
mysql_metric!(
574+
discarded_unestablished_connection,
575+
MYSQL_POOL_DISCARDED_UNESTABLISHED_CONNECTION_TOTAL
576+
);
577+
mysql_metric!(
578+
dirty_connection_return,
579+
MYSQL_POOL_DIRTY_CONNECTION_RETURN_TOTAL
580+
);
581+
mysql_metric!(
582+
discarded_expired_connection,
583+
MYSQL_POOL_DISCARDED_EXPIRED_CONNECTION_TOTAL
584+
);
585+
mysql_metric!(resetting_connection, MYSQL_POOL_RESETTING_CONNECTION_TOTAL);
586+
mysql_metric!(
587+
discarded_error_during_cleanup,
588+
MYSQL_POOL_DISCARDED_ERROR_DURING_CLEANUP_TOTAL
589+
);
590+
mysql_metric!(
591+
connection_returned_to_pool,
592+
MYSQL_POOL_CONNECTION_RETURNED_TO_POOL_TOTAL
593+
);
594+
}

0 commit comments

Comments
 (0)