8000 chore(flags): Make dependency graph function generic by haacked · Pull Request #33567 · PostHog/posthog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore(flags): Make dependency graph function generic #33567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 12, 2025

Conversation

haacked
Copy link
Contributor
@haacked haacked commented Jun 11, 2025

Important

👉 Stay up-to-date with PostHog coding conventions for a smoother review.

Problem

Related to #29016

This is a refactoring to make a generic trait for structures with dependencies such as Cohorts and Feature Flags.

Changes

No public facing changes. Code just gets better.

Did you write or update any docs for this change?

How did you test this code?

Unit tests

@haacked haacked requested a review from a team June 11, 2025 20:29
Copy link
Contributor
@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Refactors dependency graph handling in Rust code to introduce a generic DependencyProvider trait, enabling reuse across feature flags and cohorts.

  • Adds new graph_utils.rs with generic DependencyProvider trait and graph building utilities for improved code reuse
  • Implements DependencyProvider for both Cohort and FeatureFlag types to standardize dependency extraction
  • Replaces cohort-specific build_cohort_dependency_graph with generic build_dependency_graph implementation
  • Adds support for handling feature flag dependencies in property filters with proper cycle detection

4 files reviewed, 3 comments
Edit PR Review Bot Settings | Greptile

@haacked haacked removed the request for review from a team June 11, 2025 21:29
@haacked haacked marked this pull request as draft June 11, 2025 21:29
@haacked haacked force-pushed the haacked/dependency-provider branch from f003eee to f96fd6e Compare June 11, 2025 23:02
@haacked haacked marked this pull request as ready for review June 11, 2025 23:03
@haacked haacked requested a review from a team June 11, 2025 23:04
Copy link
Contributor
@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This update refactors error handling and dependency types to support a more generic dependency system across the codebase.

  • Introduces DependencyType enum and replaces cohort-specific errors with generic DependencyNotFound and DependencyCycle errors in errors.rs
  • Updates error handling in flag_matching.rs to differentiate between deleted dependencies (warning) vs actual errors
  • Makes FromFeatureAndMatch trait more generic by adding condition_index parameter to track which dependency condition failed
  • Changes error code from 'cohort_not_found' to 'dependency_not_found_cohort' for consistency in tests.rs

9 files reviewed, 4 comments
Edit PR Review Bot Settings | Greptile

Copy link
Contributor
@dmarticus dmarticus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is sweet! One comment on making sure you add this error type to our prometheus exporter, but otherwise good!

Comment on lines +518 to +521
if let FlagError::DependencyNotFound(dependency_type, dependency_id) = &e {
warn!(
"Feature flag '{}' targeting deleted {} with id {} for distinct_id '{}': {:?}",
flag.key, dependency_type, dependency_id, self.distinct_id, e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure to also add parsing for the DependencyNotFound error type to our prometheus label exporter

pub fn parse_exception_for_prometheus_label(err: &FlagError) -> &'static str {
    match err {
        FlagError::DatabaseError(msg) => {
            if msg.contains("statement timeout") {
                "timeout"
            } else if msg.contains("no more connections") {
                "no_more_connections"
            } else if msg.contains("Failed to fetch conditions") {
                "flag_condition_retry"
            } else if msg.contains("Failed to fetch group") {
                "group_mapping_retry"
            } else if msg.contains("Database healthcheck failed") {
                "healthcheck_failed"
            } else if msg.contains("query_wait_timeout") {
                "query_wait_timeout"
            } else {
                "database_error"
            }
        }
        FlagError::DatabaseUnavailable => "database_unavailable",
        FlagError::RedisUnavailable => "redis_unavailable",
        FlagError::TimeoutError => "timeout_error",
        FlagError::NoGroupTypeMappings => "no_group_type_mappings",
        FlagError::CohortNotFound(_) => "cohort_not_found",
        FlagError::CohortFiltersParsingError => "cohort_filters_parsing_error",
        _ => "unknown",
    }
    ```
    
    otherwise this will be an unknown error in our metrics (e.g. https://grafana.prod-us.posthog.dev/d/eehu20mrbkbuof/new-feature-flags?var-reason=$__all&var-team_id=$__all&var-timing_quartile=0.99&from=now-6h&to=now&timezone=utc), which is no bueno.

Comment on lines +59 to +62
FlagError::DependencyNotFound(dependency_type, _) => match dependency_type {
DependencyType::Cohort => "dependency_not_found_cohort",
DependencyType::Flag => "dependency_not_found_flag",
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmarticus, how do I add these labels to the prometheus label exporter?

Copy link
Contributor
@dmarticus dmarticus Jun 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just add them as names in parse_exception_for_prometheus_label method, like this

        FlagError::DatabaseUnavailable => "database_unavailable",
        FlagError::RedisUnavailable => "redis_unavailable",
        FlagError::TimeoutError => "timeout_error",
        FlagError::NoGroupTypeMappings => "no_group_type_mappings",
        FlagError::CohortNotFound(_) => "cohort_not_found",
        FlagError::CohortFiltersParsingError => "cohort_filters_parsing_error",
+       FlagError::DependencyNotFound(dependency_type, _) => match dependency_type {
+           DependencyType::Cohort => "dependency_not_found_cohort",
+           DependencyType::Flag => "dependency_not_found_flag",
+       },

should work i think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, that code is already there. Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean for DependencyCycle? I don't see that error type represented in this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I added some for DependencyCycle in case that's what you meant.

@haacked haacked merged commit 002f100 into master Jun 12, 2025
88 checks passed
@haacked haacked deleted the haacked/dependency-provider branch June 12, 2025 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0