Class: Datadog::OpenFeature::EvaluationEngine
- Inherits:
-
Object
- Object
- Datadog::OpenFeature::EvaluationEngine
- Defined in:
- lib/datadog/open_feature/evaluation_engine.rb
Overview
This class performs the evaluation of the feature flag
Constant Summary collapse
- ReconfigurationError =
Class.new(StandardError)
- ALLOWED_TYPES =
%i[boolean string number float integer object].freeze
Instance Method Summary collapse
- #fetch_value(flag_key, default_value:, expected_type:, evaluation_context: nil) ⇒ Object
-
#initialize(reporter, telemetry:, logger:) ⇒ EvaluationEngine
constructor
A new instance of EvaluationEngine.
-
#reconfigure!(configuration) ⇒ Object
NOTE: In a currect implementation configuration is expected to be a raw JSON string containing feature flags (straight from the remote config) in the format expected by
libdatadogwithout any modifications.
Constructor Details
#initialize(reporter, telemetry:, logger:) ⇒ EvaluationEngine
Returns a new instance of EvaluationEngine.
17 18 19 20 21 22 23 |
# File 'lib/datadog/open_feature/evaluation_engine.rb', line 17 def initialize(reporter, telemetry:, logger:) @reporter = reporter @telemetry = telemetry @logger = logger @evaluator = NoopEvaluator.new(nil) end |
Instance Method Details
#fetch_value(flag_key, default_value:, expected_type:, evaluation_context: nil) ⇒ Object
25 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 62 |
# File 'lib/datadog/open_feature/evaluation_engine.rb', line 25 def fetch_value(flag_key, default_value:, expected_type:, evaluation_context: nil) # Pre-bound to the safe default so the rescue below can still read it if the # evaluator read itself raises. observe_full_evaluation_data = false # Snapshot the evaluator once: a concurrent reconfigure! must not retroactively # apply a different environment's full-evaluation-data policy to this evaluation. evaluator = @evaluator observe_full_evaluation_data = evaluator.observe_full_evaluation_data == true unless ALLOWED_TYPES.include?(expected_type) = "unknown type #{expected_type.inspect}, allowed types #{ALLOWED_TYPES.join(", ")}" result = ResolutionDetails.build_error( value: default_value, error_code: Ext::UNKNOWN_TYPE, error_message: ) return with_observe_full_evaluation_data(result, observe_full_evaluation_data) end context = evaluation_context&.fields.to_h result = evaluator.get_assignment( flag_key, default_value: default_value, context: context, expected_type: expected_type ) result = with_observe_full_evaluation_data(result, observe_full_evaluation_data) @reporter.report(result, flag_key: flag_key, context: evaluation_context) result rescue => e @telemetry.report(e, description: "OpenFeature: Failed to fetch flag value") result = ResolutionDetails.build_error( value: default_value, error_code: Ext::GENERAL, error_message: "#{e.class}: #{e.}" ) # `== true` is load-bearing: Steep types this local as `(nil | bool)` in a rescue # body, and coercing at the use site is what narrows it to `bool`. with_observe_full_evaluation_data(result, observe_full_evaluation_data == true) end |
#reconfigure!(configuration) ⇒ Object
NOTE: In a currect implementation configuration is expected to be a raw
JSON string containing feature flags (straight from the remote config)
in the format expected by libdatadog without any modifications
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/datadog/open_feature/evaluation_engine.rb', line 67 def reconfigure!(configuration) if configuration.nil? @logger.debug("OpenFeature: Removing configuration") return @evaluator = NoopEvaluator.new(configuration) end @evaluator = NativeEvaluator.new(configuration) rescue => e = "OpenFeature: Failed to reconfigure, reverting to the previous configuration" @logger.error("#{}, #{e.class}: #{e.}") @telemetry.report(e, description: "#{} (#{e.class})") raise ReconfigurationError, "#{e.class}: #{e.}" end |