Class: PostHog::FeatureFlagsPoller

Inherits:
Object
  • Object
show all
Includes:
Logging, Utils
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

Instance Method Summary collapse

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

included, #logger

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



340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/posthog/feature_flags.rb', line 340

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



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/posthog/feature_flags.rb', line 465

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



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
411
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
# File 'lib/posthog/feature_flags.rb', line 386

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



486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/posthog/feature_flags.rb', line 486

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



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
689
690
691
692
693
# File 'lib/posthog/feature_flags.rb', line 664

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



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/posthog/feature_flags.rb', line 355

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



698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# File 'lib/posthog/feature_flags.rb', line 698

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



737
738
739
740
741
742
743
744
745
746
747
# File 'lib/posthog/feature_flags.rb', line 737

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



815
816
817
818
# File 'lib/posthog/feature_flags.rb', line 815

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_flagsObject



842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
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
# File 'lib/posthog/feature_flags.rb', line 842

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



974
975
976
# File 'lib/posthog/feature_flags.rb', line 974

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



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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# File 'lib/posthog/feature_flags.rb', line 923

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



892
893
894
895
896
897
898
899
900
# File 'lib/posthog/feature_flags.rb', line 892

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



902
903
904
905
906
907
908
909
910
# File 'lib/posthog/feature_flags.rb', line 902

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



912
913
914
915
916
917
918
919
920
# File 'lib/posthog/feature_flags.rb', line 912

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



789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
# File 'lib/posthog/feature_flags.rb', line 789

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.

Parameters:

  • property (Hash)

    Flag property with type=“flag” and dependency_chain

  • evaluation_cache (Hash)

    Cache for storing evaluation results

  • distinct_id (String)

    The distinct ID being evaluated

  • properties (Hash)

    Person properties for evaluation

  • cohort_properties (Hash)

    Cohort properties for evaluation

Returns:

  • (Boolean)

    True if all dependencies in the chain evaluate to true, false otherwise



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
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
# File 'lib/posthog/feature_flags.rb', line 581

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



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/posthog/feature_flags.rb', line 221

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



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
# File 'lib/posthog/feature_flags.rb', line 245

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

  if fallback_to_server && !only_evaluate_locally
    begin
      flags_and_payloads = get_flags(distinct_id, groups, person_properties, group_properties)

      unless flags_and_payloads.key?(:featureFlags)
        raise StandardError, "Error flags response: #{flags_and_payloads}"
      end

      # Check if feature_flags are quota limited
      if flags_and_payloads[:quotaLimited]&.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] || {})
        request_id = flags_and_payloads[:requestId]
        evaluated_at = flags_and_payloads[:evaluatedAt]
      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
  }
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
# 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
  feature_flag = nil

  @feature_flags.each do |flag|
    if key == flag[:key]
      feature_flag = flag
      break
    end
  end

  unless feature_flag.nil?
    begin
      response = _compute_flag_locally(feature_flag, distinct_id, groups, person_properties, group_properties)
      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

  if !flag_was_locally_evaluated && !only_evaluate_locally
    begin
      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] || {})
        request_id = flags_data[:requestId]
        evaluated_at = flags_data[:evaluatedAt]
      else
        logger.debug "Missing feature flags key: #{flags_data.to_json}"
        flags = {}
      end

      response = flags[key]
      response = false if response.nil?
      logger.debug "Successfully computed flag remotely: #{key} -> #{response}"
    rescue StandardError => e
      @on_error.call(-1, "Error computing flag remotely: #{e}. #{e.backtrace.join("\n")}")
    end
  end

  [response, flag_was_locally_evaluated, request_id, evaluated_at]
end

#get_feature_flag_payload(key, distinct_id, match_value = nil, groups = {}, person_properties = {}, group_properties = {}, only_evaluate_locally = false) ⇒ Object



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
# File 'lib/posthog/feature_flags.rb', line 306

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



820
821
822
823
824
825
826
# File 'lib/posthog/feature_flags.rb', line 820

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



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
# File 'lib/posthog/feature_flags.rb', line 749

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_pollerObject



334
335
336
# File 'lib/posthog/feature_flags.rb', line 334

def shutdown_poller
  @task.shutdown
end

#variant_lookup_table(flag) ⇒ Object



828
829
830
831
832
833
834
835
836
837
838
839
840
# File 'lib/posthog/feature_flags.rb', line 828

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