servlet 1.2.0

A small application runtime with an explicit operational contract
Documentation
//! Registrations preserve finite cardinality and native Prometheus encoding.
use super::*;

#[test]
fn native_instruments_encode_only_approved_series_and_escape_labels() {
    let registry = Registry::new();
    let ok = Labels::new(&[("outcome", "ok")]).unwrap();
    let escaped = Labels::new(&[("outcome", "line\n\"quoted\"")]).unwrap();
    let family = registry
        .counter_family(
            "orders_total",
            "Created orders",
            &[ok.clone(), escaped.clone()],
        )
        .unwrap();
    family.get(&ok).unwrap().inc_by(3);
    family.get(&escaped).unwrap().inc();
    assert!(
        family
            .get(&Labels::new(&[("outcome", "unknown")]).unwrap())
            .is_none()
    );
    let gauge = registry.gauge("queue_depth", "Queue depth").unwrap();
    gauge.set(4);
    let histogram = registry
        .histogram("lookup_seconds", "Lookup time", &[0.1, 0.5])
        .unwrap();
    histogram.observe(0.25);
    let output = registry.encode().unwrap();
    assert!(
        output.contains("orders_total{outcome=\"ok\"} 3\n"),
        "{output}"
    );
    assert!(output.contains("line\\n\\\"quoted\\\""), "{output}");
    assert!(!output.contains("unknown"));
    assert!(output.contains("queue_depth 4\n"));
    assert!(output.contains("lookup_seconds_bucket{le=\"0.5\"} 1\n"));
    assert!(output.contains("lookup_seconds_count 1\n"));
}

#[test]
fn conflicting_definitions_cannot_alias_histogram_output_names() {
    let registry = Registry::new();
    registry.histogram("latency", "time", &[1.0]).unwrap();
    for name in ["latency", "latency_bucket", "latency_sum", "latency_count"] {
        assert_eq!(
            registry.counter(name, "collision").unwrap_err(),
            MetricError::NameConflict
        );
    }
    let other = Registry::new();
    other.gauge("latency_sum", "existing").unwrap();
    assert_eq!(
        other.histogram("latency", "time", &[1.0]).unwrap_err(),
        MetricError::NameConflict
    );
    assert!(other.counter("unrelated", "available").is_ok());
}

#[test]
fn limits_sealing_and_registry_ownership_are_enforced() {
    let registry = Registry::new();
    let retained = registry.counter("events_total", "events").unwrap();
    registry.attach().unwrap();
    assert_eq!(
        registry.clone().attach().unwrap_err(),
        MetricError::AlreadyAttached
    );
    registry.seal();
    assert_eq!(
        registry.counter("late_total", "late").unwrap_err(),
        MetricError::Sealed
    );
    retained.inc();
    assert!(registry.encode().unwrap().contains("events_total 1\n"));
    let full = Registry::new();
    for index in 0..128 {
        full.counter(&format!("metric_{index}"), "bounded").unwrap();
    }
    assert_eq!(
        full.counter("extra", "bounded").unwrap_err(),
        MetricError::Capacity
    );
    let samples = Registry::new();
    let labels: Vec<_> = (0..64)
        .map(|index| Labels::new(&[("class", &index.to_string())]).unwrap())
        .collect();
    let buckets: Vec<_> = (1..=20).map(f64::from).collect();
    samples
        .histogram_family("first", "bounded", &buckets, &labels)
        .unwrap();
    samples
        .histogram_family("second", "bounded", &buckets, &labels)
        .unwrap();
    assert_eq!(
        samples
            .histogram_family("third", "bounded", &buckets, &labels)
            .unwrap_err(),
        MetricError::Capacity
    );
}

#[test]
fn invalid_names_schemas_buckets_and_label_values_fail_before_registration() {
    let registry = Registry::new();
    for name in ["", "servlet_shadow", "bad-name", "1bad", &"x".repeat(97)] {
        assert_eq!(
            registry.counter(name, "help").unwrap_err(),
            MetricError::InvalidDefinition
        );
    }
    for buckets in [
        &[][..],
        &[1.0, 1.0],
        &[2.0, 1.0],
        &[f64::INFINITY],
        &[f64::NAN],
    ] {
        assert_eq!(
            registry.histogram("bad", "help", buckets).unwrap_err(),
            MetricError::InvalidDefinition
        );
    }
    let first = Labels::new(&[("outcome", "ok")]).unwrap();
    let different = Labels::new(&[("other", "ok")]).unwrap();
    assert_eq!(
        registry
            .counter_family("bad", "help", &[first.clone(), different])
            .unwrap_err(),
        MetricError::InvalidDefinition
    );
    assert_eq!(
        registry
            .counter_family("bad", "help", &[first.clone(), first])
            .unwrap_err(),
        MetricError::InvalidDefinition
    );
    assert!(Labels::new(&[("name", &"x".repeat(65))]).is_err());
    for name in ["__private", "le", "quantile", "bad-name"] {
        assert!(Labels::new(&[(name, "value")]).is_err());
    }
    assert!(Labels::new(&[("same", "1"), ("same", "2")]).is_err());
    assert!(registry.counter("bad", "help").is_ok());
}

#[test]
fn native_label_hash_collisions_fail_instead_of_aliasing_counters() {
    let registry = Registry::new();
    let first = Labels::new(&[("left", "ab"), ("right", "c")]).unwrap();
    let second = Labels::new(&[("left", "a"), ("right", "bc")]).unwrap();
    assert_eq!(
        registry
            .counter_family("collision", "help", &[first, second])
            .unwrap_err(),
        MetricError::InvalidDefinition
    );
    assert!(registry.counter("collision", "help").is_ok());
}