Skip to content

Commit 4795d51

Browse files
committed
metrics: Remove spawn_blocking() calls
1 parent 4934a88 commit 4795d51

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/controllers/metrics.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::controllers::frontend_prelude::*;
22
use crate::util::errors::{custom, forbidden, not_found};
3-
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
43
use prometheus::TextEncoder;
54

65
/// Handles the `GET /api/private/metrics/:kind` endpoint.
@@ -24,12 +23,8 @@ pub async fn prometheus(app: AppState, Path(kind): Path<String>, req: Parts) ->
2423

2524
let metrics = match kind.as_str() {
2625
"service" => {
27-
let conn = app.db_read().await?;
28-
spawn_blocking(move || {
29-
let conn: &mut AsyncConnectionWrapper<_> = &mut conn.into();
30-
app.service_metrics.gather(conn)
31-
})
32-
.await?
26+
let mut conn = app.db_read().await?;
27+
app.service_metrics.gather(&mut conn).await?
3328
}
3429
"instance" => {
3530
spawn_blocking(move || Ok::<_, BoxedAppError>(app.instance_metrics.gather(&app)?))

src/metrics/service.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
1313
use crate::metrics::macros::metrics;
1414
use crate::schema::{background_jobs, crates, versions};
15-
use crate::util::diesel::Conn;
1615
use crate::util::errors::AppResult;
1716
use diesel::{dsl::count_star, prelude::*};
17+
use diesel_async::{AsyncPgConnection, RunQueryDsl};
1818
use prometheus::{proto::MetricFamily, IntGauge, IntGaugeVec};
1919

2020
metrics! {
@@ -32,11 +32,14 @@ metrics! {
3232
}
3333

3434
impl ServiceMetrics {
35-
pub(crate) fn gather(&self, conn: &mut impl Conn) -> AppResult<Vec<MetricFamily>> {
35+
pub(crate) async fn gather(
36+
&self,
37+
conn: &mut AsyncPgConnection,
38+
) -> AppResult<Vec<MetricFamily>> {
3639
self.crates_total
37-
.set(crates::table.select(count_star()).first(conn)?);
40+
.set(crates::table.select(count_star()).first(conn).await?);
3841
self.versions_total
39-
.set(versions::table.select(count_star()).first(conn)?);
42+
.set(versions::table.select(count_star()).first(conn).await?);
4043

4144
let background_jobs = background_jobs::table
4245
.group_by((background_jobs::job_type, background_jobs::priority))
@@ -45,7 +48,8 @@ impl ServiceMetrics {
4548
background_jobs::priority,
4649
count_star(),
4750
))
48-
.load::<(String, i16, i64)>(conn)?;
51+
.load::<(String, i16, i64)>(conn)
52+
.await?;
4953

5054
self.background_jobs.reset();
5155
for (job, priority, count) in background_jobs {

0 commit comments

Comments
 (0)