Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ async-stream = { version = "0.3.6", default-features = false }
async-trait = { version = "0.1.89", default-features = false }
axum = { version = "0.6.20", default-features = false }
base64 = { version = "0.23.0", default-features = false, features = ["std"] }
bitflags = { version = "2.13.2", default-features = false }
bytes = { version = "1.11.1", default-features = false, features = ["serde"] }
cfg-if = { version = "1.0.4", default-features = false }
chrono = { version = "0.4.44", default-features = false, features = ["clock", "serde"] }
Expand Down Expand Up @@ -660,6 +661,9 @@ api-client-minimal = [

top = ["vector-lib/vector-top", "vector-lib/api-client"]

# Internal typed trace event (RFC 25329). Off by default until a component consumes it.
typed-trace = ["vector-lib/typed-trace"]

aws-core = [
"aws-runtime",
"aws-config",
Expand Down
6 changes: 6 additions & 0 deletions docs/RUST_STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ fn my_function() {

- Group imports: `std` → external crates → internal (`crate::`)

## Unit tests

Unit tests for a type live in the same source file as that type (`#[cfg(test)] mod tests` at
the bottom of the file) unless they exercise behavior that spans multiple modules. Cross-module
tests belong with the composing type, not in a catch-all tests file for the crate.

## Logging Style

Always use the [Tracing crate](https://tracing.rs/tracing/)'s key/value style:
Expand Down
2 changes: 2 additions & 0 deletions lib/vector-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ vector-common = { path = "../vector-common", default-features = false, features
default = []
lua = ["dep:mlua", "dep:tokio-stream", "vrl/lua"]
test = ["vector-common/test", "proptest"]
# Internal typed trace event (RFC 25329). Off by default until a source, transform, or sink consumes it.
typed-trace = []

[[bench]]
name = "event"
Expand Down
2 changes: 2 additions & 0 deletions lib/vector-core/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ mod ser;
#[cfg(test)]
mod test;
mod trace;
#[cfg(any(test, feature = "typed-trace"))]
pub mod typed_trace;
pub mod util;
mod vrl_target;

Expand Down
184 changes: 184 additions & 0 deletions lib/vector-core/src/event/typed_trace/attributes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
//! Attribute maps mirroring OTLP `AnyValue`.

use std::collections::BTreeMap;

use bytes::Bytes;
use vector_common::byte_size_of::ByteSizeOf;
use vrl::value::KeyString;

/// Ordered map of attribute keys to [`AttrValue`]s.
pub type AttrMap = BTreeMap<KeyString, AttrValue>;

/// Ordered attribute map used throughout the typed trace model.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Attributes(AttrMap);

impl Attributes {
/// Creates an empty map.
#[must_use]
pub fn new() -> Self {
Self(AttrMap::new())
}

/// Returns the number of entries.
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}

/// Returns `true` when the map has no entries.
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

/// Returns a value by key.
#[must_use]
pub fn get(&self, key: &str) -> Option<&AttrValue> {
self.0.get(key)
}

/// Inserts `key` / `value`, returning the previous value if present.
pub fn insert(&mut self, key: impl Into<KeyString>, value: AttrValue) -> Option<AttrValue> {
self.0.insert(key.into(), value)
}

/// Removes `key` if present.
pub fn remove(&mut self, key: &str) -> Option<AttrValue> {
self.0.remove(key)
}

/// Iterates entries in key order.
pub fn iter(&self) -> impl Iterator<Item = (&KeyString, &AttrValue)> {
self.0.iter()
}

/// Returns a mutable view of the inner map.
pub fn as_map_mut(&mut self) -> &mut AttrMap {
&mut self.0
}

/// Consumes the newtype, returning the inner map.
#[must_use]
pub fn into_inner(self) -> AttrMap {
self.0
}
}

impl From<AttrMap> for Attributes {
fn from(map: AttrMap) -> Self {
Self(map)
}
}

impl FromIterator<(KeyString, AttrValue)> for Attributes {
fn from_iter<T: IntoIterator<Item = (KeyString, AttrValue)>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}

impl ByteSizeOf for Attributes {
fn allocated_bytes(&self) -> usize {
self.0.allocated_bytes()
}
}

/// Leaf attribute value. Mirrors OTLP `AnyValue`.
#[derive(Clone, Debug)]
pub enum AttrValue {
/// UTF-8 string.
String(String),
/// Arbitrary bytes.
Bytes(Bytes),
/// Boolean.
Bool(bool),
/// Signed 64-bit integer.
Integer(i64),
/// IEEE-754 floating-point value, including non-finite values.
Float(f64),
/// Homogeneous or mixed array.
Array(Vec<AttrValue>),
/// Nested map.
Map(AttrMap),
/// Explicit null.
Null,
}

impl PartialEq for AttrValue {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::String(a), Self::String(b)) => a == b,
(Self::Bytes(a), Self::Bytes(b)) => a == b,
(Self::Bool(a), Self::Bool(b)) => a == b,
(Self::Integer(a), Self::Integer(b)) => a == b,
(Self::Float(a), Self::Float(b)) => a.to_bits() == b.to_bits(),
(Self::Array(a), Self::Array(b)) => a == b,
(Self::Map(a), Self::Map(b)) => a == b,
(Self::Null, Self::Null) => true,
_ => false,
}
}
}

impl Eq for AttrValue {}

impl ByteSizeOf for AttrValue {
fn allocated_bytes(&self) -> usize {
match self {
Self::String(s) => s.len(),
Self::Bytes(b) => b.len(),
Self::Bool(_) | Self::Integer(_) | Self::Float(_) | Self::Null => 0,
Self::Array(values) => values.allocated_bytes(),
Self::Map(map) => map.allocated_bytes(),
}
}
}

impl From<String> for AttrValue {
fn from(value: String) -> Self {
Self::String(value)
}
}

impl From<&str> for AttrValue {
fn from(value: &str) -> Self {
Self::String(value.to_owned())
}
}

impl From<Bytes> for AttrValue {
fn from(value: Bytes) -> Self {
Self::Bytes(value)
}
}

impl From<bool> for AttrValue {
fn from(value: bool) -> Self {
Self::Bool(value)
}
}

impl From<i64> for AttrValue {
fn from(value: i64) -> Self {
Self::Integer(value)
}
}

impl From<f64> for AttrValue {
fn from(value: f64) -> Self {
Self::Float(value)
}
}

#[cfg(test)]
mod tests {
use similar_asserts::assert_eq;

use super::AttrValue;

#[test]
fn equality_uses_float_bits() {
assert_eq!(AttrValue::Float(f64::NAN), AttrValue::Float(f64::NAN));
assert_ne!(AttrValue::Float(0.0), AttrValue::Float(-0.0));
}
}
Loading
Loading