Class: PostHog::FeatureFlagEvaluations

Inherits:
Object
  • Object
show all
Defined in:
lib/posthog/feature_flag_evaluations.rb

Overview

A snapshot of feature flag evaluations for one distinct_id, returned by PostHog::Client#evaluate_flags. Calls to #is_enabled / #get_flag fire the ‘$feature_flag_called` event (deduped through the existing per-distinct_id cache); #get_flag_payload does not. Pass the snapshot to `capture(flags:)` to attach `$feature/<key>` and `$active_feature_flags` without a second /flags request.

Defined Under Namespace

Classes: EvaluatedFlagRecord, Host

Constant Summary collapse

EVALUATED_LOCALLY_REASON =
'Evaluated locally'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: nil, distinct_id: nil, flags: {}, groups: nil, disable_geoip: nil, request_id: nil, evaluated_at: nil, flag_definitions_loaded_at: nil, errors_while_computing: false, quota_limited: false, accessed: nil) ⇒ FeatureFlagEvaluations

Returns a new instance of FeatureFlagEvaluations.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/posthog/feature_flag_evaluations.rb', line 24

def initialize(
  host: nil,
  distinct_id: nil,
  flags: {},
  groups: nil,
  disable_geoip: nil,
  request_id: nil,
  evaluated_at: nil,
  flag_definitions_loaded_at: nil,
  errors_while_computing: false,
  quota_limited: false,
  accessed: nil
)
  @host = host
  @distinct_id = distinct_id || ''
  @flags = flags || {}
  @groups = groups
  @disable_geoip = disable_geoip
  @request_id = request_id
  @evaluated_at = evaluated_at
  @flag_definitions_loaded_at = flag_definitions_loaded_at
  @errors_while_computing = errors_while_computing
  @quota_limited = quota_limited
  @accessed = Set.new(accessed || [])
end

Instance Attribute Details

#distinct_idObject (readonly)

Returns the value of attribute distinct_id.



22
23
24
# File 'lib/posthog/feature_flag_evaluations.rb', line 22

def distinct_id
  @distinct_id
end

#evaluated_atObject (readonly)

Returns the value of attribute evaluated_at.



22
23
24
# File 'lib/posthog/feature_flag_evaluations.rb', line 22

def evaluated_at
  @evaluated_at
end

#flag_definitions_loaded_atObject (readonly)

Returns the value of attribute flag_definitions_loaded_at.



22
23
24
# File 'lib/posthog/feature_flag_evaluations.rb', line 22

def flag_definitions_loaded_at
  @flag_definitions_loaded_at
end

#groupsObject (readonly)

Returns the value of attribute groups.



22
23
24
# File 'lib/posthog/feature_flag_evaluations.rb', line 22

def groups
  @groups
end

#request_idObject (readonly)

Returns the value of attribute request_id.



22
23
24
# File 'lib/posthog/feature_flag_evaluations.rb', line 22

def request_id
  @request_id
end

Instance Method Details

#_get_event_propertiesObject

Builds the ‘$feature/<key>` and `$active_feature_flags` properties for a captured event. Called from PostHog::Client#capture when `flags:` is set.



95
96
97
98
99
100
101
102
103
104
# File 'lib/posthog/feature_flag_evaluations.rb', line 95

def _get_event_properties
  properties = {}
  active = []
  @flags.each do |key, flag|
    properties["$feature/#{key}"] = flag.enabled ? (flag.variant || true) : false
    active << key if flag.enabled
  end
  properties['$active_feature_flags'] = active.sort unless active.empty?
  properties
end

#enabled?(key) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
# File 'lib/posthog/feature_flag_evaluations.rb', line 54

def enabled?(key)
  key = key.to_s
  flag = @flags[key]
  _record_access(key, flag)
  flag&.enabled ? true : false
end

#get_flag(key) ⇒ Object



61
62
63
64
65
66
# File 'lib/posthog/feature_flag_evaluations.rb', line 61

def get_flag(key)
  key = key.to_s
  flag = @flags[key]
  _record_access(key, flag)
  _flag_value(flag)
end

#get_flag_payload(key) ⇒ Object



68
69
70
71
# File 'lib/posthog/feature_flag_evaluations.rb', line 68

def get_flag_payload(key)
  flag = @flags[key.to_s]
  flag&.payload
end

#keysObject



50
51
52
# File 'lib/posthog/feature_flag_evaluations.rb', line 50

def keys
  @flags.keys
end

#only(keys) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/posthog/feature_flag_evaluations.rb', line 80

def only(keys)
  keys = Array(keys).map(&:to_s)
  missing = keys.reject { |k| @flags.key?(k) }
  unless missing.empty?
    @host.log_warning.call(
      'FeatureFlagEvaluations#only was called with flag keys that are not in the ' \
      "evaluation set and will be dropped: #{missing.join(', ')}"
    )
  end
  filtered = @flags.slice(*keys)
  _clone_with(filtered)
end

#only_accessedObject

Order-dependent: if nothing has been accessed yet, the returned snapshot is empty. The method honors its name — pre-access flags before calling this if you want a populated result.



76
77
78
# File 'lib/posthog/feature_flag_evaluations.rb', line 76

def only_accessed
  _clone_with(@flags.slice(*@accessed))
end