use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicI64, AtomicU64};
use std::sync::{Arc, Mutex, OnceLock};
use prometheus_client::encoding::text::encode;
use prometheus_client::encoding::EncodeLabelSet;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::metrics::gauge::Gauge;
use prometheus_client::metrics::histogram::Histogram;
use prometheus_client::registry::Registry;
use thiserror::Error;
pub const DEFAULT_KERNEL_LATENCY_BUCKETS_SECONDS: [f64; 14] = [
0.00001, 0.00005, 0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1.0, 2.0, 5.0, 10.0,
];
pub const DEFAULT_HTTP_DURATION_BUCKETS_SECONDS: [f64; 12] = [
0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
];
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
#[non_exhaustive]
pub struct HttpRequestLabels {
pub route: Cow<'static, str>,
pub method: Cow<'static, str>,
pub status: Cow<'static, str>,
}
#[derive(Clone, Debug)]
pub struct RouteAllowlist {
ordered: Vec<&'static str>,
set: HashSet<&'static str>,
}
impl PartialEq for RouteAllowlist {
fn eq(&self, other: &Self) -> bool {
self.ordered == other.ordered
}
}
impl Eq for RouteAllowlist {}
impl RouteAllowlist {
pub fn new(routes: &[&'static str]) -> Arc<Self> {
let ordered = routes.to_vec();
let set = ordered.iter().copied().collect();
Arc::new(Self { ordered, set })
}
pub fn lookup(&self, route: &str) -> Option<&'static str> {
self.set.get(route).copied()
}
pub fn routes(&self) -> &[&'static str] {
&self.ordered
}
}
static ROUTE_ALLOWLIST: OnceLock<Arc<RouteAllowlist>> = OnceLock::new();
pub fn register_route_allowlist(routes: &[&'static str]) -> Result<(), LabelError> {
let list = RouteAllowlist::new(routes);
ROUTE_ALLOWLIST
.set(list)
.map_err(|_| LabelError::AllowlistAlreadyRegistered)
}
pub fn registered_route_allowlist() -> Option<Arc<RouteAllowlist>> {
ROUTE_ALLOWLIST.get().cloned()
}
#[derive(Debug, Error, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum LabelError {
#[error("route `{route}` is not in the registered HTTP route allow-list")]
UnknownRoute {
route: String,
},
#[error("HTTP method `{method}` is not in the allowed verb set")]
InvalidMethod {
method: String,
},
#[error("HTTP status code {status} is outside the valid range 100..=599")]
InvalidStatus {
status: u16,
},
#[error("HTTP route allow-list has already been registered")]
AllowlistAlreadyRegistered,
}
const ALLOWED_HTTP_METHODS: &[&str] = &[
"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE", "CONNECT",
];
static STATUS_STR: once_cell_table::StatusTable = once_cell_table::StatusTable::new();
mod once_cell_table {
use std::sync::OnceLock;
pub(crate) struct StatusTable(OnceLock<Vec<&'static str>>);
impl StatusTable {
pub(crate) const fn new() -> Self {
Self(OnceLock::new())
}
pub(crate) fn get(&self, code: u16) -> Option<&'static str> {
if !(100..=599).contains(&code) {
return None;
}
let table = self.0.get_or_init(|| {
(100..=599)
.map(|n: u16| {
let s: Box<str> = n.to_string().into_boxed_str();
&*Box::leak(s)
})
.collect()
});
Some(table[(code - 100) as usize])
}
}
}
impl HttpRequestLabels {
pub fn new(
route: impl Into<Cow<'static, str>>,
method: impl Into<Cow<'static, str>>,
status: impl Into<Cow<'static, str>>,
) -> Self {
Self {
route: route.into(),
method: method.into(),
status: status.into(),
}
}
pub fn try_new(route: &str, method: &str, status: u16) -> Result<Self, LabelError> {
Self::try_new_with_allowlist(route, method, status, ROUTE_ALLOWLIST.get())
}
pub fn try_new_with_allowlist(
route: &str,
method: &str,
status: u16,
allowlist: Option<&Arc<RouteAllowlist>>,
) -> Result<Self, LabelError> {
let route_cow: Cow<'static, str> = match allowlist {
Some(list) => match list.lookup(route) {
Some(matched) => Cow::Borrowed(matched),
None => {
return Err(LabelError::UnknownRoute {
route: route.to_string(),
});
}
},
None => Cow::Owned(route.to_string()),
};
let method_cow: Cow<'static, str> =
match ALLOWED_HTTP_METHODS.iter().copied().find(|&m| m == method) {
Some(matched) => Cow::Borrowed(matched),
None => {
return Err(LabelError::InvalidMethod {
method: method.to_string(),
});
}
};
let status_cow: Cow<'static, str> = match STATUS_STR.get(status) {
Some(s) => Cow::Borrowed(s),
None => return Err(LabelError::InvalidStatus { status }),
};
Ok(HttpRequestLabels {
route: route_cow,
method: method_cow,
status: status_cow,
})
}
}
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
#[non_exhaustive]
pub struct HttpInFlightLabels {
pub route: Cow<'static, str>,
pub method: Cow<'static, str>,
}
impl HttpInFlightLabels {
pub fn new(route: impl Into<Cow<'static, str>>, method: impl Into<Cow<'static, str>>) -> Self {
Self {
route: route.into(),
method: method.into(),
}
}
}
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
#[non_exhaustive]
pub struct TenantLabels {
pub tenant_id: Cow<'static, str>,
}
impl TenantLabels {
pub fn new(tenant_id: impl Into<Cow<'static, str>>) -> Self {
Self {
tenant_id: tenant_id.into(),
}
}
pub fn from_tenant_id(id: crate::types::TenantId) -> Self {
match intern_tenant_id(id.get()) {
Some(interned) => Self::new(Cow::Borrowed(interned)),
None => Self::new(format!("T#{}", id.get())),
}
}
}
const TENANT_LABEL_INTERN_CAP: usize = 4096;
static TENANT_LABEL_INTERN: OnceLock<Mutex<HashMap<u64, &'static str>>> = OnceLock::new();
fn intern_tenant_id(id: u64) -> Option<&'static str> {
let cache = TENANT_LABEL_INTERN.get_or_init(|| Mutex::new(HashMap::new()));
intern_tenant_id_in(cache, TENANT_LABEL_INTERN_CAP, id)
}
fn intern_tenant_id_in(
cache: &Mutex<HashMap<u64, &'static str>>,
cap: usize,
id: u64,
) -> Option<&'static str> {
{
let map = cache.lock().unwrap_or_else(|e| e.into_inner());
if let Some(&s) = map.get(&id) {
return Some(s);
}
if map.len() >= cap {
return None;
}
}
let candidate: &'static str = Box::leak(format!("T#{id}").into_boxed_str());
let mut map = cache.lock().unwrap_or_else(|e| e.into_inner());
if let Some(&s) = map.get(&id) {
return Some(s);
}
if map.len() >= cap {
return None;
}
map.insert(id, candidate);
Some(candidate)
}
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
#[non_exhaustive]
pub struct BuildInfoLabels {
pub version: String,
pub git_sha: String,
pub rustc_version: String,
pub profile: String,
pub target: String,
}
impl BuildInfoLabels {
pub fn new(
version: impl Into<Cow<'static, str>>,
git_sha: impl Into<Cow<'static, str>>,
rustc_version: impl Into<Cow<'static, str>>,
profile: impl Into<Cow<'static, str>>,
target: impl Into<Cow<'static, str>>,
) -> Self {
Self {
version: version.into().into_owned(),
git_sha: git_sha.into().into_owned(),
rustc_version: rustc_version.into().into_owned(),
profile: profile.into().into_owned(),
target: target.into().into_owned(),
}
}
}
#[derive(Debug, Clone)]
pub struct TensorWasmMetrics {
inner: Arc<TensorWasmMetricsInner>,
}
#[derive(Debug)]
struct TensorWasmMetricsInner {
registry: parking_lot::Mutex<Registry>,
active_instances: Gauge<i64, AtomicI64>,
gpu_memory_used_bytes: Gauge<u64, AtomicU64>,
kernel_dispatches_total: Counter<u64>,
kernel_latency_seconds: Histogram,
instance_spawns_total: Counter<u64>,
instance_terminations_total: Counter<u64>,
offload_success_total: Counter<u64>,
offload_fallback_total: Counter<u64>,
http_requests_total: Family<HttpRequestLabels, Counter<u64>>,
http_request_duration_seconds: Family<HttpRequestLabels, Histogram, HttpDurationCtor>,
http_requests_in_flight: Family<HttpInFlightLabels, Gauge<i64, AtomicI64>>,
build_info: Family<BuildInfoLabels, Gauge<i64, AtomicI64>>,
jobs_active: Gauge<i64, AtomicI64>,
gpu_memory_bytes_per_tenant: Family<TenantLabels, Gauge<u64, AtomicU64>>,
cpu_memory_bytes_per_tenant: Family<TenantLabels, Gauge<u64, AtomicU64>>,
streaming_chunks_emitted_total: Counter<u64>,
}
pub const BUILD_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const BUILD_GIT_SHA: &str = env!("TENSOR_WASM_GIT_SHA");
pub const BUILD_RUSTC_VERSION: &str = env!("TENSOR_WASM_RUSTC_VERSION");
pub const BUILD_PROFILE: &str = env!("TENSOR_WASM_PROFILE");
pub const BUILD_TARGET: &str = env!("TENSOR_WASM_TARGET");
pub fn current_build_info_labels() -> BuildInfoLabels {
BuildInfoLabels {
version: BUILD_VERSION.to_string(),
git_sha: BUILD_GIT_SHA.to_string(),
rustc_version: BUILD_RUSTC_VERSION.to_string(),
profile: BUILD_PROFILE.to_string(),
target: BUILD_TARGET.to_string(),
}
}
#[derive(Clone, Debug)]
pub struct HttpDurationCtor {
buckets: Arc<[f64]>,
}
impl prometheus_client::metrics::family::MetricConstructor<Histogram> for HttpDurationCtor {
fn new_metric(&self) -> Histogram {
Histogram::new(self.buckets.iter().copied())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct TensorWasmMetricsStats {
pub active_instances: i64,
pub gpu_memory_used_bytes: u64,
pub kernel_dispatches_total: u64,
pub instance_spawns_total: u64,
pub instance_terminations_total: u64,
pub offload_success_total: u64,
pub offload_fallback_total: u64,
}
impl Default for TensorWasmMetrics {
fn default() -> Self {
Self::new()
}
}
impl TensorWasmMetrics {
pub fn new() -> Self {
Self::with_all_buckets(
DEFAULT_KERNEL_LATENCY_BUCKETS_SECONDS.iter().copied(),
DEFAULT_HTTP_DURATION_BUCKETS_SECONDS.iter().copied(),
)
}
pub fn with_buckets(buckets: impl IntoIterator<Item = f64>) -> Self {
Self::with_all_buckets(
buckets,
DEFAULT_HTTP_DURATION_BUCKETS_SECONDS.iter().copied(),
)
}
pub fn with_http_buckets(http_buckets: impl IntoIterator<Item = f64>) -> Self {
Self::with_all_buckets(
DEFAULT_KERNEL_LATENCY_BUCKETS_SECONDS.iter().copied(),
http_buckets,
)
}
pub fn with_all_buckets(
kernel_buckets: impl IntoIterator<Item = f64>,
http_buckets: impl IntoIterator<Item = f64>,
) -> Self {
let mut registry = Registry::default();
let active_instances: Gauge<i64, AtomicI64> = Gauge::default();
let gpu_memory_used_bytes: Gauge<u64, AtomicU64> = Gauge::default();
let kernel_dispatches_total: Counter<u64> = Counter::default();
let kernel_latency_seconds = Histogram::new(kernel_buckets);
let instance_spawns_total: Counter<u64> = Counter::default();
let instance_terminations_total: Counter<u64> = Counter::default();
let offload_success_total: Counter<u64> = Counter::default();
let offload_fallback_total: Counter<u64> = Counter::default();
let http_requests_total: Family<HttpRequestLabels, Counter<u64>> = Family::default();
let http_buckets_arc: Arc<[f64]> = http_buckets.into_iter().collect();
let http_request_duration_seconds: Family<HttpRequestLabels, Histogram, HttpDurationCtor> =
Family::new_with_constructor(HttpDurationCtor {
buckets: http_buckets_arc,
});
let http_requests_in_flight: Family<HttpInFlightLabels, Gauge<i64, AtomicI64>> =
Family::default();
let build_info: Family<BuildInfoLabels, Gauge<i64, AtomicI64>> = Family::default();
let jobs_active: Gauge<i64, AtomicI64> = Gauge::default();
let gpu_memory_bytes_per_tenant: Family<TenantLabels, Gauge<u64, AtomicU64>> =
Family::default();
let cpu_memory_bytes_per_tenant: Family<TenantLabels, Gauge<u64, AtomicU64>> =
Family::default();
let streaming_chunks_emitted_total: Counter<u64> = Counter::default();
build_info
.get_or_create(¤t_build_info_labels())
.set(1);
registry.register(
"tensor_wasm_active_instances",
"Number of currently live Wasm instances",
active_instances.clone(),
);
registry.register(
"tensor_wasm_gpu_memory_used_bytes",
"Total GPU memory currently allocated to live instances, in bytes",
gpu_memory_used_bytes.clone(),
);
registry.register(
"tensor_wasm_kernel_dispatches",
"Cumulative count of GPU kernel dispatches issued via wasi_cuda_launch",
kernel_dispatches_total.clone(),
);
registry.register(
"tensor_wasm_kernel_latency_seconds",
"Histogram of kernel launch-to-completion latency in seconds",
kernel_latency_seconds.clone(),
);
registry.register(
"tensor_wasm_instance_spawns",
"Cumulative count of Wasm instance spawns",
instance_spawns_total.clone(),
);
registry.register(
"tensor_wasm_instance_terminations",
"Cumulative count of Wasm instance terminations",
instance_terminations_total.clone(),
);
registry.register(
"tensor_wasm_offload_success",
"Cumulative count of GPU-offloaded basic blocks that completed successfully",
offload_success_total.clone(),
);
registry.register(
"tensor_wasm_offload_fallback",
"Cumulative count of GPU offloads that deopted to the CPU fallback path",
offload_fallback_total.clone(),
);
registry.register(
"tensor_wasm_http_requests",
"Cumulative count of HTTP requests served by the API gateway, \
labelled by axum route template, method, and numeric status code",
http_requests_total.clone(),
);
registry.register(
"tensor_wasm_http_request_duration_seconds",
"Histogram of HTTP request duration in seconds, labelled by axum \
route template, method, and numeric status code",
http_request_duration_seconds.clone(),
);
registry.register(
"tensor_wasm_http_requests_in_flight",
"Number of HTTP requests currently being served, labelled by axum \
route template and method",
http_requests_in_flight.clone(),
);
registry.register(
"tensor_wasm_build_info",
"Constant `1` gauge whose labels identify the binary build \
(version, git_sha, rustc_version, profile, target). Prometheus \
info-style metric: aggregate other series against it to label \
dashboards with the live binary identity",
build_info.clone(),
);
registry.register(
"tensor_wasm_jobs_active",
"Number of async-invocation jobs currently in `Pending` state \
in the API-layer job registry. Incremented when \
`POST /functions/:id/invoke-async` accepts a job; decremented \
when the job transitions to `Completed` or `Failed`. \
v0.3.x emits a single series; per-tenant breakdown is the \
v0.4 follow-up and will reuse the `TenantLabels` shape",
jobs_active.clone(),
);
registry.register(
"tensor_wasm_gpu_memory_bytes_per_tenant",
"Per-tenant GPU memory currently reserved, in bytes. Updated \
by `tensor-wasm-tenant` on every `consume_bytes` / \
`release_bytes` accounting transition. Additive to the \
single-series `tensor_wasm_gpu_memory_used_bytes` total; \
`sum by () (tensor_wasm_gpu_memory_bytes_per_tenant)` is \
expected to track that total within scrape jitter",
gpu_memory_bytes_per_tenant.clone(),
);
registry.register(
"tensor_wasm_cpu_memory_bytes_per_tenant",
"Per-tenant CPU (linear-memory / host) memory currently \
reserved, in bytes. Updated by `tensor-wasm-tenant` on every \
`consume_bytes` / `release_bytes` accounting transition. The \
CPU counterpart to `tensor_wasm_gpu_memory_bytes_per_tenant`",
cpu_memory_bytes_per_tenant.clone(),
);
registry.register(
"tensor_wasm_streaming_chunks_emitted",
"Cumulative count of streaming chunks emitted via \
`wasi:tensor/host.emit-chunk` and successfully forwarded \
onto the SSE / chunked-transfer response body of \
`POST /functions/{id}/invoke-stream`. Rejected emits \
(caps exceeded, receiver dropped) are NOT counted",
streaming_chunks_emitted_total.clone(),
);
Self {
inner: Arc::new(TensorWasmMetricsInner {
registry: parking_lot::Mutex::new(registry),
active_instances,
gpu_memory_used_bytes,
kernel_dispatches_total,
kernel_latency_seconds,
instance_spawns_total,
instance_terminations_total,
offload_success_total,
offload_fallback_total,
http_requests_total,
http_request_duration_seconds,
http_requests_in_flight,
build_info,
jobs_active,
gpu_memory_bytes_per_tenant,
cpu_memory_bytes_per_tenant,
streaming_chunks_emitted_total,
}),
}
}
pub fn active_instances(&self) -> &Gauge<i64, AtomicI64> {
&self.inner.active_instances
}
pub fn gpu_memory_used_bytes(&self) -> &Gauge<u64, AtomicU64> {
&self.inner.gpu_memory_used_bytes
}
pub fn kernel_dispatches_total(&self) -> &Counter<u64> {
&self.inner.kernel_dispatches_total
}
pub fn kernel_latency_seconds(&self) -> &Histogram {
&self.inner.kernel_latency_seconds
}
pub fn instance_spawns_total(&self) -> &Counter<u64> {
&self.inner.instance_spawns_total
}
pub fn instance_terminations_total(&self) -> &Counter<u64> {
&self.inner.instance_terminations_total
}
pub fn offload_success_total(&self) -> &Counter<u64> {
&self.inner.offload_success_total
}
pub fn offload_fallback_total(&self) -> &Counter<u64> {
&self.inner.offload_fallback_total
}
pub fn http_requests_total(&self) -> &Family<HttpRequestLabels, Counter<u64>> {
&self.inner.http_requests_total
}
pub fn http_request_duration_seconds(
&self,
) -> &Family<HttpRequestLabels, Histogram, HttpDurationCtor> {
&self.inner.http_request_duration_seconds
}
pub fn http_requests_in_flight(&self) -> &Family<HttpInFlightLabels, Gauge<i64, AtomicI64>> {
&self.inner.http_requests_in_flight
}
pub fn build_info(&self) -> &Family<BuildInfoLabels, Gauge<i64, AtomicI64>> {
&self.inner.build_info
}
pub fn jobs_active(&self) -> &Gauge<i64, AtomicI64> {
&self.inner.jobs_active
}
pub fn gpu_memory_bytes_per_tenant(&self) -> &Family<TenantLabels, Gauge<u64, AtomicU64>> {
&self.inner.gpu_memory_bytes_per_tenant
}
pub fn cpu_memory_bytes_per_tenant(&self) -> &Family<TenantLabels, Gauge<u64, AtomicU64>> {
&self.inner.cpu_memory_bytes_per_tenant
}
pub fn streaming_chunks_emitted_total(&self) -> &Counter<u64> {
&self.inner.streaming_chunks_emitted_total
}
pub fn stats(&self) -> TensorWasmMetricsStats {
TensorWasmMetricsStats {
active_instances: self.inner.active_instances.get(),
gpu_memory_used_bytes: self.inner.gpu_memory_used_bytes.get(),
kernel_dispatches_total: self.inner.kernel_dispatches_total.get(),
instance_spawns_total: self.inner.instance_spawns_total.get(),
instance_terminations_total: self.inner.instance_terminations_total.get(),
offload_success_total: self.inner.offload_success_total.get(),
offload_fallback_total: self.inner.offload_fallback_total.get(),
}
}
pub fn encode_text(&self) -> String {
let mut out = String::with_capacity(8 * 1024);
let registry = self.inner.registry.lock();
encode(&mut out, ®istry).expect("text encoding into a String cannot fail");
out
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn construct_default() {
let m = TensorWasmMetrics::new();
let s = m.encode_text();
for name in [
"tensor_wasm_active_instances",
"tensor_wasm_gpu_memory_used_bytes",
"tensor_wasm_kernel_dispatches",
"tensor_wasm_kernel_latency_seconds",
"tensor_wasm_instance_spawns",
"tensor_wasm_instance_terminations",
"tensor_wasm_offload_success",
"tensor_wasm_offload_fallback",
"tensor_wasm_jobs_active",
] {
assert!(s.contains(name), "missing metric {name} in:\n{s}");
}
}
#[test]
fn http_request_families_observable() {
let m = TensorWasmMetrics::new();
let labels = HttpRequestLabels {
route: Cow::Borrowed("/healthz"),
method: Cow::Borrowed("GET"),
status: Cow::Borrowed("200"),
};
m.http_requests_total().get_or_create(&labels).inc();
m.http_request_duration_seconds()
.get_or_create(&labels)
.observe(0.002);
let in_flight_labels = HttpInFlightLabels {
route: Cow::Borrowed("/healthz"),
method: Cow::Borrowed("GET"),
};
m.http_requests_in_flight()
.get_or_create(&in_flight_labels)
.inc();
m.http_requests_in_flight()
.get_or_create(&in_flight_labels)
.dec();
let s = m.encode_text();
assert!(
s.contains("tensor_wasm_http_requests_total{route=\"/healthz\",method=\"GET\",status=\"200\"} 1"),
"missing labelled counter sample in:\n{s}"
);
assert!(
s.contains("tensor_wasm_http_request_duration_seconds_count"),
"missing histogram count series in:\n{s}"
);
assert!(
s.contains("tensor_wasm_http_requests_in_flight{route=\"/healthz\",method=\"GET\"} 0"),
"missing in-flight gauge sample in:\n{s}"
);
assert!(s.contains("le=\"0.025\""), "missing 0.025 bucket in:\n{s}");
}
#[test]
fn http_request_buckets_overridable() {
let m = TensorWasmMetrics::with_http_buckets([0.5f64, 1.0, 2.0]);
let labels = HttpRequestLabels {
route: Cow::Borrowed("/x"),
method: Cow::Borrowed("GET"),
status: Cow::Borrowed("200"),
};
m.http_request_duration_seconds()
.get_or_create(&labels)
.observe(0.75);
let s = m.encode_text();
assert!(s.contains("le=\"0.5\""), "got:\n{s}");
assert!(s.contains("le=\"1.0\""), "got:\n{s}");
assert!(!s.contains("le=\"0.025\""), "got:\n{s}");
}
#[test]
fn gauge_increments_observable() {
let m = TensorWasmMetrics::new();
m.active_instances().inc();
m.active_instances().inc();
m.active_instances().dec();
m.gpu_memory_used_bytes().inc_by(4096);
let s = m.encode_text();
assert!(s.contains("tensor_wasm_active_instances 1"), "got:\n{s}");
assert!(
s.contains("tensor_wasm_gpu_memory_used_bytes 4096"),
"got:\n{s}"
);
}
#[test]
fn counter_increments_observable() {
let m = TensorWasmMetrics::new();
m.kernel_dispatches_total().inc();
m.kernel_dispatches_total().inc();
m.kernel_dispatches_total().inc();
let s = m.encode_text();
assert!(
s.contains("tensor_wasm_kernel_dispatches_total 3"),
"got:\n{s}"
);
}
#[test]
fn histogram_observations_recorded() {
let m = TensorWasmMetrics::new();
m.kernel_latency_seconds().observe(0.0001);
m.kernel_latency_seconds().observe(0.5);
m.kernel_latency_seconds().observe(7.0);
let s = m.encode_text();
assert!(
s.contains("tensor_wasm_kernel_latency_seconds_count 3"),
"got:\n{s}"
);
}
#[test]
fn clone_shares_state() {
let a = TensorWasmMetrics::new();
let b = a.clone();
a.kernel_dispatches_total().inc();
b.kernel_dispatches_total().inc();
let s = a.encode_text();
assert!(
s.contains("tensor_wasm_kernel_dispatches_total 2"),
"got:\n{s}"
);
}
#[test]
fn custom_buckets_accepted() {
let m = TensorWasmMetrics::with_buckets([0.1f64, 1.0, 10.0]);
m.kernel_latency_seconds().observe(0.5);
let s = m.encode_text();
assert!(s.contains("le=\"0.1\""), "got:\n{s}");
assert!(s.contains("le=\"1.0\""), "got:\n{s}");
}
#[test]
fn getter_active_instances_returns_live_handle() {
let m = TensorWasmMetrics::new();
m.active_instances().inc();
assert_eq!(m.active_instances().get(), 1);
}
#[test]
fn getter_gpu_memory_used_bytes_returns_live_handle() {
let m = TensorWasmMetrics::new();
m.gpu_memory_used_bytes().inc_by(2048);
assert_eq!(m.gpu_memory_used_bytes().get(), 2048);
}
#[test]
fn getter_kernel_dispatches_total_returns_live_handle() {
let m = TensorWasmMetrics::new();
m.kernel_dispatches_total().inc();
m.kernel_dispatches_total().inc();
assert_eq!(m.kernel_dispatches_total().get(), 2);
}
#[test]
fn getter_kernel_latency_seconds_returns_live_handle() {
let m = TensorWasmMetrics::new();
m.kernel_latency_seconds().observe(0.123);
let s = m.encode_text();
assert!(
s.contains("tensor_wasm_kernel_latency_seconds_count 1"),
"got:\n{s}"
);
}
#[test]
fn getter_instance_spawns_total_returns_live_handle() {
let m = TensorWasmMetrics::new();
m.instance_spawns_total().inc();
assert_eq!(m.instance_spawns_total().get(), 1);
}
#[test]
fn getter_instance_terminations_total_returns_live_handle() {
let m = TensorWasmMetrics::new();
m.instance_terminations_total().inc();
m.instance_terminations_total().inc();
m.instance_terminations_total().inc();
assert_eq!(m.instance_terminations_total().get(), 3);
}
#[test]
fn getter_offload_success_total_returns_live_handle() {
let m = TensorWasmMetrics::new();
m.offload_success_total().inc();
assert_eq!(m.offload_success_total().get(), 1);
}
#[test]
fn getter_offload_fallback_total_returns_live_handle() {
let m = TensorWasmMetrics::new();
m.offload_fallback_total().inc();
m.offload_fallback_total().inc();
assert_eq!(m.offload_fallback_total().get(), 2);
}
#[test]
fn stats_returns_all_counters_at_once() {
let m = TensorWasmMetrics::new();
m.active_instances().inc();
m.active_instances().inc();
m.gpu_memory_used_bytes().inc_by(1024);
m.kernel_dispatches_total().inc();
m.instance_spawns_total().inc();
m.instance_spawns_total().inc();
m.instance_terminations_total().inc();
m.offload_success_total().inc();
m.offload_fallback_total().inc();
let s = m.stats();
assert_eq!(s.active_instances, 2);
assert_eq!(s.gpu_memory_used_bytes, 1024);
assert_eq!(s.kernel_dispatches_total, 1);
assert_eq!(s.instance_spawns_total, 2);
assert_eq!(s.instance_terminations_total, 1);
assert_eq!(s.offload_success_total, 1);
assert_eq!(s.offload_fallback_total, 1);
}
#[test]
fn stats_initial_snapshot_is_all_zero() {
let s = TensorWasmMetrics::new().stats();
assert_eq!(
s,
TensorWasmMetricsStats {
active_instances: 0,
gpu_memory_used_bytes: 0,
kernel_dispatches_total: 0,
instance_spawns_total: 0,
instance_terminations_total: 0,
offload_success_total: 0,
offload_fallback_total: 0,
}
);
}
#[test]
fn build_info_appears_in_encoded_text() {
let m = TensorWasmMetrics::new();
let s = m.encode_text();
assert!(
s.contains("tensor_wasm_build_info"),
"missing tensor_wasm_build_info in:\n{s}"
);
}
#[test]
fn build_info_value_is_one() {
let m = TensorWasmMetrics::new();
let labels = current_build_info_labels();
assert_eq!(m.build_info().get_or_create(&labels).get(), 1);
let s = m.encode_text();
let line = s
.lines()
.find(|l| l.starts_with("tensor_wasm_build_info{"))
.unwrap_or_else(|| panic!("no tensor_wasm_build_info sample line in:\n{s}"));
assert!(
line.ends_with(" 1"),
"expected build_info value of 1, got line: {line}"
);
}
#[test]
fn build_info_carries_expected_label_keys() {
let m = TensorWasmMetrics::new();
let s = m.encode_text();
for key in ["version", "git_sha", "rustc_version", "profile", "target"] {
let needle = format!("{}=\"", key);
assert!(
s.contains(&needle),
"missing label key `{key}` on tensor_wasm_build_info in:\n{s}"
);
}
let version_needle = format!("version=\"{}\"", env!("CARGO_PKG_VERSION"));
assert!(
s.contains(&version_needle),
"expected `{version_needle}` in:\n{s}"
);
}
#[test]
fn jobs_active_initial_zero_in_text() {
let m = TensorWasmMetrics::new();
let s = m.encode_text();
assert!(
s.contains("tensor_wasm_jobs_active 0"),
"missing initial-zero jobs_active sample in:\n{s}"
);
}
#[test]
fn jobs_active_inc_dec_observable() {
let m = TensorWasmMetrics::new();
m.jobs_active().inc();
m.jobs_active().inc();
m.jobs_active().dec();
assert_eq!(m.jobs_active().get(), 1);
let s = m.encode_text();
assert!(
s.contains("tensor_wasm_jobs_active 1"),
"expected jobs_active 1 after two inc + one dec, got:\n{s}"
);
}
#[test]
fn gpu_memory_per_tenant_family_observable_after_set() {
let m = TensorWasmMetrics::new();
let t1 = TenantLabels {
tenant_id: Cow::Borrowed("T#1"),
};
let t2 = TenantLabels {
tenant_id: Cow::Borrowed("T#2"),
};
m.gpu_memory_bytes_per_tenant().get_or_create(&t1).set(4096);
m.gpu_memory_bytes_per_tenant().get_or_create(&t2).set(8192);
let s = m.encode_text();
assert!(
s.contains("tensor_wasm_gpu_memory_bytes_per_tenant{tenant_id=\"T#1\"} 4096"),
"missing tenant T#1 sample in:\n{s}"
);
assert!(
s.contains("tensor_wasm_gpu_memory_bytes_per_tenant{tenant_id=\"T#2\"} 8192"),
"missing tenant T#2 sample in:\n{s}"
);
}
#[test]
fn gpu_memory_per_tenant_family_silent_until_observed() {
let m = TensorWasmMetrics::new();
let s = m.encode_text();
assert!(
!s.contains("tensor_wasm_gpu_memory_bytes_per_tenant{"),
"per-tenant gauge should be silent before observation; got:\n{s}"
);
}
#[test]
fn gpu_memory_per_tenant_total_existing_metric_preserved() {
let m = TensorWasmMetrics::new();
let s = m.encode_text();
assert!(
s.contains("tensor_wasm_gpu_memory_used_bytes"),
"single-series total must be preserved alongside the per-tenant family; got:\n{s}"
);
}
#[test]
fn cpu_memory_per_tenant_family_observable_after_set() {
let m = TensorWasmMetrics::new();
let t1 = TenantLabels {
tenant_id: Cow::Borrowed("T#1"),
};
let t2 = TenantLabels {
tenant_id: Cow::Borrowed("T#2"),
};
m.cpu_memory_bytes_per_tenant().get_or_create(&t1).set(4096);
m.cpu_memory_bytes_per_tenant().get_or_create(&t2).set(8192);
let s = m.encode_text();
assert!(
s.contains("tensor_wasm_cpu_memory_bytes_per_tenant{tenant_id=\"T#1\"} 4096"),
"missing tenant T#1 sample in:\n{s}"
);
assert!(
s.contains("tensor_wasm_cpu_memory_bytes_per_tenant{tenant_id=\"T#2\"} 8192"),
"missing tenant T#2 sample in:\n{s}"
);
}
#[test]
fn cpu_memory_per_tenant_family_silent_until_observed() {
let m = TensorWasmMetrics::new();
let s = m.encode_text();
assert!(
!s.contains("tensor_wasm_cpu_memory_bytes_per_tenant{"),
"per-tenant CPU gauge should be silent before observation; got:\n{s}"
);
}
#[test]
fn cpu_and_gpu_per_tenant_families_are_independent_series() {
let m = TensorWasmMetrics::new();
let t = TenantLabels {
tenant_id: Cow::Borrowed("T#7"),
};
m.cpu_memory_bytes_per_tenant().get_or_create(&t).set(1024);
m.gpu_memory_bytes_per_tenant().get_or_create(&t).set(2048);
let s = m.encode_text();
assert!(
s.contains("tensor_wasm_cpu_memory_bytes_per_tenant{tenant_id=\"T#7\"} 1024"),
"CPU series missing or wrong value in:\n{s}"
);
assert!(
s.contains("tensor_wasm_gpu_memory_bytes_per_tenant{tenant_id=\"T#7\"} 2048"),
"GPU series missing or wrong value in:\n{s}"
);
}
#[test]
fn build_info_labels_new_round_trips() {
let labels = BuildInfoLabels::new(
"1.2.3",
"deadbeef",
"rustc 1.99.0",
"release",
"x86_64-unknown-linux-gnu",
);
assert_eq!(labels.version, "1.2.3");
assert_eq!(labels.git_sha, "deadbeef");
assert_eq!(labels.rustc_version, "rustc 1.99.0");
assert_eq!(labels.profile, "release");
assert_eq!(labels.target, "x86_64-unknown-linux-gnu");
}
#[test]
fn build_info_labels_string_field_accepts_owned() {
let owned = String::from("foo");
let labels = BuildInfoLabels::new(
owned,
String::from("bar"),
String::from("baz"),
String::from("qux"),
String::from("quux"),
);
assert_eq!(labels.version, "foo");
assert_eq!(labels.git_sha, "bar");
assert_eq!(labels.rustc_version, "baz");
assert_eq!(labels.profile, "qux");
assert_eq!(labels.target, "quux");
}
#[test]
fn tenant_labels_from_tenant_id_renders_canonical_form() {
use crate::types::TenantId;
let labels = TenantLabels::from_tenant_id(TenantId(42));
assert_eq!(labels.tenant_id, "T#42");
assert_eq!(labels.tenant_id, TenantId(42).to_string());
}
#[test]
fn tenant_labels_from_tenant_id_interns_repeat_calls() {
use crate::types::TenantId;
let a = TenantLabels::from_tenant_id(TenantId(99_001));
let b = TenantLabels::from_tenant_id(TenantId(99_001));
assert_eq!(a.tenant_id, "T#99001");
match (&a.tenant_id, &b.tenant_id) {
(Cow::Borrowed(sa), Cow::Borrowed(sb)) => {
assert!(std::ptr::eq(*sa, *sb), "repeat calls were not interned");
}
other => panic!("expected interned Cow::Borrowed for both, got {other:?}"),
}
}
#[test]
fn intern_tenant_id_falls_back_at_capacity() {
assert_eq!(TENANT_LABEL_INTERN_CAP, 4096);
let cache: Mutex<HashMap<u64, &'static str>> = Mutex::new(HashMap::new());
assert_eq!(intern_tenant_id_in(&cache, 2, 1), Some("T#1"));
assert_eq!(intern_tenant_id_in(&cache, 2, 2), Some("T#2"));
assert_eq!(
intern_tenant_id_in(&cache, 2, 3),
None,
"a not-yet-seen id past capacity must return None",
);
assert_eq!(
intern_tenant_id_in(&cache, 2, 1),
Some("T#1"),
"a cached id must resolve regardless of remaining capacity",
);
let a = intern_tenant_id_in(&cache, 2, 2).expect("cached");
let b = intern_tenant_id_in(&cache, 2, 2).expect("cached");
assert!(std::ptr::eq(a, b), "repeat hits must share one pointer");
}
#[test]
fn intern_tenant_id_global_wrapper_resolves() {
let first = intern_tenant_id(99_002);
assert_eq!(first, Some("T#99002"));
assert_eq!(intern_tenant_id(99_002), Some("T#99002"));
}
#[test]
fn tenant_labels_new_accepts_string_and_static_str() {
let from_static = TenantLabels::new("T#1");
let from_owned = TenantLabels::new(String::from("T#2"));
assert_eq!(from_static.tenant_id, "T#1");
assert_eq!(from_owned.tenant_id, "T#2");
}
#[test]
fn route_allowlist_lookup_finds_registered_route() {
let list = RouteAllowlist::new(&["/healthz", "/v1/chat/completions", "/metrics"]);
assert_eq!(list.lookup("/healthz"), Some("/healthz"));
assert_eq!(
list.lookup("/v1/chat/completions"),
Some("/v1/chat/completions"),
);
assert_eq!(list.lookup("/metrics"), Some("/metrics"));
assert_eq!(list.lookup("/not-a-route"), None);
assert_eq!(list.lookup("/HEALTHZ"), None);
}
#[test]
fn route_allowlist_routes_preserves_declaration_order() {
let registered: &[&'static str] = &["/c", "/a", "/b"];
let list = RouteAllowlist::new(registered);
assert_eq!(list.routes(), registered);
}
#[test]
fn route_allowlist_lookup_returns_static_pointer() {
const ROUTE: &str = "/healthz";
let list = RouteAllowlist::new(&[ROUTE]);
let matched = list.lookup("/healthz").expect("registered");
assert!(
std::ptr::eq(matched.as_ptr(), ROUTE.as_ptr()),
"lookup should return the registered `&'static str`, got a new pointer",
);
}
#[test]
fn route_allowlist_lookup_handles_large_allowlists() {
let routes: Vec<&'static str> = (0..128)
.map(|i| -> &'static str { Box::leak(format!("/route_{i}").into_boxed_str()) })
.collect();
let list = RouteAllowlist::new(&routes);
for r in &routes {
assert_eq!(list.lookup(r), Some(*r));
}
assert_eq!(list.lookup("/route_999999"), None);
}
#[test]
fn build_info_constants_match_labels() {
let labels = current_build_info_labels();
assert_eq!(labels.version, BUILD_VERSION);
assert_eq!(labels.git_sha, BUILD_GIT_SHA);
assert_eq!(labels.rustc_version, BUILD_RUSTC_VERSION);
assert_eq!(labels.profile, BUILD_PROFILE);
assert_eq!(labels.target, BUILD_TARGET);
assert!(!labels.version.is_empty());
assert!(!labels.git_sha.is_empty());
assert!(!labels.rustc_version.is_empty());
assert!(!labels.profile.is_empty());
assert!(!labels.target.is_empty());
}
}
#[cfg(test)]
mod tests_b51 {
use super::*;
#[test]
fn route_allowlist_direct_surface_lookup_hit_and_miss() {
let alw = RouteAllowlist::new(&["/healthz", "/metrics"]);
assert_eq!(alw.lookup("/healthz"), Some("/healthz"));
assert_eq!(alw.lookup("/metrics"), Some("/metrics"));
assert_eq!(alw.lookup("/unknown"), None);
assert_eq!(alw.lookup("/Healthz"), None);
assert_eq!(alw.routes(), &["/healthz", "/metrics"]);
}
#[test]
fn status_table_boundaries() {
assert_eq!(STATUS_STR.get(100), Some("100"));
assert_eq!(STATUS_STR.get(599), Some("599"));
assert_eq!(STATUS_STR.get(99), None);
assert_eq!(STATUS_STR.get(600), None);
assert_eq!(STATUS_STR.get(u16::MAX), None);
}
#[test]
#[ignore = "process-global allowlist; mutually exclusive with `register_route_allowlist_is_one_shot`"]
fn try_new_rejects_unknown_route() {
let _ = register_route_allowlist(&["/known"]);
let err = HttpRequestLabels::try_new("/unknown", "GET", 200).unwrap_err();
match err {
LabelError::UnknownRoute { route } => assert_eq!(route, "/unknown"),
other => panic!("expected UnknownRoute, got {other:?}"),
}
}
#[test]
fn try_new_rejects_lowercase_method() {
let alw = RouteAllowlist::new(&["/x"]);
let err =
HttpRequestLabels::try_new_with_allowlist("/x", "get", 200, Some(&alw)).unwrap_err();
match err {
LabelError::InvalidMethod { method } => assert_eq!(method, "get"),
other => panic!("expected InvalidMethod, got {other:?}"),
}
assert!(
HttpRequestLabels::try_new_with_allowlist("/x", "GET", 200, Some(&alw)).is_ok(),
"uppercase `GET` must pass the validator",
);
}
#[test]
fn try_new_rejects_status_outside_1xx_5xx() {
let alw = RouteAllowlist::new(&["/x"]);
for bad in [99u16, 600u16] {
let err = HttpRequestLabels::try_new_with_allowlist("/x", "GET", bad, Some(&alw))
.unwrap_err();
match err {
LabelError::InvalidStatus { status } => assert_eq!(status, bad),
other => panic!("expected InvalidStatus for {bad}, got {other:?}"),
}
}
}
#[test]
#[ignore = "process-global allowlist; mutually exclusive with other tests that touch ROUTE_ALLOWLIST"]
fn register_route_allowlist_is_one_shot() {
let _ = register_route_allowlist(&["/foo"]);
let err = register_route_allowlist(&["/bar"]).unwrap_err();
assert!(
matches!(err, LabelError::AllowlistAlreadyRegistered),
"expected AllowlistAlreadyRegistered, got {err:?}",
);
}
}