Skip to content

Commit

Permalink
Merge pull request #12 from G-Core/feat/243-write-region-to-stats
Browse files Browse the repository at this point in the history
feat: adding region field to stats and string perf optimisations
  • Loading branch information
ruslanti authored Jun 17, 2024
2 parents 48c8b5d + 5567b58 commit 872dbde
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 74 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["crates/*"]
resolver = "2"

[workspace.package]
version = "0.3.10"
version = "0.4.0"
edition = "2021"
publish = false
authors = ["FastEdge Development Team"]
Expand All @@ -21,7 +21,7 @@ wasi-common = { version = "20.0" }
wasmtime-wasi-nn = { version = "20.0" }
clap = { version = "4", features = ["derive"] }
moka = { version = "0.12", features = ["sync"] }
smol_str = "0.2.1"
smol_str = { version = "0.2.1", features = ["serde"] }
anyhow = "1.0"


Expand Down
9 changes: 5 additions & 4 deletions crates/http-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ authors.workspace = true
default = []
metrics = ["runtime/metrics"]
stats = ["runtime/stats"]
tls = ["tokio-rustls", "rustls-pemfile", "hyper-rustls", "rustls"]

[dependencies]
anyhow = { workspace = true }
Expand All @@ -22,10 +23,10 @@ wasmtime-wasi-nn = { workspace = true }
wasi-common = { workspace = true }
tracing = { workspace = true }
smol_str = { workspace = true }
tokio-rustls = "0.24.1"
rustls-pemfile = "1.0.2"
hyper-rustls = "0.24.1"
rustls = "0.21.6"
tokio-rustls = { version = "0.24.1", optional = true}
rustls-pemfile = { version = "1.0.2" , optional = true}
hyper-rustls = { version = "0.24.1", optional = true }
rustls = { version = "0.21.6", optional = true }
reactor = { path = "../reactor" }
runtime = { path = "../runtime" }
http-backend = { path = "../http-backend" }
Expand Down
54 changes: 27 additions & 27 deletions crates/http-service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use std::borrow::Cow;
use std::net::SocketAddr;
use std::sync::Arc;

use anyhow::{anyhow, bail, Context, Error, Result};
use anyhow::{ bail, Context, Error, Result};
use http::header::{ACCESS_CONTROL_ALLOW_ORIGIN, CACHE_CONTROL};
use http::{HeaderMap, HeaderName, HeaderValue, Request, Response, StatusCode};
use hyper::client::connect::Connect;
use hyper::server::conn::{AddrIncoming, AddrStream};
use hyper::server::conn::AddrStream;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Server};
use smol_str::SmolStr;
use tokio_rustls::rustls;
use tokio_rustls::rustls::ServerConfig;
use tokio_util::sync::CancellationToken;
use tracing::{debug, error_span, info, info_span, trace, warn, Instrument};
use tracing::{debug, info, info_span, trace, warn, Instrument};
use wasi_common::I32Exit;
use wasmtime::Trap;
use wasmtime_wasi_nn::WasiNnCtx;
Expand All @@ -30,11 +27,14 @@ use crate::executor::{ HttpExecutor};
use runtime::util::stats::StatRow;
use runtime::util::stats::StatsWriter;

#[cfg(feature = "tls")]
use crate::tls::{load_certs, load_private_key, TlsAcceptor, TlsStream};
pub mod executor;

#[cfg(feature = "tls")]
mod tls;

pub mod executor;

pub use crate::executor::ExecutorFactory;

pub(crate) static TRACEPARENT: &str = "traceparent";
Expand All @@ -44,14 +44,18 @@ const FASTEDGE_OUT_OF_MEMORY: u16 = 531;
const FASTEDGE_EXECUTION_TIMEOUT: u16 = 532;
const FASTEDGE_EXECUTION_PANIC: u16 = 533;

#[cfg(feature = "tls")]
#[derive(Default)]
pub struct HttpsConfig {
pub ssl_certs: &'static str,
pub ssl_pkey: &'static str,
}

#[derive(Default)]
pub struct HttpConfig {
pub all_interfaces: bool,
pub port: u16,
#[cfg(feature = "tls")]
pub https: Option<HttpsConfig>,
pub cancel: CancellationToken,
}
Expand All @@ -67,7 +71,7 @@ pub struct HttpState<C> {
}

pub trait ContextHeaders {
fn append_headers(&self) -> impl Iterator<Item = (Cow<str>, Cow<str>)>;
fn append_headers(&self) -> impl Iterator<Item = (SmolStr, SmolStr)>;
}

impl<T> Service for HttpService<T>
Expand Down Expand Up @@ -100,6 +104,8 @@ where
};
let listen_addr = (interface, config.port).into();


#[cfg(feature = "tls")]
if let Some(https) = config.https {
let tls = {
// Load public certificate.
Expand All @@ -111,7 +117,7 @@ where
.with_safe_defaults()
.with_no_client_auth()
.with_single_cert(certs, key)
.map_err(|e| anyhow!(format!("{}", e)))?;
.map_err(|e| anyhow::anyhow!(format!("{}", e)))?;
// Configure ALPN to accept HTTP/2, HTTP/1.1 in that order.
cfg.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
Arc::new(cfg)
Expand All @@ -121,6 +127,9 @@ where
self.serve(listen_addr).await?
};

#[cfg(not(feature = "tls"))]
self.serve(listen_addr).await?;

Ok(())
}

Expand Down Expand Up @@ -177,7 +186,8 @@ where
Ok(())
}

async fn serve_tls(self, listen_addr: SocketAddr, tls: Arc<ServerConfig>) -> Result<()> {
#[cfg(feature = "tls")]
async fn serve_tls(self, listen_addr: SocketAddr, tls: Arc<rustls::ServerConfig>) -> Result<()> {
let service = Arc::new(self);
let make_service = make_service_fn(|_conn: &TlsStream| {
let service = service.clone();
Expand All @@ -188,15 +198,15 @@ where
async move {
service
.handle_request(req)
.instrument(error_span!("https_handler", request_id))
.instrument(tracing::error_span!("https_handler", request_id))
.await
}
});
Ok::<_, Error>(service)
}
});

let incoming = AddrIncoming::bind(&listen_addr)
let incoming = hyper::server::conn::AddrIncoming::bind(&listen_addr)
.with_context(|| format!("Unable to bind on {}", listen_addr))?;
info!("Listening on https://{}", listen_addr);
Server::builder(TlsAcceptor::new(tls, incoming))
Expand Down Expand Up @@ -288,16 +298,6 @@ where
}
};

#[cfg(feature = "stats")]
let pop = match request.headers().get("x-cdn-requestor") {
None => {
info!("missing pop info header");
"unknown"
}
Some(v) => v.to_str().unwrap(),
}
.to_string();

let start_ = std::time::Instant::now();

let response = match executor.execute(request).await {
Expand All @@ -316,13 +316,13 @@ where
app_id: cfg.app_id,
client_id: cfg.client_id,
timestamp: timestamp as u32,
app_name: app_name.to_string(),
app_name,
status_code: response.status().as_u16() as u32,
fail_reason: 0, // TODO: use AppResult
billing_plan: cfg.plan.clone(),
time_elapsed: time_elapsed.as_micros() as u64,
memory_used: memory_used.as_u64(),
pop,
.. Default::default()
};
self.context.write_stats(stat_row).await;
}
Expand Down Expand Up @@ -382,13 +382,13 @@ where
app_id: cfg.app_id,
client_id: cfg.client_id,
timestamp: timestamp as u32,
app_name: app_name.to_string(),
app_name,
status_code: status_code as u32,
fail_reason: fail_reason as u32,
billing_plan: cfg.plan.clone(),
time_elapsed: time_elapsed.as_micros() as u64,
memory_used: 0,
pop,
.. Default::default()
};
self.context.write_stats(stat_row).await;
}
Expand Down Expand Up @@ -511,7 +511,7 @@ fn app_res_headers(app_cfg: App) -> HeaderMap {
headers
}

fn app_req_headers<'a>(geo: impl Iterator<Item = (Cow<'a, str>, Cow<'a, str>)>) -> HeaderMap {
fn app_req_headers(geo: impl Iterator<Item = (SmolStr, SmolStr)>) -> HeaderMap {
let mut headers = HeaderMap::new();
for (key, value) in geo {
trace!("append new request header {}={}", key, value);
Expand Down
4 changes: 2 additions & 2 deletions crates/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ wasi-common = { workspace = true }
wasmtime-wasi-nn = { workspace = true }
smol_str = { workspace = true }
moka = { workspace = true }
wasmtime-environ = "19.0.2"
wit-component = "0.206.0"
wasmtime-environ = "20.0.2"
wit-component = "0.210.0"
tracing = { workspace = true }
bytesize = "1"
http-backend = { path = "../http-backend" }
Expand Down
9 changes: 5 additions & 4 deletions crates/runtime/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ use serde::{Deserialize, Deserializer};
use std::collections::HashMap;
use std::fmt;
use std::fmt::Formatter;
use smol_str::SmolStr;

#[derive(Debug, Clone, PartialEq, Deserialize)]
pub struct App {
pub binary_id: u64,
pub max_duration: u64,
pub mem_limit: usize,
#[serde(default)]
pub env: HashMap<String, String>,
pub env: HashMap<SmolStr, SmolStr>,
#[serde(default)]
pub rsp_headers: HashMap<String, String>,
pub rsp_headers: HashMap<SmolStr, SmolStr>,
#[serde(default)]
pub log: Log,
#[serde(default)]
pub app_id: u64,
pub client_id: u64,
pub plan: String,
pub plan: SmolStr,
#[serde(default)]
pub status: Status,
#[serde(default)]
Expand Down Expand Up @@ -72,7 +73,7 @@ impl<'de> Visitor<'de> for StatusVisitor {
2 => Ok(Status::Disabled),
3 | 4 => Ok(Status::RateLimited),
5 => Ok(Status::Suspended),
_ => Err(E::custom("not in range: [0..4]")),
_ => Err(E::custom("status not in range: [0..5]")),
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions crates/runtime/src/util/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ use clickhouse::Row;
use serde::Serialize;

#[cfg(feature = "stats")]
#[derive(Row, Debug, Serialize)]
#[derive(Row, Debug, Serialize, Default)]
pub struct StatRow {
pub app_id: u64,
pub client_id: u64,
pub timestamp: u32,
pub app_name: String,
pub app_name: smol_str::SmolStr,
pub status_code: u32,
pub fail_reason: u32,
pub billing_plan: String,
pub billing_plan: smol_str::SmolStr,
pub time_elapsed: u64,
pub memory_used: u64,
pub pop: String,
pub pop: smol_str::SmolStr,
pub region: smol_str::SmolStr
}

#[cfg(not(feature = "stats"))]
Expand Down
Loading

0 comments on commit 872dbde

Please sign in to comment.