Skip to content

Commit 0431b38

Browse files
authored
impl(gax-internal): define InstrumentationClientInfo for tracing (#3347)
1 parent ff964e6 commit 0431b38

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/gax-internal/src/http.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub struct ReqwestClient {
3939
retry_throttler: SharedRetryThrottler,
4040
polling_error_policy: Arc<dyn PollingErrorPolicy>,
4141
polling_backoff_policy: Arc<dyn PollingBackoffPolicy>,
42+
instrumentation: Option<crate::options::InstrumentationClientInfo>,
4243
}
4344

4445
impl ReqwestClient {
@@ -79,9 +80,18 @@ impl ReqwestClient {
7980
polling_backoff_policy: config
8081
.polling_backoff_policy
8182
.unwrap_or_else(|| Arc::new(ExponentialBackoff::default())),
83+
instrumentation: None,
8284
})
8385
}
8486

87+
pub fn with_instrumentation(
88+
mut self,
89+
instrumentation: Option<crate::options::InstrumentationClientInfo>,
90+
) -> Self {
91+
self.instrumentation = instrumentation;
92+
self
93+
}
94+
8595
pub fn builder(&self, method: Method, path: String) -> reqwest::RequestBuilder {
8696
self.inner
8797
.request(method, format!("{}{path}", &self.endpoint))
@@ -280,6 +290,9 @@ mod tests {
280290
use http::{HeaderMap, HeaderValue, Method};
281291
use test_case::test_case;
282292
type TestResult = std::result::Result<(), Box<dyn std::error::Error>>;
293+
use super::*;
294+
use crate::options::ClientConfig;
295+
use crate::options::InstrumentationClientInfo;
283296

284297
#[tokio::test]
285298
async fn client_http_error_bytes() -> TestResult {
@@ -405,4 +418,40 @@ mod tests {
405418
fn default_idempotency(input: Method, expected: bool) {
406419
assert!(super::default_idempotency(&input) == expected);
407420
}
421+
422+
static TEST_INSTRUMENTATION_INFO: InstrumentationClientInfo = InstrumentationClientInfo {
423+
service_name: "test-service",
424+
client_version: "1.2.3",
425+
client_artifact: "test-artifact",
426+
default_host: "test.googleapis.com",
427+
};
428+
429+
#[tokio::test]
430+
async fn reqwest_client_new() {
431+
let config = ClientConfig::default();
432+
let client = ReqwestClient::new(config, "https://test.googleapis.com")
433+
.await
434+
.unwrap();
435+
assert!(client.instrumentation.is_none());
436+
}
437+
438+
#[tokio::test]
439+
async fn reqwest_client_with_instrumentation() {
440+
let config = ClientConfig::default();
441+
let client = ReqwestClient::new(config, "https://test.googleapis.com")
442+
.await
443+
.unwrap();
444+
assert!(client.instrumentation.is_none());
445+
446+
let client = client.with_instrumentation(Some(TEST_INSTRUMENTATION_INFO));
447+
assert!(client.instrumentation.is_some());
448+
let info = client.instrumentation.unwrap();
449+
assert_eq!(info.service_name, "test-service");
450+
assert_eq!(info.client_version, "1.2.3");
451+
assert_eq!(info.client_artifact, "test-artifact");
452+
assert_eq!(info.default_host, "test.googleapis.com");
453+
454+
let client = client.with_instrumentation(None);
455+
assert!(client.instrumentation.is_none());
456+
}
408457
}

src/gax-internal/src/options.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ pub type ClientConfig = gax::client_builder::internal::ClientConfig<Credentials>
1919

2020
pub(crate) const LOGGING_VAR: &str = "GOOGLE_CLOUD_RUST_LOGGING";
2121

22+
/// Information about the client library used for instrumentation.
23+
#[derive(Copy, Clone, Debug)]
24+
pub struct InstrumentationClientInfo {
25+
/// The short service name, e.g., "appengine", "run", "firestore".
26+
pub service_name: &'static str,
27+
/// The version of the client library.
28+
pub client_version: &'static str,
29+
/// The name of the client library artifact (e.g., crate name).
30+
pub client_artifact: &'static str,
31+
/// The default hostname of the service.
32+
pub default_host: &'static str,
33+
}
34+
2235
// Returns true if the environment or client configuration enables tracing.
2336
pub fn tracing_enabled(config: &ClientConfig) -> bool {
2437
if config.tracing {

0 commit comments

Comments
 (0)