Skip to content

Commit

Permalink
add http client
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Oct 21, 2023
1 parent a62e0b3 commit bbc9311
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/kitsune-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ eyre = "0.6.8"
serde = { version = "1.0.189", features = ["derive"] }
smol_str = { version = "0.2.0", features = ["serde"] }
tokio = { version = "1.33.0", features = ["fs"] }
toml = "0.8.2"
toml = { version = "0.8.2", default-features = false, features = ["parse"] }
1 change: 0 additions & 1 deletion crates/kitsune-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ speedy-uuid = { path = "../../lib/speedy-uuid", features = ["diesel"] }
thiserror = "1.0.50"
time = "0.3.30"
tokio = { version = "1.33.0", features = ["macros", "rt"] }
toml = { version = "0.8.2", default-features = false, features = ["parse"] }
tracing = "0.1.40"
typed-builder = "0.18.0"
url = "2.4.1"
Expand Down
6 changes: 6 additions & 0 deletions crates/kitsune-http-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,12 @@ pub struct Response {
}

impl Response {
/// Convert the response into its inner `hyper` representation
#[must_use]
pub fn into_inner(self) -> HyperResponse<BoxBody<Bytes, BoxError>> {
self.inner
}

/// Read the body into a `Bytes`
///
/// # Errors
Expand Down
11 changes: 7 additions & 4 deletions crates/kitsune-observability/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ edition.workspace = true
version.workspace = true

[dependencies]
async-trait = "0.1.74"
eyre = "0.6.8"
hyper = { version = "0.14.27", default-features = false }
kitsune-config = { path = "../kitsune-config" }
kitsune-http-client = { path = "../kitsune-http-client" }
metrics = "0.21.1"
metrics-opentelemetry = { git = "https://github.com/aumetra/metrics-opentelemetry.git", rev = "7c3176266c215bb9a7cbc31b3c32f75a22824928" }
metrics-tracing-context = "0.14.0"
metrics-util = "0.15.1"
opentelemetry = { version = "0.20.0", default-features = false, features = [
"rt-tokio",
"trace",
] }
opentelemetry-http = "0.9.0"
opentelemetry-otlp = { version = "0.13.0", default-features = false, features = [
"http-proto",
"metrics",
"trace",
] }
opentelemetry = { version = "0.20.0", default-features = false, features = [
"rt-tokio",
"trace",
] }
tracing = "0.1.40"
tracing-error = "0.2.0"
tracing-opentelemetry = { version = "0.21.0", default-features = false }
Expand Down
42 changes: 39 additions & 3 deletions crates/kitsune-observability/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use async_trait::async_trait;
use eyre::Context;
use hyper::body::Body;
use kitsune_config::Configuration;
use metrics_opentelemetry::OpenTelemetryRecorder;
use metrics_tracing_context::{MetricsLayer, TracingContextLayer};
Expand All @@ -8,8 +10,9 @@ use opentelemetry::{
runtime::Tokio,
trace::{noop::NoopTracer, Tracer},
};
use opentelemetry_http::{Bytes, HttpClient, HttpError, Request, Response};
use opentelemetry_otlp::WithExportConfig;
use std::env;
use std::{env, fmt};
use tracing_error::ErrorLayer;
use tracing_opentelemetry::{OpenTelemetryLayer, PreSampledTracer};
use tracing_subscriber::{
Expand All @@ -18,6 +21,33 @@ use tracing_subscriber::{
Layer as _, Registry,
};

#[derive(Clone)]
struct HttpClientAdapter {
inner: kitsune_http_client::Client,
}

impl fmt::Debug for HttpClientAdapter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "HttpClientAdapter")
}
}

#[async_trait]
impl HttpClient for HttpClientAdapter {
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
let (parts, body) = request.into_parts();
let body = Body::from(body);
let request = Request::from_parts(parts, body);

let response = self.inner.execute(request).await?.into_inner();

let (parts, body) = response.into_parts();
let body = hyper::body::to_bytes(body).await?;

Ok(Response::from_parts(parts, body))
}
}

fn initialise_logging<T>(tracer: T) -> eyre::Result<()>
where
T: Tracer + PreSampledTracer + Send + Sync + 'static,
Expand Down Expand Up @@ -50,12 +80,17 @@ fn initialise_metrics(meter: Meter) -> eyre::Result<()> {

pub fn initialise(app_name: &'static str, config: &Configuration) -> eyre::Result<()> {
if let Some(ref opentelemetry_config) = config.opentelemetry {
let http_client = HttpClientAdapter {
inner: kitsune_http_client::Client::default(),
};

let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_endpoint(opentelemetry_config.http_endpoint.as_str()),
.with_endpoint(opentelemetry_config.http_endpoint.as_str())
.with_http_client(http_client.clone()),
)
.install_batch(Tokio)?;

Expand All @@ -66,7 +101,8 @@ pub fn initialise(app_name: &'static str, config: &Configuration) -> eyre::Resul
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_endpoint(opentelemetry_config.http_endpoint.as_str()),
.with_endpoint(opentelemetry_config.http_endpoint.as_str())
.with_http_client(http_client.clone()),
)
.build()?;

Expand Down

0 comments on commit bbc9311

Please sign in to comment.