Skip to content

Commit

Permalink
Telemetry -> Fix runtime error when instanciating with custom reqwest…
Browse files Browse the repository at this point in the history
… client
  • Loading branch information
reimarstier committed Feb 7, 2025
1 parent bf5eb29 commit 3c96a73
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .ci/docker/carl/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ services:
- OPENDUT_CUSTOM_CA1
- OPENDUT_CUSTOM_CA2
- OPENDUT_HOSTS
# Logging
- OPENDUT_LOG=opentelemetry=debug # show opentelemetry exporter logs
# Rust backtrace
- RUST_BACKTRACE=1
healthcheck:
Expand Down
5 changes: 4 additions & 1 deletion .ci/docker/telemetry/otel-collector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ receivers:
protocols:
http:
endpoint: 0.0.0.0:4318
auth:
authenticator: oidc
grpc:
endpoint: 0.0.0.0:4317
auth:
authenticator: oidc

docker_stats:
endpoint: unix:///var/run/docker.sock
collection_interval: 15s
Expand Down Expand Up @@ -70,7 +73,7 @@ service:
processors: [ batch ]
exporters: [ debug, otlp/tempo ]
metrics:
receivers: [ otlp, docker_stats ]
receivers: [ otlp ]
processors: [ batch ]
exporters: [ debug, prometheus ]
logs:
Expand Down
2 changes: 1 addition & 1 deletion opendut-util/opendut-auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ reqwest = { workspace = true, default-features = false, features = ["blocking",
serde = { workspace = true, features = ["std", "derive"] }
shadow-rs = { workspace = true, default-features = false, optional = true, features = ["tzdb"] }
thiserror = { workspace = true }
tokio = { workspace = true, optional = true }
tokio = { workspace = true, optional = true, features = ["rt-multi-thread"] }
tonic = { workspace = true }
tower = { workspace = true, optional = true }
tracing = { workspace = true, optional = true }
Expand Down
6 changes: 4 additions & 2 deletions opendut-util/opendut-auth/src/confidential/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ impl ConfidentialClient {
.map_err(|error| ConfidentialClientError::UrlParse { message: String::from("Failed to derive token url from issuer url: "), cause: error })?;

let operation = || {
self.reqwest_client.client.get(token_endpoint.clone()).send()?;
Ok(())
tokio::task::block_in_place(|| {
self.reqwest_client.client.get(token_endpoint.clone()).send()?;
Ok(())
})
};

let backoff_result = operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ impl OidcBlockingReqwestClient {
}

fn build_client(ca_certificate: Pem) -> anyhow::Result<Client> {
let reqwest_certificate = Certificate::from_pem(ca_certificate.to_string().as_bytes().iter().as_slice())
.map_err(|cause| OidcClientError::LoadCustomCA(cause.to_string()))?;
let client = Client::builder()
.redirect(reqwest::redirect::Policy::none())
.add_root_certificate(reqwest_certificate)
.build()
.map_err(|cause| OidcClientError::LoadCustomCA(cause.to_string()))?;
Ok(client)
tokio::task::block_in_place(|| {
let reqwest_certificate = Certificate::from_pem(ca_certificate.to_string().as_bytes().iter().as_slice())
.map_err(|cause| OidcClientError::LoadCustomCA(cause.to_string()))?;
let client = Client::builder()
.redirect(reqwest::redirect::Policy::none())
.add_root_certificate(reqwest_certificate)
.build()
.map_err(|cause| OidcClientError::LoadCustomCA(cause.to_string()))?;
Ok(client)
})
}

pub fn from_pem(ca_certificate: Pem) -> anyhow::Result<Self> {
Expand All @@ -72,30 +74,31 @@ impl OidcBlockingReqwestClient {
.map_err(|cause| {
OidcClientError::AuthReqwest { message: cause.to_string(), status: cause.status().unwrap_or_default().to_string(), inner: cause }
})?;
let response = client.execute(request)
.map_err(|cause: reqwest::Error| {
OidcClientError::AuthReqwest { message: cause.to_string(), status: cause.status().unwrap_or_default().to_string(), inner: cause }
})?;
let status_code = response.status();
let headers = response.headers().to_owned();
let data = response.bytes()
.map_err(|cause| {
OidcClientError::AuthReqwest { message: cause.to_string(), status: cause.status().unwrap_or_default().to_string(), inner: cause }
})?;

let returned_response = {
let mut returned_response = http::Response::builder()
.status(status_code);
for (name, value) in headers.iter() {
returned_response = returned_response.header(name, value);
}
returned_response
.body(data.to_vec())
tokio::task::block_in_place(move || {
let response = client.execute(request)
.map_err(|cause: reqwest::Error| {
OidcClientError::AuthReqwest { message: cause.to_string(), status: cause.status().unwrap_or_default().to_string(), inner: cause }
})?;
let status_code = response.status();
let headers = response.headers().to_owned();
let data = response.bytes()
.map_err(|cause| {
OidcClientError::Other(format!("Failed to build response body: {cause}"))
})?
};
OidcClientError::AuthReqwest { message: cause.to_string(), status: cause.status().unwrap_or_default().to_string(), inner: cause }
})?;

Ok(returned_response)
let returned_response = {
let mut returned_response = http::Response::builder()
.status(status_code);
for (name, value) in headers.iter() {
returned_response = returned_response.header(name, value);
}
returned_response
.body(data.to_vec())
.map_err(|cause| {
OidcClientError::Other(format!("Failed to build response body: {cause}"))
})?
};
Ok(returned_response)
})
}
}

0 comments on commit 3c96a73

Please sign in to comment.