: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
fix(flags): explain failed flag dependencies in the testing tab (#113500)
Phil Haack251ac2ecab80
diff --git a/rust/feature-flags/src/api/types.rs b/rust/feature-flags/src/api/types.rsindex e5ef0b7b3f26..e6cd121d8bac 100644--- a/rust/feature-flags/src/api/types.rs+++ b/rust/feature-flags/src/api/types.rs@@ -5,14 +5,18 @@ use crate::flags::evaluate_v2::EvaluationDetail;use crate::flags::flag_group_type_mapping::GroupTypeIndex;use crate::flags::flag_match_reason::FeatureFlagMatchReason;use crate::flags::flag_matching::FeatureFlagMatch;-use crate::flags::flag_matching_utils::match_flag_value_to_flag_filter;+use crate::flags::flag_matching_utils::{failed_flag_dependency, match_flag_value_to_flag_filter};use crate::flags::flag_models::{FeatureFlag, FeatureFlagId, FlagFilters, Holdout};use crate::properties::property_matching::{match_property, PropertyMatchingContext};use crate::properties::property_models::OperatorType;use chrono_tz::Tz;use serde::{de, Deserialize, Deserializer, Serialize};use serde_json::Value;-use std::{collections::HashMap, fmt, str::FromStr};+use std::{+ collections::{HashMap, HashSet},+ fmt,+ str::FromStr,+};use uuid::Uuid;fn format_operator_explanation(key: &str, op_label: &str, value: &Option<Value>) -> String {@@ -639,6 +643,7 @@ pub trait FromFeatureAndMatch {property_values: Option<&HashMap<String, Value>>,group_property_values: Option<&HashMap<GroupTypeIndex, HashMap<String, Value>>>,flag_evaluation_results: Option<&HashMap<FeatureFlagId, FlagValue>>,+ failed_flag_ids: Option<&HashSet<FeatureFlagId>>,cohort_matches: Option<&HashMap<CohortId, CohortMembership>>,matching_context: PropertyMatchingContext,) -> Self;@@ -657,6 +662,7 @@ impl FromFeatureAndMatch for FlagDetails {None,None,None,+ None,PropertyMatchingContext::new(Tz::UTC, false),)}@@ -668,6 +674,7 @@ impl FromFeatureAndMatch for FlagDetails {property_values: Option<&HashMap<String, Value>>,group_property_values: Option<&HashMap<GroupTypeIndex, HashMap<String, Value>>>,flag_evaluation_results: Option<&HashMap<FeatureFlagId, FlagValue>>,+ failed_flag_ids: Option<&HashSet<FeatureFlagId>>,cohort_matches: Option<&HashMap<CohortId, CohortMembership>>,matching_context: PropertyMatchingContext,) -> Self {@@ -699,6 +706,7 @@ impl FromFeatureAndMatch for FlagDetails {property_values,group_property_values,flag_evaluation_results,+ failed_flag_ids,cohort_matches,matching_context,)@@ -765,12 +773,14 @@ impl FromFeatureAndMatch for FlagDetails {}impl FlagDetails {+ #[allow(clippy::too_many_arguments)]fn build_condition_analysis(flag: &FeatureFlag,flag_match: &FeatureFlagMatch,property_values: Option<&HashMap<String, Value>>,group_property_values: Option<&HashMap<GroupTypeIndex, HashMap<String, Value>>>,flag_evaluation_results: Option<&HashMap<FeatureFlagId, FlagValue>>,+ failed_flag_ids: Option<&HashSet<FeatureFlagId>>,cohort_matches: Option<&HashMap<CohortId, CohortMembership>>,matching_context: PropertyMatchingContext,) -> Vec<ConditionAnalysis> {@@ -824,16 +834,21 @@ impl FlagDetails {// Resolve them against the actual flag evaluation results instead.if property.depends_on_feature_flag() {let empty = HashMap::new();- let property_matched = match_flag_value_to_flag_filter(- property,- flag_evaluation_results.unwrap_or(&empty),- );+ let results = flag_evaluation_results.unwrap_or(&empty);+ let property_matched = match_flag_value_to_flag_filter(property, results);// Do not expose the dependency flag's evaluated value here. The caller is// only authorized for the flag under test, not necessarily the dependency// flag, so serializing its raw value would leak it. `matched` reflects the// tested flag's own condition outcome, which the caller may already see.let expected = property.value.clone().unwrap_or(Value::Null);- let explanation = if property_matched {+ // A failed dependency has no entry in `results`.+ // `match_flag_value_to_flag_filter` returns false for a missing entry.+ let dependency_failed = failed_flag_ids+ .and_then(|failed| failed_flag_dependency(property, results, failed))+ .is_some();+ let explanation = if dependency_failed {+ format!("Flag dependency '{}' failed to evaluate", property.key)+ } else if property_matched {format!("Flag dependency '{}' satisfied the required value {}",property.key, expected@@ -1701,6 +1716,7 @@ mod tests {None,None,None,+ None,PropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);@@ -1806,6 +1822,7 @@ mod tests {Some(&group_props),None,None,+ None,PropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);@@ -1889,6 +1906,7 @@ mod tests {Some(&group_props),None,None,+ None,PropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);@@ -1954,6 +1972,7 @@ mod tests {None,Some(&flag_results),None,+ None,PropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);@@ -1989,6 +2008,7 @@ mod tests {None,Some(&flag_results),None,+ None,PropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);@@ -2025,6 +2045,7 @@ mod tests {None,None, // empty — dependency flag 42 absentNone,+ None,PropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);@@ -2091,6 +2112,7 @@ mod tests {Some(&HashMap::new()),None,None,+ None,None, // membership never resolvedPropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);@@ -2172,6 +2194,7 @@ mod tests {Some(&HashMap::new()),None,None,+ None,Some(&HashMap::from([(12345, membership)])),PropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);@@ -2244,6 +2267,7 @@ mod tests {None,None,None,+ None,PropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);@@ -2321,6 +2345,7 @@ mod tests {None,None,None,+ None,PropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);@@ -2366,6 +2391,7 @@ mod tests {None,None,None,+ None,PropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);@@ -2414,6 +2440,7 @@ mod tests {None,None,None,+ None,PropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);@@ -2473,6 +2500,7 @@ mod tests {None,None,None,+ None,PropertyMatchingContext::new(chrono_tz::Tz::UTC, false),);diff --git a/rust/feature-flags/src/flags/flag_matching.rs b/rust/feature-flags/src/flags/flag_matching.rsindex 0e4405103963..f7dcf650405b 100644--- a/rust/feature-flags/src/flags/flag_matching.rs+++ b/rust/feature-flags/src/flags/flag_matching.rs@@ -16,10 +16,10 @@ use crate::flags::flag_group_type_mapping::{};use crate::flags::flag_match_reason::FeatureFlagMatchReason;use crate::flags::flag_matching_utils::{- calculate_hash, db_operations, fetch_and_locally_cache_all_relevant_properties,- get_feature_flag_hash_key_overrides, match_flag_value_to_flag_filter,- populate_missing_initial_properties, populate_os_aliases, set_feature_flag_hash_key_overrides,- should_write_hash_key_override, track_unretried_db_error,+ calculate_hash, db_operations, failed_flag_dependency,+ fetch_and_locally_cache_all_relevant_properties, get_feature_flag_hash_key_overrides,+ match_flag_value_to_flag_filter, populate_missing_initial_properties, populate_os_aliases,+ set_feature_flag_hash_key_overrides, should_write_hash_key_override, track_unretried_db_error,};use crate::flags::flag_models::{default_has_experiment, FeatureFlag, FeatureFlagId, FeatureFlagList, FlagFilters,@@ -40,7 +40,7 @@ use crate::metrics::consts::{PROPERTY_CACHE_HITS_COUNTER, PROPERTY_CACHE_MISSES_COUNTER,};use crate::properties::property_matching::{match_property, PropertyMatchingContext};-use crate::properties::property_models::{OperatorType, PropertyFilter, PropertyType};+use crate::properties::property_models::{PropertyFilter, PropertyType};use crate::rayon_dispatcher::RayonDispatcher;use crate::utils::deadline::before_deadline;use crate::utils::graph_utils::PrecomputedDependencyGraph;@@ -305,21 +305,8 @@ impl FlagEvaluationState {self.flag_evaluation_results.insert(flag_id, flag_value);}- /// Returns the flag this filter depends on when that flag failed earlier in this request.- /// A flag with a recorded result never counts. An unsupported flag fails, but it is- /// pre-seeded `false`, so its dependents still read it as false. A malformed filter never- /// counts either, because `match_flag_value_to_flag_filter` rejects it for every value.fn failed_dependency(&self, filter: &PropertyFilter) -> Option<FeatureFlagId> {- if self.failed_flag_ids.is_empty()- || filter.operator != Some(OperatorType::FlagEvaluatesTo)- || !matches!(filter.value, Some(Value::Bool(_) | Value::String(_)))- {- return None;- }- let flag_id = filter.get_feature_flag_id()?;- (!self.flag_evaluation_results.contains_key(&flag_id)- && self.failed_flag_ids.contains(&flag_id))- .then_some(flag_id)+ failed_flag_dependency(filter, &self.flag_evaluation_results, &self.failed_flag_ids)}/// Returns true when no single value of some failed flag satisfies all of the filters on that@@ -1457,6 +1444,7 @@ impl FeatureFlagMatcher {merged_person_props.as_ref(),Some(&merged_group_props),Some(&self.flag_evaluation_state.flag_evaluation_results),+ Some(&self.flag_evaluation_state.failed_flag_ids),Some(&cohort_matches),PropertyMatchingContext::new(self.timezone,diff --git a/rust/feature-flags/src/flags/flag_matching_utils.rs b/rust/feature-flags/src/flags/flag_matching_utils.rsindex 811187407ace..2b36f1d8b774 100644--- a/rust/feature-flags/src/flags/flag_matching_utils.rs+++ b/rust/feature-flags/src/flags/flag_matching_utils.rs@@ -912,6 +912,26 @@ pub fn match_flag_value_to_flag_filter(}}+/// Returns the flag this filter depends on when that flag failed earlier in this request.+/// A flag with a recorded result never counts. An unsupported flag fails, but it is+/// pre-seeded `false`, so its dependents still read it as false. A malformed filter never+/// counts either, because `match_flag_value_to_flag_filter` rejects it for every value.+pub fn failed_flag_dependency(+ filter: &PropertyFilter,+ flag_evaluation_results: &HashMap<FeatureFlagId, FlagValue>,+ failed_flag_ids: &HashSet<FeatureFlagId>,+) -> Option<FeatureFlagId> {+ if failed_flag_ids.is_empty()+ || filter.operator != Some(OperatorType::FlagEvaluatesTo)+ || !matches!(filter.value, Some(Value::Bool(_) | Value::String(_)))+ {+ return None;+ }+ let flag_id = filter.get_feature_flag_id()?;+ (!flag_evaluation_results.contains_key(&flag_id) && failed_flag_ids.contains(&flag_id))+ .then_some(flag_id)+}+/// Retrieves feature flag hash key overrides for a list of distinct IDs with retry logic.////// This function fetches any hash key overrides that have been set for feature flagsdiff --git a/rust/feature-flags/src/flags/test_flag_matching.rs b/rust/feature-flags/src/flags/test_flag_matching.rsindex 59e6f1bf3f18..c51ad15ce472 100644--- a/rust/feature-flags/src/flags/test_flag_matching.rs+++ b/rust/feature-flags/src/flags/test_flag_matching.rs@@ -1383,7 +1383,8 @@ mod tests {empty_group_type_cache(),None,)- .with_enabled_only_flag_keys(enabled_only_flag_keys);+ .with_enabled_only_flag_keys(enabled_only_flag_keys)+ .with_detailed_analysis(true);let rollout_flag = mock!(FeatureFlag, id: 1, key: "rollout_flag".mock_into());let person_flag = mock!(FeatureFlag,@@ -1673,6 +1674,21 @@ mod tests {assert!(!details.enabled, "{key}");assert_eq!(details.reason.code, "dependency_failed", "{key}");}++ let dependency_row = |key: &str| {+ let conditions = result.flags[key].conditions.as_ref().unwrap();+ conditions[0].properties[0].clone()+ };+ let failed_dependency = dependency_row("dependent_with_catch_all_flag");+ assert_eq!(+ failed_dependency.explanation,+ "Flag dependency '2' failed to evaluate"+ );+ assert!(!failed_dependency.matched);+ assert_eq!(+ dependency_row("healthy_dependent_flag").explanation,+ "Flag dependency '1' satisfied the required value true"+ );}#[tokio::test]