Skip to content

Commit ea54ba6

Browse files
authored
feat(sidekick/rust): Update templates for otel (#2443)
- Conditionally include `INSTRUMENTATION_CLIENT_INFO` static in `lib.rs.mustache` using `lazy_static!` based on the `DetailedTracingAttributes` flag. - Conditionally add `.with_instrumentation()` call in `transport.rs.mustache` based on the `DetailedTracingAttributes` flag. Tested: 1. **Flag ON Test:** - Reset showcase: `git reset --hard HEAD && git clean -fdx` in `google-cloud-rust/src/generated/showcase` - Regenerate: `go run ./cmd/sidekick refresh -project-root ../google-cloud-rust -output src/generated/showcase -codec-option detailed-tracing-attributes=true` in `librarian` - Format: `cargo fmt -p google-cloud-showcase-v1beta1` in `google-cloud-rust` - Build & Test: `cargo build -p google-cloud-showcase-v1beta1 && cargo test -p google-cloud-showcase-v1beta1` in `google-cloud-rust` - PASSED 2. **Flag OFF Test:** - Reset showcase: `git reset --hard HEAD && git clean -fdx` in `google-cloud-rust/src/generated/showcase` - Modified `google-cloud-rust/src/generated/showcase/.sidekick.toml` to set `detailed-tracing-attributes = false` - Regenerate: `go run ./cmd/sidekick refresh -project-root ../google-cloud-rust -output src/generated/showcase` in `librarian` - Format: `cargo fmt -p google-cloud-showcase-v1beta1` in `google-cloud-rust` - Diff: `git diff` in `google-cloud-rust/src/generated/showcase` - Showed no instrumentation code added, confirming the flag works. For googleapis/google-cloud-rust#3239
1 parent 6cb55d2 commit ea54ba6

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ pub(crate) mod info {
119119
ac.rest_header_value()
120120
};
121121
}
122+
{{#Codec.DetailedTracingAttributes}}
123+
lazy_static::lazy_static! {
124+
pub(crate) static ref INSTRUMENTATION_CLIENT_INFO: gaxi::options::InstrumentationClientInfo = {
125+
let mut info = gaxi::options::InstrumentationClientInfo::default();
126+
info.service_name = "{{Name}}";
127+
info.client_version = VERSION;
128+
info.client_artifact = NAME;
129+
info.default_host = "{{Codec.DefaultHostShort}}";
130+
info
131+
};
132+
}
133+
{{/Codec.DetailedTracingAttributes}}
122134
}
123135

124136
{{/Codec.HasServices}}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,20 @@ impl std::fmt::Debug for {{Codec.Name}} {
5353
{{/Codec.PerServiceFeatures}}
5454
impl {{Codec.Name}} {
5555
pub async fn new(config: gaxi::options::ClientConfig) -> gax::client_builder::Result<Self> {
56+
{{#Codec.DetailedTracingAttributes}}
57+
let tracing_is_enabled = gaxi::options::tracing_enabled(&config);
58+
let inner = gaxi::http::ReqwestClient::new(config, crate::DEFAULT_HOST).await?;
59+
let inner = if tracing_is_enabled {
60+
inner.with_instrumentation(&*crate::info::INSTRUMENTATION_CLIENT_INFO)
61+
} else {
62+
inner
63+
};
64+
Ok(Self { inner })
65+
{{/Codec.DetailedTracingAttributes}}
66+
{{^Codec.DetailedTracingAttributes}}
5667
let inner = gaxi::http::ReqwestClient::new(config, crate::DEFAULT_HOST).await?;
5768
Ok(Self { inner })
69+
{{/Codec.DetailedTracingAttributes}}
5870
}
5971
}
6072
@@ -167,7 +179,7 @@ impl super::stub::{{Codec.Name}} for {{Codec.Name}} {
167179
{{#ReturnsEmpty}}
168180
.map(|r: gax::response::Response<{{OutputType.Codec.QualifiedName}}>| {
169181
let (parts, _) = r.into_parts();
170-
gax::response::Response::from_parts(parts, ())
182+
gax::response::Response::from_parts(parts, ())
171183
})
172184
{{/ReturnsEmpty}}
173185
}

0 commit comments

Comments
 (0)