diff --git a/README.md b/README.md index debd045..299d147 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,7 @@ Evaluations include flag metadata for the LaunchDarkly specific parts of the eva |---------------------|---------|--------------------------------------------------------------------| | `variationIndex` | integer | The index of the returned variation. Absent for default values. | | `inExperiment` | boolean | Present, and `true`, when the evaluation was part of an experiment. | +| `overrideAffected` | boolean | Present, and `true`, when a flag override affected the evaluation. Requires a LaunchDarkly Ruby SDK version with flag overrides. | | `ruleIndex` | integer | The index of the rule that matched. | | `ruleId` | string | The identifier of the rule that matched. | | `prerequisiteKey` | string | The key of the prerequisite flag that failed. | diff --git a/lib/ldclient-openfeature/impl/details_converter.rb b/lib/ldclient-openfeature/impl/details_converter.rb index 353020b..1c507df 100644 --- a/lib/ldclient-openfeature/impl/details_converter.rb +++ b/lib/ldclient-openfeature/impl/details_converter.rb @@ -9,13 +9,14 @@ module Impl class ResolutionDetailsConverter VARIATION_INDEX_KEY = 'variationIndex' IN_EXPERIMENT_KEY = 'inExperiment' + OVERRIDE_AFFECTED_KEY = 'overrideAffected' RULE_INDEX_KEY = 'ruleIndex' RULE_ID_KEY = 'ruleId' PREREQUISITE_KEY_KEY = 'prerequisiteKey' BIG_SEGMENTS_STATUS_KEY = 'bigSegmentsStatus' - private_constant :VARIATION_INDEX_KEY, :IN_EXPERIMENT_KEY, :RULE_INDEX_KEY, :RULE_ID_KEY, - :PREREQUISITE_KEY_KEY, :BIG_SEGMENTS_STATUS_KEY + private_constant :VARIATION_INDEX_KEY, :IN_EXPERIMENT_KEY, :OVERRIDE_AFFECTED_KEY, :RULE_INDEX_KEY, + :RULE_ID_KEY, :PREREQUISITE_KEY_KEY, :BIG_SEGMENTS_STATUS_KEY # # @param detail [LaunchDarkly::EvaluationDetail] @@ -61,6 +62,8 @@ def to_resolution_details(detail) metadata[VARIATION_INDEX_KEY] = variation_index unless variation_index.nil? metadata[IN_EXPERIMENT_KEY] = true if reason.in_experiment + # Only LaunchDarkly Ruby SDK versions with flag overrides define override_affected. + metadata[OVERRIDE_AFFECTED_KEY] = true if reason.respond_to?(:override_affected) && reason.override_affected metadata[RULE_INDEX_KEY] = reason.rule_index unless reason.rule_index.nil? metadata[RULE_ID_KEY] = reason.rule_id unless reason.rule_id.nil? metadata[PREREQUISITE_KEY_KEY] = reason.prerequisite_key unless reason.prerequisite_key.nil? diff --git a/spec/impl/details_converter_spec.rb b/spec/impl/details_converter_spec.rb index c4914f8..cdd5930 100644 --- a/spec/impl/details_converter_spec.rb +++ b/spec/impl/details_converter_spec.rb @@ -63,6 +63,31 @@ expect(resolution_details.flag_metadata).not_to have_key('inExperiment') end + it "includes override affected in the flag metadata for override-affected evaluations" do + reason = LaunchDarkly::EvaluationReason::rule_match(2, 'the-rule-id', false) + allow(reason).to receive(:override_affected).and_return(true) + detail = LaunchDarkly::EvaluationDetail.new(true, 1, reason) + resolution_details = details_converter.to_resolution_details(detail) + + expect(resolution_details.flag_metadata).to include('overrideAffected' => true) + end + + it "omits override affected from the flag metadata for evaluations not affected by an override" do + reason = LaunchDarkly::EvaluationReason::rule_match(2, 'the-rule-id', false) + allow(reason).to receive(:override_affected).and_return(false) + detail = LaunchDarkly::EvaluationDetail.new(true, 1, reason) + resolution_details = details_converter.to_resolution_details(detail) + + expect(resolution_details.flag_metadata).not_to have_key('overrideAffected') + end + + it "omits override affected from the flag metadata when the LaunchDarkly SDK does not support overrides" do + detail = LaunchDarkly::EvaluationDetail.new(true, 1, LaunchDarkly::EvaluationReason::fallthrough(false)) + resolution_details = details_converter.to_resolution_details(detail) + + expect(resolution_details.flag_metadata).not_to have_key('overrideAffected') + end + it "includes the rule in the flag metadata for rule matches" do detail = LaunchDarkly::EvaluationDetail.new(true, 1, LaunchDarkly::EvaluationReason::rule_match(2, 'the-rule-id', false)) resolution_details = details_converter.to_resolution_details(detail)