Skip to content

Commit

Permalink
feat: send empty bucket when no metrics after 10 min
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonfournier committed Dec 10, 2024
1 parent 3d875c2 commit 8b4ec03
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,15 +534,15 @@ end
## Development

After checking out the repo, run `bin/setup` to install dependencies.
Then, run `rake spec` to run the tests.
Then, run `bundle exec rake spec` to run the tests.
You can also run `bin/console` for an interactive prompt that will allow you to experiment.

This SDK is also built against the Unleash Client Specification tests.
To run the Ruby SDK against this test suite, you'll need to have a copy on your machine, you can clone the repository directly using:

`git clone --branch v$(ruby echo_client_spec_version.rb) https://github.com/Unleash/client-specification.git`

After doing this, `rake spec` will also run the client specification tests.
After doing this, `bundle exec rake spec` will also run the client specification tests.

To install this gem onto your local machine, run `bundle exec rake install`.

Expand Down
11 changes: 4 additions & 7 deletions lib/unleash/metrics_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def initialize

def generate_report
metrics = Unleash&.engine&.get_metrics()
return nil if metrics.nil? || metrics.empty?

{
'platformName': RUBY_ENGINE,
Expand All @@ -24,21 +23,19 @@ def generate_report
'specVersion': Unleash::CLIENT_SPECIFICATION_VERSION,
'appName': Unleash.configuration.app_name,
'instanceId': Unleash.configuration.instance_id,
'bucket': metrics
'bucket': metrics || {}
}
end

def post
Unleash.logger.debug "post() Report"

bucket = self.generate_report
if bucket.nil? && (Time.now - self.last_time >= LONGEST_WITHOUT_A_REPORT) # and last time is less then 10 minutes...
report = self.generate_report
if report[:bucket].empty? && (Time.now - self.last_time < LONGEST_WITHOUT_A_REPORT) # and last time is less then 10 minutes...
Unleash.logger.debug "Report not posted to server, as it would have been empty. (and has been empty for up to 10 min)"

return
end

response = Unleash::Util::Http.post(Unleash.configuration.client_metrics_uri, bucket.to_json)
response = Unleash::Util::Http.post(Unleash.configuration.client_metrics_uri, report.to_json)

if ['200', '202'].include? response.code
Unleash.logger.debug "Report sent to unleash server successfully. Server responded with http code #{response.code}"
Expand Down
43 changes: 30 additions & 13 deletions spec/unleash/metrics_reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# Do not test the scheduled calls from client/metrics:
Unleash.configuration.disable_client = true
Unleash.configuration.disable_metrics = true
metrics_reporter.last_time = Time.now
end

it "generates the correct report" do
Expand Down Expand Up @@ -60,19 +61,6 @@
)
end

it "generates the correct report with no metrics" do
Unleash.configure do |config|
config.url = 'http://test-url/'
config.app_name = 'my-test-app'
config.instance_id = 'rspec/test'
config.disable_client = true
end
Unleash.engine = YggdrasilEngine.new

report = metrics_reporter.generate_report
expect(report).to be_nil
end

it "sends the correct report" do
WebMock.stub_request(:post, "http://test-url/client/metrics")
.with(
Expand Down Expand Up @@ -125,6 +113,35 @@
expect(WebMock).to_not have_requested(:post, 'http://test-url/client/metrics')
end

it "generates an empty report when no metrics after 10 minutes" do
WebMock.stub_request(:post, "http://test-url/client/metrics")
.to_return(status: 200, body: "", headers: {})
Unleash.configure do |config|
config.url = 'http://test-url/'
config.app_name = 'my-test-app'
config.instance_id = 'rspec/test'
config.disable_client = true
end
Unleash.engine = YggdrasilEngine.new

metrics_reporter.last_time = Time.now - 601
report = metrics_reporter.generate_report
expect(report[:bucket]).to be_empty

metrics_reporter.post

expect(WebMock).to have_requested(:post, 'http://test-url/client/metrics')
.with(
body: hash_including(
yggdrasilVersion: anything,
specVersion: anything,
platformName: anything,
platformVersion: anything,
bucket: {}
)
)
end

it "includes metadata in the report" do
WebMock.stub_request(:post, "http://test-url/client/metrics")
.to_return(status: 200, body: "", headers: {})
Expand Down

0 comments on commit 8b4ec03

Please sign in to comment.