diff --git a/src/lib.rs b/src/lib.rs index 41cd142..4cca1b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,11 +11,23 @@ mod metrics; #[worker::send] pub async fn do_fetch( url: String, + headers: String, data: Option, ) -> Result { + let mut http_headers = Headers::new(); + // split headers by command, and then by = + for header in headers.split(",") { + let parts: Vec<&str> = header.split("=").collect(); + if parts.len() == 2 { + let key = parts[0].trim(); + let value = parts[1].trim(); + http_headers.set(key, value).expect("failed to construct header"); + } + } + let mut init = RequestInit::new(); init.method = Method::Post; - init.with_body(data); + init.with_body(data).with_headers(http_headers); Fetch::Request(Request::new_with_init(url.as_str(), &init)?) .send() .await @@ -27,6 +39,10 @@ async fn main(_req: Request, env: Env, _ctx: Context) -> Result { let cloudflare_api_url = env.var("CLOUDFLARE_API_URL")?.to_string(); let cloudflare_api_key = env.var("CLOUDFLARE_API_KEY")?.to_string(); let cloudflare_account_id = env.var("CLOUDFLARE_ACCOUNT_ID")?.to_string(); + let otlp_headers = match env.var("OTLP_HEADERS") { + Ok(val) => val.to_string(), + Err(_) => String::from(""), + }; let end = chrono::Utc::now(); let start = end - chrono::Duration::minutes(1); @@ -61,6 +77,6 @@ async fn main(_req: Request, env: Env, _ctx: Context) -> Result { }; let metrics = MetricsData::from(&mut resource_metrics); let metrics_json = serde_json::to_string(&metrics).unwrap(); - let response = do_fetch(metrics_url, Some(JsValue::from_str(&metrics_json)).into()).await?; + let response = do_fetch(metrics_url, otlp_headers, Some(JsValue::from_str(&metrics_json)).into()).await?; return Ok(response); } diff --git a/wrangler.toml b/wrangler.toml index 5964670..d2e8639 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -15,6 +15,7 @@ build = { command = "cargo install -q worker-build && worker-build --dev" } [env.dev.vars] METRICS_URL = "http://collector:4318/v1/metrics" +OTLP_HEADERS = "Authorization=Basic " CLOUDFLARE_API_URL = "https://api.cloudflare.com/client/v4/graphql" CLOUDFLARE_API_KEY = "fake" CLOUDFLARE_ACCOUNT_ID = "1234"