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: unleash-interval header #236

Merged
merged 6 commits into from
Feb 28, 2025
Merged

feat: unleash-interval header #236

merged 6 commits into from
Feb 28, 2025

Conversation

kwasniew
Copy link
Contributor

@kwasniew kwasniew commented Feb 28, 2025

About the changes

  • synchronizing refresh_interval to be identical to other SDKs (15s for fetch toggles, it was 10s before despite docs claiming it's 15s)
  • adding unleash-interval header to fetch toggles call and send metric call (they are different so can't share HTTP header setting)
  • adding connectionId to registration payload and send metrics payload

Important files

Please check toggle_fetcher and metrics_reporter since I added "seams" in those 2 in order to inject different metrics interval headers.

Discussion points

@coveralls
Copy link

coveralls commented Feb 28, 2025

Pull Request Test Coverage Report for Build 13585837720

Details

  • 10 of 10 (100.0%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.05%) to 95.289%

Totals Coverage Status
Change from base Build 13303505666: 0.05%
Covered Lines: 445
Relevant Lines: 467

💛 - Coveralls

@@ -116,6 +116,7 @@ def info
{
'appName': Unleash.configuration.app_name,
'instanceId': Unleash.configuration.instance_id,
'connectionId': Unleash.configuration.connection_id,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

added to registration payload

@@ -23,6 +23,7 @@ class Configuration
:bootstrap_config,
:strategies,
:use_delta_api
attr_reader :connection_id
Copy link
Contributor Author

Choose a reason for hiding this comment

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

exposing as read only for metrics payload and registration payload

@@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixing default

@@ -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,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

metrics payload includes connectionId

@@ -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 })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

it was the best "seam" that I found for adding a new header without branching

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we need to change number to string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was also considering using header_overrides but I learned that they erase existing headers instead of merging

@@ -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 })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is another "seam"

@@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

making it symmetrical with get

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))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no etag for post, only headers_override

@@ -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'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

test metrics interval reported correctly

@@ -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'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

test fetch toggles interval reported correctly

@@ -213,7 +213,8 @@
yggdrasilVersion: anything,
specVersion: anything,
platformName: anything,
platformVersion: anything
platformVersion: anything,
connectionId: anything
Copy link
Contributor Author

Choose a reason for hiding this comment

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

test connectionId included in the registration payload

@@ -158,7 +159,8 @@
yggdrasilVersion: anything,
specVersion: anything,
platformName: anything,
platformVersion: anything
platformVersion: anything,
connectionId: anything
Copy link
Contributor Author

Choose a reason for hiding this comment

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

test connectionId included in the metrics payload

Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

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

Apologies, I think I've made your life harder than it needs to be with the headers override code. I think you have some sane seams here and it looks good though!

@kwasniew kwasniew merged commit 751ea32 into main Feb 28, 2025
47 checks passed
@kwasniew kwasniew deleted the unleash-interval-reporting branch February 28, 2025 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants