Skip to content

Commit

Permalink
Trace stdout from test-proxy (#2274)
Browse files Browse the repository at this point in the history
* Trace stdout from test-proxy

This is a big refactor of the test-proxy wrapper that will precede other work. This will help diagnose issues that, in the past,  you could only do if you started the test-proxy separately and remembered to pass `PROXY_MANUAL_START=true` when testing.

* Trace stderr as ERROR level

* Resolve PR feedback
  • Loading branch information
heaths authored Mar 4, 2025
1 parent dd56996 commit 9c0ab12
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 106 deletions.
20 changes: 20 additions & 0 deletions sdk/core/azure_core_test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ cargo test
If you get errors, they could indicate regressions in your tests or perhaps variables or random data wasn't saved correctly.
Review any data you generate or use not coming from the service.

## Troubleshooting

Like all Azure SDK client libraries, the `azure_core_test` crate writes information with the target rooted in the crate name
followed by any modules in which the span was created e.g., `azure_core_test::proxy`.
Because this crate is used to test Azure SDK client libraries and developers will most likely want to see fewer traces from this crate,
the log levels used are a level lower than typical client libraries:

- `INFO` includes only important information e.g., the version of `test-proxy` and on what port it's listening.
- `DEBUG` includes when a test recording or playback has started and stopped.
- `TRACE` includes details like communications with the `test-proxy` itself.

The `azure_core_test` crate also traces useful information that the `test-proxy` process itself writes to `stdout` using the `test-proxy` target and the `TRACE` logging level.

You can configure tracing using the [`RUST_LOG`](https://docs.rs/env_logger) environment variable.
For example, if you wanted to see debug information from all sources by default but see `test-proxy` messages written to `stdout` you'd run:

```bash
RUST_LOG=debug,test-proxy=trace cargo test
```

[PowerShell]: https://learn.microsoft.com/powershell/scripting/install/installing-powershell
[Test Proxy]: https://github.com/Azure/azure-sdk-tools/blob/main/tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md
[Test Resources]: https://github.com/Azure/azure-sdk-tools/blob/main/eng/common/TestResources/README.md
29 changes: 22 additions & 7 deletions sdk/core/azure_core_test/examples/test_proxy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

use clap::Parser;
use clap::{ArgAction, Parser};

#[cfg(not(target_arch = "wasm32"))]
#[tokio::main]
Expand Down Expand Up @@ -46,33 +46,48 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
#[derive(Debug, Parser)]
#[command(about = "Starts the Test-Proxy service", version)]
struct Args {
/// Bind to any available port.
///
/// If not set, only port `5000` will be tried.
/// You can start the `test-proxy` service this way
/// and pass `PROXY_MANUAL_START=true` when running `cargo test` to easily view trace information.
#[arg(long)]
auto: bool,

/// Allow insecure upstream SSL certs.
#[arg(long)]
insecure: bool,

/// Number of seconds to automatically shut down when no activity.
#[arg(long, default_value_t = 300)]
pub auto_shutdown_in_seconds: u32,
auto_shutdown_in_seconds: u32,

/// Enable verbose logging.
#[arg(short, long)]
verbose: bool,
///
/// Trace level `INFO` is used by default.
/// Pass `-v` to enable `DEBUG` level tracing,
/// or `-vv` to enable `TRACE` level tracing.
#[arg(short, long, action = ArgAction::Count)]
verbose: u8,
}

impl Args {
#[cfg(not(target_arch = "wasm32"))]
fn trace_level(&self) -> tracing::level_filters::LevelFilter {
if self.verbose {
return tracing::level_filters::LevelFilter::DEBUG;
use tracing::level_filters::LevelFilter;
match self.verbose {
0 => LevelFilter::INFO,
1 => LevelFilter::DEBUG,
_ => LevelFilter::TRACE,
}
tracing::level_filters::LevelFilter::INFO
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<Args> for azure_core_test::proxy::ProxyOptions {
fn from(args: Args) -> Self {
Self {
auto: args.auto,
insecure: args.insecure,
auto_shutdown_in_seconds: args.auto_shutdown_in_seconds,
}
Expand Down
Loading

0 comments on commit 9c0ab12

Please sign in to comment.