Skip to content

Commit 619c86a

Browse files
committed
metrics: Remove spawn_blocking() calls
1 parent 7ea0c62 commit 619c86a

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/controllers/metrics.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ pub async fn prometheus(app: AppState, Path(kind): Path<String>, req: Parts) ->
2424

2525
let metrics = match kind.as_str() {
2626
"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?
27+
let mut conn = app.db_read().await?;
28+
app.service_metrics.gather(&mut conn).await?
3329
}
3430
"instance" => {
3531
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)