Class: Datadog::OpenFeature::Provider

Inherits:
Object
  • Object
show all
Includes:
OpenFeature::SDK::Provider::EventEmitter
Defined in:
lib/datadog/open_feature/provider.rb

Overview

OpenFeature feature flagging provider backed by Datadog configuration delivery.

Requires openfeature-sdk >= 0.5.1: exposures, evaluation counts and metrics are sent from provider hooks, and earlier releases run no hooks.

Hook lifecycle note: FlagEvalEVPHook is returned from #hooks so EVP uses the SDK-final EvaluationDetails. This matches FlagEvalMetricsHook and records defaults/errors produced by OpenFeature hook failures or post-provider type validation.

Implementation follows the OpenFeature contract of Provider SDK. For details see:

- https://github.com/open-feature/ruby-sdk/blob/v0.4.1/README.md#develop-a-provider
- https://github.com/open-feature/ruby-sdk/blob/v0.4.1/lib/open_feature/sdk/provider/no_op_provider.rb

In the example below you can see how to configure the OpenFeature SDK https://github.com/open-feature/ruby-sdk to use the Datadog feature flags provider.

Example:

Feature Flags use agentless configuration delivery by default. To use
Remote Configuration delivery instead, configure the source explicitly.

```ruby
# FILE: initializers/datadog.rb
Datadog.configure do |config|
config.remote.enabled = true
config.feature_flags.configuration_source = "remote_config"
end
```

And configure the OpenFeature SDK to use the Datadog feature flagging provider.

```ruby
# FILE: initializers/open_feature.rb
require 'open_feature/sdk'
require 'datadog/open_feature/provider'

OpenFeature::SDK.configure do |config|
config.set_provider(Datadog::OpenFeature::Provider.new)
end
```

Now you can create OpenFeature SDK client and use it to fetch feature flag values.

```ruby
client = OpenFeature::SDK.build_client
context = OpenFeature::SDK::EvaluationContext.new('email' => '[email protected]')

client.fetch_string_value(
flag_key: 'banner', default_value: 'Greetings!', evaluation_context: context
)
# => 'Welcome back!'
```

Constant Summary collapse

NAME =
Ext::PROVIDER_NAME
INITIALIZATION_CANCELLED_MESSAGE =
"Feature Flags provider initialization was cancelled by shutdown"
HOOKS_UNSUPPORTED_MESSAGE =
"Feature Flags: the installed openfeature-sdk does not run provider hooks, " \
"so Datadog exposures and evaluation counts are not sent; upgrade openfeature-sdk to 0.5.1 or later"
HOOKS_UNSUPPORTED_WARNING =
Core::Utils::OnlyOnce.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(experimentation_opt_out_mode: nil, enable_evaluation_telemetry_opt_out_parameter: nil) ⇒ Provider

Returns a new instance of Provider.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/datadog/open_feature/provider.rb', line 78

def initialize(experimentation_opt_out_mode: nil, enable_evaluation_telemetry_opt_out_parameter: nil)
  mode = experimentation_opt_out_mode
  mode = Datadog.configuration.feature_flags.experimentation_opt_out_mode if mode.nil?
  # Preserve the requested policy if this provider is activated after configuration changes.
  @requested_experimentation_opt_out_mode = mode.to_s.dup.freeze
  @experimentation_opt_out_mode = if OpenFeature.enabled? && Core::LIBDATADOG_API_FAILURE.nil?
    resolve_experimentation_opt_out_mode(@requested_experimentation_opt_out_mode)
  end
  @enable_evaluation_telemetry_opt_out_parameter = case enable_evaluation_telemetry_opt_out_parameter
  when nil
    Datadog.configuration.feature_flags.evaluation_telemetry_per_subject_opt_out_enabled
  when true, false
    enable_evaluation_telemetry_opt_out_parameter
  else
    raise ArgumentError, "enable_evaluation_telemetry_opt_out_parameter expects true, false, or nil"
  end
  @metadata = ::OpenFeature::SDK::Provider::ProviderMetadata.new(name: NAME).freeze
  @initialization_mutex = Mutex.new
  @initializing = false
  @initialization_thread = nil
  @awaiting_ready_after_fork = false
  @initialization_failed = false
  @provider_error_observed = false
  @ready_pending = false
  @ready_dispatch_claimed = false
  @stale_emitted = false
  @stale_pending = false
  @error_handler = nil
  @initialization_ready_handler = nil
  @waiting_component = nil
  @shutdown = false
end

Instance Attribute Details

#metadata ⇒ Object (readonly)

Returns the value of attribute metadata.



76
77
78
# File 'lib/datadog/open_feature/provider.rb', line 76

def 
  @metadata
end

Instance Method Details

#fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil) ⇒ Object



209
210
211
# File 'lib/datadog/open_feature/provider.rb', line 209

def fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil)
  evaluate(flag_key, default_value: default_value, expected_type: :boolean, evaluation_context: evaluation_context)
end

#fetch_float_value(flag_key:, default_value:, evaluation_context: nil) ⇒ Object



225
226
227
# File 'lib/datadog/open_feature/provider.rb', line 225

def fetch_float_value(flag_key:, default_value:, evaluation_context: nil)
  evaluate(flag_key, default_value: default_value, expected_type: :float, evaluation_context: evaluation_context)
end

#fetch_integer_value(flag_key:, default_value:, evaluation_context: nil) ⇒ Object



221
222
223
# File 'lib/datadog/open_feature/provider.rb', line 221

def fetch_integer_value(flag_key:, default_value:, evaluation_context: nil)
  evaluate(flag_key, default_value: default_value, expected_type: :integer, evaluation_context: evaluation_context)
end

#fetch_number_value(flag_key:, default_value:, evaluation_context: nil) ⇒ Object



217
218
219
# File 'lib/datadog/open_feature/provider.rb', line 217

def fetch_number_value(flag_key:, default_value:, evaluation_context: nil)
  evaluate(flag_key, default_value: default_value, expected_type: :number, evaluation_context: evaluation_context)
end

#fetch_object_value(flag_key:, default_value:, evaluation_context: nil) ⇒ Object



229
230
231
# File 'lib/datadog/open_feature/provider.rb', line 229

def fetch_object_value(flag_key:, default_value:, evaluation_context: nil)
  evaluate(flag_key, default_value: default_value, expected_type: :object, evaluation_context: evaluation_context)
end

#fetch_string_value(flag_key:, default_value:, evaluation_context: nil) ⇒ Object



213
214
215
# File 'lib/datadog/open_feature/provider.rb', line 213

def fetch_string_value(flag_key:, default_value:, evaluation_context: nil)
  evaluate(flag_key, default_value: default_value, expected_type: :string, evaluation_context: evaluation_context)
end

#hooks ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/datadog/open_feature/provider.rb', line 196

def hooks
  component = Datadog.send(:components).open_feature
  telemetry_hooks = [
    component&.flag_eval_metrics_hook,
    component&.flag_eval_evp_hook,
  ].compact
  if @enable_evaluation_telemetry_opt_out_parameter
    telemetry_hooks = telemetry_hooks.map { |hook| Hooks::EvaluationTelemetryOptOutHook.new(hook) }
  end

  [component&.exposure_logging_hook, *telemetry_hooks, component&.debug_logging_hook].compact
end

#init ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/datadog/open_feature/provider.rb', line 111

def init
  warn_if_hooks_unsupported
  @initialization_mutex.synchronize do
    raise INITIALIZATION_CANCELLED_MESSAGE if @shutdown

    @initializing = true
    @initialization_thread = Thread.current
    @awaiting_ready_after_fork = false
    @initialization_failed = false
    @provider_error_observed = false
    @ready_pending = false
    @ready_dispatch_claimed = false
    @stale_emitted = false
    @stale_pending = false
  end
  Datadog.logger.debug { "Feature Flags: provider initialization started" }
  ready_handler_installed = install_initialization_ready_handler

  component, failure = OpenFeature.activate_provider(self)
  cancel_initialization_if_shutdown!
  fail_initialization(component, failure || "Feature Flags component could not be activated") unless component

  @initialization_mutex.synchronize do
    raise INITIALIZATION_CANCELLED_MESSAGE if @shutdown

    @waiting_component = component
  end
  # Shutdown publishes this flag through the component lock; taking our mutex here would invert lock order.
  wait_result = component.wait_for_configuration { @shutdown }
  cancel_initialization_if_shutdown!

  case wait_result
  when Component::CONFIGURATION_READY
    @initialization_mutex.synchronize { @initializing = false } unless ready_handler_installed
    Datadog.logger.debug { "Feature Flags: provider initialized successfully" }
  when Component::CONFIGURATION_TIMEOUT
    message = "Feature Flags provider initialization timed out while waiting for configuration"
    Datadog.logger.error(message)
    fail_initialization(component, message)
  else
    fail_initialization(component, "Feature Flags provider initialization stopped before configuration arrived")
  end
rescue
  prepare_initialization_failure(component) if @initialization_mutex.synchronize { @initializing }
  raise
ensure
  @initialization_mutex.synchronize do
    @waiting_component = nil
    # The SDK still owes READY after init returns; the initializing thread owns that dispatch.
    @initialization_thread = nil unless @initializing
  end
end

#shutdown ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/datadog/open_feature/provider.rb', line 164

def shutdown
  configuration = @configuration
  # @type var error_handler: Provider::provider_event_handler?
  error_handler = nil
  # @type var initialization_ready_handler: Provider::provider_event_handler?
  initialization_ready_handler = nil
  waiting_component = nil #: Component?
  @initialization_mutex.synchronize do
    return if @shutdown

    @shutdown = true
    @initializing = false
    @initialization_thread = nil
    error_handler = @error_handler
    initialization_ready_handler = @initialization_ready_handler
    waiting_component = @waiting_component
    @error_handler = nil
    @initialization_ready_handler = nil
  end

  waiting_component&.wake_configuration_waiters
  configuration&.remove_handler(::OpenFeature::SDK::ProviderEvent::PROVIDER_ERROR, error_handler) if error_handler
  if initialization_ready_handler
    configuration&.remove_handler(
      ::OpenFeature::SDK::ProviderEvent::PROVIDER_READY,
      initialization_ready_handler,
    )
  end
  OpenFeature.deactivate_provider(self)
  Datadog.logger.debug { "Feature Flags: provider shut down" }
end