:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(cohorts): improve hogvm exec o11y (#67059)
Matheus Batistad962b992ea0a
diff --git a/rust/cohort-stream-processor/src/hogvm/executor.rs b/rust/cohort-stream-processor/src/hogvm/executor.rsindex 8d64ead3ce76..de29e86ca118 100644--- a/rust/cohort-stream-processor/src/hogvm/executor.rs+++ b/rust/cohort-stream-processor/src/hogvm/executor.rs@@ -4,15 +4,21 @@//! The hot path reuses one [`CohortEvaluator`] per event: the STL context and globals are set once//! and only the program is swapped per condition, amortizing the context build across all conditions.-use std::sync::Arc;+use std::sync::{Arc, LazyLock};+use dashmap::DashSet;use hogvm::{sync_execute, ExecutionContext, Program, VmError};use metrics::counter;use serde_json::Value;-use tracing::debug;+use tracing::info;use crate::observability::metrics::{STAGE1_HOGVM_ERROR, STAGE1_HOGVM_UNKNOWN_FUNCTION};+/// Unknown-native function names already logged once. Bounded by the HogQL native surface+/// (~hundreds max), so it never grows without bound; keeps the per-event unknown-function path+/// observable in prod logs without a line per event.+static SEEN_UNKNOWN_FNS: LazyLock<DashSet<String>> = LazyLock::new(DashSet::new);+/// HogVM stack ceiling for cohort evaluation. `with_defaults` caps the stack at 128, far below real/// cohort bytecode: a `person.properties.X IN (...)` leaf compiles to a tuple build that pushes every/// list element before `In` pops them, so peak depth ≈ list size. Production catalogs already hold@@ -122,18 +128,74 @@ fn outcome_to_bool(outcome: EvalOutcome) -> bool {EvalOutcome::Matched(matched) => matched,EvalOutcome::UnknownFunction(name) => {// `name` is bytecode-derived from user cohort filters; keep it out of the metric label- // (bounded counter) and surface the specific function only in a debug log.+ // (bounded counter) and surface the specific function in a log instead. Log each+ // distinct name once at info — the counter already tracks volume, so a per-event line+ // would be spam.counter!(STAGE1_HOGVM_UNKNOWN_FUNCTION).increment(1);- debug!(function = %name, "cohort bytecode called a function with no registered Rust native");+ // Steady state is a read-only `contains` (no allocation; the same handful of names recur+ // forever). Only on first sight do we clone-and-insert: `insert` returns `true` for+ // exactly one thread, so the log fires once even when several race past `contains` at once.+ if !SEEN_UNKNOWN_FNS.contains(name.as_str()) && SEEN_UNKNOWN_FNS.insert(name.clone()) {+ info!(function = %name, "cohort bytecode called a function with no registered Rust native");+ }false}- EvalOutcome::VmError(_) => {- counter!(STAGE1_HOGVM_ERROR).increment(1);+ EvalOutcome::VmError(error) => {+ counter!(STAGE1_HOGVM_ERROR, "reason" => vm_error_reason(&error)).increment(1);false}}}+/// Collapse a [`VmError`] into a bounded `reason` label for [`STAGE1_HOGVM_ERROR`]. The bucket set is+/// fixed and small so the Prometheus series count stays constant; the `_` arm keeps the match total+/// over the `#[non_exhaustive]` enum and folds in `VmError::Other`. `UnknownFunction`/`UnknownSymbol`+/// are normally routed to [`EvalOutcome::UnknownFunction`] (separate counter) — mapped here only for+/// totality in case [`classify_failure`] ever changes.+fn vm_error_reason(error: &VmError) -> &'static str {+ match error {+ VmError::InvalidValue(..)+ | VmError::CannotCoerce(..)+ | VmError::InvalidNumber(_)+ | VmError::IntegerOverflow => "type_coercion",++ VmError::StackOverflow | VmError::StackUnderflow | VmError::StackIndexOutOfBounds => {+ "stack"+ }++ VmError::NotImplemented(_) => "not_implemented",++ VmError::UnknownGlobal(_) | VmError::UnknownProperty(_) => "unknown_ref",++ VmError::NotAnOperation(_)+ | VmError::InvalidOperation(_)+ | VmError::EndOfProgram(_)+ | VmError::InvalidBytecode(_)+ | VmError::InvalidCall(_)+ | VmError::NotEnoughArguments(..)+ | VmError::CaptureOutOfBounds(_)+ | VmError::NoFrame => "program",++ VmError::UncaughtException(..) | VmError::InvalidException => "exception",++ VmError::DivisionByZero+ | VmError::HeapIndexOutOfBounds+ | VmError::UseAfterFree+ | VmError::ExpectedObject+ | VmError::UnexpectedPopTry+ | VmError::InvalidIndex+ | VmError::CycleDetected+ | VmError::IndexOutOfBounds(..)+ | VmError::OutOfResource(_)+ | VmError::NativeCallFailed(_)+ | VmError::InvalidRegex(..) => "runtime",++ VmError::UnknownFunction(_) | VmError::UnknownSymbol(_) => "unknown_function",++ _ => "other", // VmError::Other + any future #[non_exhaustive] variant+ }+}+/// `UnknownFunction` and `UnknownSymbol` collapse to the same metric. The catch-all arm is/// mandatory because `VmError` is `#[non_exhaustive]`.fn classify_failure(error: VmError) -> EvalOutcome {@@ -412,6 +474,40 @@ mod tests {bc}+ #[test]+ fn vm_error_reason_buckets_one_representative_per_class() {+ // One representative `VmError` per bucket. Catches a forgotten or mis-bucketed variant: a+ // new `#[non_exhaustive]` variant left unhandled silently falls into `other`, and moving an+ // existing variant (e.g. `DivisionByZero` out of `runtime`) breaks the label breakdown.+ let cases: [(VmError, &str); 11] = [+ (+ VmError::InvalidValue("a".into(), "b".into()),+ "type_coercion",+ ),+ (VmError::IntegerOverflow, "type_coercion"),+ (VmError::StackOverflow, "stack"),+ (VmError::NotImplemented("x".into()), "not_implemented"),+ (VmError::UnknownGlobal("g".into()), "unknown_ref"),+ (VmError::InvalidBytecode("b".into()), "program"),+ (+ VmError::UncaughtException("t".into(), "m".into()),+ "exception",+ ),+ (VmError::DivisionByZero, "runtime"),+ // The `max_steps` blowup (`vm.rs` synthesizes `OutOfResource`) must stay visible.+ (VmError::OutOfResource("steps".into()), "runtime"),+ (VmError::UnknownFunction("f".into()), "unknown_function"),+ (VmError::Other("o".into()), "other"),+ ];+ for (error, expected) in &cases {+ assert_eq!(+ vm_error_reason(error),+ *expected,+ "wrong bucket for {error:?}"+ );+ }+ }+#[test]fn large_in_list_membership_is_correct_at_raised_depth() {// A 200-element list overflows the default depth; at the cohort ceiling it must yield correctdiff --git a/rust/cohort-stream-processor/src/observability/metrics.rs b/rust/cohort-stream-processor/src/observability/metrics.rsindex 10f8c41207c4..5ee6f370aec1 100644--- a/rust/cohort-stream-processor/src/observability/metrics.rs+++ b/rust/cohort-stream-processor/src/observability/metrics.rs@@ -124,7 +124,9 @@ pub const STORE_MERGE_MALFORMED_TOTAL: &str = "store_merge_malformed_total";/// Cohort bytecode invoked a symbol with no registered native (counter). The function name is/// logged, not labelled, to keep cardinality bounded.pub const STAGE1_HOGVM_UNKNOWN_FUNCTION: &str = "stage1_hogvm_unknown_function_total";-/// Any other VM/program failure during cohort evaluation, coerced to `false` (counter).+/// Any other VM/program failure during cohort evaluation, coerced to `false`, labelled by `reason`+/// (a bounded semantic bucket: `type_coercion`|`stack`|`program`|`runtime`|… — see+/// `vm_error_reason`) (counter).pub const STAGE1_HOGVM_ERROR: &str = "stage1_hogvm_error_total";/// `properties`/`person_properties` JSON parse failure, labelled by `field` (counter).pub const STAGE1_GLOBALS_PARSE_ERROR: &str = "stage1_globals_parse_error_total";