Skip to content

Commit e97c157

Browse files
authored
Merge pull request #6380 from Turbo87/more-tracing
Add more `tracing` instrumentation
2 parents 980c318 + 1c57e10 commit e97c157

File tree

10 files changed

+42
-4
lines changed

10 files changed

+42
-4
lines changed

cargo-registry-index/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ impl Repository {
299299
/// - If cloning the crate index fails.
300300
/// - If reading the global git config fails.
301301
///
302+
#[instrument(skip_all)]
302303
pub fn open(repository_config: &RepositoryConfig) -> anyhow::Result<Self> {
303304
let checkout_path = tempfile::Builder::new()
304305
.prefix("git")
@@ -401,6 +402,7 @@ impl Repository {
401402
///
402403
/// Note that `modified_file` expects a file path **relative** to the
403404
/// repository working folder!
405+
#[instrument(skip_all, fields(message = %msg))]
404406
fn perform_commit_and_push(&self, msg: &str, modified_file: &Path) -> anyhow::Result<()> {
405407
// git add $file
406408
let mut index = self.repository.index()?;
@@ -421,6 +423,7 @@ impl Repository {
421423

422424
/// Gets a list of files that have been modified since a given `starting_commit`
423425
/// (use `starting_commit = None` for a list of all files).
426+
#[instrument(skip_all)]
424427
pub fn get_files_modified_since(
425428
&self,
426429
starting_commit: Option<&str>,
@@ -464,6 +467,7 @@ impl Repository {
464467
}
465468

466469
/// Push the current branch to the provided refname
470+
#[instrument(skip_all)]
467471
fn push(&self, refspec: &str) -> anyhow::Result<()> {
468472
let mut ref_status = Ok(());
469473
let mut callback_called = false;
@@ -513,6 +517,7 @@ impl Repository {
513517

514518
/// Fetches any changes from the `origin` remote and performs a hard reset
515519
/// to the tip of the `origin/master` branch.
520+
#[instrument(skip_all)]
516521
pub fn reset_head(&self) -> anyhow::Result<()> {
517522
let mut origin = self.repository.find_remote("origin")?;
518523
let original_head = self.head_oid()?;
@@ -549,6 +554,7 @@ impl Repository {
549554
}
550555

551556
/// Reset `HEAD` to a single commit with all the index contents, but no parent
557+
#[instrument(skip_all)]
552558
pub fn squash_to_single_commit(&self, msg: &str) -> anyhow::Result<()> {
553559
let tree = self.repository.find_commit(self.head_oid()?)?.tree()?;
554560
let sig = self.repository.signature()?;
@@ -583,6 +589,7 @@ impl Repository {
583589
///
584590
/// This function also temporarily sets the `GIT_SSH_COMMAND` environment
585591
/// variable to ensure that `git push` commands are able to succeed.
592+
#[instrument(skip_all)]
586593
pub fn run_via_cli(command: &mut Command, credentials: &Credentials) -> anyhow::Result<()> {
587594
let temp_key_path = credentials
588595
.ssh_key()

src/app.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,15 @@ impl App {
213213
}
214214

215215
/// Obtain a read/write database connection from the primary pool
216+
#[instrument(skip_all)]
216217
pub fn db_write(&self) -> Result<DieselPooledConn<'_>, PoolError> {
217218
self.primary_database.get()
218219
}
219220

220221
/// Obtain a readonly database connection from the replica pool
221222
///
222223
/// If the replica pool is disabled or unavailable, the primary pool is used instead.
224+
#[instrument(skip_all)]
223225
pub fn db_read(&self) -> Result<DieselPooledConn<'_>, PoolError> {
224226
let read_only_pool = self.read_only_replica_database.as_ref();
225227
match read_only_pool.map(|pool| pool.get()) {
@@ -248,6 +250,7 @@ impl App {
248250
/// Obtain a readonly database connection from the primary pool
249251
///
250252
/// If the primary pool is unavailable, the replica pool is used instead, if not disabled.
253+
#[instrument(skip_all)]
251254
pub fn db_read_prefer_primary(&self) -> Result<DieselPooledConn<'_>, PoolError> {
252255
match (
253256
self.primary_database.get(),

src/auth.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ impl AuthCheck {
5555
}
5656
}
5757

58+
#[instrument(name = "auth.check", skip_all)]
5859
pub fn check<T: RequestPartsExt>(
5960
&self,
6061
request: &T,
@@ -156,6 +157,7 @@ impl Authentication {
156157
}
157158
}
158159

160+
#[instrument(skip_all)]
159161
fn authenticate_via_cookie<T: RequestPartsExt>(
160162
req: &T,
161163
conn: &mut PgConnection,
@@ -177,6 +179,7 @@ fn authenticate_via_cookie<T: RequestPartsExt>(
177179
Ok(Some(CookieAuthentication { user }))
178180
}
179181

182+
#[instrument(skip_all)]
180183
fn authenticate_via_token<T: RequestPartsExt>(
181184
req: &T,
182185
conn: &mut PgConnection,
@@ -207,6 +210,7 @@ fn authenticate_via_token<T: RequestPartsExt>(
207210
Ok(Some(TokenAuthentication { user, token }))
208211
}
209212

213+
#[instrument(skip_all)]
210214
fn authenticate<T: RequestPartsExt>(req: &T, conn: &mut PgConnection) -> AppResult<Authentication> {
211215
controllers::util::verify_origin(req)?;
212216

src/background_jobs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ impl Environment {
339339
}
340340
}
341341

342+
#[instrument(skip_all)]
342343
pub fn lock_index(&self) -> Result<MutexGuard<'_, Repository>, PerformError> {
343344
let repo = self.index.lock().unwrap_or_else(PoisonError::into_inner);
344345
repo.reset_head()?;

src/controllers/krate/publish.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ pub fn missing_metadata_error_message(missing: &[&str]) -> String {
350350
)
351351
}
352352

353+
#[instrument(skip_all)]
353354
pub fn add_dependencies(
354355
conn: &mut PgConnection,
355356
deps: &[EncodableCrateDependency],
@@ -425,6 +426,7 @@ pub fn add_dependencies(
425426
Ok(git_deps)
426427
}
427428

429+
#[instrument(skip_all, fields(%pkg_name))]
428430
fn verify_tarball(
429431
pkg_name: &str,
430432
tarball: &[u8],

src/controllers/krate/search.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,12 @@ pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
280280
//
281281
// If this becomes a problem in the future the crates count could be denormalized, at least
282282
// for the filterless happy path.
283-
let total: i64 = crates::table.count().get_result(conn)?;
283+
let total: i64 = info_span!("db.query", message = "SELECT COUNT(*) FROM crates")
284+
.in_scope(|| crates::table.count().get_result(conn))?;
284285

285-
let results: Vec<(Crate, bool, Option<i64>)> = query.load(conn)?;
286+
let results: Vec<(Crate, bool, Option<i64>)> =
287+
info_span!("db.query", message = "SELECT ... FROM crates")
288+
.in_scope(|| query.load(conn))?;
286289

287290
let next_page = if let Some(last) = results.last() {
288291
let mut params = IndexMap::new();
@@ -298,7 +301,9 @@ pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
298301
(total, next_page, None, results, conn)
299302
} else {
300303
let query = query.pages_pagination(pagination);
301-
let data: Paginated<(Crate, bool, Option<i64>)> = query.load(conn)?;
304+
let data: Paginated<(Crate, bool, Option<i64>)> =
305+
info_span!("db.query", message = "SELECT ..., COUNT(*) FROM crates")
306+
.in_scope(|| query.load(conn))?;
302307
(
303308
data.total(),
304309
data.next_page_params().map(|p| req.query_with_params(p)),
@@ -315,7 +320,8 @@ pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
315320
.collect::<Vec<_>>();
316321
let crates = data.into_iter().map(|(c, _, _)| c).collect::<Vec<_>>();
317322

318-
let versions: Vec<Version> = crates.versions().load(conn)?;
323+
let versions: Vec<Version> = info_span!("db.query", message = "SELECT ... FROM versions")
324+
.in_scope(|| crates.versions().load(conn))?;
319325
let versions = versions
320326
.grouped_by(&crates)
321327
.into_iter()

src/db.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ impl DieselPool {
7777
DieselPool::Test(Arc::new(Mutex::new(conn)))
7878
}
7979

80+
#[instrument(name = "db.connect", skip_all)]
8081
pub fn get(&self) -> Result<DieselPooledConn<'_>, PoolError> {
8182
match self {
8283
DieselPool::Pool {
@@ -112,6 +113,7 @@ impl DieselPool {
112113
}
113114
}
114115

116+
#[instrument(skip_all)]
115117
pub fn wait_until_healthy(&self, timeout: Duration) -> Result<(), PoolError> {
116118
match self {
117119
DieselPool::Pool { pool, .. } | DieselPool::BackgroundJobPool { pool } => {

src/models/krate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ impl Crate {
416416
}
417417

418418
/// Returns (dependency, dependent crate name, dependent crate downloads)
419+
#[instrument(skip_all, fields(krate.name = %self.name))]
419420
pub(crate) fn reverse_dependencies(
420421
&self,
421422
conn: &mut PgConnection,

src/uploaders.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl Uploader {
109109
///
110110
/// This function can panic on an `Self::Local` during development.
111111
/// Production and tests use `Self::S3` which should not panic.
112+
#[instrument(skip_all, fields(%path))]
112113
pub fn upload<R: Into<Body>>(
113114
&self,
114115
client: &Client,
@@ -149,6 +150,7 @@ impl Uploader {
149150
}
150151

151152
/// Deletes a file using the configured uploader (either `S3`, `Local`).
153+
#[instrument(skip_all, fields(%path))]
152154
pub fn delete(&self, client: &Client, path: &str, upload_bucket: UploadBucket) -> Result<()> {
153155
match *self {
154156
Uploader::S3 {
@@ -176,6 +178,7 @@ impl Uploader {
176178
}
177179

178180
/// Uploads a crate and returns the checksum of the uploaded crate file.
181+
#[instrument(skip_all)]
179182
pub fn upload_crate<R: Into<Body>>(
180183
&self,
181184
http_client: &Client,
@@ -201,6 +204,7 @@ impl Uploader {
201204
Ok(())
202205
}
203206

207+
#[instrument(skip_all)]
204208
pub(crate) fn upload_readme(
205209
&self,
206210
http_client: &Client,
@@ -225,6 +229,7 @@ impl Uploader {
225229
Ok(())
226230
}
227231

232+
#[instrument(skip_all)]
228233
pub(crate) fn upload_index(
229234
&self,
230235
http_client: &Client,
@@ -248,6 +253,7 @@ impl Uploader {
248253
Ok(())
249254
}
250255

256+
#[instrument(skip_all)]
251257
pub(crate) fn delete_index(&self, http_client: &Client, crate_name: &str) -> Result<()> {
252258
let path = Uploader::index_path(crate_name);
253259
self.delete(http_client, &path, UploadBucket::Index)?;

src/worker/readmes.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use diesel::PgConnection;
77
use crate::background_jobs::Environment;
88
use crate::models::Version;
99

10+
#[instrument(skip_all, fields(krate.name))]
1011
pub fn perform_render_and_upload_readme(
1112
conn: &mut PgConnection,
1213
env: &Environment,
@@ -19,6 +20,8 @@ pub fn perform_render_and_upload_readme(
1920
use crate::schema::*;
2021
use diesel::prelude::*;
2122

23+
info!(?version_id, "Rendering README");
24+
2225
let rendered = text_to_html(text, readme_path, base_url, pkg_path_in_vcs);
2326

2427
conn.transaction(|conn| {
@@ -28,6 +31,9 @@ pub fn perform_render_and_upload_readme(
2831
.inner_join(crates::table)
2932
.select((crates::name, versions::num))
3033
.first(conn)?;
34+
35+
tracing::Span::current().record("krate.name", tracing::field::display(&crate_name));
36+
3137
env.uploader
3238
.upload_readme(env.http_client(), &crate_name, &vers, rendered)?;
3339
Ok(())

0 commit comments

Comments
 (0)