Skip to content

Commit 6c28e7c

Browse files
authored
impl(gax-internal): recorder and logging (#5148)
Introduce `WithClientLogging` a decorator for `std::future::Future` that creates the L3 logs entries with the future completes.
1 parent 6566a1d commit 6c28e7c

7 files changed

Lines changed: 399 additions & 4 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gax-internal/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ httptest.workspace = true
134134
mockall.workspace = true
135135
opentelemetry_sdk = { workspace = true, features = ["logs", "metrics", "testing"] }
136136
opentelemetry-appender-tracing = { workspace = true }
137+
pretty_assertions.workspace = true
137138
scoped-env.workspace = true
138139
serde_with.workspace = true
139140
serial_test.workspace = true

src/gax-internal/src/observability.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ mod client_signals;
4444
#[cfg(google_cloud_unstable_tracing)]
4545
pub use client_signals::{
4646
ClientRequestAttributes, ClientSignalsExt, DurationMetric, RequestRecorder, RequestStart,
47+
WithClientLogging,
4748
};
4849

4950
#[cfg(google_cloud_unstable_tracing)]

src/gax-internal/src/observability/client_signals.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ mod client_signals_ext;
1616
mod duration_metric;
1717
mod recorder;
1818
mod request_start;
19+
mod with_client_logging;
1920
mod with_client_signals;
2021

2122
pub use client_signals_ext::ClientSignalsExt;
2223
pub use duration_metric::DurationMetric;
2324
pub use recorder::{ClientRequestAttributes, RequestRecorder};
2425
pub use request_start::RequestStart;
26+
pub use with_client_logging::WithClientLogging;
2527
pub use with_client_signals::WithClientSignals;
2628

2729
/// An extension to disable terminal actionable error logging.
@@ -213,7 +215,7 @@ mod tests {
213215
default_host: "example.com",
214216
};
215217
pub(crate) static URL_TEMPLATE: &str = "/v1/projects/{}:test_method";
216-
pub(crate) static METHOD: &str = "test-method";
218+
pub(crate) static METHOD: &str = "google.test.v1.Service/TestMethod";
217219
pub(crate) const DELAY: Duration = Duration::from_millis(750);
218220
const COMMON_ATTRIBUTES: [(&str, &str); 3] = [
219221
("rpc.system.name", "http"),

src/gax-internal/src/observability/client_signals/duration_metric.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ mod tests {
210210
.build();
211211
let metric = DurationMetric::new_with_provider(&TEST_INFO, Arc::new(provider.clone()));
212212
let options = RequestOptions::default().insert_extension(PathTemplate(URL_TEMPLATE));
213-
let start = RequestStart::new(&TEST_INFO, &options, "test-method");
213+
let start = RequestStart::new(&TEST_INFO, &options, METHOD);
214214
// Use a long pause so it gets recorded as such.
215215
tokio::time::sleep(DELAY).await;
216216
let error = Error::service(
@@ -241,7 +241,7 @@ mod tests {
241241
.build();
242242
let metric = DurationMetric::new_with_provider(&TEST_INFO, Arc::new(provider.clone()));
243243
let options = RequestOptions::default().insert_extension(PathTemplate(URL_TEMPLATE));
244-
let start = RequestStart::new(&TEST_INFO, &options, "test-method");
244+
let start = RequestStart::new(&TEST_INFO, &options, METHOD);
245245
// Use a long pause so it gets recorded as such.
246246
tokio::time::sleep(DELAY).await;
247247
let error = Error::http(429, http::HeaderMap::new(), bytes::Bytes::new());

src/gax-internal/src/observability/client_signals/recorder.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::observability::attributes::RPC_SYSTEM_HTTP;
15+
use crate::observability::attributes::{GCP_CLIENT_REPO_GOOGLEAPIS, RPC_SYSTEM_HTTP};
1616
use crate::options::InstrumentationClientInfo;
1717
#[cfg(feature = "_internal-http-client")]
1818
use google_cloud_gax::error::Error;
@@ -259,6 +259,34 @@ impl ClientSnapshot {
259259
self.info.default_host
260260
}
261261

262+
/// Returns the service name (e.g. `storage`).
263+
///
264+
/// Use with the "gcp.client.service" attribute.
265+
pub fn service_name(&self) -> &'static str {
266+
self.info.service_name
267+
}
268+
269+
/// Returns the service version (e.g. `1.2.3`).
270+
///
271+
/// Use with the "gcp.client.version" attribute.
272+
pub fn client_version(&self) -> &'static str {
273+
self.info.client_version
274+
}
275+
276+
/// Returns the GitHub repository.
277+
///
278+
/// Use with the "gcp.client.repo" attribute.
279+
pub fn client_repo(&self) -> &'static str {
280+
GCP_CLIENT_REPO_GOOGLEAPIS
281+
}
282+
283+
/// Returns the Rust crate.
284+
///
285+
/// Use as instrumentation name, and with the "gcp.client.artifact" attribute.
286+
pub fn client_artifact(&self) -> &'static str {
287+
self.info.client_artifact
288+
}
289+
262290
/// Returns the RPC system (HTTP or gRPC) used in the last low-level request.
263291
///
264292
/// Use with the "rpc.system.name" attribute.
@@ -313,6 +341,30 @@ impl ClientSnapshot {
313341
.and_then(|s| s.http_status_code)
314342
}
315343

344+
/// Returns the HTTP method (e.g. POST) used in the last request.
345+
///
346+
/// Note that this may not be populated for gRPC requests.
347+
///
348+
/// Use with the "http.request.method" attribute.
349+
pub fn http_method(&self) -> Option<&str> {
350+
self.transport_snapshot
351+
.as_ref()
352+
.and_then(|s| s.http_method.as_ref().map(|m| m.as_str()))
353+
}
354+
355+
/// Returns the "resend count" of the last request, if it was a retry.
356+
///
357+
/// The resend count of the initial attempt is `None`, and starts at 1 for each retry attempt
358+
/// made.
359+
///
360+
/// Use with the "http.request.resend_count" attribute.
361+
pub fn http_resend_count(&self) -> Option<u32> {
362+
if self.attempt_count <= 1 {
363+
return None;
364+
}
365+
Some(self.attempt_count - 1)
366+
}
367+
316368
/// Returns the full URL used in the last request.
317369
///
318370
/// Note that this may not be populated for gRPC requests.

0 commit comments

Comments
 (0)