From 78d344db97a94d72114e32dfdf18fa9086143188 Mon Sep 17 00:00:00 2001 From: Jesse White Date: Thu, 2 May 2024 20:33:22 -0400 Subject: [PATCH] wire up a custom http client --- .gitignore | 1 + Cargo.lock | 1 + Cargo.toml | 1 + src/http.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ 5 files changed, 47 insertions(+) create mode 100644 src/http.rs diff --git a/.gitignore b/.gitignore index f31f42e..d3478cf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /node_modules .env .idea +.wrangler diff --git a/Cargo.lock b/Cargo.lock index e7e7f90..bdf8e4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1788,6 +1788,7 @@ version = "0.1.0" dependencies = [ "getrandom", "graphql_client", + "http 0.2.12", "opentelemetry", "opentelemetry-http", "opentelemetry-otlp", diff --git a/Cargo.toml b/Cargo.toml index a0a6df8..7520b46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/http.rs b/src/http.rs new file mode 100644 index 0000000..ddd48ea --- /dev/null +++ b/src/http.rs @@ -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> { + 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>) -> Result, HttpError> { + let res = Response::builder() + .status(200) + .body(Bytes::new()) + .unwrap(); + Ok(res) + } + + +} diff --git a/src/lib.rs b/src/lib.rs index 7393767..ccf9075 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ use opentelemetry_sdk::metrics::SdkMeterProvider; use worker::*; mod gql; +mod http; fn init_metrics() -> core::result::Result { @@ -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();