Skip to content

Commit

Permalink
feat: unleash-interval header (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Feb 28, 2025
1 parent 978dfe2 commit 751ea32
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/unleash/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def info
{
'appName': Unleash.configuration.app_name,
'instanceId': Unleash.configuration.instance_id,
'connectionId': Unleash.configuration.connection_id,
'sdkVersion': "unleash-client-ruby:" + Unleash::VERSION,
'strategies': Unleash.strategies.known_strategies,
'started': Time.now.iso8601(Unleash::TIME_RESOLUTION),
Expand Down
3 changes: 2 additions & 1 deletion lib/unleash/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Configuration
:bootstrap_config,
:strategies,
:use_delta_api
attr_reader :connection_id

def initialize(opts = {})
validate_custom_http_headers!(opts[:custom_http_headers]) if opts.has_key?(:custom_http_headers)
Expand Down Expand Up @@ -102,7 +103,7 @@ def set_defaults
self.project_name = nil
self.disable_client = false
self.disable_metrics = false
self.refresh_interval = 10
self.refresh_interval = 15
self.metrics_interval = 60
self.timeout = 30
self.retry_limit = Float::INFINITY
Expand Down
5 changes: 4 additions & 1 deletion lib/unleash/metrics_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def generate_report
'specVersion': Unleash::CLIENT_SPECIFICATION_VERSION,
'appName': Unleash.configuration.app_name,
'instanceId': Unleash.configuration.instance_id,
'connectionId': Unleash.configuration.connection_id,
'bucket': metrics || {}
}
end
Expand All @@ -37,7 +38,9 @@ def post
return
end

response = Unleash::Util::Http.post(Unleash.configuration.client_metrics_uri, report.to_json)
headers = (Unleash.configuration.http_headers || {}).dup
headers.merge!({ 'UNLEASH-INTERVAL' => Unleash.configuration.metrics_interval.to_s })
response = Unleash::Util::Http.post(Unleash.configuration.client_metrics_uri, report.to_json, headers)

if ['200', '202'].include? response.code
Unleash.logger.debug "Report sent to unleash server successfully. Server responded with http code #{response.code}"
Expand Down
4 changes: 3 additions & 1 deletion lib/unleash/toggle_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def fetch
Unleash.logger.debug "fetch()"
return if Unleash.configuration.disable_client

response = Unleash::Util::Http.get(Unleash.configuration.fetch_toggles_uri, etag)
headers = (Unleash.configuration.http_headers || {}).dup
headers.merge!({ 'UNLEASH-INTERVAL' => Unleash.configuration.refresh_interval.to_s })
response = Unleash::Util::Http.get(Unleash.configuration.fetch_toggles_uri, etag, headers)

if response.code == '304'
Unleash.logger.debug "No changes according to the unleash server, nothing to do."
Expand Down
4 changes: 2 additions & 2 deletions lib/unleash/util/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def self.get(uri, etag = nil, headers_override = nil)
http.request(request)
end

def self.post(uri, body)
def self.post(uri, body, headers_override = nil)
http = http_connection(uri)

request = Net::HTTP::Post.new(uri.request_uri, http_headers)
request = Net::HTTP::Post.new(uri.request_uri, http_headers(nil, headers_override))
request.body = body

http.request(request)
Expand Down
21 changes: 15 additions & 6 deletions spec/unleash/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}"
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'Unleash-Interval' => '60'
}
)
.to_return(status: 200, body: "", headers: {})
Expand All @@ -56,7 +57,8 @@
'Unleash-Connection-Id' => fixed_uuid,
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'X-Api-Key' => '123'
'X-Api-Key' => '123',
'Unleash-Interval' => '15'
}
)
.to_return(status: 200, body: simple_features.to_json, headers: {})
Expand Down Expand Up @@ -92,6 +94,7 @@
.with(headers: { 'UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'UNLEASH-INSTANCEID': 'rspec/test' })
.with(headers: { 'UNLEASH-CONNECTION-ID': fixed_uuid })
.with(headers: { 'UNLEASH-INTERVAL': '15' })
).to have_been_made.once

# Test now sending of metrics
Expand All @@ -104,6 +107,7 @@
.with(headers: { 'UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'UNLEASH-INSTANCEID': 'rspec/test' })
.with(headers: { 'UNLEASH-CONNECTION-ID': fixed_uuid })
.with(headers: { 'UNLEASH-INTERVAL': '60' })
).not_to have_been_made

# Sending metrics, if they have been evaluated:
Expand All @@ -117,6 +121,7 @@
.with(headers: { 'UNLEASH-APPNAME': 'my-test-app' })
.with(headers: { 'UNLEASH-INSTANCEID': 'rspec/test' })
.with(headers: { 'UNLEASH-CONNECTION-ID': fixed_uuid })
.with(headers: { 'UNLEASH-INTERVAL': '60' })
.with{ |request| JSON.parse(request.body)['bucket']['toggles']['Feature.A']['yes'] == 2 }
).to have_been_made.once
end
Expand Down Expand Up @@ -174,7 +179,8 @@
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'X-Api-Key' => '123'
'X-Api-Key' => '123',
'Unleash-Interval' => '15'
}
)
.to_return(status: 200, body: features_response_body, headers: {})
Expand Down Expand Up @@ -288,7 +294,8 @@
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'X-Api-Key' => '123'
'X-Api-Key' => '123',
'Unleash-Interval' => '15'
}
)
.to_return(status: 200, body: "", headers: {})
Expand Down Expand Up @@ -348,7 +355,8 @@
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'X-Api-Key' => '123'
'X-Api-Key' => '123',
'Unleash-Interval' => '15'
}
)
.to_return(status: 200, body: features_response_body, headers: {})
Expand Down Expand Up @@ -642,7 +650,8 @@
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'X-Api-Key' => '123'
'X-Api-Key' => '123',
'Unleash-Interval' => '15'
}
)
.to_return(status: 200, body: body, headers: {})
Expand Down
5 changes: 3 additions & 2 deletions spec/unleash/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
expect(config.custom_http_headers).to eq({})
expect(config.disable_metrics).to be_falsey

expect(config.refresh_interval).to eq(10)
expect(config.refresh_interval).to eq(15)
expect(config.metrics_interval).to eq(60)
expect(config.timeout).to eq(30)
expect(config.retry_limit).to eq(Float::INFINITY)
Expand Down Expand Up @@ -213,7 +213,8 @@
yggdrasilVersion: anything,
specVersion: anything,
platformName: anything,
platformVersion: anything
platformVersion: anything,
connectionId: anything
)
)
end
Expand Down
6 changes: 4 additions & 2 deletions spec/unleash/metrics_reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
'Unleash-Appname' => 'my-test-app',
'Unleash-Instanceid' => 'rspec/test',
'User-Agent' => "UnleashClientRuby/#{Unleash::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION} [#{RUBY_PLATFORM}]",
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}"
'Unleash-Sdk' => "unleash-client-ruby:#{Unleash::VERSION}",
'Unleash-Interval' => "60"
}
)
.to_return(status: 200, body: "", headers: {})
Expand Down Expand Up @@ -158,7 +159,8 @@
yggdrasilVersion: anything,
specVersion: anything,
platformName: anything,
platformVersion: anything
platformVersion: anything,
connectionId: anything
)
)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unleash/toggle_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
describe '#fetch!' do
let(:engine) { YggdrasilEngine.new }

context 'when fetching toggles succeds' do
context 'when fetching toggles succeeds' do
before do
_toggle_fetcher = described_class.new engine
end
Expand Down

0 comments on commit 751ea32

Please sign in to comment.