Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: yggdrasil engine #152

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fb3bdf8
feat: splat apply patch from local work
sighphyre Sep 18, 2023
eec8121
chore: hack in local path for yggdrasil
sighphyre Sep 18, 2023
61ad46c
chore: trim out diff that shouldn't have been checked in
sighphyre Oct 23, 2023
5797508
wip: implement custom strategies, this definitely needs a rebase
sighphyre Nov 3, 2023
4b3a598
green tests!
sighphyre Nov 10, 2023
211fcdf
chore: use published package rather than a symlink
sighphyre Nov 15, 2023
6382a94
chore: merge main
sighphyre Nov 15, 2023
a0707de
wip: trim some unneeded strings and apply format
sighphyre Nov 15, 2023
c39fe95
wip: trim some unneeded nil checks
sighphyre Nov 15, 2023
5df5067
wip: move default_variant to Variant
sighphyre Nov 15, 2023
f6071b9
wip: more null checks removed
sighphyre Nov 15, 2023
88b46c7
wip: make internal public api for custom strategies match existing api
sighphyre Nov 15, 2023
7f55e6b
wip: remove class and references to FeatureToggle.default_variant
sighphyre Nov 15, 2023
5851724
wip: remove new log message
sighphyre Nov 15, 2023
49925d5
wip: bring custom strategies closer to existing api
sighphyre Nov 15, 2023
0116bef
wip: remove unused parameters and functions and take rubocop suggestion
sighphyre Nov 15, 2023
766d392
wip: remove :toggles, :toggle_metrics, and :segment_cache from Unleas…
sighphyre Nov 15, 2023
6530299
wip: refactor get_variant method and restore logging
sighphyre Nov 15, 2023
c367b0b
chore: apply some rubocop lints
sighphyre Nov 15, 2023
1cd34b2
wip: minor refactor to toggle fethcer
sighphyre Nov 16, 2023
5b5bb20
wip: bump yggdrasil
sighphyre Feb 16, 2024
aaa6b0f
wip: merge main
sighphyre Feb 16, 2024
8f0c038
chore: bump ygg version
sighphyre Feb 19, 2024
3122986
wip: include version of ygg that contains gem for jruby9.2
sighphyre Feb 20, 2024
3e44bc3
chore: bump ygg to a version that should include macos universal arti…
sighphyre Feb 20, 2024
05c83ce
chore: bump yggdrasil version
sighphyre Feb 20, 2024
e0d37af
chore: bump ygg again
sighphyre Feb 20, 2024
156356c
chore: exclude spec folder in coveralls
sighphyre Feb 21, 2024
9be1f29
chore: remove variant override, don't need it anymore
sighphyre Feb 21, 2024
8e64f84
chore: disable some coverage that I don't think makes sense
sighphyre Feb 21, 2024
cf89cc5
fix: make strategies report correct data shape during registration
sighphyre Feb 21, 2024
9dc382e
chore: clean up gem dependency
sighphyre Feb 21, 2024
950eb8a
chore: no longer save on shutdown, there's no point in doing that
sighphyre Jul 30, 2024
b7f1b7a
chore: merge main
sighphyre Sep 4, 2024
3fb4688
fix: convert all context values to strings prior to send to ygg
sighphyre Sep 5, 2024
0518b74
chore: bump ygg to test macos
sighphyre Sep 5, 2024
92642d6
chore: bump yggdrasil version
sighphyre Sep 20, 2024
f143ca2
chore: merge main
sighphyre Sep 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/unleash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Unleash
TIME_RESOLUTION = 3

class << self
attr_accessor :configuration, :toggle_fetcher, :toggles, :toggle_metrics, :reporter, :segment_cache, :logger
attr_accessor :configuration, :toggle_fetcher, :reporter, :logger, :engine
end

self.configuration = Unleash::Configuration.new
Expand Down
44 changes: 0 additions & 44 deletions lib/unleash/activation_strategy.rb

This file was deleted.

38 changes: 24 additions & 14 deletions lib/unleash/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'unleash/toggle_fetcher'
require 'unleash/metrics_reporter'
require 'unleash/scheduled_executor'
require 'unleash/feature_toggle'
require 'unleash/variant'
require 'unleash/util/http'
require 'logger'
require 'time'
Expand All @@ -11,14 +11,17 @@ module Unleash
class Client
attr_accessor :fetcher_scheduled_executor, :metrics_scheduled_executor

# rubocop:disable Metrics/AbcSize
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Belongs in the Rubocop config but we probably can't get away from this one. It's the only warning generated by this PR but this initialize is literally already at the limit

def initialize(*opts)
Unleash.configuration = Unleash::Configuration.new(*opts) unless opts.empty?
Unleash.configuration.validate!

Unleash.logger = Unleash.configuration.logger.clone
Unleash.logger.level = Unleash.configuration.log_level
Unleash.engine = YggdrasilEngine.new
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've chosen to allow the client to new up Ygg, other consumers do nil checks when they want to access it - that's the metrics handling code and the fetching code

Unleash.engine.register_custom_strategies(Unleash.configuration.strategies.custom_strategies)

Unleash.toggle_fetcher = Unleash::ToggleFetcher.new
Unleash.toggle_fetcher = Unleash::ToggleFetcher.new Unleash.engine
if Unleash.configuration.disable_client
Unleash.logger.warn "Unleash::Client is disabled! Will only return default (or bootstrapped if available) results!"
Unleash.logger.warn "Unleash::Client is disabled! Metrics and MetricsReporter are also disabled!"
Expand All @@ -30,6 +33,7 @@ def initialize(*opts)
start_toggle_fetcher
start_metrics unless Unleash.configuration.disable_metrics
end
# rubocop:enable Metrics/AbcSize

def is_enabled?(feature, context = nil, default_value_param = false, &fallback_blk)
Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} with context #{context}"
Expand All @@ -40,15 +44,16 @@ def is_enabled?(feature, context = nil, default_value_param = false, &fallback_b
default_value_param
end

toggle_as_hash = Unleash&.toggles&.select{ |toggle| toggle['name'] == feature }&.first
if toggle_as_hash.nil?
toggle_enabled = Unleash.engine.enabled?(feature, context)
if toggle_enabled.nil?
Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
Unleash.engine.count_toggle(feature, false)
return default_value
end

toggle = Unleash::FeatureToggle.new(toggle_as_hash, Unleash&.segment_cache)
Unleash.engine.count_toggle(feature, toggle_enabled)

toggle.is_enabled?(context)
toggle_enabled
end

def is_disabled?(feature, context = nil, default_value_param = true, &fallback_blk)
Expand All @@ -73,21 +78,27 @@ def if_disabled(feature, context = nil, default_value = true, &blk)
def get_variant(feature, context = Unleash::Context.new, fallback_variant = disabled_variant)
Unleash.logger.debug "Unleash::Client.get_variant for feature: #{feature} with context #{context}"

toggle_as_hash = Unleash&.toggles&.select{ |toggle| toggle['name'] == feature }&.first

if toggle_as_hash.nil?
toggle_enabled = Unleash.engine.enabled?(feature, context)
if toggle_enabled.nil?
Unleash.logger.debug "Unleash::Client.get_variant feature: #{feature} not found"
Unleash.engine.count_toggle(feature, false)
return fallback_variant
end

toggle = Unleash::FeatureToggle.new(toggle_as_hash)
variant = toggle.get_variant(context, fallback_variant)
Unleash.engine.count_toggle(feature, toggle_enabled)

variant = Unleash.engine.get_variant(feature, context)

if variant.nil?
Unleash.logger.debug "Unleash::Client.get_variant variants for feature: #{feature} not found"
Unleash.engine.count_variant(feature, "disabled")
return fallback_variant
end

variant = Variant.new(variant)

Unleash.engine.count_variant(feature, variant.name)

# TODO: Add to README: name, payload, enabled (bool)

variant
Expand Down Expand Up @@ -117,7 +128,7 @@ def info
'appName': Unleash.configuration.app_name,
'instanceId': Unleash.configuration.instance_id,
'sdkVersion': "unleash-client-ruby:" + Unleash::VERSION,
'strategies': Unleash.strategies.keys,
'strategies': Unleash.strategies.known_strategies,
'started': Time.now.iso8601(Unleash::TIME_RESOLUTION),
'interval': Unleash.configuration.metrics_interval_in_millis
}
Expand All @@ -136,7 +147,6 @@ def start_toggle_fetcher
end

def start_metrics
Unleash.toggle_metrics = Unleash::Metrics.new
Unleash.reporter = Unleash::MetricsReporter.new
self.metrics_scheduled_executor = Unleash::ScheduledExecutor.new(
'MetricsReporter',
Expand All @@ -162,7 +172,7 @@ def register
end

def disabled_variant
@disabled_variant ||= Unleash::FeatureToggle.disabled_variant
@disabled_variant ||= Unleash::Variant.disabled_variant
end

def first_fetch_is_eager
Expand Down
4 changes: 2 additions & 2 deletions lib/unleash/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def metrics_interval_in_millis
def validate!
return if self.disable_client

raise ArgumentError, "URL and app_name are required parameters." if self.app_name.nil? || self.url.nil?
raise ArgumentError, "app_name is a required parameter." if self.app_name.nil?
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bootstrap only should be a viable option (we can do this in the others), relaxing this constraint makes testing a ton easier but I may be able to roll this one back if we need

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid point.


validate_custom_http_headers!(self.custom_http_headers)
validate_custom_http_headers!(self.custom_http_headers) unless self.url.nil?
end

def refresh_backup_file!
Expand Down
115 changes: 0 additions & 115 deletions lib/unleash/constraint.rb

This file was deleted.

16 changes: 16 additions & 0 deletions lib/unleash/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ def to_s
",app_name=#{@app_name},environment=#{@environment}>"
end

def as_json
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Yggdrasil to use so we can send the context as a JSON blob

{
appName: self.app_name,
environment: self.environment,
userId: self.user_id,
sessionId: self.session_id,
remoteAddress: self.remote_address,
currentTime: self.current_time,
properties: self.properties
}
end

def to_json(*options)
as_json(*options).to_json(*options)
end

def to_h
ATTRS.map{ |attr| [attr, self.send(attr)] }.to_h.merge(properties: @properties)
end
Expand Down
Loading
Loading