From a52843ae667550b9974f5690de376ac5411ccc99 Mon Sep 17 00:00:00 2001 From: konsti Date: Tue, 2 Apr 2024 14:34:31 +0200 Subject: [PATCH] Add instrumentation support for develop (#2019) * Add instrumentation support for develop * Fix build without default features --------- Co-authored-by: messense --- Cargo.toml | 4 ++++ src/build_context.rs | 2 ++ src/build_options.rs | 3 ++- src/compile.rs | 3 ++- src/develop.rs | 4 ++++ src/main.rs | 23 +++++++++++++++++++---- src/module_writer.rs | 3 ++- src/project_layout.rs | 3 ++- src/python_interpreter/mod.rs | 3 ++- 9 files changed, 39 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b258b645..8c48c20c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -194,6 +194,10 @@ faster-tests = [] # Deprecated features, keep it now for compatibility human-panic = [] +[profile.profiling] +inherits = "release" +debug = true + # Without this, compressing the .gz archive becomes notably slow for debug builds [profile.dev.package.miniz_oxide] opt-level = 3 diff --git a/src/build_context.rs b/src/build_context.rs index 82cc62ab..9e84a360 100644 --- a/src/build_context.rs +++ b/src/build_context.rs @@ -30,6 +30,7 @@ use std::fmt::{Display, Formatter}; use std::io; use std::path::{Path, PathBuf}; use std::str::FromStr; +use tracing::instrument; /// The way the rust code is used in the wheel #[derive(Clone, Debug, PartialEq, Eq)] @@ -199,6 +200,7 @@ pub type BuiltWheelMetadata = (PathBuf, String); impl BuildContext { /// Checks which kind of bindings we have (pyo3/rust-cypthon or cffi or bin) and calls the /// correct builder. + #[instrument(skip_all)] pub fn build_wheels(&self) -> Result> { use itertools::Itertools; diff --git a/src/build_options.rs b/src/build_options.rs index 7c4f002d..c8aa6db6 100644 --- a/src/build_options.rs +++ b/src/build_options.rs @@ -15,7 +15,7 @@ use std::collections::{HashMap, HashSet}; use std::env; use std::ops::{Deref, DerefMut}; use std::path::PathBuf; -use tracing::debug; +use tracing::{debug, instrument}; // This is used for BridgeModel::Bindings("pyo3-ffi") and BridgeModel::Bindings("pyo3"). // These should be treated almost identically but must be correctly identified @@ -481,6 +481,7 @@ impl BuildOptions { } /// Tries to fill the missing metadata for a BuildContext by querying cargo and python + #[instrument(skip_all)] pub fn into_build_context( self, release: bool, diff --git a/src/compile.rs b/src/compile.rs index d46f1316..62582ec8 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -13,7 +13,7 @@ use std::io::{BufReader, Read}; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; use std::str; -use tracing::{debug, trace}; +use tracing::{debug, instrument, trace}; /// The first version of pyo3 that supports building Windows abi3 wheel /// without `PYO3_NO_PYTHON` environment variable @@ -577,6 +577,7 @@ fn compile_target( /// to import the module with error if it's missing or named incorrectly /// /// Currently the check is only run on linux, macOS and Windows +#[instrument(skip_all)] pub fn warn_missing_py_init(artifact: &Path, module_name: &str) -> Result<()> { let py_init = format!("PyInit_{module_name}"); let mut fd = File::open(artifact)?; diff --git a/src/develop.rs b/src/develop.rs index dfd71d20..9483a56a 100644 --- a/src/develop.rs +++ b/src/develop.rs @@ -14,6 +14,7 @@ use std::path::Path; use std::path::PathBuf; use std::process::Command; use tempfile::TempDir; +use tracing::instrument; use url::Url; /// Install the crate as module in the current virtualenv @@ -76,6 +77,7 @@ fn make_pip_command(python_path: &Path, pip_path: Option<&Path>) -> Command { } } +#[instrument(skip_all)] fn install_dependencies( build_context: &BuildContext, extras: &[String], @@ -121,6 +123,7 @@ fn install_dependencies( Ok(()) } +#[instrument(skip_all, fields(wheel_filename = %wheel_filename.display()))] fn pip_install_wheel( build_context: &BuildContext, python: &Path, @@ -166,6 +169,7 @@ fn pip_install_wheel( /// When a maturin package is installed using `pip install -e`, pip takes care of writing the /// correct URL, however when a maturin package is installed with `maturin develop`, the URL is /// set to the path to the temporary wheel file created during installation. +#[instrument(skip_all)] fn fix_direct_url( build_context: &BuildContext, python: &Path, diff --git a/src/main.rs b/src/main.rs index cdbec3cf..1ed35790 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,9 @@ use maturin::{generate_json_schema, GenerateJsonSchemaOptions}; use maturin::{upload_ui, PublishOpt}; use std::env; use std::path::PathBuf; -use tracing::debug; +use tracing::{debug, instrument}; +#[cfg(feature = "log")] +use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; #[derive(Debug, Parser)] #[command( @@ -304,10 +306,8 @@ fn pep517(subcommand: Pep517Command) -> Result<()> { Ok(()) } +#[instrument] fn run() -> Result<()> { - #[cfg(feature = "log")] - tracing_subscriber::fmt::init(); - #[cfg(feature = "zig")] { // Allow symlink `maturin` to `ar` to invoke `zig ar` @@ -464,6 +464,21 @@ fn main() { #[cfg(not(debug_assertions))] setup_panic_hook(); + #[cfg(feature = "log")] + { + let logger = tracing_subscriber::fmt::layer() + // Avoid showing all the details from the spans + .compact() + // Log the timing of each span + .with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE); + + tracing_subscriber::registry() + // `RUST_LOG` support + .with(tracing_subscriber::EnvFilter::from_default_env()) + .with(logger) + .init(); + } + if let Err(e) = run() { eprintln!("💥 maturin failed"); for cause in e.chain() { diff --git a/src/module_writer.rs b/src/module_writer.rs index 990f7bd6..9918463e 100644 --- a/src/module_writer.rs +++ b/src/module_writer.rs @@ -30,7 +30,7 @@ use std::path::{Path, PathBuf}; use std::process::{Command, Output}; use std::str; use tempfile::{tempdir, TempDir}; -use tracing::debug; +use tracing::{debug, instrument}; use zip::{self, DateTime, ZipWriter}; /// Allows writing the module to a wheel or add it directly to the virtualenv @@ -706,6 +706,7 @@ fn handle_cffi_call_result( /// Copies the shared library into the module, which is the only extra file needed with bindings #[allow(clippy::too_many_arguments)] +#[instrument(skip_all)] pub fn write_bindings_module( writer: &mut impl ModuleWriter, project_layout: &ProjectLayout, diff --git a/src/project_layout.rs b/src/project_layout.rs index e7746fff..1714530b 100644 --- a/src/project_layout.rs +++ b/src/project_layout.rs @@ -7,7 +7,7 @@ use std::collections::HashSet; use std::env; use std::io; use std::path::{Path, PathBuf}; -use tracing::debug; +use tracing::{debug, instrument}; const PYPROJECT_TOML: &str = "pyproject.toml"; @@ -312,6 +312,7 @@ impl ProjectResolver { } } + #[instrument(skip_all)] fn resolve_cargo_metadata( manifest_path: &Path, cargo_options: &CargoOptions, diff --git a/src/python_interpreter/mod.rs b/src/python_interpreter/mod.rs index 5654e8a4..0f11c0a5 100644 --- a/src/python_interpreter/mod.rs +++ b/src/python_interpreter/mod.rs @@ -12,7 +12,7 @@ use std::ops::Deref; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; use std::str::{self, FromStr}; -use tracing::debug; +use tracing::{debug, instrument}; mod config; @@ -573,6 +573,7 @@ impl PythonInterpreter { /// Checks whether the given command is a python interpreter and returns a /// [PythonInterpreter] if that is the case + #[instrument(skip_all, fields(executable = %executable.as_ref().display()))] pub fn check_executable( executable: impl AsRef, target: &Target,