Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
70 changes: 70 additions & 0 deletions crates/ourios-bench/src/c1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ impl C1Accumulator {
if matches!(emitted.body_kind, BodyKind::Structured) {
return;
}
// Absent-body records are excluded for the same reason with a
// simpler proof: no body arrived on the wire, so no template
// was allocated and no Drain pipeline ran (`BodyKind::Absent`'s
// contract) — there are no bytes to reconstruct byte-for-byte.
// Real corpora contain these at scale now: the OTel-Demo
// (post-2.2.0) emits body-less event records
// (`eventName` + attributes, `body` unset — ourios#549 has the
// capture evidence), and counting them in the denominator made
// C1 fail on rows that carry no reconstruction obligation.
if matches!(emitted.body_kind, BodyKind::Absent) {
return;
}
// Non-lossy, string body. §3.4.2 says we count this
// record in the denominator regardless of whether the
// snapshot lookup succeeds; an absent snapshot for a
Expand Down Expand Up @@ -392,4 +404,62 @@ mod tests {
// doesn't appear in `lossy_count`.
assert!(c1.lossy_flag_ratio.abs() < f64::EPSILON);
}

/// The absent-body twin of the structured exclusion: a record with
/// no wire body (the OTel-Demo post-2.2.0 event shape, ourios#549)
/// carries no reconstruction obligation and must not land in C1's
/// denominator — before this exclusion, three such rows failed the
/// gate on the first real capture containing them.
#[test]
fn absent_body_record_is_excluded_from_denominator() {
use ourios_core::otlp::OtlpLogRecord;
use ourios_core::record::{BodyKind, MinedRecord};
use ourios_core::tenant::TenantId;

let emitted = MinedRecord {
tenant_id: TenantId::new("bench-tenant"),
// The miner's real absent-body shape: no template is
// allocated (`cluster.rs` emits `NO_TEMPLATE` = 0), so the
// fixture matches an emittable record, not an invented one.
template_id: ourios_miner::cluster::NO_TEMPLATE,
template_version: 0,
severity_number: 9,
severity_text: None,
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved
scope_name: None,
scope_version: None,
scope_attributes: Vec::new(),
resource_schema_url: None,
scope_schema_url: None,
time_unix_nano: 0,
observed_time_unix_nano: None,
attributes: Vec::new(),
dropped_attributes_count: 0,
resource_attributes: Vec::new(),
trace_id: None,
span_id: None,
flags: 0,
event_name: Some("shipping.feature_flag.evaluated".to_string()),
body_kind: BodyKind::Absent,
params: Vec::new(),
separators: Vec::new(),
body: None,
confidence: 1.0,
lossy_flag: false,
};
let input = OtlpLogRecord {
tenant_id: TenantId::new("bench-tenant"),
..Default::default()
};

let mut acc = C1Accumulator::new();
acc.record(&input, &emitted, None);
let c1 = acc.finalize();

assert_eq!(
c1.non_lossy_total, 0,
"absent-body records are excluded from the denominator",
);
assert!(c1.pass, "an absent-body record alone must not fail C1");
assert!(c1.lossy_flag_ratio.abs() < f64::EPSILON);
}
}
12 changes: 9 additions & 3 deletions crates/ourios-bench/src/corpus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,15 @@ fn jsonl_line_records(
line_no: usize,
tenant: &TenantId,
) -> Result<Vec<OtlpLogRecord>, BenchError> {
let logs_data: LogsData = serde_json::from_str(raw).map_err(|e| BenchError::Corpus {
detail: format!("parse OTLP/JSON at {}:{}: {e}", path.display(), line_no),
})?;
// Lenient per ourios#549: real captures (OTel-Demo main's
// empty-body events) carry unset-AnyValue encodings upstream
// with-serde rejects; valid lines stay on the direct path.
let logs_data: LogsData =
ourios_core::otlp::lenient_json::from_slice(raw.as_bytes()).map_err(|e| {
BenchError::Corpus {
detail: format!("parse OTLP/JSON at {}:{}: {e}", path.display(), line_no),
}
})?;
let mut records = Vec::new();
for rl in logs_data.resource_logs {
// Resource attributes are copied onto every record
Expand Down
8 changes: 6 additions & 2 deletions crates/ourios-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,18 @@ jsonwebtoken = { version = "10", default-features = false, features = ["rust_cry
# rustls stack — no new cargo-deny surface.
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"], optional = true }
# Derive for the discovery-document subset the verifier reads.
serde = { version = "1", default-features = false, features = ["derive"], optional = true }
# Non-optional since the lenient OTLP/JSON module needs the
# `DeserializeOwned` bound; already unconditionally in the tree via
# `serde_json`, so this adds no crate weight. The `derive` feature
# stays behind `oidc` (the only user of derived impls).
serde = { version = "1", default-features = false }
# Async runtime types for the JWKS cache (`RwLock`) and the verifier's
# refresh path. Runtime-free feature set; the binary provides the runtime.
tokio = { version = "1", default-features = false, features = ["sync"], optional = true }

[features]
# The RFC 0029 OIDC bearer layer (verifier + JWKS cache).
oidc = ["dep:jsonwebtoken", "dep:reqwest", "dep:serde", "dep:tokio"]
oidc = ["dep:jsonwebtoken", "dep:reqwest", "serde/derive", "dep:tokio"]

[dev-dependencies]
# `init_in_memory` collects the alias counters' exported metric stream
Expand Down
184 changes: 184 additions & 0 deletions crates/ourios-core/src/otlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,117 @@ impl Default for TenantId {
}
}

/// Lenient OTLP/JSON parsing: a workaround for `opentelemetry-proto`'s
/// `with-serde` deserialiser rejecting proto3-JSON's two valid
/// encodings of an **unset** `AnyValue` — `{}` (a message with no
/// fields set; the `OTel` Collector's file exporter emits it for
/// empty-body event records) and `null` (what `with-serde`'s own
/// serialiser emits for `value: None`) — with "Invalid data for
/// Value, no known keys found". Tracked upstream; until the fix
/// ships, both OTLP/JSON entry points (the receiver's
/// `application/json` transport and the bench corpus loader) parse
/// through here.
///
/// Semantics: the direct parse runs first, so the hot path is
/// untouched for the overwhelmingly common valid input. On failure,
/// unset-`AnyValue` encodings at the two `Option`-typed positions —
/// `LogRecord.body` and `KeyValue.value` (including inside
/// `kvlistValue`) — are rewritten to an **absent field** and the
/// parse retried once.
///
/// **Bounded fidelity concession, by design**: the faithful decode of
/// a present-but-unset `AnyValue` is `Some(AnyValue { value: None })`
/// (what prost produces from the protobuf transport, per the RFC 0018
/// preserve-don't-correct rule) — but the broken upstream deserialiser
/// leaves NO encoding that reaches that state, so this shim accepts
/// the closest expressible reading: absent (`None`). What is conceded
/// is exactly the presence bit of an EMPTY value, nothing else; for
/// bodies even that is invisible ([`Body::from_any_value`] collapses
/// both states identically). The interim contract — including the
/// signal that flips when the upstream fix restores full fidelity —
/// is pinned by the cross-transport equivalence test in
/// `ourios-ingester` (`rfc0003_6_unset_any_value_decodes_equivalently_across_transports`).
/// An unset `AnyValue` **inside `arrayValue`** has no absent encoding
/// (position matters) and stays a parse error — matching the
/// pre-existing behaviour, resolved only by the upstream fix.
pub mod lenient_json {
use serde::de::DeserializeOwned;

/// [`serde_json::from_slice`] with the unset-`AnyValue` retry.
///
/// # Errors
///
/// The retry's parse error when normalisation changed the
/// document, the original parse error otherwise.
pub fn from_slice<T: DeserializeOwned>(bytes: &[u8]) -> Result<T, serde_json::Error> {
from_slice_flagged(bytes).map(|(v, _)| v)
}

/// [`from_slice`], also reporting WHICH path parsed: `true` means
/// the direct parse failed and the lenient retry succeeded — the
/// observability hook (`CLAUDE.md` §6.3) that lets the receiver
/// count lenient decodes (`ourios.ingest.json.lenient` on the
/// batches counter) so operators see upstream-rejected-but-valid
/// payloads arriving, and so the shim's dormancy is visible once
/// the upstream fix ships.
///
/// # Errors
///
/// Exactly [`from_slice`]'s.
pub fn from_slice_flagged<T: DeserializeOwned>(
bytes: &[u8],
) -> Result<(T, bool), serde_json::Error> {
match serde_json::from_slice(bytes) {
Ok(v) => Ok((v, false)),
Err(first) => {
let Ok(mut tree) = serde_json::from_slice::<serde_json::Value>(bytes) else {
return Err(first);
};
if !strip_unset_any_values(&mut tree) {
return Err(first);
}
serde_json::from_value(tree).map(|v| (v, true))
}
}
}

/// Remove `"body"` / `"value"` keys whose value encodes an unset
/// `AnyValue` (`{}` or `null`), recursively. Returns whether the
/// tree changed. The two key names are unambiguous in the OTLP/JSON
/// log tree: user data only ever appears as the *values of* `key` /
/// `value` / `body` fields, never as sibling object keys, so no
/// user-controlled content can be misread as one of these
/// positions.
fn strip_unset_any_values(tree: &mut serde_json::Value) -> bool {
let mut changed = false;
match tree {
serde_json::Value::Object(map) => {
for slot in ["body", "value"] {
let is_unset = match map.get(slot) {
Some(serde_json::Value::Null) => true,
Some(serde_json::Value::Object(inner)) => inner.is_empty(),
_ => false,
};
if is_unset {
map.remove(slot);
changed = true;
}
}
for v in map.values_mut() {
changed |= strip_unset_any_values(v);
}
}
serde_json::Value::Array(items) => {
for v in items {
changed |= strip_unset_any_values(v);
}
}
_ => {}
}
changed
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1086,4 +1197,77 @@ mod tests {
assert_eq!(r.event_name, None);
assert_eq!(r.body, None);
}

/// The exact record shape the OTel-Demo (`main`, post-2.2.0) file
/// exporter emits for an empty-body event — the input that
/// motivated [`lenient_json`] (ourios#549). Kept byte-faithful to
/// the captured corpus line.
const DEMO_EMPTY_BODY_EVENT: &str = r#"{"resourceLogs":[{"resource":{"attributes":[{"key":"service.name","value":{"stringValue":"shipping"}}]},"scopeLogs":[{"scope":{"name":"shipping::shipping_service"},"logRecords":[{"observedTimeUnixNano":"1784401032379902551","severityNumber":9,"severityText":"INFO","body":{},"attributes":[{"key":"feature_flag_key","value":{"stringValue":"intlShippingSlowdown"}}],"eventName":"shipping.feature_flag.evaluated"}]}]}]}"#;

#[test]
fn lenient_json_accepts_the_demo_empty_body_event() {
use opentelemetry_proto::tonic::logs::v1::LogsData;
// Future-proof against the upstream fix landing: today the
// direct parse rejects this and only the lenient retry accepts
// (the ingester's RFC0003.6 equivalence test pins that state as
// the designated flip signal); once upstream is fixed the direct
// parse succeeds with Some(AnyValue { value: None }). Either
// way the body must be absent-or-unset — the one downstream
// state Body::from_any_value maps both to.
let parsed: LogsData =
lenient_json::from_slice(DEMO_EMPTY_BODY_EVENT.as_bytes()).expect("lenient parse");
let record = &parsed.resource_logs[0].scope_logs[0].log_records[0];
assert!(
record
.body
.as_ref()
.and_then(|b| b.value.as_ref())
.is_none()
);
assert_eq!(record.event_name, "shipping.feature_flag.evaluated");
assert_eq!(record.attributes.len(), 1, "attributes survive untouched");
}

#[test]
fn lenient_json_accepts_both_unset_encodings_in_both_positions() {
use opentelemetry_proto::tonic::logs::v1::LogsData;
// Both spellings of the unset state (`null` and `{}` —
// proto3-JSON's empty-message encoding) at both Option-typed
// positions (`body`, `KeyValue.value`); the sibling test above
// covers `"body": {}` on the real demo record.
let doc = r#"{"resourceLogs":[{"resource":{},"scopeLogs":[{"logRecords":[{"body":null,"attributes":[{"key":"a","value":{}},{"key":"b","value":null}]}]}]}]}"#;
let parsed: LogsData = lenient_json::from_slice(doc.as_bytes()).expect("lenient parse");
let record = &parsed.resource_logs[0].scope_logs[0].log_records[0];
// Absent-or-unset (not strictly absent): once upstream accepts
// these encodings directly, `{}` parses to present-but-unset.
let unset = |v: &Option<opentelemetry_proto::tonic::common::v1::AnyValue>| {
v.as_ref().and_then(|av| av.value.as_ref()).is_none()
};
assert!(unset(&record.body));
assert!(unset(&record.attributes[0].value));
assert!(unset(&record.attributes[1].value));
}

#[test]
fn lenient_json_does_not_loosen_unknown_key_rejection() {
use opentelemetry_proto::tonic::logs::v1::LogsData;
// An AnyValue object with keys — just no KNOWN key — stays an
// error: the retry only rewrites genuinely EMPTY encodings, it
// must not paper over malformed values.
let doc =
r#"{"resourceLogs":[{"scopeLogs":[{"logRecords":[{"body":{"bogusValue":1}}]}]}]}"#;
assert!(lenient_json::from_slice::<LogsData>(doc.as_bytes()).is_err());
}

#[test]
fn lenient_json_leaves_valid_input_on_the_direct_path() {
use opentelemetry_proto::tonic::logs::v1::LogsData;
let doc = r#"{"resourceLogs":[{"scopeLogs":[{"logRecords":[{"body":{"stringValue":"hello"}}]}]}]}"#;
let parsed: LogsData = lenient_json::from_slice(doc.as_bytes()).expect("parse");
let record = &parsed.resource_logs[0].scope_logs[0].log_records[0];
assert!(matches!(
record.body.as_ref().and_then(|b| b.value.as_ref()),
Some(AvValue::StringValue(s)) if s == "hello"
));
}
}
16 changes: 15 additions & 1 deletion crates/ourios-ingester/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,27 @@ impl IngestMetrics {
/// out-of-range ones with `error.type = severity_out_of_range`
/// (RFC 0018 §3.5) — one counter, reason on a low-cardinality attribute,
/// not a bespoke metric.
/// `lenient_json` marks a batch whose OTLP/JSON payload only parsed
/// via the lenient unset-`AnyValue` retry (ourios#549): recorded as
/// the registry's `ourios.ingest.json.lenient` attribute on the
/// batches counter — the same one-instrument-plus-attribute shape as
/// `error.type`, and the operator/dormancy signal for the interim
/// upstream workaround.
pub fn record_batch(
&self,
record_count: usize,
severity_out_of_range: usize,
lenient_json: bool,
elapsed: Duration,
) {
self.batches.add(1, &[]);
if lenient_json {
self.batches.add(
1,
&[KeyValue::new(semconv::OURIOS_INGEST_JSON_LENIENT, true)],
);
} else {
self.batches.add(1, &[]);
}
let in_range = record_count.saturating_sub(severity_out_of_range);
if in_range > 0 {
self.records.add(to_u64(in_range), &[]);
Expand Down
17 changes: 15 additions & 2 deletions crates/ourios-ingester/src/receiver/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,23 @@ pub fn decode_protobuf(bytes: &[u8]) -> Result<ExportLogsServiceRequest, DecodeE
/// accepted as number or string, base64 bytes, lowerCamelCase keys —
/// are handled by the proto types' `with-serde` deserialiser.
///
/// Parses through [`ourios_core::otlp::lenient_json`]: the upstream
/// `with-serde` deserialiser rejects proto3-JSON's valid encodings of
/// an UNSET `AnyValue` (`{}` / `null` — real exporters emit them for
/// empty-body events), which would 400 a spec-compliant client
/// (ourios#549). The lenient path is a failed-parse retry only; valid
/// input never leaves `serde_json`'s direct path.
///
/// The returned `bool` is `true` when only the lenient retry parsed
/// the payload — threaded to `ourios.ingest.batches`' \
/// `ourios.ingest.json.lenient` attribute (`CLAUDE.md` §6.3: the
/// operator signal for upstream-rejected-but-valid payloads, and the
/// shim-dormancy signal once the upstream fix ships).
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved
///
/// # Errors
///
/// Returns [`DecodeError::Json`] if `bytes` is not well-formed OTLP/JSON
/// for an `ExportLogsServiceRequest`.
pub fn decode_json(bytes: &[u8]) -> Result<ExportLogsServiceRequest, DecodeError> {
serde_json::from_slice(bytes).map_err(DecodeError::Json)
pub fn decode_json(bytes: &[u8]) -> Result<(ExportLogsServiceRequest, bool), DecodeError> {
ourios_core::otlp::lenient_json::from_slice_flagged(bytes).map_err(DecodeError::Json)
}
Comment thread
jensholdgaard marked this conversation as resolved.
Resolved
6 changes: 4 additions & 2 deletions crates/ourios-ingester/src/receiver/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ impl LogsService for LogsReceiver {
// contract. `ingest` is async now (it awaits the batched fsync),
// so this is `spawn`, not `spawn_blocking`.
let pipeline = self.pipeline.clone();
match tokio::spawn(async move { pipeline.ingest_bound(export, binding.as_ref()).await })
.await
match tokio::spawn(
async move { pipeline.ingest_bound(export, binding.as_ref(), false).await },
)
.await
{
Ok(Ok(_)) => Ok(Response::new(ExportLogsServiceResponse::default())),
Ok(Err(e)) => Err(ingest_error_status(&e)),
Expand Down
Loading