Class: PostHog::FeatureFlagsPoller
- Inherits:
-
Object
- Object
- PostHog::FeatureFlagsPoller
- Defined in:
- lib/posthog/feature_flags.rb
Constant Summary
Constants included from Utils
Utils::UTC_OFFSET_WITHOUT_COLON, Utils::UTC_OFFSET_WITH_COLON
Class Method Summary collapse
-
.compare(lhs, rhs, operator) ⇒ Object
Class methods.
- .match_cohort(property, property_values, cohort_properties) ⇒ Object
- .match_property(property, property_values, cohort_properties = {}) ⇒ Object
- .match_property_group(property_group, property_values, cohort_properties) ⇒ Object
- .matches_dependency_value(expected_value, actual_value) ⇒ Object
- .relative_date_parse_for_feature_flag_matching(value) ⇒ Object
Instance Method Summary collapse
- #_compute_flag_locally(flag, distinct_id, groups = {}, person_properties = {}, group_properties = {}) ⇒ Object
- #_compute_flag_payload_locally(key, match_value) ⇒ Object
-
#_hash(key, distinct_id, salt = '') ⇒ Object
This function takes a distinct_id and a feature flag key and returns a float between 0 and 1.
- #_load_feature_flags ⇒ Object
-
#_mask_tokens_in_url(url) ⇒ Object
rubocop:enable Lint/ShadowedException.
-
#_request(uri, request_object, timeout = nil, include_etag: false) ⇒ Object
rubocop:disable Lint/ShadowedException.
- #_request_feature_flag_definitions(etag: nil) ⇒ Object
- #_request_feature_flag_evaluation(data = {}) ⇒ Object
- #_request_remote_config_payload(flag_key) ⇒ Object
- #condition_match(flag, distinct_id, condition, properties, evaluation_cache, cohort_properties = {}) ⇒ Object
-
#evaluate_flag_dependency(property, evaluation_cache, distinct_id, properties, cohort_properties) ⇒ Boolean
Evaluates a flag dependency property according to the dependency chain algorithm.
- #get_all_flags(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
- #get_all_flags_and_payloads(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false, raise_on_error = false) ⇒ Object
- #get_feature_flag(key, distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
- #get_feature_flag_payload(key, distinct_id, match_value = nil, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
- #get_feature_payloads(distinct_id, groups = {}, person_properties = {}, group_properties = {}, _only_evaluate_locally = false) ⇒ Object
- #get_feature_variants(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false, raise_on_error = false) ⇒ Object
- #get_flags(distinct_id, groups = {}, person_properties = {}, group_properties = {}) ⇒ Object
- #get_matching_variant(flag, distinct_id) ⇒ Object
- #get_remote_config_payload(flag_key) ⇒ Object
-
#initialize(polling_interval, personal_api_key, project_api_key, host, feature_flag_request_timeout_seconds, on_error = nil) ⇒ FeatureFlagsPoller
constructor
A new instance of FeatureFlagsPoller.
- #load_feature_flags(force_reload = false) ⇒ Object
- #match_feature_flag_properties(flag, distinct_id, properties, evaluation_cache, cohort_properties = {}) ⇒ Object
- #shutdown_poller ⇒ Object
- #variant_lookup_table(flag) ⇒ Object
Methods included from Utils
convert_to_datetime, date_in_iso8601, datetime_in_iso8601, formatted_offset, get_by_symbol_or_string_key, is_valid_regex, isoify_dates, isoify_dates!, seconds_to_utc_offset, stringify_keys, symbolize_keys, symbolize_keys!, time_in_iso8601, uid
Methods included from Logging
Constructor Details
#initialize(polling_interval, personal_api_key, project_api_key, host, feature_flag_request_timeout_seconds, on_error = nil) ⇒ FeatureFlagsPoller
Returns a new instance of FeatureFlagsPoller.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/posthog/feature_flags.rb', line 26 def initialize( polling_interval, personal_api_key, project_api_key, host, feature_flag_request_timeout_seconds, on_error = nil ) @polling_interval = polling_interval || 30 @personal_api_key = personal_api_key @project_api_key = project_api_key @host = host @feature_flags = Concurrent::Array.new @group_type_mapping = Concurrent::Hash.new @cohorts = Concurrent::Hash.new @loaded_flags_successfully_once = Concurrent::AtomicBoolean.new @feature_flags_by_key = nil @feature_flag_request_timeout_seconds = feature_flag_request_timeout_seconds @on_error = on_error || proc { |status, error| } @quota_limited = Concurrent::AtomicBoolean.new(false) @flags_etag = Concurrent::AtomicReference.new(nil) @task = Concurrent::TimerTask.new( execution_interval: polling_interval ) { _load_feature_flags } # If no personal API key, disable local evaluation & thus polling for definitions if @personal_api_key.nil? logger.info 'No personal API key provided, disabling local evaluation' @loaded_flags_successfully_once.make_true else # load once before timer load_feature_flags @task.execute end end |
Class Method Details
.compare(lhs, rhs, operator) ⇒ Object
Class methods
366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/posthog/feature_flags.rb', line 366 def self.compare(lhs, rhs, operator) case operator when 'gt' lhs > rhs when 'gte' lhs >= rhs when 'lt' lhs < rhs when 'lte' lhs <= rhs else raise "Invalid operator: #{operator}" end end |
.match_cohort(property, property_values, cohort_properties) ⇒ Object
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/posthog/feature_flags.rb', line 491 def self.match_cohort(property, property_values, cohort_properties) # Cohort properties are in the form of property groups like this: # { # "cohort_id" => { # "type" => "AND|OR", # "values" => [{ # "key" => "property_name", "value" => "property_value" # }] # } # } cohort_id = extract_value(property, :value).to_s property_group = find_cohort_property(cohort_properties, cohort_id) unless property_group raise RequiresServerEvaluation, "cohort #{cohort_id} not found in local cohorts - likely a static cohort that requires server evaluation" end match_property_group(property_group, property_values, cohort_properties) end |
.match_property(property, property_values, cohort_properties = {}) ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/posthog/feature_flags.rb', line 412 def self.match_property(property, property_values, cohort_properties = {}) # only looks for matches where key exists in property_values # doesn't support operator is_not_set PostHog::Utils.symbolize_keys! property PostHog::Utils.symbolize_keys! property_values # Handle cohort properties return match_cohort(property, property_values, cohort_properties) if extract_value(property, :type) == 'cohort' key = property[:key].to_sym value = property[:value] operator = property[:operator] || 'exact' if !property_values.key?(key) raise InconclusiveMatchError, "Property #{key} not found in property_values" elsif operator == 'is_not_set' raise InconclusiveMatchError, 'Operator is_not_set not supported' end override_value = property_values[key] case operator when 'exact', 'is_not' if value.is_a?(Array) values_stringified = value.map { |val| val.to_s.downcase } return values_stringified.any?(override_value.to_s.downcase) if operator == 'exact' return values_stringified.none?(override_value.to_s.downcase) end if operator == 'exact' value.to_s.downcase == override_value.to_s.downcase else value.to_s.downcase != override_value.to_s.downcase end when 'is_set' property_values.key?(key) when 'icontains' override_value.to_s.downcase.include?(value.to_s.downcase) when 'not_icontains' !override_value.to_s.downcase.include?(value.to_s.downcase) when 'regex' PostHog::Utils.is_valid_regex(value.to_s) && !Regexp.new(value.to_s).match(override_value.to_s).nil? when 'not_regex' PostHog::Utils.is_valid_regex(value.to_s) && Regexp.new(value.to_s).match(override_value.to_s).nil? when 'gt', 'gte', 'lt', 'lte' parsed_value = nil begin parsed_value = Float(value) rescue StandardError # rubocop:disable Lint/SuppressedException end if !parsed_value.nil? && !override_value.nil? if override_value.is_a?(String) compare(override_value, value.to_s, operator) else compare(override_value, parsed_value, operator) end else compare(override_value.to_s, value.to_s, operator) end when 'is_date_before', 'is_date_after' override_date = PostHog::Utils.convert_to_datetime(override_value.to_s) parsed_date = relative_date_parse_for_feature_flag_matching(value.to_s) parsed_date = PostHog::Utils.convert_to_datetime(value.to_s) if parsed_date.nil? raise InconclusiveMatchError, 'Invalid date format' unless parsed_date if operator == 'is_date_before' override_date < parsed_date elsif operator == 'is_date_after' override_date > parsed_date end else raise InconclusiveMatchError, "Unknown operator: #{operator}" end end |
.match_property_group(property_group, property_values, cohort_properties) ⇒ Object
512 513 514 515 516 517 518 519 520 521 522 523 524 525 |
# File 'lib/posthog/feature_flags.rb', line 512 def self.match_property_group(property_group, property_values, cohort_properties) return true if property_group.nil? || property_group.empty? group_type = extract_value(property_group, :type) properties = extract_value(property_group, :values) return true if properties.nil? || properties.empty? if nested_property_group?(properties) match_nested_property_group(properties, group_type, property_values, cohort_properties) else match_regular_property_group(properties, group_type, property_values, cohort_properties) end end |
.matches_dependency_value(expected_value, actual_value) ⇒ Object
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 |
# File 'lib/posthog/feature_flags.rb', line 690 def self.matches_dependency_value(expected_value, actual_value) # Check if the actual flag value matches the expected dependency value. # # - String variant case: check for exact match or boolean true # - Boolean case: must match expected boolean value # # @param expected_value [Object] The expected value from the property # @param actual_value [Object] The actual value returned by the flag evaluation # @return [Boolean] True if the values match according to flag dependency rules # String variant case - check for exact match or boolean true if actual_value.is_a?(String) && !actual_value.empty? if expected_value.is_a?(TrueClass) || expected_value.is_a?(FalseClass) # Any variant matches boolean true return expected_value elsif expected_value.is_a?(String) # variants are case-sensitive, hence our comparison is too return actual_value == expected_value else return false end # Boolean case - must match expected boolean value elsif actual_value.is_a?(TrueClass) || actual_value.is_a?(FalseClass) return actual_value == expected_value if expected_value.is_a?(TrueClass) || expected_value.is_a?(FalseClass) end # Default case false end |
.relative_date_parse_for_feature_flag_matching(value) ⇒ Object
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/posthog/feature_flags.rb', line 381 def self.relative_date_parse_for_feature_flag_matching(value) match = /^-?([0-9]+)([a-z])$/.match(value) parsed_dt = DateTime.now.new_offset(0) return unless match number = match[1].to_i if number >= 10_000 # Guard against overflow, disallow numbers greater than 10_000 return nil end interval = match[2] case interval when 'h' parsed_dt -= (number / 24.0) when 'd' parsed_dt = parsed_dt.prev_day(number) when 'w' parsed_dt = parsed_dt.prev_day(number * 7) when 'm' parsed_dt = parsed_dt.prev_month(number) when 'y' parsed_dt = parsed_dt.prev_year(number) else return nil end parsed_dt end |
Instance Method Details
#_compute_flag_locally(flag, distinct_id, groups = {}, person_properties = {}, group_properties = {}) ⇒ Object
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 |
# File 'lib/posthog/feature_flags.rb', line 724 def _compute_flag_locally(flag, distinct_id, groups = {}, person_properties = {}, group_properties = {}) raise RequiresServerEvaluation, 'Flag has experience continuity enabled' if flag[:ensure_experience_continuity] return false unless flag[:active] # Create evaluation cache for flag dependencies evaluation_cache = {} flag_filters = flag[:filters] || {} aggregation_group_type_index = flag_filters[:aggregation_group_type_index] if aggregation_group_type_index.nil? return match_feature_flag_properties(flag, distinct_id, person_properties, evaluation_cache, @cohorts) end group_name = @group_type_mapping[aggregation_group_type_index.to_s.to_sym] if group_name.nil? logger.warn( "[FEATURE FLAGS] Unknown group type index #{aggregation_group_type_index} for feature flag #{flag[:key]}" ) # failover to `/flags/` raise InconclusiveMatchError, 'Flag has unknown group type index' end group_name_symbol = group_name.to_sym unless groups.key?(group_name_symbol) # Group flags are never enabled if appropriate `groups` aren't passed in # don't failover to `/flags/`, since response will be the same logger.warn "[FEATURE FLAGS] Can't compute group feature flag: #{flag[:key]} without group names passed in" return false end focused_group_properties = group_properties[group_name_symbol] match_feature_flag_properties(flag, groups[group_name_symbol], focused_group_properties, evaluation_cache, @cohorts) end |
#_compute_flag_payload_locally(key, match_value) ⇒ Object
763 764 765 766 767 768 769 770 771 772 773 |
# File 'lib/posthog/feature_flags.rb', line 763 def _compute_flag_payload_locally(key, match_value) return nil if @feature_flags_by_key.nil? response = nil if [true, false].include? match_value response = @feature_flags_by_key.dig(key, :filters, :payloads, match_value.to_s.to_sym) elsif match_value.is_a? String response = @feature_flags_by_key.dig(key, :filters, :payloads, match_value.to_sym) end response end |
#_hash(key, distinct_id, salt = '') ⇒ Object
This function takes a distinct_id and a feature flag key and returns a float between 0 and 1. Given the same distinct_id and key, it’ll always return the same float. These floats are uniformly distributed between 0 and 1, so if we want to show this feature to 20% of traffic we can do _hash(key, distinct_id) < 0.2
841 842 843 844 |
# File 'lib/posthog/feature_flags.rb', line 841 def _hash(key, distinct_id, salt = '') hash_key = Digest::SHA1.hexdigest "#{key}.#{distinct_id}#{salt}" (Integer(hash_key[0..14], 16).to_f / 0xfffffffffffffff) end |
#_load_feature_flags ⇒ Object
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 |
# File 'lib/posthog/feature_flags.rb', line 868 def _load_feature_flags begin res = _request_feature_flag_definitions(etag: @flags_etag.value) rescue StandardError => e @on_error.call(-1, e.to_s) return end # Handle 304 Not Modified - flags haven't changed, skip processing # Only update ETag if the 304 response includes one if res[:not_modified] @flags_etag.value = res[:etag] if res[:etag] logger.debug '[FEATURE FLAGS] Flags not modified (304), using cached data' return end # Handle quota limits with 402 status if res.is_a?(Hash) && res[:status] == 402 logger.warn( '[FEATURE FLAGS] Feature flags quota limit exceeded - unsetting all local flags. ' \ 'Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts' ) @feature_flags = Concurrent::Array.new @feature_flags_by_key = {} @group_type_mapping = Concurrent::Hash.new @cohorts = Concurrent::Hash.new @loaded_flags_successfully_once.make_false @quota_limited.make_true return end if res.key?(:flags) # Only update ETag on successful responses with flag data @flags_etag.value = res[:etag] @feature_flags = res[:flags] || [] @feature_flags_by_key = {} @feature_flags.each do |flag| @feature_flags_by_key[flag[:key]] = flag unless flag[:key].nil? end @group_type_mapping = res[:group_type_mapping] || {} @cohorts = res[:cohorts] || {} logger.debug "Loaded #{@feature_flags.length} feature flags and #{@cohorts.length} cohorts" @loaded_flags_successfully_once.make_true if @loaded_flags_successfully_once.false? else logger.debug "Failed to load feature flags: #{res}" end end |
#_mask_tokens_in_url(url) ⇒ Object
rubocop:enable Lint/ShadowedException
1000 1001 1002 |
# File 'lib/posthog/feature_flags.rb', line 1000 def _mask_tokens_in_url(url) url.gsub(/token=([^&]{10})[^&]*/, 'token=\1...') end |
#_request(uri, request_object, timeout = nil, include_etag: false) ⇒ Object
rubocop:disable Lint/ShadowedException
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 |
# File 'lib/posthog/feature_flags.rb', line 949 def _request(uri, request_object, timeout = nil, include_etag: false) request_object['User-Agent'] = "posthog-ruby#{PostHog::VERSION}" request_timeout = timeout || 10 begin Net::HTTP.start( uri.hostname, uri.port, use_ssl: uri.scheme == 'https', read_timeout: request_timeout ) do |http| res = http.request(request_object) status_code = res.code.to_i etag = include_etag ? res['ETag'] : nil # Handle 304 Not Modified - return special response indicating no change if status_code == 304 logger.debug("#{request_object.method} #{_mask_tokens_in_url(uri.to_s)} returned 304 Not Modified") return { not_modified: true, etag: etag, status: status_code } end # Parse response body to hash begin response = JSON.parse(res.body, { symbolize_names: true }) # Only add status (and etag if requested) if response is a hash extra_fields = { status: status_code } extra_fields[:etag] = etag if include_etag response = response.merge(extra_fields) if response.is_a?(Hash) return response rescue JSON::ParserError # Handle case when response isn't valid JSON error_response = { error: 'Invalid JSON response', body: res.body, status: status_code } error_response[:etag] = etag if include_etag return error_response end end rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ReadTimeout, Net::WriteTimeout, Net::ProtocolError logger.debug("Unable to complete request to #{uri}") raise end end |
#_request_feature_flag_definitions(etag: nil) ⇒ Object
918 919 920 921 922 923 924 925 926 |
# File 'lib/posthog/feature_flags.rb', line 918 def _request_feature_flag_definitions(etag: nil) uri = URI("#{@host}/api/feature_flag/local_evaluation") uri.query = URI.encode_www_form([['token', @project_api_key], %w[send_cohorts true]]) req = Net::HTTP::Get.new(uri) req['Authorization'] = "Bearer #{@personal_api_key}" req['If-None-Match'] = etag if etag _request(uri, req, nil, include_etag: true) end |
#_request_feature_flag_evaluation(data = {}) ⇒ Object
928 929 930 931 932 933 934 935 936 |
# File 'lib/posthog/feature_flags.rb', line 928 def _request_feature_flag_evaluation(data = {}) uri = URI("#{@host}/flags/?v=2") req = Net::HTTP::Post.new(uri) req['Content-Type'] = 'application/json' data['token'] = @project_api_key req.body = data.to_json _request(uri, req, @feature_flag_request_timeout_seconds) end |
#_request_remote_config_payload(flag_key) ⇒ Object
938 939 940 941 942 943 944 945 946 |
# File 'lib/posthog/feature_flags.rb', line 938 def _request_remote_config_payload(flag_key) uri = URI("#{@host}/api/projects/@current/feature_flags/#{flag_key}/remote_config") uri.query = URI.encode_www_form([['token', @project_api_key]]) req = Net::HTTP::Get.new(uri) req['Content-Type'] = 'application/json' req['Authorization'] = "Bearer #{@personal_api_key}" _request(uri, req, @feature_flag_request_timeout_seconds) end |
#condition_match(flag, distinct_id, condition, properties, evaluation_cache, cohort_properties = {}) ⇒ Object
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 |
# File 'lib/posthog/feature_flags.rb', line 815 def condition_match(flag, distinct_id, condition, properties, evaluation_cache, cohort_properties = {}) rollout_percentage = condition[:rollout_percentage] unless (condition[:properties] || []).empty? unless condition[:properties].all? do |prop| if prop[:type] == 'flag' evaluate_flag_dependency(prop, evaluation_cache, distinct_id, properties, cohort_properties) else FeatureFlagsPoller.match_property(prop, properties, cohort_properties) end end return false end return true if rollout_percentage.nil? end return false if !rollout_percentage.nil? && (_hash(flag[:key], distinct_id) > (rollout_percentage.to_f / 100)) true end |
#evaluate_flag_dependency(property, evaluation_cache, distinct_id, properties, cohort_properties) ⇒ Boolean
Evaluates a flag dependency property according to the dependency chain algorithm.
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
# File 'lib/posthog/feature_flags.rb', line 607 def evaluate_flag_dependency(property, evaluation_cache, distinct_id, properties, cohort_properties) if property[:operator] != 'flag_evaluates_to' # Should never happen, but just in case raise InconclusiveMatchError, "Operator #{property[:operator]} not supported for flag dependencies" end if @feature_flags_by_key.nil? || evaluation_cache.nil? # Cannot evaluate flag dependencies without required context raise InconclusiveMatchError, "Cannot evaluate flag dependency on '#{property[:key] || 'unknown'}' " \ 'without feature flags loaded or evaluation_cache' end # Check if dependency_chain is present - it should always be provided for flag dependencies unless property.key?(:dependency_chain) # Missing dependency_chain indicates malformed server data raise InconclusiveMatchError, "Flag dependency property for '#{property[:key] || 'unknown'}' " \ "is missing required 'dependency_chain' field" end dependency_chain = property[:dependency_chain] # Handle circular dependency (empty chain means circular) if dependency_chain.empty? PostHog::Logging.logger&.debug("Circular dependency detected for flag: #{property[:key]}") raise InconclusiveMatchError, "Circular dependency detected for flag '#{property[:key] || 'unknown'}'" end # Evaluate all dependencies in the chain order dependency_chain.each do |dep_flag_key| unless evaluation_cache.key?(dep_flag_key) # Need to evaluate this dependency first dep_flag = @feature_flags_by_key[dep_flag_key] if dep_flag.nil? # Missing flag dependency - cannot evaluate locally evaluation_cache[dep_flag_key] = nil raise InconclusiveMatchError, "Cannot evaluate flag dependency '#{dep_flag_key}' - flag not found in local flags" elsif !dep_flag[:active] # Check if the flag is active (same check as in _compute_flag_locally) evaluation_cache[dep_flag_key] = false else # Recursively evaluate the dependency using existing instance method begin dep_result = match_feature_flag_properties( dep_flag, distinct_id, properties, evaluation_cache, cohort_properties ) evaluation_cache[dep_flag_key] = dep_result rescue InconclusiveMatchError => e # If we can't evaluate a dependency, store nil and propagate the error evaluation_cache[dep_flag_key] = nil raise InconclusiveMatchError, "Cannot evaluate flag dependency '#{dep_flag_key}': #{e.message}" end end end # Check the cached result cached_result = evaluation_cache[dep_flag_key] if cached_result.nil? # Previously inconclusive - raise error again raise InconclusiveMatchError, "Flag dependency '#{dep_flag_key}' was previously inconclusive" elsif !cached_result # Definitive False result - dependency failed return false end end # Get the expected value of the immediate dependency and the actual value expected_value = property[:value] # The flag we want to evaluate is defined by :key which should ALSO be the last key in the dependency chain actual_value = evaluation_cache[property[:key]] self.class.matches_dependency_value(expected_value, actual_value) end |
#get_all_flags(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/posthog/feature_flags.rb', line 236 def get_all_flags( distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false ) if @quota_limited.true? logger.debug 'Not fetching flags from server - quota limited' return {} end # returns a string hash of all flags response = get_all_flags_and_payloads( distinct_id, groups, person_properties, group_properties, only_evaluate_locally ) response[:featureFlags] end |
#get_all_flags_and_payloads(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false, raise_on_error = false) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/posthog/feature_flags.rb', line 260 def get_all_flags_and_payloads( distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false, raise_on_error = false ) load_feature_flags flags = {} payloads = {} fallback_to_server = @feature_flags.empty? request_id = nil # Only for /flags requests evaluated_at = nil # Only for /flags requests @feature_flags.each do |flag| match_value = _compute_flag_locally(flag, distinct_id, groups, person_properties, group_properties) flags[flag[:key]] = match_value match_payload = _compute_flag_payload_locally(flag[:key], match_value) payloads[flag[:key]] = match_payload if match_payload rescue RequiresServerEvaluation, InconclusiveMatchError fallback_to_server = true rescue StandardError => e @on_error.call(-1, "Error computing flag locally: #{e}. #{e.backtrace.join("\n")} ") fallback_to_server = true end errors_while_computing = false quota_limited = nil status_code = nil if fallback_to_server && !only_evaluate_locally begin flags_and_payloads = get_flags(distinct_id, groups, person_properties, group_properties) errors_while_computing = flags_and_payloads[:errorsWhileComputingFlags] || false quota_limited = flags_and_payloads[:quotaLimited] status_code = flags_and_payloads[:status] unless flags_and_payloads.key?(:featureFlags) raise StandardError, "Error flags response: #{flags_and_payloads}" end request_id = flags_and_payloads[:requestId] evaluated_at = flags_and_payloads[:evaluatedAt] # Check if feature_flags are quota limited if quota_limited&.include?('feature_flags') logger.warn '[FEATURE FLAGS] Quota limited for feature flags' flags = {} payloads = {} else flags = stringify_keys(flags_and_payloads[:featureFlags] || {}) payloads = stringify_keys(flags_and_payloads[:featureFlagPayloads] || {}) end rescue StandardError => e @on_error.call(-1, "Error computing flag remotely: #{e}") raise if raise_on_error end end { featureFlags: flags, featureFlagPayloads: payloads, requestId: request_id, evaluatedAt: evaluated_at, errorsWhileComputingFlags: errors_while_computing, quotaLimited: quota_limited, status: status_code } end |
#get_feature_flag(key, distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/posthog/feature_flags.rb', line 152 def get_feature_flag( key, distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false ) # make sure they're loaded on first run load_feature_flags symbolize_keys! groups symbolize_keys! person_properties symbolize_keys! group_properties group_properties.each_value do |value| symbolize_keys!(value) end response = nil payload = nil feature_flag = @feature_flags_by_key&.[](key) unless feature_flag.nil? begin response = _compute_flag_locally(feature_flag, distinct_id, groups, person_properties, group_properties) payload = _compute_flag_payload_locally(key, response) unless response.nil? logger.debug "Successfully computed flag locally: #{key} -> #{response}" rescue RequiresServerEvaluation, InconclusiveMatchError => e logger.debug "Failed to compute flag #{key} locally: #{e}" rescue StandardError => e @on_error.call(-1, "Error computing flag locally: #{e}. #{e.backtrace.join("\n")}") end end flag_was_locally_evaluated = !response.nil? request_id = nil evaluated_at = nil feature_flag_error = nil if !flag_was_locally_evaluated && !only_evaluate_locally begin errors = [] flags_data = get_all_flags_and_payloads(distinct_id, groups, person_properties, group_properties, only_evaluate_locally, true) if flags_data.key?(:featureFlags) flags = stringify_keys(flags_data[:featureFlags] || {}) payloads = stringify_keys(flags_data[:featureFlagPayloads] || {}) request_id = flags_data[:requestId] evaluated_at = flags_data[:evaluatedAt] else logger.debug "Missing feature flags key: #{flags_data.to_json}" flags = {} payloads = {} end status = flags_data[:status] errors << FeatureFlagError.api_error(status) if status && status >= 400 errors << FeatureFlagError::ERRORS_WHILE_COMPUTING if flags_data[:errorsWhileComputingFlags] errors << FeatureFlagError::QUOTA_LIMITED if flags_data[:quotaLimited]&.include?('feature_flags') errors << FeatureFlagError::FLAG_MISSING unless flags.key?(key.to_s) response = flags[key] response = false if response.nil? payload = payloads[key] feature_flag_error = errors.join(',') unless errors.empty? logger.debug "Successfully computed flag remotely: #{key} -> #{response}" rescue Timeout::Error => e @on_error.call(-1, "Timeout while fetching flags remotely: #{e}") feature_flag_error = FeatureFlagError::TIMEOUT rescue Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError, SocketError => e @on_error.call(-1, "Connection error while fetching flags remotely: #{e}") feature_flag_error = FeatureFlagError::CONNECTION_ERROR rescue StandardError => e @on_error.call(-1, "Error computing flag remotely: #{e}. #{e.backtrace.join("\n")}") feature_flag_error = FeatureFlagError::UNKNOWN_ERROR end end [response, flag_was_locally_evaluated, request_id, evaluated_at, feature_flag_error, payload] end |
#get_feature_flag_payload(key, distinct_id, match_value = nil, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/posthog/feature_flags.rb', line 332 def get_feature_flag_payload( key, distinct_id, match_value = nil, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false ) if match_value.nil? match_value = get_feature_flag( key, distinct_id, groups, person_properties, group_properties, true )[0] end response = nil response = _compute_flag_payload_locally(key, match_value) unless match_value.nil? if response.nil? && !only_evaluate_locally flags_payloads = get_feature_payloads(distinct_id, groups, person_properties, group_properties) response = flags_payloads[key.downcase] || nil end response end |
#get_feature_payloads(distinct_id, groups = {}, person_properties = {}, group_properties = {}, _only_evaluate_locally = false) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/posthog/feature_flags.rb', line 95 def get_feature_payloads( distinct_id, groups = {}, person_properties = {}, group_properties = {}, _only_evaluate_locally = false ) flags_data = get_all_flags_and_payloads( distinct_id, groups, person_properties, group_properties ) if flags_data.key?(:featureFlagPayloads) stringify_keys(flags_data[:featureFlagPayloads] || {}) else logger.debug "Missing feature flag payloads key: #{flags_data.to_json}" {} end end |
#get_feature_variants(distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false, raise_on_error = false) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/posthog/feature_flags.rb', line 69 def get_feature_variants( distinct_id, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false, raise_on_error = false ) # TODO: Convert to options hash for easier argument passing flags_data = get_all_flags_and_payloads( distinct_id, groups, person_properties, group_properties, only_evaluate_locally, raise_on_error ) if flags_data.key?(:featureFlags) stringify_keys(flags_data[:featureFlags] || {}) else logger.debug "Missing feature flags key: #{flags_data.to_json}" {} end end |
#get_flags(distinct_id, groups = {}, person_properties = {}, group_properties = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/posthog/feature_flags.rb', line 117 def get_flags(distinct_id, groups = {}, person_properties = {}, group_properties = {}) request_data = { distinct_id: distinct_id, groups: groups, person_properties: person_properties, group_properties: group_properties } flags_response = _request_feature_flag_evaluation(request_data) # Only normalize if we have flags in the response if flags_response[:flags] # v4 format flags_hash = flags_response[:flags].transform_values do |flag| FeatureFlag.new(flag) end flags_response[:flags] = flags_hash flags_response[:featureFlags] = flags_hash.transform_values(&:get_value).transform_keys(&:to_sym) flags_response[:featureFlagPayloads] = flags_hash.transform_values(&:payload).transform_keys(&:to_sym) elsif flags_response[:featureFlags] # v3 format flags_response[:featureFlags] = flags_response[:featureFlags] || {} flags_response[:featureFlagPayloads] = flags_response[:featureFlagPayloads] || {} flags_response[:flags] = flags_response[:featureFlags].to_h do |key, value| [key, FeatureFlag.from_value_and_payload(key, value, flags_response[:featureFlagPayloads][key])] end end flags_response end |
#get_matching_variant(flag, distinct_id) ⇒ Object
846 847 848 849 850 851 852 |
# File 'lib/posthog/feature_flags.rb', line 846 def get_matching_variant(flag, distinct_id) hash_value = _hash(flag[:key], distinct_id, 'variant') matching_variant = variant_lookup_table(flag).find do |variant| hash_value >= variant[:value_min] and hash_value < variant[:value_max] end matching_variant.nil? ? nil : matching_variant[:key] end |
#get_remote_config_payload(flag_key) ⇒ Object
148 149 150 |
# File 'lib/posthog/feature_flags.rb', line 148 def get_remote_config_payload(flag_key) _request_remote_config_payload(flag_key) end |
#load_feature_flags(force_reload = false) ⇒ Object
63 64 65 66 67 |
# File 'lib/posthog/feature_flags.rb', line 63 def load_feature_flags(force_reload = false) return unless @loaded_flags_successfully_once.false? || force_reload _load_feature_flags end |
#match_feature_flag_properties(flag, distinct_id, properties, evaluation_cache, cohort_properties = {}) ⇒ Object
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 |
# File 'lib/posthog/feature_flags.rb', line 775 def match_feature_flag_properties(flag, distinct_id, properties, evaluation_cache, cohort_properties = {}) flag_filters = flag[:filters] || {} flag_conditions = flag_filters[:groups] || [] is_inconclusive = false result = nil # NOTE: This NEEDS to be `each` because `each_key` breaks flag_conditions.each do |condition| if condition_match(flag, distinct_id, condition, properties, evaluation_cache, cohort_properties) variant_override = condition[:variant] flag_multivariate = flag_filters[:multivariate] || {} flag_variants = flag_multivariate[:variants] || [] variant = if flag_variants.map { |variant| variant[:key] }.include?(condition[:variant]) variant_override else get_matching_variant(flag, distinct_id) end result = variant || true break end rescue RequiresServerEvaluation # Static cohort or other missing server-side data - must fallback to API raise rescue InconclusiveMatchError # Evaluation error (bad regex, invalid date, missing property, etc.) # Track that we had an inconclusive match, but try other conditions is_inconclusive = true end if !result.nil? return result elsif is_inconclusive raise InconclusiveMatchError, "Can't determine if feature flag is enabled or not with given properties" end # We can only return False when all conditions are False false end |
#shutdown_poller ⇒ Object
360 361 362 |
# File 'lib/posthog/feature_flags.rb', line 360 def shutdown_poller @task.shutdown end |
#variant_lookup_table(flag) ⇒ Object
854 855 856 857 858 859 860 861 862 863 864 865 866 |
# File 'lib/posthog/feature_flags.rb', line 854 def variant_lookup_table(flag) lookup_table = [] value_min = 0 flag_filters = flag[:filters] || {} variants = flag_filters[:multivariate] || {} multivariates = variants[:variants] || [] multivariates.each do |variant| value_max = value_min + (variant[:rollout_percentage].to_f / 100) lookup_table << { value_min: value_min, value_max: value_max, key: variant[:key] } value_min = value_max end lookup_table end |