Skip to content

Commit

Permalink
refactor(dynamic_routing): add info logs to log the grpc request and …
Browse files Browse the repository at this point in the history
…response
  • Loading branch information
Chethan-rao committed Dec 30, 2024
1 parent 8092c1f commit 2556a28
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 40 deletions.
9 changes: 9 additions & 0 deletions crates/external_services/src/grpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,12 @@ impl<T> AddHeaders for tonic::Request<T> {
});
}
}

pub(crate) fn create_grpc_request<T: Debug>(message: T, headers: GrpcHeaders) -> tonic::Request<T> {
let mut request = tonic::Request::new(message);
request.add_headers_to_grpc_request(headers);

logger::info!(dynamic_routing_request=?request);

request
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use elimination_rate::{
LabelWithBucketName, UpdateEliminationBucketRequest, UpdateEliminationBucketResponse,
};
use error_stack::ResultExt;
use router_env::{instrument, logger, tracing};
#[allow(
missing_docs,
unused_qualifications,
Expand All @@ -21,7 +22,7 @@ pub mod elimination_rate {
}

use super::{Client, DynamicRoutingError, DynamicRoutingResult};
use crate::grpc_client::{AddHeaders, GrpcHeaders};
use crate::grpc_client::{self, GrpcHeaders};

/// The trait Elimination Based Routing would have the functions required to support performance, calculation and invalidation bucket
#[async_trait::async_trait]
Expand Down Expand Up @@ -54,6 +55,7 @@ pub trait EliminationBasedRouting: dyn_clone::DynClone + Send + Sync {

#[async_trait::async_trait]
impl EliminationBasedRouting for EliminationAnalyserClient<Client> {
#[instrument(skip_all)]
async fn perform_elimination_routing(
&self,
id: String,
Expand All @@ -69,14 +71,15 @@ impl EliminationBasedRouting for EliminationAnalyserClient<Client> {

let config = configs.map(ForeignTryFrom::foreign_try_from).transpose()?;

let mut request = tonic::Request::new(EliminationRequest {
id,
params,
labels,
config,
});

request.add_headers_to_grpc_request(headers);
let request = grpc_client::create_grpc_request(
EliminationRequest {
id,
params,
labels,
config,
},
headers,
);

let response = self
.clone()
Expand All @@ -87,9 +90,12 @@ impl EliminationBasedRouting for EliminationAnalyserClient<Client> {
))?
.into_inner();

logger::info!(dynamic_routing_response=?response);

Ok(response)
}

#[instrument(skip_all)]
async fn update_elimination_bucket_config(
&self,
id: String,
Expand All @@ -110,14 +116,15 @@ impl EliminationBasedRouting for EliminationAnalyserClient<Client> {
})
.collect::<Vec<_>>();

let mut request = tonic::Request::new(UpdateEliminationBucketRequest {
id,
params,
labels_with_bucket_name,
config,
});

request.add_headers_to_grpc_request(headers);
let request = grpc_client::create_grpc_request(
UpdateEliminationBucketRequest {
id,
params,
labels_with_bucket_name,
config,
},
headers,
);

let response = self
.clone()
Expand All @@ -127,16 +134,19 @@ impl EliminationBasedRouting for EliminationAnalyserClient<Client> {
"Failed to update the elimination bucket".to_string(),
))?
.into_inner();

logger::info!(dynamic_routing_response=?response);

Ok(response)
}

#[instrument(skip_all)]
async fn invalidate_elimination_bucket(
&self,
id: String,
headers: GrpcHeaders,
) -> DynamicRoutingResult<InvalidateBucketResponse> {
let mut request = tonic::Request::new(InvalidateBucketRequest { id });

request.add_headers_to_grpc_request(headers);
let request = grpc_client::create_grpc_request(InvalidateBucketRequest { id }, headers);

let response = self
.clone()
Expand All @@ -146,6 +156,9 @@ impl EliminationBasedRouting for EliminationAnalyserClient<Client> {
"Failed to invalidate the elimination bucket".to_string(),
))?
.into_inner();

logger::info!(dynamic_routing_response=?response);

Ok(response)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use api_models::routing::{
};
use common_utils::{ext_traits::OptionExt, transformers::ForeignTryFrom};
use error_stack::ResultExt;
use router_env::{instrument, logger, tracing};
pub use success_rate::{
success_rate_calculator_client::SuccessRateCalculatorClient, CalSuccessRateConfig,
CalSuccessRateRequest, CalSuccessRateResponse,
Expand All @@ -21,7 +22,7 @@ pub mod success_rate {
tonic::include_proto!("success_rate");
}
use super::{Client, DynamicRoutingError, DynamicRoutingResult};
use crate::grpc_client::{AddHeaders, GrpcHeaders};
use crate::grpc_client::{self, GrpcHeaders};
/// The trait Success Based Dynamic Routing would have the functions required to support the calculation and updation window
#[async_trait::async_trait]
pub trait SuccessBasedDynamicRouting: dyn_clone::DynClone + Send + Sync {
Expand Down Expand Up @@ -53,6 +54,7 @@ pub trait SuccessBasedDynamicRouting: dyn_clone::DynClone + Send + Sync {

#[async_trait::async_trait]
impl SuccessBasedDynamicRouting for SuccessRateCalculatorClient<Client> {
#[instrument(skip_all)]
async fn calculate_success_rate(
&self,
id: String,
Expand All @@ -71,14 +73,15 @@ impl SuccessBasedDynamicRouting for SuccessRateCalculatorClient<Client> {
.map(ForeignTryFrom::foreign_try_from)
.transpose()?;

let mut request = tonic::Request::new(CalSuccessRateRequest {
id,
params,
labels,
config,
});

request.add_headers_to_grpc_request(headers);
let request = grpc_client::create_grpc_request(
CalSuccessRateRequest {
id,
params,
labels,
config,
},
headers,
);

let response = self
.clone()
Expand All @@ -89,9 +92,12 @@ impl SuccessBasedDynamicRouting for SuccessRateCalculatorClient<Client> {
))?
.into_inner();

logger::info!(dynamic_routing_response=?response);

Ok(response)
}

#[instrument(skip_all)]
async fn update_success_rate(
&self,
id: String,
Expand All @@ -113,14 +119,15 @@ impl SuccessBasedDynamicRouting for SuccessRateCalculatorClient<Client> {
})
.collect();

let mut request = tonic::Request::new(UpdateSuccessRateWindowRequest {
id,
params,
labels_with_status,
config,
});

request.add_headers_to_grpc_request(headers);
let request = grpc_client::create_grpc_request(
UpdateSuccessRateWindowRequest {
id,
params,
labels_with_status,
config,
},
headers,
);

let response = self
.clone()
Expand All @@ -131,16 +138,18 @@ impl SuccessBasedDynamicRouting for SuccessRateCalculatorClient<Client> {
))?
.into_inner();

logger::info!(dynamic_routing_response=?response);

Ok(response)
}

#[instrument(skip_all)]
async fn invalidate_success_rate_routing_keys(
&self,
id: String,
headers: GrpcHeaders,
) -> DynamicRoutingResult<InvalidateWindowsResponse> {
let mut request = tonic::Request::new(InvalidateWindowsRequest { id });

request.add_headers_to_grpc_request(headers);
let request = grpc_client::create_grpc_request(InvalidateWindowsRequest { id }, headers);

let response = self
.clone()
Expand All @@ -150,6 +159,9 @@ impl SuccessBasedDynamicRouting for SuccessRateCalculatorClient<Client> {
"Failed to invalidate the success rate routing keys".to_string(),
))?
.into_inner();

logger::info!(dynamic_routing_response=?response);

Ok(response)
}
}
Expand Down

0 comments on commit 2556a28

Please sign in to comment.