From 12d152707ab13fcabe27f4ba74ac680b41ee0dca Mon Sep 17 00:00:00 2001 From: Jens Holdgaard Pedersen Date: Sat, 11 Jul 2026 17:44:03 +0200 Subject: [PATCH] =?UTF-8?q?feat(bench):=20rfc=200031=20=E2=80=94=20bytes-r?= =?UTF-8?q?ead=20measurement=20channel=20(=C2=A73.6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both halves of the RFC 0031 §3.6 primary gate metric, feeding the RFC0031.2–.5 bytes-read ratios. Ourios half (mostly existed): QueryStats.bytes_read is already folded from the engine's bytes_scanned scan metric on the RFC 0016 path and live-asserted in the querier's execution tests. Expose it through the comparative harness: `OuriosAnswer { lines, bytes_read }` via `ourios_query_answer` — one query yields both the equivalence lines and the measurement; `ourios_query_lines` stays as a thin wrapper (the RFC0031.1 container test is untouched). Loki half: `parse_loki_bytes_processed` reads `data.stats.summary.totalBytesProcessed` from the query_range response, sharing the extracted `parse_loki_root` error-response guard with `parse_loki_streams`. A missing stats block is an ERROR, not a silent 0 — a zero would fake a perfect pruning ratio in the L-gates. Tests: the fixture round-trip now runs through ourios_query_answer and asserts bytes_read > 0 on the real store; the bytes parser covers the value, missing-summary, and Loki-error-response cases. Co-Authored-By: Claude Opus 4.8 (1M context) --- crates/ourios-bench/src/comparative.rs | 171 ++++++++++++++++++++----- crates/ourios-bench/src/lib.rs | 5 +- 2 files changed, 145 insertions(+), 31 deletions(-) diff --git a/crates/ourios-bench/src/comparative.rs b/crates/ourios-bench/src/comparative.rs index 8fd91501b..c37909355 100644 --- a/crates/ourios-bench/src/comparative.rs +++ b/crates/ourios-bench/src/comparative.rs @@ -203,7 +203,47 @@ fn tally(lines: &[LineKey]) -> HashMap<&LineKey, u64> { /// Run a logs-DSL query against the Ourios store at `bucket_root` and /// return the matching rows as [`LineKey`]s — the Ourios half of the -/// RFC0031.1 equivalence check. +/// RFC0031.1 equivalence check. Thin wrapper over [`ourios_query_answer`] +/// for callers that need only the lines. +/// +/// # Errors +/// +/// Exactly [`ourios_query_answer`]'s. +pub fn ourios_query_lines( + bucket_root: &Path, + tenant: &TenantId, + dsl: &str, + now_unix_nano: u64, + default_window_nanos: u64, +) -> Result, BenchError> { + ourios_query_answer( + bucket_root, + tenant, + dsl, + now_unix_nano, + default_window_nanos, + ) + .map(|answer| answer.lines) +} + +/// The Ourios side of a comparative query: the matching rows as +/// [`LineKey`]s plus the **bytes read from storage** to answer it — the +/// RFC 0031 §3.6 primary gate metric (`QueryStats::bytes_read`, folded +/// from the engine's `bytes_scanned` scan metric on the RFC 0016 path). +#[derive(Debug, Clone)] +pub struct OuriosAnswer { + /// The matching rows, keyed for [`compare_lines`]. + pub lines: Vec, + /// Bytes read from object storage to answer the query — the + /// measurement the L-gates ratio against Loki's + /// `totalBytesProcessed` ([`parse_loki_bytes_processed`]). + pub bytes_read: u64, +} + +/// Run a logs-DSL query against the Ourios store at `bucket_root` and +/// return the matching rows **and** the bytes-read measurement — the +/// Ourios half of both the RFC0031.1 equivalence check and the +/// RFC0031.2–.5 bytes-read gates. /// /// Runs the querier **in-process** (RFC 0031 §7: no served binary). The /// query MUST carry a `limit` large enough to render **every** matching @@ -219,13 +259,13 @@ fn tally(lines: &[LineKey]) -> HashMap<&LineKey, u64> { /// matching row (missing or too-small `limit`), or a returned row carries /// a body kind the equivalence extraction does not yet lower (a structured /// or absent body — the string-body case always lowers). -pub fn ourios_query_lines( +pub fn ourios_query_answer( bucket_root: &Path, tenant: &TenantId, dsl: &str, now_unix_nano: u64, default_window_nanos: u64, -) -> Result, BenchError> { +) -> Result { let query = ourios_querier::dsl::parse(dsl).map_err(|e| BenchError::Pipeline { detail: format!("comparative DSL parse `{dsl}`: {e}"), })?; @@ -256,6 +296,7 @@ pub fn ourios_query_lines( ), }); } + let bytes_read = result.stats.bytes_read; result .records .iter() @@ -265,7 +306,8 @@ pub fn ourios_query_lines( body: body_bytes(&row.body)?, }) }) - .collect() + .collect::, _>>() + .map(|lines| OuriosAnswer { lines, bytes_read }) } /// Canonical byte identity of a query row's body for equivalence keying. @@ -315,28 +357,7 @@ fn body_bytes(body: &ourios_querier::LogBody) -> Result, BenchError> { /// timestamp string that isn't a `u64`. Malformed-entry errors carry the /// stream + value indices for debugging against real Loki responses. pub fn parse_loki_streams(response_json: &str) -> Result, BenchError> { - let root: serde_json::Value = - serde_json::from_str(response_json).map_err(|e| BenchError::Pipeline { - detail: format!("Loki response is not JSON: {e}"), - })?; - - // A Loki error response ({"status":"error", "errorType":..., "error":...}) - // would otherwise fail below as "missing data.result" — surface Loki's - // own diagnostic instead, which is what an operator needs to see. - if root.get("status").and_then(serde_json::Value::as_str) == Some("error") { - let error_type = root - .get("errorType") - .and_then(serde_json::Value::as_str) - .unwrap_or("unknown"); - let error = root - .get("error") - .and_then(serde_json::Value::as_str) - .unwrap_or("(no message)"); - return Err(BenchError::Pipeline { - detail: format!("Loki query error [{error_type}]: {error}"), - }); - } - + let root = parse_loki_root(response_json)?; let result = root .get("data") .and_then(|d| d.get("result")) @@ -382,6 +403,57 @@ pub fn parse_loki_streams(response_json: &str) -> Result, BenchErro Ok(lines) } +/// Parse a Loki `query_range` response's **bytes-processed** measurement +/// (`data.stats.summary.totalBytesProcessed`) — the Loki half of the +/// RFC 0031 §3.6 bytes-read gate metric, ratioed against +/// [`OuriosAnswer::bytes_read`]. Loki attaches the stats block to every +/// successful query response; its absence is an error rather than a +/// silent `0`, because a zero would fake a perfect pruning ratio. +/// +/// # Errors +/// +/// [`BenchError::Pipeline`] if the response isn't JSON, is a Loki error +/// response (surfaces Loki's `errorType` / `error`), or carries no +/// numeric `data.stats.summary.totalBytesProcessed`. +pub fn parse_loki_bytes_processed(response_json: &str) -> Result { + let root = parse_loki_root(response_json)?; + root.get("data") + .and_then(|d| d.get("stats")) + .and_then(|s| s.get("summary")) + .and_then(|s| s.get("totalBytesProcessed")) + .and_then(serde_json::Value::as_u64) + .ok_or_else(|| BenchError::Pipeline { + detail: "Loki response missing numeric `data.stats.summary.totalBytesProcessed` \ + — refusing to record 0 bytes for a query that ran" + .to_string(), + }) +} + +/// Parse a Loki response to its JSON root, surfacing a Loki **error** +/// response (`status == "error"`) as Loki's own diagnostic — shared by +/// [`parse_loki_streams`] and [`parse_loki_bytes_processed`] so an error +/// body never reads as a confusing structural parse failure. +fn parse_loki_root(response_json: &str) -> Result { + let root: serde_json::Value = + serde_json::from_str(response_json).map_err(|e| BenchError::Pipeline { + detail: format!("Loki response is not JSON: {e}"), + })?; + if root.get("status").and_then(serde_json::Value::as_str) == Some("error") { + let error_type = root + .get("errorType") + .and_then(serde_json::Value::as_str) + .unwrap_or("unknown"); + let error = root + .get("error") + .and_then(serde_json::Value::as_str) + .unwrap_or("(no message)"); + return Err(BenchError::Pipeline { + detail: format!("Loki query error [{error_type}]: {error}"), + }); + } + Ok(root) +} + /// The `service.name` every comparative-fixture record carries — the /// resource identity both systems key on (Ourios: promoted service /// column; Loki: the `service_name` stream label its OTLP ingest derives). @@ -690,19 +762,60 @@ mod tests { let tenant = TenantId::new(built.tenant); let now = built.max_effective_time_unix_nano + 1; let window = built.max_effective_time_unix_nano - built.min_effective_time_unix_nano + 2; - let extracted = ourios_query_lines( + let answer = ourios_query_answer( bucket.path(), &tenant, "severity >= 0 | limit 1000", now, window, ) - .expect("extract lines"); + .expect("query answer"); assert!( - compare_lines(&extracted, &fixture_line_keys(&records), 8).is_equal(), + compare_lines(&answer.lines, &fixture_line_keys(&records), 8).is_equal(), "Ourios round-trip of the fixture must equal the expected LineKeys", ); + // The measurement channel: answering the query read real bytes from + // storage (the RFC 0031 §3.6 primary gate metric must never be a + // silent 0 for a query that scanned data). + assert!( + answer.bytes_read > 0, + "bytes_read must be non-zero for a query that scanned the store", + ); + } + + #[test] + fn parse_loki_bytes_processed_reads_the_summary() { + let response = r#"{ + "status": "success", + "data": { + "resultType": "streams", + "result": [], + "stats": { "summary": { "totalBytesProcessed": 4096 } } + } + }"#; + assert_eq!( + parse_loki_bytes_processed(response).expect("parse bytes"), + 4096, + ); + } + + #[test] + fn parse_loki_bytes_processed_refuses_a_missing_summary() { + // Absent stats must be an error, not a silent 0 — a zero would fake + // a perfect pruning ratio in the L-gates. + let no_stats = r#"{"status":"success","data":{"result":[]}}"#; + assert!(parse_loki_bytes_processed(no_stats).is_err()); + + // And a Loki error response surfaces Loki's own diagnostic. + let err = parse_loki_bytes_processed( + r#"{"status":"error","errorType":"too_many_requests","error":"throttled"}"#, + ) + .expect_err("error response must error"); + let BenchError::Pipeline { detail } = err else { + panic!("expected a pipeline error"); + }; + assert!(detail.contains("throttled"), "{detail}"); } #[test] diff --git a/crates/ourios-bench/src/lib.rs b/crates/ourios-bench/src/lib.rs index cb231d19b..097b25549 100644 --- a/crates/ourios-bench/src/lib.rs +++ b/crates/ourios-bench/src/lib.rs @@ -43,9 +43,10 @@ mod store; pub use calibrate::{CALIBRATION_DIR, extract_manifest, write_manifest}; pub use comparative::{ - AggKey, EquivalenceOutcome, FIXTURE_SERVICE, FixtureRecord, LineKey, Mismatch, + AggKey, EquivalenceOutcome, FIXTURE_SERVICE, FixtureRecord, LineKey, Mismatch, OuriosAnswer, comparative_fixture, compare_aggregations, compare_lines, fixture_jsonl, fixture_line_keys, - fixture_logs_data, ourios_query_lines, parse_loki_streams, + fixture_logs_data, ourios_query_answer, ourios_query_lines, parse_loki_bytes_processed, + parse_loki_streams, }; pub use corpus::TxtSeverity; pub use reference::ReferenceCorpus;