Skip to content

Commit

Permalink
wire up a custom http client
Browse files Browse the repository at this point in the history
  • Loading branch information
j-white committed May 3, 2024
1 parent 904eda4 commit 78d344d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/node_modules
.env
.idea
.wrangler
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ getrandom = { version = "0.2.14", features = ["js"] }
serde_json = "1.0.116"
opentelemetry-http = { version="0.11.1" }
opentelemetry-otlp = { version="0.15.0", features = ["metrics", "http-proto"], default-features = false }
http = { version = "0.2.12"}

[profile.release]
opt-level = "s" # optimize for size in release builds
Expand Down
42 changes: 42 additions & 0 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::fmt::{Debug, Formatter};
use std::future::Future;
use opentelemetry_http::{Bytes, HttpClient, HttpError, Request, Response};
use worker::async_trait::async_trait;
use std::error::Error;

pub struct MyClient {
inner: reqwest::Client,
}

impl MyClient {
pub fn new() -> MyClient {
let inner = reqwest::Client::new();
Self { inner }
}

pub fn execute(
&self,
request: reqwest::Request,
) -> impl Future<Output = Result<reqwest::Response, reqwest::Error>> {
self.inner.execute(request)
}
}

impl Debug for MyClient {
fn fmt(&self, _f: &mut Formatter<'_>) -> std::fmt::Result {
todo!()
}
}

#[async_trait]
impl HttpClient for MyClient {
async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
let res = Response::builder()
.status(200)
.body(Bytes::new())
.unwrap();
Ok(res)
}


}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use opentelemetry_sdk::metrics::SdkMeterProvider;
use worker::*;

mod gql;
mod http;

fn init_metrics() ->
core::result::Result<SdkMeterProvider, MetricsError> {
Expand All @@ -17,6 +18,7 @@ fn init_metrics() ->
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_http_client(http::MyClient::new())
.with_export_config(export_config),
)
.build();
Expand Down

0 comments on commit 78d344d

Please sign in to comment.