Repository navigation
feat(flags): support known-absent property keys in local evaluation #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
9a14431
000e694
e87f1a0
5e6534e
4a9350f
65ba5e8
1239d3f
46c551d
531bc1a
56990d5
bffa4f7
8e398d5
2b8680f
9b38999
bbc33a7
d067af4
ff7a55c
bc4f65b
02dddc8
df5c6ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "posthog-ruby": patch | ||
| --- | ||
|
|
||
| Local evaluation now evaluates a flag dependency like a standalone flag, with its own aggregation, properties and bucketing identifier. A group-aggregated dependency reads its group's properties and is `false` when that group isn't passed, matching `/flags`. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "posthog-ruby": minor | ||
| --- | ||
|
|
||
| Add `person_property_keys_known_absent:` and `group_property_keys_known_absent:` to flag evaluation methods and `SendFeatureFlagsOptions`. A listed key is treated as absent during local evaluation, even when a value for it is passed, so `is_not_set` and the negated operators match and other operators don't, without a remote request. Fallback `/flags` requests carry the same keys. Each list allows at most 1000 keys, per group type for groups. A longer list raises `ArgumentError`, except on `capture(send_feature_flags:)`, which logs a warning and captures the event without flags. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[nit] "Without a remote request" is a bit strong here. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "posthog-ruby": patch | ||
| --- | ||
|
|
||
| Local flag evaluation now skips a group condition without a usable group key one condition at a time, as the server does. A group flag with a person condition (`aggregation_group_type_index: null`) is no longer `false` without the group key: the person condition still decides, also when the flag is a dependency. A group flag with a holdout that excludes anyone and no usable group key is now unresolved with `missing_context` and falls back to `/flags`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "posthog-ruby": patch | ||
| --- | ||
|
|
||
| Treat an empty, `nil`, boolean, array, or hash group key as no group key during local flag evaluation, as the server does. A condition aggregated on that group type is skipped, and a flag whose conditions are all skipped resolves to `false` locally instead of bucketing on the key's string form. |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module PostHog | ||
| # Person and group property keys the caller knows are absent for one flag evaluation. | ||
| # | ||
| # @api private | ||
| class KnownAbsentPropertyKeys | ||
| # The flags service rejects a longer list, counting duplicates, per group type for groups. | ||
| MAX_KEYS = 1000 | ||
| PERSON_FIELD = 'person_property_keys_known_absent' | ||
| GROUP_FIELD = 'group_property_keys_known_absent' | ||
| # Persons-table fields such as `created_at` use this prefix and are never hidden by known absence. | ||
| PERSON_METADATA_PREFIX = '__posthog_person_metadata__' | ||
| # A listed key matches these operators and no others. | ||
| MATCHING_OPERATORS = %w[is_not_set is_not not_icontains not_starts_with not_ends_with not_regex].freeze | ||
|
|
||
| # @return [Set<String>] Listed person keys used for local matching. | ||
| attr_reader :person_keys | ||
|
|
||
| # Validates and normalizes the public inputs. | ||
| # | ||
| # @param person_property_keys_known_absent [Array<String, Symbol>, Set<String, Symbol>, nil] | ||
| # @param group_property_keys_known_absent [Hash{String, Symbol => Array<String, Symbol>, Set<String, Symbol>}, nil] | ||
| # @return [PostHog::KnownAbsentPropertyKeys, nil] nil when neither input was provided. | ||
| # @raise [ArgumentError] when an input has the wrong type or a list holds more than {MAX_KEYS} keys. | ||
| def self.build(person_property_keys_known_absent, group_property_keys_known_absent) | ||
| return nil if person_property_keys_known_absent.nil? && group_property_keys_known_absent.nil? | ||
|
|
||
| person = if person_property_keys_known_absent.nil? | ||
| [] | ||
| else | ||
| validate_keys(person_property_keys_known_absent, PERSON_FIELD) | ||
| end | ||
|
|
||
| groups = {} | ||
| unless group_property_keys_known_absent.nil? | ||
| unless group_property_keys_known_absent.is_a?(Hash) | ||
| raise ArgumentError, "#{GROUP_FIELD} must be a Hash of group type to property keys" | ||
| end | ||
|
|
||
| group_property_keys_known_absent.each do |group_type, keys| | ||
| unless group_type.is_a?(String) || group_type.is_a?(Symbol) | ||
| raise ArgumentError, "#{GROUP_FIELD} must use String or Symbol group types" | ||
| end | ||
|
|
||
| field = "#{GROUP_FIELD}[#{group_type}]" | ||
| # A String and a Symbol can name the same group type, so their keys are merged before the limit check. | ||
| merged = (groups[group_type.to_s] || []) + validate_keys(keys, field) | ||
| raise ArgumentError, "#{field} allows at most #{MAX_KEYS} keys, got #{merged.size}" if merged.size > MAX_KEYS | ||
|
|
||
| groups[group_type.to_s] = merged | ||
| end | ||
| end | ||
|
|
||
| new(person, groups) | ||
| end | ||
|
|
||
| def self.validate_keys(keys, field) | ||
| raise ArgumentError, "#{field} must be an Array of property keys" unless keys.is_a?(Array) || keys.is_a?(Set) | ||
| raise ArgumentError, "#{field} allows at most #{MAX_KEYS} keys, got #{keys.size}" if keys.size > MAX_KEYS | ||
|
|
||
| keys.map do |key| | ||
| unless key.is_a?(String) || key.is_a?(Symbol) | ||
| raise ArgumentError, "#{field} must contain only String or Symbol keys" | ||
| end | ||
|
|
||
| key.to_s | ||
| end | ||
| end | ||
| private_class_method :validate_keys | ||
|
|
||
| def initialize(person, groups) | ||
| @person = person | ||
| @groups = groups | ||
| @person_keys = person.reject { |key| key.start_with?(PERSON_METADATA_PREFIX) }.to_set.freeze | ||
| @group_keys = groups.to_h { |group_type, keys| [group_type, keys.to_set.freeze] }.freeze | ||
| end | ||
|
|
||
| # @param group_type [String, Symbol] | ||
| # @return [Set<String>, nil] Listed keys for that group type only. | ||
| def group_keys(group_type) | ||
| @group_keys[group_type.to_s] | ||
| end | ||
|
|
||
| # @return [Scope] The listed person keys, for filters with `type: "person"`. | ||
| def person_scope | ||
| Scope.new(person_keys, nil) | ||
| end | ||
|
|
||
| # @param group_type [String, Symbol] | ||
| # @param group_type_index [Integer, String] The index that group_type maps to. | ||
| # @return [Scope] The listed keys for that group type, for group filters of that group type. | ||
| def group_scope(group_type, group_type_index) | ||
| Scope.new(group_keys(group_type), group_type_index) | ||
| end | ||
|
|
||
| # Listed keys for one evaluation scope: person properties, or the properties of one group type. | ||
| # Any other filter type, such as `person_metadata`, never resolves from a listing. | ||
| class Scope | ||
| # @return [Integer, String, nil] The group type index, or nil for the person scope. | ||
| attr_reader :group_type_index | ||
|
|
||
| def initialize(keys, group_type_index) | ||
| @keys = keys | ||
| @group_type_index = group_type_index | ||
| end | ||
|
|
||
| # @param aggregation_group_type_index [Integer, String, nil] A flag's aggregation, nil for persons. | ||
| def aggregation?(aggregation_group_type_index) | ||
| aggregation_group_type_index.to_s == @group_type_index.to_s | ||
| end | ||
|
|
||
| # @param property [Hash] A property filter. | ||
| # @return [Boolean] Whether the filter's key is listed for this scope and the filter reads this scope. | ||
| def covers?(property) | ||
| return false unless @keys&.include?((property[:key] || property['key']).to_s) | ||
|
|
||
| type = property[:type] || property['type'] | ||
| return type == 'person' if @group_type_index.nil? | ||
|
|
||
| filter_group_type_index = property[:group_type_index] || property['group_type_index'] | ||
| # Like the flags service, a group filter without its own index reads the scope it is evaluated in. | ||
| type == 'group' && (filter_group_type_index.nil? || aggregation?(filter_group_type_index)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] When a group filter's own posthog-rs reads that group type's properties and listing in feature_flags.rs#L1247 (PostHog/posthog-rs#388), and go and dotnet do the same. Python (PostHog/posthog-python#1280), php (PostHog/posthog-php#328) and js (PostHog/posthog-js#5819) still read the condition's group. Would it be worth routing that filter to its own group type's properties and known-absent keys? |
||
| end | ||
| end | ||
|
|
||
| # @return [Hash] The `/flags` request body fields, omitting a field with nothing listed. | ||
| def request_fields | ||
| fields = {} | ||
| fields[PERSON_FIELD.to_sym] = @person unless @person.empty? | ||
| groups = @groups.reject { |_group_type, keys| keys.empty? } | ||
| fields[GROUP_FIELD.to_sym] = groups unless groups.empty? | ||
| fields | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] This reads as if a group-aggregated dependency is always
falsewithout its group. Conditions are skipped one at a time though, so a person condition (aggregation_group_type_index: null) in that dependency still decides, which is what.changeset/group-condition-skip-per-condition.mdsays. Maybe reword to "its conditions on a group that isn't passed are skipped" so the two entries don't contradict each other?