Skip to content

Commit

Permalink
GSB http proxy - logging reponses, status codes and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Sep 9, 2024
1 parent 01fcd8c commit 2fc0e1f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
43 changes: 28 additions & 15 deletions exe-unit/components/gsb-http-proxy/src/gsb_to_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ impl GsbToHttpProxy {

pub fn bind(&mut self, gsb_path: &str) -> Handle {
let this = self.clone();
bus::bind(gsb_path, move |message: GsbHttpCallMessage| {
bus::bind_with_caller(gsb_path, move |caller, message: GsbHttpCallMessage| {
let mut this = this.clone();
async move { Ok(this.pass(message).await) }
async move { Ok(this.pass(caller, message).await) }
})
}

Expand All @@ -67,10 +67,13 @@ impl GsbToHttpProxy {
})
}

pub async fn pass(&mut self, message: GsbHttpCallMessage) -> GsbHttpCallResponse {
pub async fn pass(
&mut self,
caller: String,
message: GsbHttpCallMessage,
) -> GsbHttpCallResponse {
let url = format!("{}{}", self.base_url, message.path);
log::info!("Gsb to http call - Url: {url}");

let path = message.path.clone();
let mut counters = self.counters.clone();

let method = match Method::from_bytes(message.method.to_uppercase().as_bytes()) {
Expand All @@ -82,9 +85,10 @@ impl GsbToHttpProxy {
)
}
};
let builder = Self::create_request_builder(method, &url, message.headers, message.body);
let builder =
Self::create_request_builder(method.clone(), &url, message.headers, message.body);

log::debug!("Calling {}", &url);
log::info!("Gsb proxy http call {method} to {url} from {caller}");
let response_handler = counters.on_request();
let response = builder
.send()
Expand All @@ -98,18 +102,27 @@ impl GsbToHttpProxy {
response_handler.on_response();
match response.bytes().await {
Ok(bytes) => {
log::info!(
"GSB http proxy: response for {method} `{path}` status: {status_code}"
);
GsbHttpCallResponse::new(bytes.to_vec(), response_headers, status_code)
}
Err(err) => GsbHttpCallResponse::with_message(
format!("Error in response: {err}").into_bytes(),
StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
),
Err(err) => {
log::info!("GSB http proxy: response for {method} `{path}` status: {status_code}, error: {err}");
GsbHttpCallResponse::with_message(
format!("Error in response: {err}").into_bytes(),
StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
)
}
}
}
Err(err) => GsbHttpCallResponse::with_message(
format!("Error in response: {err}").into_bytes(),
StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
),
Err(err) => {
log::info!("GSB http proxy: error calling {method} `{path}`: {err}");
GsbHttpCallResponse::with_message(
format!("Error in response: {err}").into_bytes(),
StatusCode::INTERNAL_SERVER_ERROR.as_u16(),
)
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions exe-unit/components/gsb-http-proxy/src/http_to_gsb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ impl HttpToGsbProxy {
};

let msg = GsbHttpCallMessage {
method,
path,
method: method.clone(),
path: path.clone(),
body,
headers: Headers::default().filter(&headers),
};
Expand All @@ -93,14 +93,14 @@ impl HttpToGsbProxy {

log::info!("Proxy http {msg} call to [{}]", endpoint.addr());
let result = endpoint
.call(msg.clone())
.call(msg)
.await
.unwrap_or_else(|e| Err(HttpProxyStatusError::from(e)));

match result {
Ok(r) => {
log::info!(
"Http proxy response for {msg} call to [{}]: status: {}",
"Http proxy: response for {method} `{path}` call to [{}]: status: {}",
endpoint.addr(),
r.header.status_code
);
Expand All @@ -116,7 +116,7 @@ impl HttpToGsbProxy {
}
Err(err) => {
log::warn!(
"Http proxy error calling {msg} at [{}]: error: {err}",
"Http proxy: error calling {method} `{path}` at [{}]: error: {err}",
endpoint.addr()
);
HttpToGsbProxyResponse {
Expand Down

0 comments on commit 2fc0e1f

Please sign in to comment.