Skip to content

Native API - Option to dump Rust tracing to file#458

Open
cwshugg wants to merge 10 commits into
Azure:mainfrom
cwshugg:user/connorshugg/trace_capturing
Open

Native API - Option to dump Rust tracing to file#458
cwshugg wants to merge 10 commits into
Azure:mainfrom
cwshugg:user/connorshugg/trace_capturing

Conversation

@cwshugg

@cwshugg cwshugg commented Jun 12, 2026

Copy link
Copy Markdown

Hi - This PR implements a basic tracing subscriber that is toggled via an environment variable: AZIHSM_NATIVEAPI_TRACE_FILE. If set, the native API's logs will be dumped to the provided file path.

I implemented this with the goal of making the trace logs accessible. I needed a way to extract the logs from the native API to debug an issue and saw no existing way to do so. (Though, if I missed one, please point me to and I'll be happy to try it out + close this PR if necessary.)

By default (if AZIHSM_NATIVEAPI_TRACE_FILE is not set), or if the trace subscriber fails to initialize (for one reason or another) there is no change in behavior to the native API, and no performance impact. When tracing is enabled, there may be slight performance impacts depending on the workload, as the subscriber is shared across the process and protected by a mutex lock.

Copilot AI review requested due to automatic review settings June 12, 2026 19:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds opt-in, file-based tracing initialization for the native API, controlled via environment variables, and documents how to enable/consume that output.

Changes:

  • Introduce a one-time, env-var-driven tracing subscriber that writes structured events to a file.
  • Initialize tracing on first ABI entrypoint execution to cover the full native API call path.
  • Add diagnostics documentation and wire up tracing-subscriber dependency/features.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
api/native/src/tracing.rs New module that installs a global tracing_subscriber writing to a configured file
api/native/src/lib.rs Calls tracing init from abi_boundary and adds the new module
api/native/doc/chapter_11_diagnostics.md Documents env vars, filter syntax, examples, and behavior notes
api/native/Cargo.toml Adds tracing-subscriber dependency with required features

Comment thread api/native/src/tracing.rs Outdated
Comment thread api/native/doc/chapter_11_diagnostics.md
Comment thread api/native/doc/chapter_11_diagnostics.md
Comment thread api/native/src/tracing.rs Outdated
Comment thread api/native/src/tracing.rs Outdated
Comment thread api/native/src/tracing.rs Outdated
Comment thread api/native/Cargo.toml Outdated
@cwshugg cwshugg marked this pull request as draft June 12, 2026 19:17
@cwshugg cwshugg marked this pull request as ready for review June 12, 2026 20:39
Copilot AI review requested due to automatic review settings June 12, 2026 20:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

Comment thread api/native/src/trace_file.rs
Comment thread api/native/src/trace_file.rs
Comment thread api/native/doc/chapter_11_diagnostics.md
Comment thread api/native/src/trace_file.rs Outdated
Comment thread api/native/src/tracing_subscriber.rs Outdated
Copilot AI review requested due to automatic review settings June 12, 2026 20:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

Comment thread api/native/Cargo.toml
open-enum.workspace = true
parking_lot.workspace = true
tracing.workspace = true
tracing-subscriber = { features = ["env-filter", "fmt"], workspace = true }

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not seeing issues with compilation locally - but I expect the cargo xtask ... checks done on this PR should catch anything I missed.

Comment thread api/native/doc/chapter_11_diagnostics.md
Comment thread api/native/src/lib.rs
pub(crate) fn abi_boundary<F: FnOnce() -> Result<(), AzihsmStatus> + UnwindSafe>(
f: F,
) -> AzihsmStatus {
let _ = std::panic::catch_unwind(trace_file::init_trace_file);
Comment on lines +88 to +99
// Attempt to open/create the trace file.
let mut opts = std::fs::OpenOptions::new();
opts.create(true).write(true);
if append {
opts.append(true);
} else {
opts.truncate(true);
}
let file = match opts.open(&trace_path) {
Ok(f) => f,
Err(_) => return,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants