diff --git a/lib/unleash/client.rb b/lib/unleash/client.rb index 2e1642ee..9538a86c 100644 --- a/lib/unleash/client.rb +++ b/lib/unleash/client.rb @@ -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), diff --git a/lib/unleash/configuration.rb b/lib/unleash/configuration.rb index 8247df69..7a32ad7f 100644 --- a/lib/unleash/configuration.rb +++ b/lib/unleash/configuration.rb @@ -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) @@ -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 diff --git a/lib/unleash/metrics_reporter.rb b/lib/unleash/metrics_reporter.rb index 021876ea..58e283ca 100755 --- a/lib/unleash/metrics_reporter.rb +++ b/lib/unleash/metrics_reporter.rb @@ -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 @@ -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}" diff --git a/lib/unleash/toggle_fetcher.rb b/lib/unleash/toggle_fetcher.rb index 3001ba2c..1c881dfd 100755 --- a/lib/unleash/toggle_fetcher.rb +++ b/lib/unleash/toggle_fetcher.rb @@ -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." diff --git a/lib/unleash/util/http.rb b/lib/unleash/util/http.rb index 287d8f02..c8b66808 100644 --- a/lib/unleash/util/http.rb +++ b/lib/unleash/util/http.rb @@ -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) diff --git a/spec/unleash/client_spec.rb b/spec/unleash/client_spec.rb index 20ef073c..93292a1a 100644 --- a/spec/unleash/client_spec.rb +++ b/spec/unleash/client_spec.rb @@ -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: {}) @@ -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: {}) @@ -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 @@ -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: @@ -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 @@ -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: {}) @@ -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: {}) @@ -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: {}) @@ -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: {}) diff --git a/spec/unleash/configuration_spec.rb b/spec/unleash/configuration_spec.rb index bf307d3e..e8dc83d3 100644 --- a/spec/unleash/configuration_spec.rb +++ b/spec/unleash/configuration_spec.rb @@ -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) @@ -213,7 +213,8 @@ yggdrasilVersion: anything, specVersion: anything, platformName: anything, - platformVersion: anything + platformVersion: anything, + connectionId: anything ) ) end diff --git a/spec/unleash/metrics_reporter_spec.rb b/spec/unleash/metrics_reporter_spec.rb index 9b07868c..f26d7fe1 100644 --- a/spec/unleash/metrics_reporter_spec.rb +++ b/spec/unleash/metrics_reporter_spec.rb @@ -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: {}) @@ -158,7 +159,8 @@ yggdrasilVersion: anything, specVersion: anything, platformName: anything, - platformVersion: anything + platformVersion: anything, + connectionId: anything ) ) end diff --git a/spec/unleash/toggle_fetcher_spec.rb b/spec/unleash/toggle_fetcher_spec.rb index 7e7b9aad..613c1c08 100644 --- a/spec/unleash/toggle_fetcher_spec.rb +++ b/spec/unleash/toggle_fetcher_spec.rb @@ -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