Skip to content

Commit 2df412e

Browse files
committedMar 1, 2024
add more instrumentation for performance tracing
1 parent e486388 commit 2df412e

File tree

4 files changed

+66
-63
lines changed

4 files changed

+66
-63
lines changed
 

‎src/storage/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,23 @@ impl AsyncStorage {
134134
})
135135
}
136136

137+
#[instrument]
137138
pub(crate) async fn exists(&self, path: &str) -> Result<bool> {
138139
match &self.backend {
139140
StorageBackend::Database(db) => db.exists(path).await,
140141
StorageBackend::S3(s3) => s3.exists(path).await,
141142
}
142143
}
143144

145+
#[instrument]
144146
pub(crate) async fn get_public_access(&self, path: &str) -> Result<bool> {
145147
match &self.backend {
146148
StorageBackend::Database(db) => db.get_public_access(path).await,
147149
StorageBackend::S3(s3) => s3.get_public_access(path).await,
148150
}
149151
}
150152

153+
#[instrument]
151154
pub(crate) async fn set_public_access(&self, path: &str, public: bool) -> Result<()> {
152155
match &self.backend {
153156
StorageBackend::Database(db) => db.set_public_access(path, public).await,
@@ -227,6 +230,7 @@ impl AsyncStorage {
227230
})
228231
}
229232

233+
#[instrument]
230234
pub(crate) async fn rustdoc_file_exists(
231235
&self,
232236
name: &str,
@@ -272,6 +276,7 @@ impl AsyncStorage {
272276
}
273277
}
274278

279+
#[instrument]
275280
pub(crate) async fn get(&self, path: &str, max_size: usize) -> Result<Blob> {
276281
let mut blob = match &self.backend {
277282
StorageBackend::Database(db) => db.get(path, max_size, None).await,
@@ -284,6 +289,7 @@ impl AsyncStorage {
284289
Ok(blob)
285290
}
286291

292+
#[instrument]
287293
pub(super) async fn get_range(
288294
&self,
289295
path: &str,

‎src/web/crate_details.rs

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ pub struct Release {
9797
}
9898

9999
impl CrateDetails {
100+
#[tracing::instrument(skip(conn))]
100101
pub async fn new(
101102
conn: &mut sqlx::PgConnection,
102103
name: &str,

‎src/web/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,6 @@ impl MatchedRelease {
203203
self.release.version
204204
}
205205

206-
fn id(&self) -> i32 {
207-
self.release.id
208-
}
209-
210206
fn version(&self) -> &Version {
211207
&self.release.version
212208
}
@@ -232,6 +228,7 @@ impl MatchedRelease {
232228
/// This function will also check for crates where dashes in the name (`-`) have been replaced with
233229
/// underscores (`_`) and vice-versa. The return value will indicate whether the crate name has
234230
/// been matched exactly, or if there has been a "correction" in the name that matched instead.
231+
#[instrument(skip(conn))]
235232
async fn match_version(
236233
conn: &mut sqlx::PgConnection,
237234
name: &str,

‎src/web/rustdoc.rs

+58-59
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::{
3434
collections::{BTreeMap, HashMap},
3535
sync::Arc,
3636
};
37-
use tracing::{debug, error, instrument, trace};
37+
use tracing::{debug, error, info_span, instrument, trace, Instrument};
3838

3939
static DOC_RUST_LANG_ORG_REDIRECTS: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
4040
HashMap::from([
@@ -126,7 +126,9 @@ pub(crate) async fn rustdoc_redirector_handler(
126126
.is_ok()
127127
{
128128
rendering_time.step("serve static asset");
129-
return try_serve_legacy_toolchain_asset(storage, config, params.name).await;
129+
return try_serve_legacy_toolchain_asset(storage, config, params.name)
130+
.instrument(info_span!("serve static asset"))
131+
.await;
130132
}
131133
}
132134

@@ -176,84 +178,77 @@ pub(crate) async fn rustdoc_redirector_handler(
176178
if target.ends_with(".js") {
177179
// this URL is actually from a crate-internal path, serve it there instead
178180
rendering_time.step("serve JS for crate");
179-
let version = matched_release.into_version();
180181

181-
let krate = CrateDetails::new(&mut conn, &crate_name, &version, params.version.clone())
182-
.await?
183-
.ok_or(AxumNope::ResourceNotFound)?;
182+
return async {
183+
let version = matched_release.into_version();
184184

185-
rendering_time.step("fetch from storage");
185+
let krate =
186+
CrateDetails::new(&mut conn, &crate_name, &version, params.version.clone())
187+
.await?
188+
.ok_or(AxumNope::ResourceNotFound)?;
186189

187-
match storage
188-
.fetch_rustdoc_file(
189-
&crate_name,
190-
&version.to_string(),
191-
krate.latest_build_id.unwrap_or(0),
192-
target,
193-
krate.archive_storage,
194-
Some(&mut rendering_time),
195-
)
196-
.await
197-
{
198-
Ok(blob) => return Ok(File(blob).into_response()),
199-
Err(err) => {
200-
if !matches!(err.downcast_ref(), Some(AxumNope::ResourceNotFound))
201-
&& !matches!(err.downcast_ref(), Some(crate::storage::PathNotFoundError))
202-
{
203-
debug!(?target, ?err, "got error serving file");
204-
}
205-
// FIXME: we sometimes still get requests for toolchain
206-
// specific static assets under the crate/version/ path.
207-
// This is fixed in rustdoc, but pending a rebuild for
208-
// docs that were affected by this bug.
209-
// https://github.com/rust-lang/docs.rs/issues/1979
210-
if target.starts_with("search-") || target.starts_with("settings-") {
211-
return try_serve_legacy_toolchain_asset(storage, config, target).await;
212-
} else {
213-
return Err(err.into());
190+
rendering_time.step("fetch from storage");
191+
192+
match storage
193+
.fetch_rustdoc_file(
194+
&crate_name,
195+
&version.to_string(),
196+
krate.latest_build_id.unwrap_or(0),
197+
target,
198+
krate.archive_storage,
199+
Some(&mut rendering_time),
200+
)
201+
.await
202+
{
203+
Ok(blob) => Ok(File(blob).into_response()),
204+
Err(err) => {
205+
if !matches!(err.downcast_ref(), Some(AxumNope::ResourceNotFound))
206+
&& !matches!(
207+
err.downcast_ref(),
208+
Some(crate::storage::PathNotFoundError)
209+
)
210+
{
211+
debug!(?target, ?err, "got error serving file");
212+
}
213+
// FIXME: we sometimes still get requests for toolchain
214+
// specific static assets under the crate/version/ path.
215+
// This is fixed in rustdoc, but pending a rebuild for
216+
// docs that were affected by this bug.
217+
// https://github.com/rust-lang/docs.rs/issues/1979
218+
if target.starts_with("search-") || target.starts_with("settings-") {
219+
try_serve_legacy_toolchain_asset(storage, config, target).await
220+
} else {
221+
Err(err.into())
222+
}
214223
}
215224
}
216225
}
226+
.instrument(info_span!("serve JS for crate"))
227+
.await;
217228
}
218229
}
219230

220231
let matched_release = matched_release.into_canonical_req_version();
221232

222-
// get target name and whether it has docs
223-
// FIXME: This is a bit inefficient but allowing us to use less code in general
224-
rendering_time.step("fetch release doc status");
225-
let (target_name, has_docs): (String, bool) = {
226-
let row = sqlx::query!(
227-
"SELECT
228-
target_name,
229-
rustdoc_status
230-
FROM releases
231-
WHERE releases.id = $1",
232-
matched_release.id(),
233-
)
234-
.fetch_one(&mut *conn)
235-
.await?;
236-
237-
(row.target_name, row.rustdoc_status)
238-
};
239-
240233
let mut target = params.target.as_deref();
241-
if target == Some("index.html") || target == Some(&target_name) {
234+
if target == Some("index.html") || target == Some(matched_release.target_name()) {
242235
target = None;
243236
}
244237

245-
if has_docs {
238+
if matched_release.rustdoc_status() {
246239
rendering_time.step("redirect to doc");
247240

248241
let url_str = if let Some(target) = target {
249242
format!(
250-
"/{crate_name}/{}/{target}/{target_name}/",
251-
matched_release.req_version
243+
"/{crate_name}/{}/{target}/{}/",
244+
matched_release.req_version,
245+
matched_release.target_name(),
252246
)
253247
} else {
254248
format!(
255-
"/{crate_name}/{}/{target_name}/",
256-
matched_release.req_version
249+
"/{crate_name}/{}/{}/",
250+
matched_release.req_version,
251+
matched_release.target_name(),
257252
)
258253
};
259254

@@ -663,6 +658,7 @@ pub(crate) async fn rustdoc_html_server_handler(
663658
))
664659
}
665660
})
661+
.instrument(info_span!("rewrite html"))
666662
.await?
667663
}
668664

@@ -731,6 +727,7 @@ fn path_for_version(
731727
(path, query_params)
732728
}
733729

730+
#[instrument(skip_all)]
734731
pub(crate) async fn target_redirect_handler(
735732
Path((name, req_version, req_path)): Path<(String, ReqVersion, String)>,
736733
mut conn: DbConnection,
@@ -805,7 +802,7 @@ pub(crate) struct BadgeQueryParams {
805802
version: Option<ReqVersion>,
806803
}
807804

808-
#[instrument]
805+
#[instrument(skip_all)]
809806
pub(crate) async fn badge_handler(
810807
Path(name): Path<String>,
811808
Query(query): Query<BadgeQueryParams>,
@@ -823,6 +820,7 @@ pub(crate) async fn badge_handler(
823820
))
824821
}
825822

823+
#[instrument(skip_all)]
826824
pub(crate) async fn download_handler(
827825
Path((name, req_version)): Path<(String, ReqVersion)>,
828826
mut conn: DbConnection,
@@ -866,6 +864,7 @@ pub(crate) async fn download_handler(
866864
/// Serves shared resources used by rustdoc-generated documentation.
867865
///
868866
/// This serves files from S3, and is pointed to by the `--static-root-path` flag to rustdoc.
867+
#[instrument(skip_all)]
869868
pub(crate) async fn static_asset_handler(
870869
Path(path): Path<String>,
871870
Extension(storage): Extension<Arc<AsyncStorage>>,

0 commit comments

Comments
 (0)
Please sign in to comment.