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
-
.parse_semver(value) ⇒ Object
Parse a semver string into a comparable [major, minor, patch] integer array.
- .relative_date_parse_for_feature_flag_matching(value) ⇒ Object
-
.semver_caret_bounds(value) ⇒ Object
Returns bounds for caret (^) range per semver spec: ^X.Y.Z where X > 0 → >=X.Y.Z <(X+1).0.0 ^0.Y.Z where Y > 0 → >=0.Y.Z <0.(Y+1).0 ^0.0.Z → >=0.0.Z <0.0.(Z+1).
-
.semver_tilde_bounds(value) ⇒ Object
Returns bounds for tilde (~) range: ~X → >=X.0.0 <(X+1).0.0 ~X.Y → >=X.Y.0 <X.(Y+1).0 ~X.Y.Z → >=X.Y.Z <X.(Y+1).0.
-
.semver_wildcard_bounds(value) ⇒ Object
Returns bounds for wildcard (*) range: X.* or X → >=X.0.0 <(X+1).0.0 X.Y.* → >=X.Y.0 <X.(Y+1).0.
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
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
379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/posthog/feature_flags.rb', line 379 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
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 |
# File 'lib/posthog/feature_flags.rb', line 631 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
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
# File 'lib/posthog/feature_flags.rb', line 522 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 when 'semver_eq', 'semver_neq', 'semver_gt', 'semver_gte', 'semver_lt', 'semver_lte' override_parsed = parse_semver(override_value) flag_parsed = parse_semver(value) case operator when 'semver_eq' override_parsed == flag_parsed when 'semver_neq' override_parsed != flag_parsed when 'semver_gt' (override_parsed <=> flag_parsed) == 1 when 'semver_gte' (override_parsed <=> flag_parsed) >= 0 when 'semver_lt' (override_parsed <=> flag_parsed) == -1 when 'semver_lte' (override_parsed <=> flag_parsed) <= 0 end when 'semver_tilde' override_parsed = parse_semver(override_value) lower, upper = semver_tilde_bounds(value) (override_parsed <=> lower) >= 0 && (override_parsed <=> upper) == -1 when 'semver_caret' override_parsed = parse_semver(override_value) lower, upper = semver_caret_bounds(value) (override_parsed <=> lower) >= 0 && (override_parsed <=> upper) == -1 when 'semver_wildcard' override_parsed = parse_semver(override_value) lower, upper = semver_wildcard_bounds(value) (override_parsed <=> lower) >= 0 && (override_parsed <=> upper) == -1 else raise InconclusiveMatchError, "Unknown operator: #{operator}" end end |
.match_property_group(property_group, property_values, cohort_properties) ⇒ Object
652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
# File 'lib/posthog/feature_flags.rb', line 652 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
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 |
# File 'lib/posthog/feature_flags.rb', line 830 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 |
.parse_semver(value) ⇒ Object
Parse a semver string into a comparable [major, minor, patch] integer array. Handles v-prefix, whitespace, pre-release suffixes. Defaults missing components to 0.
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/posthog/feature_flags.rb', line 427 def self.parse_semver(value) raise InconclusiveMatchError, 'Invalid semver format' if value.nil? text = value.to_s.strip.sub(/^[vV]/, '') raise InconclusiveMatchError, 'Invalid semver format' if text.empty? # Strip pre-release and build metadata suffixes text = text.split('-')[0].split('+')[0] parts = text.split('.') raise InconclusiveMatchError, 'Invalid semver format' if parts.empty? || parts[0].to_s.empty? # Check for leading dot or non-numeric parts parts.each do |part| raise InconclusiveMatchError, 'Invalid semver format' if part.empty? || part !~ /^\d+$/ end major = parts[0].to_i minor = parts.length > 1 ? parts[1].to_i : 0 patch = parts.length > 2 ? parts[2].to_i : 0 [major, minor, patch] end |
.relative_date_parse_for_feature_flag_matching(value) ⇒ Object
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/posthog/feature_flags.rb', line 394 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 |
.semver_caret_bounds(value) ⇒ Object
Returns bounds for caret (^) range per semver spec: ^X.Y.Z where X > 0 → >=X.Y.Z <(X+1).0.0 ^0.Y.Z where Y > 0 → >=0.Y.Z <0.(Y+1).0 ^0.0.Z → >=0.0.Z <0.0.(Z+1)
480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/posthog/feature_flags.rb', line 480 def self.semver_caret_bounds(value) major, minor, patch = parse_semver(value) lower = [major, minor, patch] upper = if major.positive? [major + 1, 0, 0] elsif minor.positive? [0, minor + 1, 0] else [0, 0, patch + 1] end [lower, upper] end |
.semver_tilde_bounds(value) ⇒ Object
Returns bounds for tilde (~) range: ~X → >=X.0.0 <(X+1).0.0 ~X.Y → >=X.Y.0 <X.(Y+1).0 ~X.Y.Z → >=X.Y.Z <X.(Y+1).0
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/posthog/feature_flags.rb', line 456 def self.semver_tilde_bounds(value) major, minor, patch = parse_semver(value) lower = [major, minor, patch] # Determine how many components were provided text = value.to_s.strip.sub(/^[vV]/, '') text = text.split('-')[0].split('+')[0] component_count = text.split('.').length upper = if component_count == 1 # Major-only: bump major [major + 1, 0, 0] else # Major.minor or major.minor.patch: bump minor [major, minor + 1, 0] end [lower, upper] end |
.semver_wildcard_bounds(value) ⇒ Object
Returns bounds for wildcard (*) range: X.* or X → >=X.0.0 <(X+1).0.0 X.Y.* → >=X.Y.0 <X.(Y+1).0
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'lib/posthog/feature_flags.rb', line 498 def self.semver_wildcard_bounds(value) cleaned = value.to_s.strip.sub(/^[vV]/, '').gsub('*', '').chomp('.') parts = cleaned.split('.').reject(&:empty?) raise InconclusiveMatchError, 'Invalid semver wildcard format' if parts.empty? parts.each do |part| raise InconclusiveMatchError, 'Invalid semver wildcard format' if part !~ /^\d+$/ end major = parts[0].to_i case parts.length when 1 [[major, 0, 0], [major + 1, 0, 0]] when 2 minor = parts[1].to_i [[major, minor, 0], [major, minor + 1, 0]] else minor = parts[1].to_i patch = parts[2].to_i [[major, minor, patch], [major, minor, patch + 1]] end end |
Instance Method Details
#_compute_flag_locally(flag, distinct_id, groups = {}, person_properties = {}, group_properties = {}) ⇒ Object
864 865 866 867 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 |
# File 'lib/posthog/feature_flags.rb', line 864 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
903 904 905 906 907 908 909 910 911 912 913 |
# File 'lib/posthog/feature_flags.rb', line 903 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
981 982 983 984 |
# File 'lib/posthog/feature_flags.rb', line 981 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
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 |
# File 'lib/posthog/feature_flags.rb', line 1008 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
1140 1141 1142 |
# File 'lib/posthog/feature_flags.rb', line 1140 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
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 |
# File 'lib/posthog/feature_flags.rb', line 1089 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
1058 1059 1060 1061 1062 1063 1064 1065 1066 |
# File 'lib/posthog/feature_flags.rb', line 1058 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
1068 1069 1070 1071 1072 1073 1074 1075 1076 |
# File 'lib/posthog/feature_flags.rb', line 1068 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
1078 1079 1080 1081 1082 1083 1084 1085 1086 |
# File 'lib/posthog/feature_flags.rb', line 1078 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
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 |
# File 'lib/posthog/feature_flags.rb', line 955 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.
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 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 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 |
# File 'lib/posthog/feature_flags.rb', line 747 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
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/posthog/feature_flags.rb', line 238 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
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 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/posthog/feature_flags.rb', line 262 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 server_flags = stringify_keys(flags_and_payloads[:featureFlags] || {}) server_payloads = stringify_keys(flags_and_payloads[:featureFlagPayloads] || {}) if errors_while_computing # Merge server results into locally-evaluated results so that flags which # failed server-side evaluation (already filtered out by get_flags) don't # overwrite valid locally-evaluated values. flags = stringify_keys(flags).merge(server_flags) payloads = stringify_keys(payloads).merge(server_payloads) else flags = server_flags payloads = server_payloads end 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
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 235 236 |
# File 'lib/posthog/feature_flags.rb', line 154 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
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/posthog/feature_flags.rb', line 345 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 147 148 |
# 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 # Filter out flags that failed evaluation to avoid overwriting cached values successful_flags = flags_hash.reject { |_, flag| flag.failed == true } flags_response[:featureFlags] = successful_flags.transform_values(&:get_value).transform_keys(&:to_sym) flags_response[:featureFlagPayloads] = successful_flags.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
986 987 988 989 990 991 992 |
# File 'lib/posthog/feature_flags.rb', line 986 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
150 151 152 |
# File 'lib/posthog/feature_flags.rb', line 150 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
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 |
# File 'lib/posthog/feature_flags.rb', line 915 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
373 374 375 |
# File 'lib/posthog/feature_flags.rb', line 373 def shutdown_poller @task.shutdown end |
#variant_lookup_table(flag) ⇒ Object
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 |
# File 'lib/posthog/feature_flags.rb', line 994 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 |