Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

open-feature/ruby-sdk

Repository files navigation

OpenFeature Ruby SDK


OpenFeature is an open specification that provides a vendor-agnostic, community-driven API for feature flagging that works with your favorite feature flag management tool or in-house solution.

Quick start

Requirements

Supported Ruby Version OS
Ruby 3.1.4 Windows, MacOS, Linux
Ruby 3.2.3 Windows, MacOS, Linux
Ruby 3.3.0 Windows, MacOS, Linux

Install

Install the gem and add to the application's Gemfile by executing:

bundle add openfeature-sdk

If bundler is not being used to manage dependencies, install the gem by executing:

gem install openfeature-sdk

Usage

true, "flag2" => 1 } )) end # Create a client client = OpenFeature::SDK.build_client # fetching boolean value feature flag bool_value = client.fetch_boolean_value(flag_key: 'boolean_flag', default_value: false) # a details method is also available for more information about the flag evaluation # see `ResolutionDetails` for more info bool_details = client.fetch_boolean_details(flag_key: 'boolean_flag', default_value: false) # fetching string value feature flag string_value = client.fetch_string_value(flag_key: 'string_flag', default_value: 'default') # fetching number value feature flag float_value = client.fetch_number_value(flag_key: 'number_value', default_value: 1.0) integer_value = client.fetch_number_value(flag_key: 'number_value', default_value: 1) # get an object value object = client.fetch_object_value(flag_key: 'object_value', default_value: { name: 'object'})">require 'open_feature/sdk'
require 'json' # For JSON.dump

# API Initialization and configuration

OpenFeature::SDK.configure do |config|
# your provider of choice, which will be used as the default provider
config.set_provider(OpenFeature::SDK::Provider::InMemoryProvider.new(
{
"flag1" => true,
"flag2" => 1
}
))
end

# Create a client
client = OpenFeature::SDK.build_client

# fetching boolean value feature flag
bool_value = client.fetch_boolean_value(flag_key: 'boolean_flag', default_value: false)

# a details method is also available for more information about the flag evaluation
# see `ResolutionDetails` for more info
bool_details = client.fetch_boolean_details(flag_key: 'boolean_flag', default_value: false)

# fetching string value feature flag
string_value = client.fetch_string_value(flag_key: 'string_flag', default_value: 'default')

# fetching number value feature flag
float_value = client.fetch_number_value(flag_key: 'number_value', default_value: 1.0)
integer_value = client.fetch_number_value(flag_key: 'number_value', default_value: 1)

# get an object value
object = client.fetch_object_value(flag_key: 'object_value', default_value: { name: 'object'})

Features

Status Features Description
Providers Integrate with a commercial, open source, or in-house feature management tool.
Targeting Contextually-aware flag evaluation using evaluation context.
Hooks Add functionality to various stages of the flag evaluation life-cycle.
Logging Integrate with popular logging packages.
Domains Logically bind clients with providers.
Eventing React to state changes in the provider or flag management system.
Shutdown Gracefully clean up a provider during application shutdown.
Transaction Context Propagation Set a specific evaluation context for a transaction (e.g. an HTTP request or a thread)
Extending Extend OpenFeature with custom providers and hooks.

Implemented: | In-progress: | Not implemented yet:

Providers

Providers are an abstraction between a flag management system and the OpenFeature SDK. Look here for a complete list of available providers. If the provider you're looking for hasn't been created yet, see the develop a provider section to learn how to build it yourself.

Once you've added a provider as a dependency, it can be registered with OpenFeature like this:

OpenFeature::SDK.configure do |config|
# your provider of choice, which will be used as the default provider
config.set_provider(OpenFeature::SDK::Provider::InMemoryProvider.new(
{
"v2_enabled" => true,
}
))
end

Blocking Provider Registration

If you need to ensure that a provider is fully initialized before continuing, you can use set_provider_and_wait:

# Using the SDK directly
begin
OpenFeature::SDK.set_provider_and_wait(my_provider)
puts "Provider is ready!"
rescue OpenFeature::SDK::ProviderInitializationError => e
puts "Provider failed to initialize: #{e.message}"
puts "Error code: #{e.error_code}"
# original_error contains the underlying exception that caused the initialization failure
puts "Original error: #{e.original_error}"
end

# With custom timeout (default is 30 seconds)
OpenFeature::SDK.set_provider_and_wait(my_provider, timeout: 60)

# Domain-specific provider
OpenFeature::SDK.set_provider_and_wait(my_provider, domain: "feature-flags")

# Via configuration block
OpenFeature::SDK.configure do |config|
begin
config.set_provider_and_wait(my_provider)
rescue OpenFeature::SDK::ProviderInitializationError => e
# Handle initialization failure
end
end

The set_provider_and_wait method:

  • Waits for the provider's init method to complete successfully
  • Raises ProviderInitializationError if initialization fails or times out
  • Provides access to the provider instance, error code, and original exception for debugging
  • The original_error field contains the underlying exception that caused the initialization failure
  • Uses the same thread-safe provider switching as set_provider

In some situations, it may be beneficial to register multiple providers in the same application. This is possible using domains, which is covered in more detail below.

Targeting

Sometimes, the value of a flag must consider some dynamic criteria about the application or user, such as the user's location, IP, email address, or the server's location. In OpenFeature, we refer to this as targeting. If the flag management system you're using supports targeting, you can provide the input data using the evaluation context.

true) )">OpenFeature::SDK.configure do |config|
# you can set a global evaluation context here
config.evaluation_context = OpenFeature::SDK::EvaluationContext.new("host" => "myhost.com")
end

# Evaluation context can be set on a client as well
client_with_context = OpenFeature::SDK.build_client(
evaluation_context: OpenFeature::SDK::EvaluationContext.new("controller_name" => "admin")
)

# Invocation evaluation context can also be passed in during flag evaluation.
# During flag evaluation, invocation context takes precedence over client context
# which takes precedence over API (aka global) context.
bool_value = client.fetch_boolean_value(
flag_key: 'boolean_flag',
default_value: false,
evaluation_context: OpenFeature::SDK::EvaluationContext.new("is_friday" => true)
)

Hooks

Coming Soon! Issue available to be worked on.

Logging

Coming Soon! Issue available to work on.

Domains

Clients can be assigned to a domain. A domain is a logical identifier which can be used to associate clients with a particular provider. If a domain has no associated provider, the default provider is used.

OpenFeature::SDK.configure do |config|
config.set_provider(OpenFeature::SDK::Provider::NoOpProvider.new, domain: "legacy_flags")
end

# Create a client for a different domain, this will use the provider assigned to that domain
legacy_flag_client = OpenFeature::SDK.build_client(domain: "legacy_flags")

Eventing

Events allow you to react to state changes in the provider or underlying flag management system, such as flag definition changes, provider readiness, or error conditions. Initialization events (PROVIDER_READY on success, PROVIDER_ERROR on failure) are dispatched for every provider. Some providers support additional events, such as PROVIDER_CONFIGURATION_CHANGED.

Please refer to the documentation of the provider you're using to see what events are supported.

# Register event handlers at the API (global) level
ready_handler = ->(event_details) do
puts "Provider #{event_details[:provider].metadata.name} is ready!"
end

OpenFeature::SDK.add_handler(OpenFeature::SDK::ProviderEvent::PROVIDER_READY, ready_handler)

# The SDK automatically emits lifecycle events. Providers can emit additional spontaneous events
# using the EventEmitter mixin to signal internal state changes like configuration updates.
class MyEventAwareProvider
include OpenFeature::SDK::Provider::EventEmitter

def init(evaluation_context)
# Start background process to monitor for configuration changes
# Note: SDK automatically emits PROVIDER_READY when init completes successfully
start_background_process
end

def start_background_process
Thread.new do
# Monitor for configuration changes and emit events when they occur
if configuration_changed?
emit_event(
OpenFeature::SDK::ProviderEvent::PROVIDER_CONFIGURATION_CHANGED,
message: "Flag configuration updated"
)
end
end
end
end

# Remove specific handlers when no longer needed
OpenFeature::SDK.remove_handler(OpenFeature::SDK::ProviderEvent::PROVIDER_READY, ready_handler)

Shutdown

Coming Soon! Issue available to be worked on.

Transaction Context Propagation

Coming Soon! Issue available to be worked on.

Extending

Develop a provider

To develop a provider, you need to create a new project and include the OpenFeature SDK as a dependency. This can be a new repository or included in the existing contrib repository available under the OpenFeature organization. You'll then need to write the provider by implementing the Provider duck.

class MyProvider
def init
# Perform any initialization steps with flag management system here
# Return value is ignored
# **Note** The OpenFeature spec defines a lifecycle method called `initialize` to be called when a new provider is set.
# To avoid conflicting with the Ruby `initialize` method, this method should be named `init` when creating a provider.
end

def shutdown
# Perform any shutdown/reclamation steps with flag management system here
# Return value is ignored
end

def fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil)
# Retrieve a boolean value from provider source
end

def fetch_string_value(flag_key:, default_value:, evaluation_context: nil)
# Retrieve a string value from provider source
end

def fetch_number_value(flag_key:, default_value:, evaluation_context: nil)
# Retrieve a numeric value from provider source
end

def fetch_integer_value(flag_key:, default_value:, evaluation_context: nil)
# Retrieve a integer value from provider source
end

def fetch_float_value(flag_key:, default_value:, evaluation_context: nil)
# Retrieve a float value from provider source
end

def fetch_object_value(flag_key:, default_value:, evaluation_context: nil)
# Retrieve a hash value from provider source
end
end

Built a new provider? Let us know so we can add it to the docs!

Develop a hook

Coming Soon! Issue available to be worked on.

Support the project

Contributing

Interested in contributing? Great, we'd love your help! To get started, take a look at the CONTRIBUTING guide.

Thanks to everyone who has already contributed

Made with contrib.rocks.

About

Ruby implementation of the OpenFeature SDK

Topics

Resources

Readme

License

Apache-2.0 license

Code of conduct

Code of conduct

Contributing

Contributing

Security policy

Security policy

Stars

Watchers

Forks

Packages

Contributors