Skip to content

Commit

Permalink
WIP:
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Sep 6, 2023
1 parent f530318 commit 93a6a10
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl Storage {
version: &str,
path: &str,
archive_storage: bool,
fetch_time: Option<&mut RenderingTimesRecorder>,
fetch_time: Option<&RenderingTimesRecorder>,
) -> Result<Blob> {
trace!("fetch rustdoc file");
Ok(if archive_storage {
Expand Down Expand Up @@ -313,7 +313,7 @@ impl Storage {
archive_path: &str,
path: &str,
max_size: usize,
mut fetch_time: Option<&mut RenderingTimesRecorder>,
mut fetch_time: Option<&RenderingTimesRecorder>,
) -> Result<Blob> {
if let Some(ref mut t) = fetch_time {
t.step("find path in index");
Expand Down
21 changes: 14 additions & 7 deletions src/web/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ use axum::{
response::IntoResponse,
};
use prometheus::{proto::MetricFamily, Encoder, HistogramVec, TextEncoder};
use std::{borrow::Cow, sync::Arc, time::Instant};
use std::{
borrow::Cow,
sync::{Arc, Mutex},
time::Instant,
};
#[cfg(test)]
use tracing::debug;

Expand Down Expand Up @@ -120,27 +124,30 @@ struct RenderingTime {

pub(crate) struct RenderingTimesRecorder<'a> {
metric: &'a HistogramVec,
current: Option<RenderingTime>,
current: Mutex<Option<RenderingTime>>,
}

impl<'a> RenderingTimesRecorder<'a> {
pub(crate) fn new(metric: &'a HistogramVec) -> Self {
Self {
metric,
current: None,
current: Mutex::new(None),
}
}

pub(crate) fn step(&mut self, step: &'static str) {
pub(crate) fn step(&self, step: &'static str) {
self.record_current();
self.current = Some(RenderingTime {
let mut current = self.current.lock().unwrap();
*current = Some(RenderingTime {
start: Instant::now(),
step,
});
}

fn record_current(&mut self) {
if let Some(current) = self.current.take() {
fn record_current(&self) {
let mut current = self.current.lock().unwrap();

if let Some(current) = current.take() {
#[cfg(test)]
debug!(
"rendering time - {}: {:?}",
Expand Down
8 changes: 4 additions & 4 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub(crate) async fn rustdoc_redirector_handler(
)?)
}

let mut rendering_time = RenderingTimesRecorder::new(&metrics.rustdoc_redirect_rendering_times);
let rendering_time = RenderingTimesRecorder::new(&metrics.rustdoc_redirect_rendering_times);

// global static assets for older builds are served from the root, which ends up
// in this handler as `params.name`.
Expand Down Expand Up @@ -199,7 +199,7 @@ pub(crate) async fn rustdoc_redirector_handler(
&version,
&target,
krate.archive_storage,
None, // FIXME: &mut rendering_time, re-add this when storage is async
Some(&rendering_time),
)
}
})
Expand Down Expand Up @@ -379,7 +379,7 @@ pub(crate) async fn rustdoc_html_server_handler(
Extension(updater): Extension<Arc<RepositoryStatsUpdater>>,
uri: Uri,
) -> AxumResult<AxumResponse> {
let mut rendering_time = RenderingTimesRecorder::new(&metrics.rustdoc_rendering_times);
let rendering_time = RenderingTimesRecorder::new(&metrics.rustdoc_rendering_times);

// since we directly use the Uri-path and not the extracted params from the router,
// we have to percent-decode the string here.
Expand Down Expand Up @@ -520,7 +520,7 @@ pub(crate) async fn rustdoc_html_server_handler(
&version,
&storage_path,
krate.archive_storage,
None, // FIXME: &mut rendering_time, re-add this when storage is async
Some(&rendering_time),
)
}
})
Expand Down

0 comments on commit 93a6a10

Please sign in to comment.