-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add database connection pool metrics
- Loading branch information
Showing
7 changed files
with
200 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
quickwit/quickwit-metastore/src/metastore/postgres/metrics.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (C) 2024 Quickwit, Inc. | ||
// | ||
// Quickwit is offered under the AGPL v3.0 and as commercial software. | ||
// For commercial licensing, contact us at [email protected]. | ||
// | ||
// AGPL: | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use once_cell::sync::Lazy; | ||
use quickwit_common::metrics::{new_gauge, IntGauge}; | ||
|
||
#[derive(Clone)] | ||
pub(super) struct PostgresMetrics { | ||
pub acquire_connections: IntGauge, | ||
pub active_connections: IntGauge, | ||
pub idle_connections: IntGauge, | ||
} | ||
|
||
impl Default for PostgresMetrics { | ||
fn default() -> Self { | ||
Self { | ||
acquire_connections: new_gauge( | ||
"acquire_connections", | ||
"Number of connections being acquired.", | ||
"metastore", | ||
&[], | ||
), | ||
active_connections: new_gauge( | ||
"active_connections", | ||
"Number of active (used + idle) connections.", | ||
"metastore", | ||
&[], | ||
), | ||
idle_connections: new_gauge( | ||
"idle_connections", | ||
"Number of idle connections.", | ||
"metastore", | ||
&[], | ||
), | ||
} | ||
} | ||
} | ||
|
||
pub(super) static POSTGRES_METRICS: Lazy<PostgresMetrics> = Lazy::new(PostgresMetrics::default); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
quickwit/quickwit-metastore/src/metastore/postgres/pool.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright (C) 2024 Quickwit, Inc. | ||
// | ||
// Quickwit is offered under the AGPL v3.0 and as commercial software. | ||
// For commercial licensing, contact us at [email protected]. | ||
// | ||
// AGPL: | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as | ||
// published by the Free Software Foundation, either version 3 of the | ||
// License, or (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use futures::future::BoxFuture; | ||
use futures::stream::BoxStream; | ||
use quickwit_common::metrics::GaugeGuard; | ||
use sqlx::database::HasStatement; | ||
use sqlx::pool::maybe::MaybePoolConnection; | ||
use sqlx::pool::PoolConnection; | ||
use sqlx::{ | ||
Acquire, Database, Describe, Either, Error, Execute, Executor, Pool, Postgres, Transaction, | ||
}; | ||
|
||
use super::metrics::POSTGRES_METRICS; | ||
|
||
#[derive(Debug)] | ||
pub(super) struct TrackedPool<DB: Database> { | ||
inner_pool: Pool<DB>, | ||
} | ||
|
||
impl TrackedPool<Postgres> { | ||
pub fn new(inner_pool: Pool<Postgres>) -> Self { | ||
Self { inner_pool } | ||
} | ||
} | ||
|
||
impl<DB: Database> Clone for TrackedPool<DB> { | ||
fn clone(&self) -> Self { | ||
Self { | ||
inner_pool: self.inner_pool.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a, DB: Database> Acquire<'a> for &TrackedPool<DB> { | ||
type Database = DB; | ||
|
||
type Connection = PoolConnection<DB>; | ||
|
||
fn acquire(self) -> BoxFuture<'static, Result<Self::Connection, Error>> { | ||
let acquire_fut = self.inner_pool.acquire(); | ||
|
||
POSTGRES_METRICS | ||
.active_connections | ||
.set(self.inner_pool.size() as i64); | ||
POSTGRES_METRICS | ||
.idle_connections | ||
.set(self.inner_pool.num_idle() as i64); | ||
|
||
Box::pin(async move { | ||
let mut gauge_guard = GaugeGuard::from_gauge(&POSTGRES_METRICS.acquire_connections); | ||
gauge_guard.add(1); | ||
|
||
let conn = acquire_fut.await?; | ||
Ok(conn) | ||
}) | ||
} | ||
|
||
fn begin(self) -> BoxFuture<'static, Result<Transaction<'a, DB>, Error>> { | ||
let conn = self.acquire(); | ||
|
||
Box::pin(async move { | ||
Transaction::begin(MaybePoolConnection::PoolConnection(conn.await?)).await | ||
}) | ||
} | ||
} | ||
|
||
impl<'p, DB: Database> Executor<'p> for &'_ TrackedPool<DB> | ||
where for<'c> &'c mut DB::Connection: Executor<'c, Database = DB> | ||
{ | ||
type Database = DB; | ||
|
||
fn fetch_many<'e, 'q: 'e, E: 'q>( | ||
self, | ||
query: E, | ||
) -> BoxStream<'e, Result<Either<DB::QueryResult, DB::Row>, Error>> | ||
where | ||
E: Execute<'q, Self::Database>, | ||
{ | ||
self.inner_pool.fetch_many(query) | ||
} | ||
|
||
fn fetch_optional<'e, 'q: 'e, E: 'q>( | ||
self, | ||
query: E, | ||
) -> BoxFuture<'e, Result<Option<DB::Row>, Error>> | ||
where | ||
E: Execute<'q, Self::Database>, | ||
{ | ||
self.inner_pool.fetch_optional(query) | ||
} | ||
|
||
fn prepare_with<'e, 'q: 'e>( | ||
self, | ||
sql: &'q str, | ||
parameters: &'e [<Self::Database as Database>::TypeInfo], | ||
) -> BoxFuture<'e, Result<<Self::Database as HasStatement<'q>>::Statement, Error>> { | ||
self.inner_pool.prepare_with(sql, parameters) | ||
} | ||
|
||
#[doc(hidden)] | ||
fn describe<'e, 'q: 'e>( | ||
self, | ||
sql: &'q str, | ||
) -> BoxFuture<'e, Result<Describe<Self::Database>, Error>> { | ||
self.inner_pool.describe(sql) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters