#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct EventArray {
#[prost(oneof = "event_array::Events", tags = "1, 2, 3")]
pub events: ::core::option::Option<event_array::Events>,
}
pub mod event_array {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Events {
#[prost(message, tag = "1")]
Logs(super::LogArray),
#[prost(message, tag = "2")]
Metrics(super::MetricArray),
#[prost(message, tag = "3")]
Traces(super::TraceArray),
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct LogArray {
#[prost(message, repeated, tag = "1")]
pub logs: ::prost::alloc::vec::Vec<Log>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct MetricArray {
#[prost(message, repeated, tag = "1")]
pub metrics: ::prost::alloc::vec::Vec<Metric>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct TraceArray {
#[prost(message, repeated, tag = "1")]
pub traces: ::prost::alloc::vec::Vec<Trace>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct EventWrapper {
#[prost(oneof = "event_wrapper::Event", tags = "1, 2, 3")]
pub event: ::core::option::Option<event_wrapper::Event>,
}
pub mod event_wrapper {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Event {
#[prost(message, tag = "1")]
Log(super::Log),
#[prost(message, tag = "2")]
Metric(super::Metric),
#[prost(message, tag = "3")]
Trace(super::Trace),
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Log {
#[prost(btree_map = "string, message", tag = "1")]
pub fields: ::prost::alloc::collections::BTreeMap<::prost::alloc::string::String, Value>,
#[prost(message, optional, tag = "2")]
pub value: ::core::option::Option<Value>,
#[deprecated]
#[prost(message, optional, tag = "3")]
pub metadata: ::core::option::Option<Value>,
#[prost(message, optional, tag = "4")]
pub metadata_full: ::core::option::Option<Metadata>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Trace {
#[prost(btree_map = "string, message", tag = "1")]
pub fields: ::prost::alloc::collections::BTreeMap<::prost::alloc::string::String, Value>,
#[deprecated]
#[prost(message, optional, tag = "2")]
pub metadata: ::core::option::Option<Value>,
#[prost(message, optional, tag = "3")]
pub metadata_full: ::core::option::Option<Metadata>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ValueMap {
#[prost(btree_map = "string, message", tag = "1")]
pub fields: ::prost::alloc::collections::BTreeMap<::prost::alloc::string::String, Value>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ValueArray {
#[prost(message, repeated, tag = "1")]
pub items: ::prost::alloc::vec::Vec<Value>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Value {
#[prost(oneof = "value::Kind", tags = "1, 2, 4, 5, 6, 7, 8, 9")]
pub kind: ::core::option::Option<value::Kind>,
}
pub mod value {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Kind {
#[prost(bytes, tag = "1")]
RawBytes(::prost::bytes::Bytes),
#[prost(message, tag = "2")]
Timestamp(::prost_types::Timestamp),
#[prost(int64, tag = "4")]
Integer(i64),
#[prost(double, tag = "5")]
Float(f64),
#[prost(bool, tag = "6")]
Boolean(bool),
#[prost(message, tag = "7")]
Map(super::ValueMap),
#[prost(message, tag = "8")]
Array(super::ValueArray),
#[prost(enumeration = "super::ValueNull", tag = "9")]
Null(i32),
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct DatadogOriginMetadata {
#[prost(uint32, optional, tag = "1")]
pub origin_product: ::core::option::Option<u32>,
#[prost(uint32, optional, tag = "2")]
pub origin_category: ::core::option::Option<u32>,
#[prost(uint32, optional, tag = "3")]
pub origin_service: ::core::option::Option<u32>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Secrets {
#[prost(btree_map = "string, string", tag = "1")]
pub entries: ::prost::alloc::collections::BTreeMap<
::prost::alloc::string::String,
::prost::alloc::string::String,
>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct OutputId {
#[prost(string, tag = "1")]
pub component: ::prost::alloc::string::String,
#[prost(string, optional, tag = "2")]
pub port: ::core::option::Option<::prost::alloc::string::String>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Metadata {
#[prost(message, optional, tag = "1")]
pub value: ::core::option::Option<Value>,
#[prost(message, optional, tag = "2")]
pub datadog_origin_metadata: ::core::option::Option<DatadogOriginMetadata>,
#[prost(string, optional, tag = "3")]
pub source_id: ::core::option::Option<::prost::alloc::string::String>,
#[prost(string, optional, tag = "4")]
pub source_type: ::core::option::Option<::prost::alloc::string::String>,
#[prost(message, optional, tag = "5")]
pub upstream_id: ::core::option::Option<OutputId>,
#[prost(message, optional, tag = "6")]
pub secrets: ::core::option::Option<Secrets>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Metric {
#[prost(string, tag = "1")]
pub name: ::prost::alloc::string::String,
#[prost(message, optional, tag = "2")]
pub timestamp: ::core::option::Option<::prost_types::Timestamp>,
#[prost(btree_map = "string, string", tag = "3")]
pub tags_v1: ::prost::alloc::collections::BTreeMap<
::prost::alloc::string::String,
::prost::alloc::string::String,
>,
#[prost(btree_map = "string, message", tag = "20")]
pub tags_v2: ::prost::alloc::collections::BTreeMap<::prost::alloc::string::String, TagValues>,
#[prost(enumeration = "metric::Kind", tag = "4")]
pub kind: i32,
#[prost(string, tag = "11")]
pub namespace: ::prost::alloc::string::String,
#[prost(uint32, tag = "18")]
pub interval_ms: u32,
#[deprecated]
#[prost(message, optional, tag = "19")]
pub metadata: ::core::option::Option<Value>,
#[prost(message, optional, tag = "21")]
pub metadata_full: ::core::option::Option<Metadata>,
#[prost(
oneof = "metric::Value",
tags = "5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17"
)]
pub value: ::core::option::Option<metric::Value>,
}
pub mod metric {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum Kind {
Incremental = 0,
Absolute = 1,
}
impl Kind {
pub fn as_str_name(&self) -> &'static str {
match self {
Kind::Incremental => "Incremental",
Kind::Absolute => "Absolute",
}
}
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
match value {
"Incremental" => Some(Self::Incremental),
"Absolute" => Some(Self::Absolute),
_ => None,
}
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Value {
#[prost(message, tag = "5")]
Counter(super::Counter),
#[prost(message, tag = "6")]
Gauge(super::Gauge),
#[prost(message, tag = "7")]
Set(super::Set),
#[prost(message, tag = "8")]
Distribution1(super::Distribution1),
#[prost(message, tag = "9")]
AggregatedHistogram1(super::AggregatedHistogram1),
#[prost(message, tag = "10")]
AggregatedSummary1(super::AggregatedSummary1),
#[prost(message, tag = "12")]
Distribution2(super::Distribution2),
#[prost(message, tag = "13")]
AggregatedHistogram2(super::AggregatedHistogram2),
#[prost(message, tag = "14")]
AggregatedSummary2(super::AggregatedSummary2),
#[prost(message, tag = "15")]
Sketch(super::Sketch),
#[prost(message, tag = "16")]
AggregatedHistogram3(super::AggregatedHistogram3),
#[prost(message, tag = "17")]
AggregatedSummary3(super::AggregatedSummary3),
}
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct TagValues {
#[prost(message, repeated, tag = "1")]
pub values: ::prost::alloc::vec::Vec<TagValue>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct TagValue {
#[prost(string, optional, tag = "1")]
pub value: ::core::option::Option<::prost::alloc::string::String>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Counter {
#[prost(double, tag = "1")]
pub value: f64,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Gauge {
#[prost(double, tag = "1")]
pub value: f64,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Set {
#[prost(string, repeated, tag = "1")]
pub values: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Distribution1 {
#[prost(double, repeated, tag = "1")]
pub values: ::prost::alloc::vec::Vec<f64>,
#[prost(uint32, repeated, tag = "2")]
pub sample_rates: ::prost::alloc::vec::Vec<u32>,
#[prost(enumeration = "StatisticKind", tag = "3")]
pub statistic: i32,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Distribution2 {
#[prost(message, repeated, tag = "1")]
pub samples: ::prost::alloc::vec::Vec<DistributionSample>,
#[prost(enumeration = "StatisticKind", tag = "2")]
pub statistic: i32,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct DistributionSample {
#[prost(double, tag = "1")]
pub value: f64,
#[prost(uint32, tag = "2")]
pub rate: u32,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AggregatedHistogram1 {
#[prost(double, repeated, tag = "1")]
pub buckets: ::prost::alloc::vec::Vec<f64>,
#[prost(uint32, repeated, tag = "2")]
pub counts: ::prost::alloc::vec::Vec<u32>,
#[prost(uint32, tag = "3")]
pub count: u32,
#[prost(double, tag = "4")]
pub sum: f64,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AggregatedHistogram2 {
#[prost(message, repeated, tag = "1")]
pub buckets: ::prost::alloc::vec::Vec<HistogramBucket>,
#[prost(uint32, tag = "2")]
pub count: u32,
#[prost(double, tag = "3")]
pub sum: f64,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AggregatedHistogram3 {
#[prost(message, repeated, tag = "1")]
pub buckets: ::prost::alloc::vec::Vec<HistogramBucket3>,
#[prost(uint64, tag = "2")]
pub count: u64,
#[prost(double, tag = "3")]
pub sum: f64,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct HistogramBucket {
#[prost(double, tag = "1")]
pub upper_limit: f64,
#[prost(uint32, tag = "2")]
pub count: u32,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct HistogramBucket3 {
#[prost(double, tag = "1")]
pub upper_limit: f64,
#[prost(uint64, tag = "2")]
pub count: u64,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AggregatedSummary1 {
#[prost(double, repeated, tag = "1")]
pub quantiles: ::prost::alloc::vec::Vec<f64>,
#[prost(double, repeated, tag = "2")]
pub values: ::prost::alloc::vec::Vec<f64>,
#[prost(uint32, tag = "3")]
pub count: u32,
#[prost(double, tag = "4")]
pub sum: f64,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AggregatedSummary2 {
#[prost(message, repeated, tag = "1")]
pub quantiles: ::prost::alloc::vec::Vec<SummaryQuantile>,
#[prost(uint32, tag = "2")]
pub count: u32,
#[prost(double, tag = "3")]
pub sum: f64,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AggregatedSummary3 {
#[prost(message, repeated, tag = "1")]
pub quantiles: ::prost::alloc::vec::Vec<SummaryQuantile>,
#[prost(uint64, tag = "2")]
pub count: u64,
#[prost(double, tag = "3")]
pub sum: f64,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct SummaryQuantile {
#[prost(double, tag = "1")]
pub quantile: f64,
#[prost(double, tag = "2")]
pub value: f64,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Sketch {
#[prost(oneof = "sketch::Sketch", tags = "1")]
pub sketch: ::core::option::Option<sketch::Sketch>,
}
pub mod sketch {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct AgentDdSketch {
#[prost(uint32, tag = "1")]
pub count: u32,
#[prost(double, tag = "2")]
pub min: f64,
#[prost(double, tag = "3")]
pub max: f64,
#[prost(double, tag = "4")]
pub sum: f64,
#[prost(double, tag = "5")]
pub avg: f64,
#[prost(sint32, repeated, tag = "6")]
pub k: ::prost::alloc::vec::Vec<i32>,
#[prost(uint32, repeated, tag = "7")]
pub n: ::prost::alloc::vec::Vec<u32>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Sketch {
#[prost(message, tag = "1")]
AgentDdSketch(AgentDdSketch),
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum ValueNull {
NullValue = 0,
}
impl ValueNull {
pub fn as_str_name(&self) -> &'static str {
match self {
ValueNull::NullValue => "NULL_VALUE",
}
}
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
match value {
"NULL_VALUE" => Some(Self::NullValue),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum StatisticKind {
Histogram = 0,
Summary = 1,
}
impl StatisticKind {
pub fn as_str_name(&self) -> &'static str {
match self {
StatisticKind::Histogram => "Histogram",
StatisticKind::Summary => "Summary",
}
}
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
match value {
"Histogram" => Some(Self::Histogram),
"Summary" => Some(Self::Summary),
_ => None,
}
}
}
#[cfg(test)]
mod test {
use std::collections::BTreeMap;
use prost::{Message, bytes::Bytes};
use super::*;
fn roundtrip<T: Message + Default + PartialEq + std::fmt::Debug>(msg: &T) -> T {
let buf = msg.encode_to_vec();
assert_eq!(
buf.len(),
msg.encoded_len(),
"encoded_len() disagrees with the encoding of {msg:?}"
);
let decoded = T::decode(buf.as_slice()).expect("unable to decode own encoding");
assert_eq!(&decoded, msg, "round trip changed the message");
decoded
}
fn key(tag: u32, wire_type: u32) -> Vec<u8> {
let mut v = (tag << 3) | wire_type;
let mut out = vec![];
loop {
if v < 0x80 {
out.push(v as u8);
return out;
}
out.push(((v & 0x7f) | 0x80) as u8);
v >>= 7;
}
}
fn int(v: i64) -> Value {
Value {
kind: Some(value::Kind::Integer(v)),
}
}
fn bytes(v: &str) -> Value {
Value {
kind: Some(value::Kind::RawBytes(Bytes::copy_from_slice(v.as_bytes()))),
}
}
fn map_fields(v: &Value) -> &BTreeMap<String, Value> {
match v.kind.as_ref().expect("value has no kind") {
value::Kind::Map(m) => &m.fields,
x => panic!("expected a map, got {x:?}"),
}
}
fn array_items(v: &Value) -> &[Value] {
match v.kind.as_ref().expect("value has no kind") {
value::Kind::Array(a) => &a.items,
x => panic!("expected an array, got {x:?}"),
}
}
#[test]
fn test_key_helper() {
assert_eq!(key(1, 2), vec![10]);
assert_eq!(key(4, 0), vec![32]);
assert_eq!(key(9, 0), vec![72]);
assert_eq!(key(16, 2), vec![0x82, 0x01]);
}
#[test]
fn test_value_null_enum() {
assert_eq!(ValueNull::NullValue as i32, 0);
assert_eq!(ValueNull::NullValue.as_str_name(), "NULL_VALUE");
assert_eq!(
ValueNull::from_str_name("NULL_VALUE"),
Some(ValueNull::NullValue)
);
assert_eq!(
ValueNull::from_str_name(ValueNull::NullValue.as_str_name()),
Some(ValueNull::NullValue)
);
assert_eq!(ValueNull::from_str_name("null_value"), None);
assert_eq!(ValueNull::from_str_name("NullValue"), None);
assert_eq!(ValueNull::from_str_name("Null"), None);
assert_eq!(ValueNull::from_str_name(""), None);
assert_eq!(ValueNull::try_from(0).unwrap(), ValueNull::NullValue);
assert!(ValueNull::try_from(1).is_err());
assert!(ValueNull::try_from(-1).is_err());
}
#[test]
fn test_statistic_kind_enum() {
assert_eq!(StatisticKind::Histogram as i32, 0);
assert_eq!(StatisticKind::Summary as i32, 1);
assert_eq!(StatisticKind::Histogram.as_str_name(), "Histogram");
assert_eq!(StatisticKind::Summary.as_str_name(), "Summary");
for v in [StatisticKind::Histogram, StatisticKind::Summary] {
assert_eq!(StatisticKind::from_str_name(v.as_str_name()), Some(v));
assert_eq!(StatisticKind::try_from(v as i32).unwrap(), v);
}
assert_eq!(StatisticKind::from_str_name("histogram"), None);
assert_eq!(StatisticKind::from_str_name("SUMMARY"), None);
assert_eq!(StatisticKind::from_str_name(""), None);
assert!(StatisticKind::try_from(2).is_err());
assert!(StatisticKind::try_from(-1).is_err());
assert!(StatisticKind::Histogram < StatisticKind::Summary);
}
#[test]
fn test_metric_kind_enum() {
assert_eq!(metric::Kind::Incremental as i32, 0);
assert_eq!(metric::Kind::Absolute as i32, 1);
assert_eq!(metric::Kind::Incremental.as_str_name(), "Incremental");
assert_eq!(metric::Kind::Absolute.as_str_name(), "Absolute");
for v in [metric::Kind::Incremental, metric::Kind::Absolute] {
assert_eq!(metric::Kind::from_str_name(v.as_str_name()), Some(v));
assert_eq!(metric::Kind::try_from(v as i32).unwrap(), v);
}
assert_eq!(metric::Kind::from_str_name("incremental"), None);
assert_eq!(metric::Kind::from_str_name("ABSOLUTE"), None);
assert_eq!(metric::Kind::from_str_name(""), None);
assert!(metric::Kind::try_from(2).is_err());
}
#[test]
fn test_value_kind_wire_format() {
let cases = vec![
(value::Kind::RawBytes(Bytes::from_static(b"x")), 1, 2),
(
value::Kind::Timestamp(prost_types::Timestamp {
seconds: 1,
nanos: 2,
}),
2,
2,
),
(value::Kind::Integer(1), 4, 0),
(value::Kind::Float(1.0), 5, 1),
(value::Kind::Boolean(true), 6, 0),
(
value::Kind::Map(ValueMap {
fields: BTreeMap::from([("a".to_owned(), int(1))]),
}),
7,
2,
),
(
value::Kind::Array(ValueArray {
items: vec![int(1)],
}),
8,
2,
),
(value::Kind::Null(ValueNull::NullValue as i32), 9, 0),
];
for (kind, tag, wire_type) in cases {
let v = Value {
kind: Some(kind.clone()),
};
let buf = v.encode_to_vec();
assert!(
buf.starts_with(&key(tag, wire_type)),
"{kind:?} should be encoded with tag {tag}/wire type {wire_type}, got {buf:?}"
);
roundtrip(&v);
}
}
#[test]
fn test_value_missing_vs_null() {
let empty = Value::default();
assert_eq!(empty.kind, None);
assert_eq!(empty.encoded_len(), 0);
assert!(empty.encode_to_vec().is_empty());
assert_eq!(Value::decode([].as_slice()).unwrap(), empty);
let null = Value {
kind: Some(value::Kind::Null(ValueNull::NullValue as i32)),
};
assert_ne!(empty, null);
assert_eq!(null.encode_to_vec(), vec![72, 0]);
roundtrip(&null);
}
#[test]
fn test_value_null_preserves_unknown_enum_value() {
let v = Value {
kind: Some(value::Kind::Null(7)),
};
assert!(ValueNull::try_from(7).is_err());
assert_eq!(roundtrip(&v).kind, Some(value::Kind::Null(7)));
}
#[test]
fn test_value_integer_edges() {
for i in [0, 1, -1, 127, 128, i64::MIN, i64::MAX] {
roundtrip(&int(i));
}
assert_eq!(int(0).encode_to_vec(), vec![32, 0]);
assert_eq!(int(-1).encoded_len(), 11);
assert_eq!(int(i64::MIN).encoded_len(), 11);
assert_eq!(int(i64::MAX).encoded_len(), 10);
assert_eq!(int(1).encoded_len(), 2);
}
#[test]
fn test_value_float_edges() {
for f in [
0.0,
-0.0,
0.5,
-1.5,
f64::MAX,
f64::MIN,
f64::MIN_POSITIVE,
f64::EPSILON,
f64::INFINITY,
f64::NEG_INFINITY,
] {
let v = Value {
kind: Some(value::Kind::Float(f)),
};
assert_eq!(v.encoded_len(), 9, "{f} should be encoded as a double");
let Some(value::Kind::Float(g)) = roundtrip(&v).kind else {
panic!("decoded into a different kind");
};
assert_eq!(
g.to_bits(),
f.to_bits(),
"{f} did not survive the round trip"
);
}
let v = Value {
kind: Some(value::Kind::Float(f64::NAN)),
};
match Value::decode(v.encode_to_vec().as_slice()).unwrap().kind {
Some(value::Kind::Float(g)) => assert!(g.is_nan()),
x => panic!("expected a float, got {x:?}"),
}
}
#[test]
fn test_value_raw_bytes() {
for b in [
&b""[..],
&b"\x00"[..],
&[0xff, 0xfe, 0x00, 0x80][..],
"h\u{e9}llo \u{1f600}".as_bytes(),
] {
let v = Value {
kind: Some(value::Kind::RawBytes(Bytes::copy_from_slice(b))),
};
let Some(value::Kind::RawBytes(got)) = roundtrip(&v).kind else {
panic!("decoded into a different kind");
};
assert_eq!(&got[..], b);
}
assert_eq!(bytes("").encode_to_vec(), vec![10, 0]);
let v = bytes("h\u{e9}llo");
assert_eq!(v.encode_to_vec()[1], 6);
assert_eq!(v.encoded_len(), 8);
}
#[test]
fn test_value_timestamp() {
for (seconds, nanos) in [
(0, 0),
(1, 1),
(-1, 500_000_000),
(1_700_000_000, 999_999_999),
(i64::MIN, 0),
(i64::MAX, i32::MAX),
] {
roundtrip(&Value {
kind: Some(value::Kind::Timestamp(prost_types::Timestamp {
seconds,
nanos,
})),
});
}
let v = Value {
kind: Some(value::Kind::Timestamp(prost_types::Timestamp::default())),
};
assert_eq!(v.encode_to_vec(), vec![18, 0]);
assert_eq!(roundtrip(&v), v);
}
#[test]
fn test_value_map_sorted_and_deterministic() {
let mk = |keys: [&str; 3]| ValueMap {
fields: keys.iter().map(|k| ((*k).to_owned(), int(1))).collect(),
};
assert_eq!(
mk(["a", "b", "c"]).encode_to_vec(),
mk(["c", "a", "b"]).encode_to_vec()
);
assert_eq!(
mk(["b", "c", "a"]).encode_to_vec(),
mk(["c", "b", "a"]).encode_to_vec()
);
let buf = mk(["c", "a", "b"]).encode_to_vec();
assert!(buf.starts_with(&[10, 7, 10, 1, b'a']), "got {buf:?}");
let decoded = ValueMap::decode(buf.as_slice()).unwrap();
assert_eq!(
decoded
.fields
.keys()
.map(String::as_str)
.collect::<Vec<_>>(),
vec!["a", "b", "c"]
);
}
#[test]
fn test_value_map_and_array_empty_and_nested() {
let empty_map = Value {
kind: Some(value::Kind::Map(ValueMap::default())),
};
assert_eq!(empty_map.encode_to_vec(), vec![58, 0]);
assert!(map_fields(&roundtrip(&empty_map)).is_empty());
let empty_array = Value {
kind: Some(value::Kind::Array(ValueArray::default())),
};
assert_eq!(empty_array.encode_to_vec(), vec![66, 0]);
assert!(array_items(&roundtrip(&empty_array)).is_empty());
assert_ne!(empty_map, empty_array);
let arr = Value {
kind: Some(value::Kind::Array(ValueArray {
items: vec![int(2), int(1), int(2), Value::default()],
})),
};
let items = array_items(&roundtrip(&arr)).to_vec();
assert_eq!(items, vec![int(2), int(1), int(2), Value::default()]);
let nested = Value {
kind: Some(value::Kind::Map(ValueMap {
fields: BTreeMap::from([(
"outer".to_owned(),
Value {
kind: Some(value::Kind::Array(ValueArray {
items: vec![Value {
kind: Some(value::Kind::Map(ValueMap {
fields: BTreeMap::from([("inner".to_owned(), bytes("deep"))]),
})),
}],
})),
},
)]),
})),
};
let decoded = roundtrip(&nested);
let inner = &array_items(&map_fields(&decoded)["outer"])[0];
assert_eq!(map_fields(inner)["inner"], bytes("deep"));
}
#[test]
fn test_event_array_variants() {
let log = Log {
value: Some(int(1)),
..Default::default()
};
let metric = Metric {
name: "m".to_owned(),
..Default::default()
};
let trace = Trace {
fields: BTreeMap::from([("t".to_owned(), int(1))]),
..Default::default()
};
let cases = vec![
(
event_array::Events::Logs(LogArray {
logs: vec![log.clone(), log.clone()],
}),
1,
),
(
event_array::Events::Metrics(MetricArray {
metrics: vec![metric],
}),
2,
),
(
event_array::Events::Traces(TraceArray {
traces: vec![trace],
}),
3,
),
];
for (events, tag) in cases {
let ev = EventArray {
events: Some(events.clone()),
};
assert!(
ev.encode_to_vec().starts_with(&key(tag, 2)),
"{events:?} should use tag {tag}"
);
roundtrip(&ev);
}
let ev = EventArray {
events: Some(event_array::Events::Logs(LogArray {
logs: vec![log.clone(), log],
})),
};
let Some(event_array::Events::Logs(logs)) = roundtrip(&ev).events else {
panic!("decoded into a different variant");
};
assert_eq!(logs.logs.len(), 2);
assert_eq!(EventArray::default().encoded_len(), 0);
assert_eq!(EventArray::decode([].as_slice()).unwrap().events, None);
}
#[test]
fn test_event_wrapper_variants() {
let cases = vec![
(
event_wrapper::Event::Log(Log {
value: Some(bytes("x")),
..Default::default()
}),
1,
),
(
event_wrapper::Event::Metric(Metric {
name: "n".to_owned(),
..Default::default()
}),
2,
),
(event_wrapper::Event::Trace(Trace::default()), 3),
];
for (event, tag) in cases {
let ev = EventWrapper {
event: Some(event.clone()),
};
assert!(
ev.encode_to_vec().starts_with(&key(tag, 2)),
"{event:?} should use tag {tag}"
);
roundtrip(&ev);
}
assert_eq!(EventWrapper::default().encoded_len(), 0);
}
#[test]
fn test_log_fields() {
let meta = Metadata {
value: Some(bytes("meta")),
datadog_origin_metadata: Some(DatadogOriginMetadata {
origin_product: Some(1),
origin_category: None,
origin_service: Some(0),
}),
source_id: Some("sid".to_owned()),
source_type: Some(String::new()),
upstream_id: Some(OutputId {
component: "c".to_owned(),
port: None,
}),
secrets: Some(Secrets {
entries: BTreeMap::from([("k".to_owned(), "v".to_owned())]),
}),
};
let log = Log {
fields: BTreeMap::from([(".".to_owned(), int(1)), ("b".to_owned(), bytes("x"))]),
value: Some(bytes("value")),
metadata: Some(bytes("deprecated")),
metadata_full: Some(meta),
..Default::default()
};
let decoded = roundtrip(&log);
assert_eq!(decoded.fields.len(), 2);
assert_eq!(decoded.fields["."], int(1));
assert_eq!(decoded.value, Some(bytes("value")));
let meta = decoded.metadata_full.unwrap();
assert_eq!(meta.source_type.as_deref(), Some(""));
assert_eq!(meta.upstream_id.unwrap().port, None);
let dd = meta.datadog_origin_metadata.unwrap();
assert_eq!(dd.origin_product, Some(1));
assert_eq!(dd.origin_category, None);
assert_eq!(dd.origin_service, Some(0));
let deprecated_only = Log {
metadata: Some(int(1)),
..Default::default()
};
let full_only = Log {
metadata_full: Some(Metadata {
value: Some(int(1)),
..Default::default()
}),
..Default::default()
};
assert!(deprecated_only.encode_to_vec().starts_with(&key(3, 2)));
assert!(full_only.encode_to_vec().starts_with(&key(4, 2)));
assert_ne!(deprecated_only.encode_to_vec(), full_only.encode_to_vec());
assert_eq!(roundtrip(&deprecated_only).metadata_full, None);
assert_eq!(roundtrip(&full_only).metadata, None);
assert_eq!(Log::default().encoded_len(), 0);
}
#[test]
fn test_optional_scalars_empty_vs_absent() {
assert_eq!(TagValue { value: None }.encoded_len(), 0);
let empty = TagValue {
value: Some(String::new()),
};
assert_eq!(empty.encode_to_vec(), vec![10, 0]);
assert_eq!(roundtrip(&empty).value.as_deref(), Some(""));
assert_eq!(roundtrip(&TagValue { value: None }).value, None);
assert_ne!(TagValue { value: None }, empty);
let id = OutputId {
component: String::new(),
port: Some(String::new()),
};
assert_eq!(id.encode_to_vec(), vec![18, 0]);
assert_eq!(roundtrip(&id).port.as_deref(), Some(""));
assert_eq!(OutputId::default().encoded_len(), 0);
let secrets = Secrets {
entries: BTreeMap::from([
("k".to_owned(), String::new()),
("z".to_owned(), "v".to_owned()),
]),
};
let decoded = roundtrip(&secrets);
assert_eq!(decoded.entries["k"], "");
assert_eq!(decoded.entries["z"], "v");
assert_eq!(Secrets::default().encoded_len(), 0);
}
#[test]
fn test_metric_all_fields() {
let m = Metric {
name: "requests".to_owned(),
timestamp: Some(prost_types::Timestamp {
seconds: 1_700_000_000,
nanos: 123,
}),
tags_v1: BTreeMap::from([("a".to_owned(), "1".to_owned())]),
tags_v2: BTreeMap::from([(
"b".to_owned(),
TagValues {
values: vec![
TagValue {
value: Some("x".to_owned()),
},
TagValue { value: None },
],
},
)]),
kind: metric::Kind::Absolute as i32,
namespace: "ns".to_owned(),
interval_ms: 1000,
metadata: Some(int(1)),
metadata_full: Some(Metadata::default()),
value: Some(metric::Value::Counter(Counter { value: 1.5 })),
};
let decoded = roundtrip(&m);
assert_eq!(decoded.name, "requests");
assert_eq!(decoded.kind, metric::Kind::Absolute as i32);
assert_eq!(decoded.interval_ms, 1000);
assert_eq!(decoded.tags_v1["a"], "1");
assert_eq!(decoded.tags_v2["b"].values.len(), 2);
assert_eq!(decoded.tags_v2["b"].values[1].value, None);
let tags_only = Metric {
tags_v2: m.tags_v2.clone(),
..Default::default()
};
assert_eq!(key(20, 2), vec![0xa2, 0x01]);
assert!(
tags_only.encode_to_vec().starts_with(&key(20, 2)),
"got {:?}",
tags_only.encode_to_vec()
);
roundtrip(&tags_only);
let m = Metric {
kind: 42,
..Default::default()
};
assert!(metric::Kind::try_from(42).is_err());
assert_eq!(roundtrip(&m).kind, 42);
assert_eq!(Metric::default().encoded_len(), 0);
}
#[test]
fn test_metric_value_variants() {
let cases = vec![
(metric::Value::Counter(Counter { value: 1.0 }), 5),
(metric::Value::Gauge(Gauge { value: -1.0 }), 6),
(
metric::Value::Set(Set {
values: vec!["a".to_owned(), String::new(), "a".to_owned()],
}),
7,
),
(
metric::Value::Distribution1(Distribution1 {
values: vec![1.0, 2.0],
sample_rates: vec![1, 2],
statistic: StatisticKind::Summary as i32,
}),
8,
),
(
metric::Value::AggregatedHistogram1(AggregatedHistogram1 {
buckets: vec![1.0, 2.0],
counts: vec![1, 2],
count: 3,
sum: 3.0,
}),
9,
),
(
metric::Value::AggregatedSummary1(AggregatedSummary1 {
quantiles: vec![0.5],
values: vec![1.0],
count: 1,
sum: 1.0,
}),
10,
),
(
metric::Value::Distribution2(Distribution2 {
samples: vec![DistributionSample {
value: 1.0,
rate: 2,
}],
statistic: StatisticKind::Histogram as i32,
}),
12,
),
(
metric::Value::AggregatedHistogram2(AggregatedHistogram2 {
buckets: vec![HistogramBucket {
upper_limit: 1.0,
count: u32::MAX,
}],
count: u32::MAX,
sum: 1.0,
}),
13,
),
(
metric::Value::AggregatedSummary2(AggregatedSummary2 {
quantiles: vec![SummaryQuantile {
quantile: 0.99,
value: 1.0,
}],
count: 1,
sum: 1.0,
}),
14,
),
(
metric::Value::Sketch(Sketch {
sketch: Some(sketch::Sketch::AgentDdSketch(sketch::AgentDdSketch {
count: 1,
min: -1.0,
max: 1.0,
sum: 0.0,
avg: 0.0,
k: vec![-1, 0, 1],
n: vec![1, 2, 3],
})),
}),
15,
),
(
metric::Value::AggregatedHistogram3(AggregatedHistogram3 {
buckets: vec![HistogramBucket3 {
upper_limit: f64::INFINITY,
count: u64::MAX,
}],
count: u64::MAX,
sum: 1.0,
}),
16,
),
(
metric::Value::AggregatedSummary3(AggregatedSummary3 {
quantiles: vec![SummaryQuantile {
quantile: 0.5,
value: 1.0,
}],
count: u64::MAX,
sum: 1.0,
}),
17,
),
];
assert_eq!(cases.len(), 12, "all metric value variants must be covered");
for (value, tag) in cases {
let m = Metric {
value: Some(value.clone()),
..Default::default()
};
assert!(
m.encode_to_vec().starts_with(&key(tag, 2)),
"{value:?} should use tag {tag}, got {:?}",
m.encode_to_vec()
);
let decoded = roundtrip(&m);
assert_eq!(decoded.value, Some(value));
}
}
#[test]
fn test_repeated_and_packed_encoding() {
let d = Distribution1 {
values: vec![1.0, 2.0],
..Default::default()
};
let buf = d.encode_to_vec();
assert_eq!(buf[0], key(1, 2)[0]);
assert_eq!(buf[1], 16);
assert_eq!(buf.len(), 18);
let s = Set {
values: vec!["ab".to_owned(), String::new()],
};
assert_eq!(s.encode_to_vec(), vec![10, 2, b'a', b'b', 10, 0]);
assert_eq!(roundtrip(&s).values, vec!["ab".to_owned(), String::new()]);
let sk = sketch::AgentDdSketch {
k: vec![-1],
..Default::default()
};
assert_eq!(sk.encode_to_vec(), vec![50, 1, 1]);
roundtrip(&sketch::AgentDdSketch {
k: vec![i32::MIN, -1, 0, 1, i32::MAX],
n: vec![1, 2, 3, 4, 5],
..Default::default()
});
assert_eq!(Distribution1::default().encoded_len(), 0);
assert_eq!(Set::default().encoded_len(), 0);
assert_eq!(sketch::AgentDdSketch::default().encoded_len(), 0);
assert_eq!(Sketch::default().encoded_len(), 0);
}
#[test]
fn test_proto3_implicit_presence() {
assert_eq!(Counter { value: 0.0 }.encoded_len(), 0);
assert_eq!(Gauge { value: 0.0 }.encoded_len(), 0);
assert_eq!(
Distribution1 {
statistic: StatisticKind::Histogram as i32,
..Default::default()
}
.encoded_len(),
0
);
assert_eq!(
Distribution1 {
statistic: StatisticKind::Summary as i32,
..Default::default()
}
.encode_to_vec(),
vec![24, 1]
);
let c = Counter { value: -0.0 };
assert_ne!(c.value.to_bits(), 0.0f64.to_bits());
assert_eq!(c.encoded_len(), 0);
assert_eq!(
Counter::decode([].as_slice()).unwrap().value.to_bits(),
0.0f64.to_bits()
);
let v = Value {
kind: Some(value::Kind::Float(-0.0)),
};
let Some(value::Kind::Float(f)) = roundtrip(&v).kind else {
panic!("decoded into a different kind");
};
assert_eq!(f.to_bits(), (-0.0f64).to_bits());
}
#[test]
fn test_decode_unknown_fields_are_skipped() {
let unknown = [key(3, 0).as_slice(), &[5]].concat();
assert_eq!(Value::decode(unknown.as_slice()).unwrap(), Value::default());
let mixed = [&unknown[..], &int(7).encode_to_vec()[..]].concat();
assert_eq!(Value::decode(mixed.as_slice()).unwrap(), int(7));
let mut buf = Log {
value: Some(int(1)),
..Default::default()
}
.encode_to_vec();
buf.extend([key(99, 2).as_slice(), &[2, 0xde, 0xad]].concat());
let decoded = Log::decode(buf.as_slice()).unwrap();
assert_eq!(decoded.value, Some(int(1)));
}
#[test]
fn test_decode_malformed_input() {
assert!(Value::decode([10, 5, 1].as_slice()).is_err());
assert!(Value::decode([34, 1, 5].as_slice()).is_err());
assert!(Value::decode([32, 0x80].as_slice()).is_err());
assert!(Value::decode([0, 0].as_slice()).is_err());
assert!(EventArray::decode([10, 3, 10, 5, 1].as_slice()).is_err());
}
#[test]
fn test_prepare_event_wire_structure() {
let ev = crate::vector::prepare_event(serde_json::json!({
"s": "str",
"i": 42,
"neg": -42,
"f": 1.5,
"b": false,
"n": null,
"arr": [1, "two", null],
"obj": {"k": "v"},
"empty_obj": {},
"empty_arr": [],
"big": 18446744073709551615_u64,
"": "empty key",
}));
let decoded = roundtrip(&ev);
let Some(event_array::Events::Logs(logs)) = decoded.events else {
panic!("expected a log array");
};
assert_eq!(logs.logs.len(), 1);
let log = &logs.logs[0];
assert_eq!(log.fields.len(), 1);
assert_eq!(
log.fields["."].kind,
Some(value::Kind::Null(ValueNull::NullValue as i32))
);
assert_eq!(log.metadata, None);
assert_eq!(log.metadata_full, None);
let fields = map_fields(log.value.as_ref().expect("log has no value"));
assert_eq!(fields["s"], bytes("str"));
assert_eq!(fields["i"], int(42));
assert_eq!(fields["neg"], int(-42));
assert_eq!(fields["f"].kind, Some(value::Kind::Float(1.5)));
assert_eq!(fields["b"].kind, Some(value::Kind::Boolean(false)));
assert_eq!(
fields["n"].kind,
Some(value::Kind::Null(ValueNull::NullValue as i32))
);
assert_eq!(fields["big"], bytes("18446744073709551615"));
assert!(map_fields(&fields["empty_obj"]).is_empty());
assert!(array_items(&fields["empty_arr"]).is_empty());
assert_eq!(fields[""], bytes("empty key"));
assert_eq!(map_fields(&fields["obj"])["k"], bytes("v"));
assert_eq!(
array_items(&fields["arr"]).to_vec(),
vec![
int(1),
bytes("two"),
Value {
kind: Some(value::Kind::Null(ValueNull::NullValue as i32))
}
]
);
}
#[test]
fn test_encode_value_timestamp() {
let cases = [
(0, 0),
(1_700_000_000, 123_456_789),
(-1, 500_000_000),
(-1_000_000, 1),
];
for (seconds, nanos) in cases {
let ts = chrono::DateTime::from_timestamp(seconds, nanos).unwrap();
let v = crate::vector::encode_value(vrl::value::Value::Timestamp(ts));
assert_eq!(
v.kind,
Some(value::Kind::Timestamp(prost_types::Timestamp {
seconds,
nanos: nanos as i32,
})),
"{ts} was encoded incorrectly"
);
roundtrip(&v);
}
}
#[test]
fn test_encode_value_kind_mapping() {
use vrl::value::Value as V;
assert_eq!(
crate::vector::encode_value(V::Bytes(Bytes::from_static(b"\xff\x00"))).kind,
Some(value::Kind::RawBytes(Bytes::from_static(b"\xff\x00")))
);
assert_eq!(
crate::vector::encode_value(V::Integer(-7)).kind,
int(-7).kind
);
assert_eq!(
crate::vector::encode_value(V::from_f64_or_zero(-0.5)).kind,
Some(value::Kind::Float(-0.5))
);
assert_eq!(
crate::vector::encode_value(V::Boolean(true)).kind,
Some(value::Kind::Boolean(true))
);
assert_eq!(
crate::vector::encode_value(V::Null).kind,
Some(value::Kind::Null(ValueNull::NullValue as i32))
);
let rx = vrl::value::ValueRegex::new(std::sync::Arc::new(crate::regex!("^a.*z$")));
assert_eq!(
crate::vector::encode_value(V::Regex(rx)).kind,
Some(value::Kind::RawBytes(Bytes::from_static(b"^a.*z$")))
);
assert_eq!(
crate::vector::encode_value(V::Array(vec![V::Integer(1)])).kind,
Some(value::Kind::Array(ValueArray {
items: vec![int(1)]
}))
);
assert_eq!(
crate::vector::encode_value(V::Object(
[("k".into(), V::Integer(1))].into_iter().collect()
))
.kind,
Some(value::Kind::Map(ValueMap {
fields: BTreeMap::from([("k".to_owned(), int(1))])
}))
);
}
}