Skip to content

Commit f090d09

Browse files
Nemo157Joshua Nelson
authored and
Joshua Nelson
committed
Add metrics tracking DatabaseFileHandler usage
1 parent fd6bfc5 commit f090d09

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/web/file.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Database based file handler
22
33
use crate::storage::{Blob, Storage};
4-
use crate::{error::Result, Config};
4+
use crate::{error::Result, Config, Metrics};
55
use iron::{status, Handler, IronResult, Request, Response};
66

77
#[derive(Debug)]
@@ -60,6 +60,15 @@ impl Handler for DatabaseFileHandler {
6060
let storage = extension!(req, Storage);
6161
let config = extension!(req, Config);
6262
if let Ok(file) = File::from_path(&storage, &path, &config) {
63+
let metrics = extension!(req, Metrics);
64+
65+
// Because all requests that don't hit another handler go through here, we will get all
66+
// requests successful or not recorded by the RequestRecorder, so we inject an extra
67+
// "database success" route to keep track of how often we succeed vs 404
68+
metrics
69+
.routes_visited
70+
.with_label_values(&["database success"])
71+
.inc();
6372
Ok(file.serve())
6473
} else {
6574
Err(super::error::Nope::CrateNotFound.into())

src/web/metrics.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ mod tests {
133133
("/-/sitemap.xml", "static resource"),
134134
("/-/static/style.css", "static resource"),
135135
("/-/static/vendored.css", "static resource"),
136+
("/rustdoc/rcc/0.0.0/rcc/index.html", "database"),
137+
("/rustdoc/gcc/0.0.0/gcc/index.html", "database"),
136138
];
137139

138140
wrapper(|env| {
@@ -179,6 +181,15 @@ mod tests {
179181
);
180182
}
181183

184+
// extra metrics for the "database success" hack
185+
assert_eq!(
186+
metrics
187+
.routes_visited
188+
.with_label_values(&["database success"])
189+
.get(),
190+
2
191+
);
192+
182193
Ok(())
183194
})
184195
}

src/web/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ use iron::{
103103
status::Status,
104104
Chain, Handler, Iron, IronError, IronResult, Listening, Request, Response, Url,
105105
};
106+
use metrics::RequestRecorder;
106107
use page::TemplateData;
107108
use postgres::Client;
108109
use router::NoRoute;
@@ -148,7 +149,7 @@ impl CratesfyiHandler {
148149
router_handler: Box::new(router_chain),
149150
database_file_handler: Box::new(routes::BlockBlacklistedPrefixes::new(
150151
blacklisted_prefixes,
151-
Box::new(file::DatabaseFileHandler),
152+
Box::new(RequestRecorder::new(file::DatabaseFileHandler, "database")),
152153
)),
153154
inject_extensions,
154155
})

0 commit comments

Comments
 (0)