Class: Datadog::OpenFeature::EvaluationEngine

Inherits:
Object
  • Object
show all
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
RAW_DATA_WARNING =
"Feature Flags: debug logging is enabled, so feature flag logs include raw evaluation data " \
"(targeting keys, evaluation context attributes, flag values, and default values) that may contain " \
"personal or sensitive information. Unset DD_TRACE_DEBUG when you no longer need it."

Instance Method Summary collapse

Constructor Details

#initialize(telemetry:, logger:) ⇒ EvaluationEngine

Returns a new instance of EvaluationEngine.



24
25
26
27
28
29
30
# File 'lib/datadog/open_feature/evaluation_engine.rb', line 24

def initialize(telemetry:, logger:)
  @telemetry = telemetry
  @logger = logger
  @raw_data_warning = Core::Utils::OnlyOnce.new

  @evaluator = NoopEvaluator.new(nil)
end

Instance Method Details

#fetch_value(flag_key, default_value:, expected_type:, evaluation_context: nil, experimentation_opt_out_mode: :disabled) ⇒ Object



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
63
64
65
66
67
68
69
70
71
72
# File 'lib/datadog/open_feature/evaluation_engine.rb', line 32

def fetch_value(
  flag_key, default_value:, expected_type:, evaluation_context: nil, experimentation_opt_out_mode: :disabled
)
  # 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)
    message = "unknown type #{expected_type.inspect}, allowed types #{ALLOWED_TYPES.join(", ")}"
    result = ResolutionDetails.build_error(
      value: default_value, error_code: Ext::UNKNOWN_TYPE, error_message: 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,
    experimentation_opt_out_mode: experimentation_opt_out_mode,
  )

  result = with_observe_full_evaluation_data(result, observe_full_evaluation_data)
  log_resolution(flag_key, expected_type, result)

  result
rescue => e
  @telemetry.report(e, description: "OpenFeature: Failed to fetch flag value")
  log_resolution_failure(flag_key, e)

  result = ResolutionDetails.build_error(
    value: default_value, error_code: Ext::GENERAL, error_message: "#{e.class}: #{e.message}",
  )
  # `== 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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/datadog/open_feature/evaluation_engine.rb', line 77

def reconfigure!(configuration)
  if configuration.nil?
    @logger.debug("Feature Flags: configuration removed")

    return @evaluator = NoopEvaluator.new(configuration)
  end

  @evaluator = NativeEvaluator.new(configuration)
  @logger.debug { "Feature Flags: configuration applied; #{configuration_summary(configuration)}" }
rescue => e
  message = "OpenFeature: Failed to reconfigure, reverting to the previous configuration"

  @logger.error("#{message}, #{e.class}: #{e.message}")
  @telemetry.report(e, description: "#{message} (#{e.class})")

  raise ReconfigurationError, "#{e.class}: #{e.message}"
end

#warn_raw_data_logging ⇒ Object

Public so Component can warn before handing out DebugLoggingHook, which logs raw data.



96
97
98
99
100
# File 'lib/datadog/open_feature/evaluation_engine.rb', line 96

def warn_raw_data_logging
  return unless @logger.debug?

  @raw_data_warning.run { @logger.warn(RAW_DATA_WARNING) }
end