@@ -39,6 +39,7 @@ pub struct ReqwestClient {
39
39
retry_throttler : SharedRetryThrottler ,
40
40
polling_error_policy : Arc < dyn PollingErrorPolicy > ,
41
41
polling_backoff_policy : Arc < dyn PollingBackoffPolicy > ,
42
+ instrumentation : Option < crate :: options:: InstrumentationClientInfo > ,
42
43
}
43
44
44
45
impl ReqwestClient {
@@ -79,9 +80,18 @@ impl ReqwestClient {
79
80
polling_backoff_policy : config
80
81
. polling_backoff_policy
81
82
. unwrap_or_else ( || Arc :: new ( ExponentialBackoff :: default ( ) ) ) ,
83
+ instrumentation : None ,
82
84
} )
83
85
}
84
86
87
+ pub fn with_instrumentation (
88
+ mut self ,
89
+ instrumentation : Option < crate :: options:: InstrumentationClientInfo > ,
90
+ ) -> Self {
91
+ self . instrumentation = instrumentation;
92
+ self
93
+ }
94
+
85
95
pub fn builder ( & self , method : Method , path : String ) -> reqwest:: RequestBuilder {
86
96
self . inner
87
97
. request ( method, format ! ( "{}{path}" , & self . endpoint) )
@@ -280,6 +290,9 @@ mod tests {
280
290
use http:: { HeaderMap , HeaderValue , Method } ;
281
291
use test_case:: test_case;
282
292
type TestResult = std:: result:: Result < ( ) , Box < dyn std:: error:: Error > > ;
293
+ use super :: * ;
294
+ use crate :: options:: ClientConfig ;
295
+ use crate :: options:: InstrumentationClientInfo ;
283
296
284
297
#[ tokio:: test]
285
298
async fn client_http_error_bytes ( ) -> TestResult {
@@ -405,4 +418,40 @@ mod tests {
405
418
fn default_idempotency ( input : Method , expected : bool ) {
406
419
assert ! ( super :: default_idempotency( & input) == expected) ;
407
420
}
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
+ }
408
457
}
0 commit comments