Skip to content

Commit 6008979

Browse files
authored
refactor(rust): use unstable tracing cfg (#2590)
This change introduces conditional compilation to the Rust code generation templates to support the `google_cloud_unstable_tracing` cfg flag. - Updates `common/Cargo.toml.mustache` to include `[lints] workspace = true` when `Codec.DetailedTracingAttributes` is true. - Refactors `crate/src/transport.rs.mustache` to use `if cfg!(google_cloud_unstable_tracing)` blocks for expression-level conditional compilation, ensuring `path_template` is handled correctly in both branches. - Adds `#[allow(unused_variables)]` to suppress warnings when `google_cloud_unstable_tracing` is not enabled. This allows for the inclusion of detailed tracing attributes only when the unstable flag is active, while ensuring the code compiles and runs correctly in all configurations. Testing: Pushed googleapis/google-cloud-rust#3545 and ran integration tests. The following tests were performed on the `google-cloud-showcase-v1beta1` crate in the `google-cloud-rust` repository: 1. **DetailedTracingAttributes On, google_cloud_unstable_tracing On:** * Regenerate: `go run ./cmd/sidekick refresh -project-root ../google-cloud-rust -output src/generated/showcase -codec-option detailed-tracing-attributes=true` (in librarian directory) * Format: `cargo fmt -p google-cloud-showcase-v1beta1` (in google-cloud-rust directory) * Test: `RUSTFLAGS="--cfg google_cloud_unstable_tracing" cargo clippy -p google-cloud-showcase-v1beta1 -- -D warnings && RUSTFLAGS="--cfg google_cloud_unstable_tracing" cargo test -p google-cloud-showcase-v1beta1` (in google-cloud-rust directory) * Result: Passed 2. **DetailedTracingAttributes On, google_cloud_unstable_tracing Off:** * Regenerate: (Same as above) * Format: (Same as above) * Test: `cargo clippy -p google-cloud-showcase-v1beta1 -- -D warnings && cargo test -p google-cloud-showcase-v1beta1` (in google-cloud-rust directory) * Result: Passed 3. **DetailedTracingAttributes Off, google_cloud_unstable_tracing On:** * Regenerate: `go run ./cmd/sidekick refresh -project-root ../google-cloud-rust -output src/generated/showcase -codec-option detailed-tracing-attributes=false` (in librarian directory) * Format: `cargo fmt -p google-cloud-showcase-v1beta1` (in google-cloud-rust directory) * Test: `RUSTFLAGS="--cfg google_cloud_unstable_tracing" cargo clippy -p google-cloud-showcase-v1beta1 -- -D warnings && RUSTFLAGS="--cfg google_cloud_unstable_tracing" cargo test -p google-cloud-showcase-v1beta1` (in google-cloud-rust directory) * Result: Passed 4. **DetailedTracingAttributes Off, google_cloud_unstable_tracing Off:** * Regenerate: (Same as above) * Format: (Same as above) * Test: `cargo clippy -p google-cloud-showcase-v1beta1 -- -D warnings && cargo test -p google-cloud-showcase-v1beta1` (in google-cloud-rust directory) * Result: Passed
1 parent e4958d0 commit 6008979

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

internal/sidekick/internal/rust/templates/common/Cargo.toml.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ rust-version.workspace = true
3232
{{#Codec.NotForPublication}}
3333
publish = false
3434
{{/Codec.NotForPublication}}
35+
{{#Codec.DetailedTracingAttributes}}
36+
37+
[lints]
38+
workspace = true
39+
{{/Codec.DetailedTracingAttributes}}
3540
{{#Codec.PerServiceFeatures}}
3641

3742
[features]

internal/sidekick/internal/rust/templates/crate/src/lib.rs.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ limitations under the License.
4242
//! This crate contains traits, types, and functions to interact with {{{Title}}}
4343
//! Most applications will use the structs defined in the [client] module.
4444
//! {{#Codec.ReleaseLevelIsGA}}
45-
//!
45+
//!
4646
//! The client library types and functions are stable and not expected to change.
4747
//! Please note that Google Cloud services do change from time to time. The client
4848
//! libraries are designed to preserve backwards compatibility when the service
@@ -120,6 +120,7 @@ pub(crate) mod info {
120120
};
121121
}
122122
{{#Codec.DetailedTracingAttributes}}
123+
#[cfg(google_cloud_unstable_tracing)]
123124
lazy_static::lazy_static! {
124125
pub(crate) static ref INSTRUMENTATION_CLIENT_INFO: gaxi::options::InstrumentationClientInfo = {
125126
let mut info = gaxi::options::InstrumentationClientInfo::default();

internal/sidekick/internal/rust/templates/crate/src/transport.rs.mustache

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ impl std::fmt::Debug for {{Codec.Name}} {
5454
impl {{Codec.Name}} {
5555
pub async fn new(config: gaxi::options::ClientConfig) -> gax::client_builder::Result<Self> {
5656
{{#Codec.DetailedTracingAttributes}}
57+
#[cfg(google_cloud_unstable_tracing)]
5758
let tracing_is_enabled = gaxi::options::tracing_enabled(&config);
5859
let inner = gaxi::http::ReqwestClient::new(config, crate::DEFAULT_HOST).await?;
60+
#[cfg(google_cloud_unstable_tracing)]
5961
let inner = if tracing_is_enabled {
6062
inner.with_instrumentation(&crate::info::INSTRUMENTATION_CLIENT_INFO)
6163
} else {
@@ -93,7 +95,7 @@ impl super::stub::{{Codec.Name}} for {{Codec.Name}} {
9395
);
9496
{{/HasAutoPopulatedFields}}
9597
{{#Codec.DetailedTracingAttributes}}
96-
let (builder, method, path_template) = None
98+
let (builder, method, _path_template) = None
9799
{{/Codec.DetailedTracingAttributes}}
98100
{{^Codec.DetailedTracingAttributes}}
99101
let (builder, method) = None
@@ -158,7 +160,8 @@ impl super::stub::{{Codec.Name}} for {{Codec.Name}} {
158160
gax::error::Error::binding(BindingError { paths })
159161
})??;
160162
{{#Codec.DetailedTracingAttributes}}
161-
let options = gax::options::internal::set_path_template(options, path_template);
163+
#[cfg(google_cloud_unstable_tracing)]
164+
let options = gax::options::internal::set_path_template(options, _path_template);
162165
{{/Codec.DetailedTracingAttributes}}
163166
let options = gax::options::internal::set_default_idempotency(
164167
options,

0 commit comments

Comments
 (0)