Skip to content

Commit

Permalink
Add tracing spans for procmacros expansion (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Draggu authored Dec 18, 2024
1 parent 4b080df commit 911d67b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/lang/proc_macros/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,22 @@ impl ProcMacroClient {
}
}

#[tracing::instrument(level = "trace", skip_all)]
pub fn request_attribute(&self, params: ExpandAttributeParams) {
self.send_request::<ExpandAttribute>(params, RequestParams::Attribute)
}

#[tracing::instrument(level = "trace", skip_all)]
pub fn request_derives(&self, params: ExpandDeriveParams) {
self.send_request::<ExpandDerive>(params, RequestParams::Derive)
}

#[tracing::instrument(level = "trace", skip_all)]
pub fn request_inline_macros(&self, params: ExpandInlineMacroParams) {
self.send_request::<ExpandInline>(params, RequestParams::Inline)
}

#[tracing::instrument(level = "trace", skip_all)]
pub fn start_initialize(&self) {
if let Err(err) = self.request_defined_macros() {
error!("failed to request defined macros: {err:?}");
Expand All @@ -65,6 +69,7 @@ impl ProcMacroClient {
}
}

#[tracing::instrument(level = "trace", skip_all)]
pub fn finish_initialize(&self) -> Result<DefinedMacrosResponse> {
self.handle_defined_macros()
.inspect_err(|err| error!("failed to fetch defined macros: {err:?}"))
Expand Down Expand Up @@ -151,6 +156,7 @@ impl ProcMacroClient {
}
}

#[tracing::instrument(level = "trace", skip_all)]
fn failed(&self) {
let _ = self.error_channel.try_send(());
}
Expand Down
8 changes: 8 additions & 0 deletions src/lang/proc_macros/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl ProcMacroClientController {
/// Start proc-macro-server after config reload.
/// Note that this will only try to go from `ClientStatus::Pending` to
/// `ClientStatus::Starting` if config allows this.
#[tracing::instrument(level = "trace", skip_all)]
pub fn on_config_change(&mut self, db: &mut AnalysisDatabase, config: &Config) {
if db.proc_macro_client_status().is_pending() {
self.try_initialize(db, config);
Expand All @@ -92,6 +93,7 @@ impl ProcMacroClientController {
///
/// A new server instance is only started if there are available restart attempts left.
/// This ensures that a fresh proc-macro-server is used.
#[tracing::instrument(level = "trace", skip_all)]
pub fn force_restart(&mut self, db: &mut AnalysisDatabase, config: &Config) {
// We have to make sure that snapshots will not report errors from previous client after we
// create new one.
Expand All @@ -108,13 +110,15 @@ impl ProcMacroClientController {
}

/// Check if an error was reported. If so, try to restart.
#[tracing::instrument(level = "trace", skip_all)]
pub fn handle_error(&mut self, db: &mut AnalysisDatabase, config: &Config) {
if !self.try_initialize(db, config) {
self.fatal_failed(db, InitializationFailedInfo::NoMoreRetries);
}
}

/// If the client is ready, apply all available responses.
#[tracing::instrument(level = "trace", skip_all)]
pub fn on_response(&mut self, db: &mut AnalysisDatabase, config: &Config) {
match db.proc_macro_client_status() {
ClientStatus::Starting(client) => {
Expand Down Expand Up @@ -148,6 +152,7 @@ impl ProcMacroClientController {
/// Tries starting proc-macro-server initialization process, if allowed by config.
///
/// Returns value indicating if initialization was attempted.
#[tracing::instrument(level = "trace", skip_all)]
fn try_initialize(&mut self, db: &mut AnalysisDatabase, config: &Config) -> bool {
// Keep the rate limiter check as second condition when config doesn't allow it to make
// sure it is not impacted.
Expand All @@ -161,6 +166,7 @@ impl ProcMacroClientController {
}

/// Spawns proc-macro-server.
#[tracing::instrument(level = "trace", skip_all)]
fn spawn_server(&mut self, db: &mut AnalysisDatabase) {
match self.scarb.proc_macro_server() {
Ok(proc_macro_server) => {
Expand All @@ -184,6 +190,7 @@ impl ProcMacroClientController {
}
}

#[tracing::instrument(level = "trace", skip_all)]
fn fatal_failed(
&self,
db: &mut AnalysisDatabase,
Expand All @@ -197,6 +204,7 @@ impl ProcMacroClientController {
});
}

#[tracing::instrument(level = "trace", skip_all)]
fn apply_responses(
&mut self,
db: &mut AnalysisDatabase,
Expand Down
2 changes: 2 additions & 0 deletions src/lang/proc_macros/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct ProcMacroPlugin {
}

impl MacroPlugin for ProcMacroPlugin {
#[tracing::instrument(level = "trace", skip_all)]
fn generate_code(
&self,
db: &dyn cairo_lang_syntax::node::db::SyntaxGroup,
Expand All @@ -66,6 +67,7 @@ impl MacroPlugin for ProcMacroPlugin {
struct InlineProcMacroPlugin;

impl InlineMacroExprPlugin for InlineProcMacroPlugin {
#[tracing::instrument(level = "trace", skip_all)]
fn generate_code(
&self,
db: &dyn cairo_lang_syntax::node::db::SyntaxGroup,
Expand Down

0 comments on commit 911d67b

Please sign in to comment.