Skip to content

Commit 9da04c0

Browse files
committed
storage: ensure uploaded_files_total metric is recorded
1 parent b2424ec commit 9da04c0

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

src/storage/database.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
use super::{Blob, StorageTransaction};
22
use crate::db::Pool;
3+
use crate::Metrics;
34
use chrono::{DateTime, NaiveDateTime, Utc};
45
use failure::Error;
56
use postgres::Transaction;
7+
use std::sync::Arc;
68

79
pub(crate) struct DatabaseBackend {
810
pool: Pool,
11+
metrics: Arc<Metrics>,
912
}
1013

1114
impl DatabaseBackend {
12-
pub(crate) fn new(pool: Pool) -> Self {
13-
Self { pool }
15+
pub(crate) fn new(pool: Pool, metrics: Arc<Metrics>) -> Self {
16+
Self { pool, metrics }
1417
}
1518

1619
pub(super) fn exists(&self, path: &str) -> Result<bool, Error> {
@@ -68,12 +71,14 @@ impl DatabaseBackend {
6871
pub(super) fn start_connection(&self) -> Result<DatabaseClient, Error> {
6972
Ok(DatabaseClient {
7073
conn: self.pool.get()?,
74+
metrics: self.metrics.clone(),
7175
})
7276
}
7377
}
7478

7579
pub(super) struct DatabaseClient {
7680
conn: crate::db::PoolClient,
81+
metrics: Arc<Metrics>,
7782
}
7883

7984
impl DatabaseClient {
@@ -82,12 +87,14 @@ impl DatabaseClient {
8287
) -> Result<DatabaseStorageTransaction<'_>, Error> {
8388
Ok(DatabaseStorageTransaction {
8489
transaction: self.conn.transaction()?,
90+
metrics: &self.metrics,
8591
})
8692
}
8793
}
8894

8995
pub(super) struct DatabaseStorageTransaction<'a> {
9096
transaction: Transaction<'a>,
97+
metrics: &'a Metrics,
9198
}
9299

93100
impl<'a> StorageTransaction for DatabaseStorageTransaction<'a> {
@@ -101,6 +108,7 @@ impl<'a> StorageTransaction for DatabaseStorageTransaction<'a> {
101108
SET mime = EXCLUDED.mime, content = EXCLUDED.content, compression = EXCLUDED.compression",
102109
&[&blob.path, &blob.mime, &blob.content, &compression],
103110
)?;
111+
self.metrics.uploaded_files_total.inc();
104112
}
105113
Ok(())
106114
}

src/storage/mod.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl Storage {
8181
let backend = if let Some(c) = s3::s3_client() {
8282
StorageBackend::S3(Box::new(S3Backend::new(c, metrics, config)?))
8383
} else {
84-
StorageBackend::Database(DatabaseBackend::new(pool))
84+
StorageBackend::Database(DatabaseBackend::new(pool, metrics))
8585
};
8686
Ok(Storage { backend })
8787
}
@@ -98,9 +98,9 @@ impl Storage {
9898
}
9999

100100
#[cfg(test)]
101-
pub(crate) fn temp_new_db(pool: Pool) -> Result<Self, Error> {
101+
pub(crate) fn temp_new_db(pool: Pool, metrics: Arc<Metrics>) -> Result<Self, Error> {
102102
Ok(Storage {
103-
backend: StorageBackend::Database(DatabaseBackend::new(pool)),
103+
backend: StorageBackend::Database(DatabaseBackend::new(pool, metrics)),
104104
})
105105
}
106106

@@ -391,7 +391,7 @@ mod backend_tests {
391391
Ok(())
392392
}
393393

394-
fn test_store_blobs(storage: &Storage) -> Result<(), Error> {
394+
fn test_store_blobs(storage: &Storage, metrics: &Metrics) -> Result<(), Error> {
395395
const NAMES: &[&str] = &[
396396
"a",
397397
"b",
@@ -419,10 +419,12 @@ mod backend_tests {
419419
assert_eq!(blob.mime, actual.mime);
420420
}
421421

422+
assert_eq!(NAMES.len(), metrics.uploaded_files_total.get() as usize);
423+
422424
Ok(())
423425
}
424426

425-
fn test_store_all(storage: &Storage) -> Result<(), Error> {
427+
fn test_store_all(storage: &Storage, metrics: &Metrics) -> Result<(), Error> {
426428
let dir = tempfile::Builder::new()
427429
.prefix("docs.rs-upload-test")
428430
.tempdir()?;
@@ -464,6 +466,8 @@ mod backend_tests {
464466
expected_algs.insert(CompressionAlgorithm::default());
465467
assert_eq!(algs, expected_algs);
466468

469+
assert_eq!(2, metrics.uploaded_files_total.get());
470+
467471
Ok(())
468472
}
469473

@@ -559,7 +563,11 @@ mod backend_tests {
559563
// Remember to add the test name to the macro below when adding a new one.
560564

561565
macro_rules! backend_tests {
562-
(backends($env:ident) { $($backend:ident => $create:expr,)* } tests $tests:tt ) => {
566+
(
567+
backends($env:ident) { $($backend:ident => $create:expr,)* }
568+
tests $tests:tt
569+
tests_with_metrics $tests_with_metrics:tt
570+
) => {
563571
$(
564572
mod $backend {
565573
use crate::test::TestEnvironment;
@@ -571,6 +579,7 @@ mod backend_tests {
571579
}
572580

573581
backend_tests!(@tests $tests);
582+
backend_tests!(@tests_with_metrics $tests_with_metrics);
574583
}
575584
)*
576585
};
@@ -584,6 +593,16 @@ mod backend_tests {
584593
}
585594
)*
586595
};
596+
(@tests_with_metrics { $($test:ident,)* }) => {
597+
$(
598+
#[test]
599+
fn $test() {
600+
crate::test::wrapper(|env| {
601+
super::$test(&*get_storage(env), &*env.metrics())
602+
});
603+
}
604+
)*
605+
};
587606
}
588607

589608
backend_tests! {
@@ -597,10 +616,13 @@ mod backend_tests {
597616
test_exists,
598617
test_get_object,
599618
test_get_too_big,
600-
test_store_blobs,
601-
test_store_all,
602619
test_delete_prefix,
603620
test_delete_percent,
604621
}
622+
623+
tests_with_metrics {
624+
test_store_blobs,
625+
test_store_all,
626+
}
605627
}
606628
}

src/test/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl TestEnvironment {
218218
self.storage_db
219219
.get_or_init(|| {
220220
Arc::new(
221-
Storage::temp_new_db(self.db().pool())
221+
Storage::temp_new_db(self.db().pool(), self.metrics())
222222
.expect("failed to initialize the storage"),
223223
)
224224
})

0 commit comments

Comments
 (0)