servlet 1.2.0

A small application runtime with an explicit operational contract
Documentation
//! Bound data before it enters the SDK's recording span and completed-span queue.
use super::config::TraceLimits;
use opentelemetry::{Array, KeyValue, Value, trace::Status};
use std::{borrow::Cow, mem::size_of};

fn prefix(value: &str, bytes: usize) -> &str {
    let mut end = value.len().min(bytes);
    while !value.is_char_boundary(end) {
        end -= 1;
    }
    &value[..end]
}

pub(super) fn text(value: impl Into<Cow<'static, str>>, bytes: usize) -> Cow<'static, str> {
    match value.into() {
        Cow::Borrowed(value) => Cow::Borrowed(prefix(value, bytes)),
        Cow::Owned(value) if value.capacity() <= bytes => Cow::Owned(value),
        Cow::Owned(value) => Cow::Owned(prefix(&value, bytes).to_owned()),
    }
}

pub(super) fn collection<T>(values: &mut Vec<T>, max: usize) {
    if values.capacity() > max {
        values.truncate(max);
        // Move the retained prefix into a fresh allocation. Collecting an owned
        // vector iterator can reuse the original buffer even after truncation.
        let mut bounded = Vec::with_capacity(values.len());
        bounded.append(values);
        *values = bounded;
    }
}

pub(super) fn attribute(kv: &mut KeyValue, limits: &TraceLimits) {
    if kv.key.as_str().len() > 128 {
        kv.key = prefix(kv.key.as_str(), 128).to_owned().into();
    }
    match &mut kv.value {
        Value::String(value) => {
            if value.as_str().len() > limits.value_bytes {
                *value = prefix(value.as_str(), limits.value_bytes).to_owned().into();
            }
        }
        Value::Array(array) => match array {
            Array::Bool(values) => collection(values, limits.value_bytes),
            Array::I64(values) => collection(values, limits.value_bytes / size_of::<i64>()),
            Array::F64(values) => collection(values, limits.value_bytes / size_of::<f64>()),
            Array::String(values) => {
                collection(values, 16);
                let mut remaining = limits.value_bytes;
                for value in values {
                    if value.as_str().len() > remaining {
                        *value = prefix(value.as_str(), remaining).to_owned().into();
                    }
                    remaining -= value.as_str().len();
                }
            }
            _ => *array = Array::String(Vec::new()),
        },
        Value::Bool(_) | Value::I64(_) | Value::F64(_) => {}
        _ => kv.value = Value::String("[unsupported]".into()),
    }
}

pub(super) fn attributes(values: &mut Vec<KeyValue>, max: u32, limits: &TraceLimits) {
    collection(values, max as usize);
    for kv in values {
        attribute(kv, limits);
    }
}

pub(super) fn status(value: Status, limits: &TraceLimits) -> Status {
    match value {
        Status::Error { description } => Status::error(text(description, limits.value_bytes)),
        other => other,
    }
}
#[cfg(test)]
#[path = "bounds_test.rs"]
mod bounds_test;