55use std:: time:: Duration ;
66
77use qcs_api_client_common:: configuration:: { ClientConfiguration , TokenError } ;
8+ #[ cfg( feature = "tracing" ) ]
9+ use qcs_api_client_grpc:: tonic:: wrap_channel_with_tracing;
10+ #[ cfg( feature = "tracing" ) ]
11+ use qcs_api_client_grpc:: tonic:: CustomTraceService ;
812#[ cfg( feature = "grpc-web" ) ]
913use qcs_api_client_grpc:: tonic:: { wrap_channel_with_grpc_web, GrpcWebWrapperLayerService } ;
1014use qcs_api_client_grpc:: {
@@ -15,6 +19,7 @@ use qcs_api_client_grpc::{
1519 } ,
1620} ;
1721use qcs_api_client_openapi:: apis:: configuration:: Configuration as OpenApiConfiguration ;
22+ #[ cfg( not( any( feature = "grpc-web" , feature = "tracing" ) ) ) ]
1823use tonic:: transport:: Channel ;
1924use tonic:: Status ;
2025
@@ -29,17 +34,27 @@ const DEFAULT_MAX_MESSAGE_DECODING_SIZE: usize = 50 * 1024 * 1024;
2934/// It is public so that users can create gRPC clients with different APIs using a "raw" connection
3035/// initialized by this library. This ensures that the exact Tonic version used for such clients
3136/// matches what this library uses.
32- #[ cfg( not( feature = "grpc-web" ) ) ]
37+ #[ cfg( not( any ( feature = "grpc-web" , feature = "tracing" ) ) ) ]
3338pub type GrpcConnection = RetryService < RefreshService < Channel , ClientConfiguration > > ;
3439
3540/// A type alias for the underlying gRPC connection used by all gRPC clients within this library.
3641/// It is public so that users can create gRPC clients with different APIs using a "raw" connection
3742/// initialized by this library. This ensures that the exact Tonic version used for such clients
3843/// matches what this library uses.
39- #[ cfg( feature = "grpc-web" ) ]
44+ #[ cfg( all ( feature = "grpc-web" , not ( feature = "tracing" ) ) ) ]
4045pub type GrpcConnection =
4146 GrpcWebWrapperLayerService < RetryService < RefreshService < Channel , ClientConfiguration > > > ;
4247
48+ /// A type alias for the underlying gRPC connection used by all gRPC clients within this library.
49+ /// It is public so that users can create gRPC clients with different APIs using a "raw" connection
50+ /// initialized by this library. This ensures that the exact Tonic version used for such clients
51+ /// matches what this library uses.
52+ ///
53+ /// The underlying [`tonic::transport::Channel`] is wrapped by [`CustomTraceService`],
54+ /// [`RefreshService`], and [`RetryService`] to provide tracing, token refresh, and retry.
55+ #[ cfg( all( not( feature = "grpc-web" ) , feature = "tracing" ) ) ]
56+ pub type GrpcConnection = RetryService < RefreshService < CustomTraceService , ClientConfiguration > > ;
57+
4358/// TODO: make configurable at the client level.
4459/// <https://github.com/rigetti/qcs-sdk-rust/issues/239>
4560pub ( crate ) static DEFAULT_HTTP_API_TIMEOUT : Duration = Duration :: from_secs ( 10 ) ;
@@ -103,11 +118,25 @@ impl Qcs {
103118 ) -> Result < TranslationClient < GrpcConnection > , GrpcError < TokenError > > {
104119 let uri = parse_uri ( translation_grpc_endpoint) ?;
105120 let channel = get_channel ( uri) ?;
106- let service =
107- wrap_channel_with_retry ( wrap_channel_with ( channel, self . get_config ( ) . clone ( ) ) ) ;
121+
122+ // First add tracing if enabled
123+ #[ cfg( feature = "tracing" ) ]
124+ let channel = wrap_channel_with_tracing (
125+ channel,
126+ translation_grpc_endpoint. to_string ( ) ,
127+ self . get_config ( )
128+ . tracing_configuration ( )
129+ . cloned ( )
130+ . unwrap_or_default ( ) ,
131+ ) ;
132+
133+ // Then wrap with refresh and retry
134+ let channel = wrap_channel_with ( channel, self . get_config ( ) . clone ( ) ) ;
135+ let channel = wrap_channel_with_retry ( channel) ;
136+
108137 #[ cfg( feature = "grpc-web" ) ]
109- let service = wrap_channel_with_grpc_web ( service) ;
110- Ok ( TranslationClient :: new ( service )
138+ let channel = wrap_channel_with_grpc_web ( service) ;
139+ Ok ( TranslationClient :: new ( channel )
111140 . max_encoding_message_size ( DEFAULT_MAX_MESSAGE_ENCODING_SIZE )
112141 . max_decoding_message_size ( DEFAULT_MAX_MESSAGE_DECODING_SIZE ) )
113142 }
0 commit comments