Skip to main content

oxabl_cache_format/
telemetry.rs

1//! `OXTL` sealed telemetry report and the report it carries.
2
3use crate::cursor::Cursor;
4use crate::error::{FormatError, Result};
5use crate::frame::{read_header, write_header};
6use crate::limits::TELEMETRY_BYTES;
7use crate::writer::Writer;
8
9const MAGIC: [u8; 4] = *b"OXTL";
10const NONCE_LEN: usize = 12;
11const TAG_LEN: usize = 16;
12
13/// Bytes before the ciphertext: magic, version, flags, epoch and nonce.
14pub const TELEMETRY_HEADER_LEN: usize = 6 + 4 + NONCE_LEN;
15/// Length of an encoded [`TelemetryReport`].
16pub const TELEMETRY_REPORT_LEN: usize = 16 + 8 + 1 + 1 + 4 * 4 + 8 + 4 + 4;
17
18/// The outer shape of a sealed report: what the server may check without a key.
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct SealedTelemetry<'a> {
21    pub epoch: u32,
22    pub nonce: [u8; NONCE_LEN],
23    /// Ciphertext followed by the 16-byte tag.
24    pub sealed: &'a [u8],
25}
26
27impl<'a> SealedTelemetry<'a> {
28    /// The header the sealed bytes are authenticated with, as written on the wire.
29    pub fn header(epoch: u32, nonce: &[u8; NONCE_LEN]) -> Vec<u8> {
30        let mut w = Writer::with_capacity(TELEMETRY_HEADER_LEN);
31        write_header(&mut w, MAGIC, 0);
32        w.u32(epoch);
33        w.bytes(nonce);
34        w.into_vec()
35    }
36
37    pub fn decode(bytes: &'a [u8]) -> Result<Self> {
38        if bytes.len() > TELEMETRY_BYTES {
39            return Err(FormatError::LimitExceeded("telemetry report"));
40        }
41        let mut c = Cursor::new(bytes);
42        read_header(&mut c, MAGIC, 0)?;
43        let epoch = c.u32()?;
44        let nonce = c.array()?;
45        let sealed = c.take(c.remaining())?;
46        if sealed.len() <= TAG_LEN {
47            return Err(FormatError::Truncated);
48        }
49        Ok(Self {
50            epoch,
51            nonce,
52            sealed,
53        })
54    }
55}
56
57/// What a report measures.
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
59pub enum TelemetryKind {
60    /// A fetch: keys asked for, entries installed.
61    Pull,
62    /// A store: sets put, sets committed or already present.
63    Push,
64}
65
66/// How the reporting client ran.
67#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
68pub enum TelemetryMode {
69    Ci,
70    Developer,
71    ReadOnly,
72}
73
74/// One fetch or store as its client measured it. Holds no identity, key or path.
75#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76pub struct TelemetryReport {
77    /// Random; lets a reader drop a report sent twice.
78    pub id: [u8; 16],
79    /// Unix seconds on the client's clock when the operation ended.
80    pub at: u64,
81    pub kind: TelemetryKind,
82    pub mode: TelemetryMode,
83    /// Keys asked for, or sets put.
84    pub requested: u32,
85    /// Entries installed, or sets committed or already present.
86    pub hits: u32,
87    /// Keys not in the cache, or sets the cache refused for quota.
88    pub misses: u32,
89    /// Everything else that did not succeed.
90    pub failed: u32,
91    /// Bytes received, or bytes uploaded.
92    pub bytes: u64,
93    /// Wall time of the whole operation.
94    pub duration_ms: u32,
95    /// Round trip of one small call to the server, measured by the same handle.
96    pub rtt_ms: u32,
97}
98
99impl TelemetryKind {
100    fn code(self) -> u8 {
101        match self {
102            Self::Pull => 1,
103            Self::Push => 2,
104        }
105    }
106
107    fn from_code(code: u8) -> Result<Self> {
108        match code {
109            1 => Ok(Self::Pull),
110            2 => Ok(Self::Push),
111            _ => Err(FormatError::Invalid("telemetry kind")),
112        }
113    }
114}
115
116impl TelemetryMode {
117    fn code(self) -> u8 {
118        match self {
119            Self::Ci => 1,
120            Self::Developer => 2,
121            Self::ReadOnly => 3,
122        }
123    }
124
125    fn from_code(code: u8) -> Result<Self> {
126        match code {
127            1 => Ok(Self::Ci),
128            2 => Ok(Self::Developer),
129            3 => Ok(Self::ReadOnly),
130            _ => Err(FormatError::Invalid("telemetry mode")),
131        }
132    }
133}
134
135impl TelemetryReport {
136    pub fn encode(&self) -> Vec<u8> {
137        let mut w = Writer::with_capacity(TELEMETRY_REPORT_LEN);
138        w.bytes(&self.id);
139        w.u64(self.at);
140        w.u8(self.kind.code());
141        w.u8(self.mode.code());
142        w.u32(self.requested);
143        w.u32(self.hits);
144        w.u32(self.misses);
145        w.u32(self.failed);
146        w.u64(self.bytes);
147        w.u32(self.duration_ms);
148        w.u32(self.rtt_ms);
149        w.into_vec()
150    }
151
152    pub fn decode(bytes: &[u8]) -> Result<Self> {
153        let mut c = Cursor::new(bytes);
154        let report = Self {
155            id: c.array()?,
156            at: c.u64()?,
157            kind: TelemetryKind::from_code(c.u8()?)?,
158            mode: TelemetryMode::from_code(c.u8()?)?,
159            requested: c.u32()?,
160            hits: c.u32()?,
161            misses: c.u32()?,
162            failed: c.u32()?,
163            bytes: c.u64()?,
164            duration_ms: c.u32()?,
165            rtt_ms: c.u32()?,
166        };
167        c.finish()?;
168        if u64::from(report.hits) + u64::from(report.misses) + u64::from(report.failed)
169            > u64::from(report.requested)
170        {
171            return Err(FormatError::Invalid("telemetry counts exceed the request"));
172        }
173        Ok(report)
174    }
175}