use core::fmt;
use std::collections::BTreeMap;
use astrs_time::HlcTimestamp;
use oxicode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use crate::common::log::LogRecord;
use crate::common::metrics::MetricBatch;
use crate::ids::{DataflowId, NodeId, PortRef, SubscriptionId};
use crate::metadata::Metadata;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode)]
pub struct DataFrame {
pub subscription: SubscriptionId,
pub dataflow: DataflowId,
pub source: PortRef,
pub metadata: Metadata,
pub payload: Vec<u8>,
}
impl DataFrame {
#[must_use]
pub const fn new(
subscription: SubscriptionId,
dataflow: DataflowId,
source: PortRef,
metadata: Metadata,
payload: Vec<u8>,
) -> Self {
Self {
subscription,
dataflow,
source,
metadata,
payload,
}
}
#[must_use]
pub fn payload_len(&self) -> usize {
self.payload.len()
}
#[must_use]
pub fn into_payload(self) -> Vec<u8> {
self.payload
}
#[must_use]
pub fn bitwise_eq(&self, other: &Self) -> bool {
self.subscription == other.subscription
&& self.dataflow == other.dataflow
&& self.source == other.source
&& self.metadata.bitwise_eq(&other.metadata)
&& self.payload == other.payload
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
pub struct LogFrame {
pub subscription: SubscriptionId,
pub record: LogRecord,
}
impl LogFrame {
#[must_use]
pub const fn new(subscription: SubscriptionId, record: LogRecord) -> Self {
Self {
subscription,
record,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode)]
pub struct TelemetryFrame {
pub subscription: SubscriptionId,
pub batch: MetricBatch,
}
impl TelemetryFrame {
#[must_use]
pub const fn new(subscription: SubscriptionId, batch: MetricBatch) -> Self {
Self {
subscription,
batch,
}
}
}
#[derive(
Debug,
Clone,
Copy,
Default,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Serialize,
Deserialize,
Encode,
Decode,
)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum SpanStatus {
#[default]
#[oxicode(variant = 0)]
Unset,
#[oxicode(variant = 1)]
Ok,
#[oxicode(variant = 2)]
Error,
}
impl SpanStatus {
pub const ALL: &'static [Self] = &[Self::Unset, Self::Ok, Self::Error];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Unset => "unset",
Self::Ok => "ok",
Self::Error => "error",
}
}
}
impl fmt::Display for SpanStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
pub struct TraceSpan {
pub trace_id: String,
pub span_id: String,
pub parent_span_id: Option<String>,
pub name: String,
pub start: HlcTimestamp,
pub end: Option<HlcTimestamp>,
pub dataflow: Option<DataflowId>,
pub node: Option<NodeId>,
pub status: SpanStatus,
pub attributes: BTreeMap<String, String>,
}
impl TraceSpan {
#[must_use]
pub fn new(
trace_id: impl Into<String>,
span_id: impl Into<String>,
name: impl Into<String>,
start: HlcTimestamp,
) -> Self {
Self {
trace_id: trace_id.into(),
span_id: span_id.into(),
parent_span_id: None,
name: name.into(),
start,
end: None,
dataflow: None,
node: None,
status: SpanStatus::Unset,
attributes: BTreeMap::new(),
}
}
#[must_use]
pub fn with_parent(mut self, parent_span_id: impl Into<String>) -> Self {
self.parent_span_id = Some(parent_span_id.into());
self
}
#[must_use]
pub const fn with_end(mut self, end: HlcTimestamp) -> Self {
self.end = Some(end);
self
}
#[must_use]
pub const fn with_status(mut self, status: SpanStatus) -> Self {
self.status = status;
self
}
#[must_use]
pub fn with_attribute(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.attributes.insert(key.into(), value.into());
self
}
#[must_use]
pub fn is_root(&self) -> bool {
self.parent_span_id.is_none()
}
#[must_use]
pub fn duration_us(&self) -> Option<u64> {
let end = self.end?;
let elapsed = end.physical_duration_since(&self.start)?;
u64::try_from(elapsed.as_micros()).ok()
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
pub struct TraceData {
pub spans: Vec<TraceSpan>,
pub truncated: bool,
}
impl TraceData {
#[must_use]
pub const fn empty() -> Self {
Self {
spans: Vec::new(),
truncated: false,
}
}
#[must_use]
pub fn trace_ids(&self) -> Vec<&str> {
let mut ids: Vec<&str> = self
.spans
.iter()
.map(|span| span.trace_id.as_str())
.collect();
ids.sort_unstable();
ids.dedup();
ids
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::codec::{WireDecode, WireEncode, round_trip};
use crate::common::log::LogLevel;
use crate::common::metrics::MetricPoint;
use crate::metadata::Parameter;
fn port() -> PortRef {
"camera/image".parse().unwrap()
}
#[test]
fn data_frames_round_trip_with_their_payload_intact() {
let mut metadata = Metadata::new(HlcTimestamp::new(9, 1));
metadata.set_seq(3);
let frame = DataFrame::new(
SubscriptionId::new(7),
DataflowId::from_u128(2),
port(),
metadata,
(0u8..=255).collect(),
);
let bytes = frame.encode_to_vec().unwrap();
let decoded = DataFrame::decode_exact(&bytes).unwrap();
assert_eq!(decoded, frame);
assert_eq!(decoded.payload_len(), 256);
assert_eq!(decoded.payload, (0u8..=255).collect::<Vec<_>>());
}
#[test]
fn an_empty_payload_round_trips() {
let frame = DataFrame::new(
SubscriptionId::NONE,
DataflowId::NIL,
port(),
Metadata::default(),
Vec::new(),
);
assert_eq!(round_trip(&frame).unwrap(), frame);
assert_eq!(frame.payload_len(), 0);
assert!(frame.clone().into_payload().is_empty());
}
#[test]
fn data_frames_compare_bitwise_through_nan_metadata() {
let mut metadata = Metadata::new(HlcTimestamp::new(1, 0));
metadata.insert("f", Parameter::Float(f64::NAN)).unwrap();
let frame = DataFrame::new(
SubscriptionId::FIRST,
DataflowId::NIL,
port(),
metadata,
vec![1],
);
let decoded = DataFrame::decode_exact(&frame.encode_to_vec().unwrap()).unwrap();
assert_ne!(decoded, frame);
assert!(decoded.bitwise_eq(&frame));
}
#[test]
fn log_frames_round_trip() {
let frame = LogFrame::new(
SubscriptionId::new(4),
LogRecord::new(HlcTimestamp::new(1, 0), LogLevel::Warn, "careful"),
);
assert_eq!(round_trip(&frame).unwrap(), frame);
assert_eq!(frame.record.message, "careful");
}
#[test]
fn telemetry_frames_round_trip() {
let frame = TelemetryFrame::new(
SubscriptionId::new(5),
MetricBatch::new(HlcTimestamp::new(2, 0), "astrs_daemon")
.with_point(MetricPoint::counter("frames_total", 12)),
);
assert_eq!(round_trip(&frame).unwrap(), frame);
assert_eq!(frame.batch.points.len(), 1);
}
#[test]
fn span_statuses_round_trip_and_name_themselves() {
let mut names = std::collections::BTreeSet::new();
for status in SpanStatus::ALL.iter().copied() {
assert_eq!(round_trip(&status).unwrap(), status);
assert!(names.insert(status.as_str()));
assert_eq!(status.to_string(), status.as_str());
}
assert_eq!(names.len(), 3);
assert_eq!(SpanStatus::default(), SpanStatus::Unset);
}
#[test]
fn spans_measure_their_own_duration() {
let span = TraceSpan::new("t", "s", "deliver", HlcTimestamp::new(1_000, 0))
.with_end(HlcTimestamp::new(1_000 + 2_500_000, 0));
assert_eq!(span.duration_us(), Some(2_500));
assert!(span.is_root());
}
#[test]
fn an_open_span_has_no_duration() {
let span = TraceSpan::new("t", "s", "process", HlcTimestamp::new(1, 0));
assert_eq!(span.duration_us(), None);
}
#[test]
fn a_backwards_span_reports_none_rather_than_zero() {
let span = TraceSpan::new("t", "s", "process", HlcTimestamp::new(1_000, 0))
.with_end(HlcTimestamp::new(500, 0));
assert_eq!(span.duration_us(), None);
}
#[test]
fn spans_round_trip_with_every_field_set() {
let span = TraceSpan::new("trace-1", "span-2", "publish", HlcTimestamp::new(5, 0))
.with_parent("span-1")
.with_end(HlcTimestamp::new(6, 0))
.with_status(SpanStatus::Error)
.with_attribute("output", "image");
assert!(!span.is_root());
assert_eq!(round_trip(&span).unwrap(), span);
}
#[test]
fn trace_data_collects_distinct_trace_ids() {
let data = TraceData {
spans: vec![
TraceSpan::new("b", "1", "n", HlcTimestamp::EPOCH),
TraceSpan::new("a", "2", "n", HlcTimestamp::EPOCH),
TraceSpan::new("a", "3", "n", HlcTimestamp::EPOCH),
],
truncated: true,
};
assert_eq!(data.trace_ids(), vec!["a", "b"]);
assert_eq!(round_trip(&data).unwrap(), data);
assert!(TraceData::empty().trace_ids().is_empty());
assert_eq!(TraceData::default(), TraceData::empty());
}
}