Skip to content

Commit

Permalink
Sync api metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kushudai committed Nov 22, 2024
1 parent bba9696 commit c58206b
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ SOFTWARE.
//! This crate provides access to a set of strongly typed apis to interact with consul (https://www.consul.io/)
#![deny(missing_docs)]

use http::StatusCode;
use http_body_util::BodyExt;

#[cfg(feature = "metrics")]
Expand Down Expand Up @@ -398,6 +399,9 @@ impl Consul {
// TODO: Emit OpenTelemetry span for this request

let url = self.build_create_or_update_url(request);
#[cfg(feature = "metrics")]
let mut metrics_info_wrapper =
MetricInfoWrapper::new(HttpMethod::Put, Function::CreateOrUpdateKey, None, self.metrics_tx.clone());
let result = ureq::put(&url)
.set(
"X-Consul-Token",
Expand All @@ -406,10 +410,15 @@ impl Consul {
.send_bytes(&value);

let response = result.map_err(|e| match e {
ureq::Error::Status(code, response) => ConsulError::UnexpectedResponseCode(
hyper::StatusCode::from_u16(code).unwrap_or_default(),
response.into_string().unwrap_or_default(),
),
ureq::Error::Status(code, response) => {
let code = hyper::StatusCode::from_u16(code).unwrap_or_default();
#[cfg(feature = "metrics")]
{
metrics_info_wrapper.set_status(code);
drop(metrics_info_wrapper.clone());
}
ConsulError::UnexpectedResponseCode(code, response.into_string().unwrap_or_default())
},
ureq::Error::Transport(t) => {
ConsulError::TransportError(t.kind(), t.message().unwrap_or_default().to_string())
}
Expand All @@ -418,6 +427,11 @@ impl Consul {
if status == 200 {
let val = response.into_string()?;
let response: bool = std::str::FromStr::from_str(val.trim())?;
#[cfg(feature = "metrics")]
{
metrics_info_wrapper.set_status(StatusCode::OK);
drop(metrics_info_wrapper.clone());
}
return Ok(response);
}

Expand Down

0 comments on commit c58206b

Please sign in to comment.