Skip to content

Commit 03a6b17

Browse files
committed
Log HTML rewriting errors
1 parent 482d566 commit 03a6b17

File tree

4 files changed

+211
-4
lines changed

4 files changed

+211
-4
lines changed

Cargo.lock

Lines changed: 171 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/html.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::web::page::TemplateData;
22
use lol_html::errors::RewritingError;
3+
use std::path::Path;
34
use tera::Context;
45

56
/// Rewrite a rustdoc page to have the docs.rs header
@@ -13,6 +14,7 @@ pub(crate) fn rewrite_lol(
1314
max_allowed_memory_usage: usize,
1415
ctx: Context,
1516
templates: &TemplateData,
17+
file_path: &Path,
1618
) -> Result<Vec<u8>, RewritingError> {
1719
use lol_html::html_content::{ContentType, Element};
1820
use lol_html::{ElementContentHandlers, HtmlRewriter, MemorySettings, Settings};
@@ -23,6 +25,7 @@ pub(crate) fn rewrite_lol(
2325

2426
let head_handler = |head: &mut Element| {
2527
head.append(&tera_head, ContentType::Html);
28+
2629
Ok(())
2730
};
2831

@@ -77,11 +80,33 @@ pub(crate) fn rewrite_lol(
7780

7881
// The input and output are always strings, we just use `&[u8]` so we only have to validate once.
7982
let mut buffer = Vec::new();
83+
// TODO: Make the rewriter persistent?
8084
let mut writer = HtmlRewriter::try_new(settings, |bytes: &[u8]| {
8185
buffer.extend_from_slice(bytes);
8286
})
8387
.expect("utf8 is a valid encoding");
84-
writer.write(html)?;
85-
writer.end()?;
88+
89+
log_memory_errors(writer.write(html), file_path)?;
90+
log_memory_errors(writer.end(), file_path)?;
91+
8692
Ok(buffer)
8793
}
94+
95+
/// Logs OOM errors encountered while rewriting a html file
96+
fn log_memory_errors<T>(
97+
result: Result<T, RewritingError>,
98+
file_path: &Path,
99+
) -> Result<T, RewritingError> {
100+
match result {
101+
err @ Err(RewritingError::MemoryLimitExceeded(..)) => {
102+
crate::web::metrics::HTML_REWRITE_OOMS.inc();
103+
log::error!(
104+
"Failed to serve the rustdoc file '{}' because rewriting it hit a memory limit",
105+
file_path.display(),
106+
);
107+
108+
err
109+
}
110+
result => result,
111+
}
112+
}

src/web/metrics.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ pub static CURRENTLY_RUNNING_THREADS: Lazy<IntGauge> = Lazy::new(|| {
147147
.unwrap()
148148
});
149149

150+
pub static HTML_REWRITE_OOMS: Lazy<IntGauge> = Lazy::new(|| {
151+
register_int_gauge!(
152+
"docsrs_html_rewrite_ooms",
153+
"The number of attempted files that failed due to a memory limit"
154+
)
155+
.unwrap()
156+
});
157+
150158
pub fn metrics_handler(req: &mut Request) -> IronResult<Response> {
151159
let pool = extension!(req, Pool);
152160
let queue = extension!(req, BuildQueue);

0 commit comments

Comments
 (0)