From 72873f672bd5a5b079a46edcaecdeab58a47e24e Mon Sep 17 00:00:00 2001 From: Dave Qorashi Date: Wed, 29 Jun 2016 15:48:19 -0700 Subject: [PATCH 1/2] Fix failing specs in Ruby 2.2.3 * While running specs in Ruby 2.2.3, three of specs failed. All of them are related to DateTime usages. Converting the time to UTC helps those specs to pass. * Update spec code to use new RSpec syntax --- lib/segment/analytics/utils.rb | 2 +- spec/segment/analytics/worker_spec.rb | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/segment/analytics/utils.rb b/lib/segment/analytics/utils.rb index db96405..de15b3e 100644 --- a/lib/segment/analytics/utils.rb +++ b/lib/segment/analytics/utils.rb @@ -63,7 +63,7 @@ def datetime_in_iso8601 datetime def time_in_iso8601 time, fraction_digits = 3 fraction = if fraction_digits > 0 - (".%06i" % time.usec)[0, fraction_digits + 1] + (".%06i" % time.utc.usec)[0, fraction_digits + 1] end "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{fraction}#{formatted_offset(time, true, 'Z')}" diff --git a/spec/segment/analytics/worker_spec.rb b/spec/segment/analytics/worker_spec.rb index 1accd22..b161a8c 100644 --- a/spec/segment/analytics/worker_spec.rb +++ b/spec/segment/analytics/worker_spec.rb @@ -22,7 +22,7 @@ class Analytics it 'does not error if the endpoint is unreachable' do expect do - Net::HTTP.any_instance.stub(:post).and_raise(Exception) + allow_any_instance_of(Net::HTTP).to receive(:post).and_raise(Exception) queue = Queue.new queue << {} @@ -31,12 +31,11 @@ class Analytics expect(queue).to be_empty - Net::HTTP.any_instance.unstub(:post) end.to_not raise_error end it 'executes the error handler, before the request phase ends, if the request is invalid' do - Segment::Analytics::Request.any_instance.stub(:post).and_return(Segment::Analytics::Response.new(400, "Some error")) + allow_any_instance_of(Segment::Analytics::Request).to receive(:post).and_return(Segment::Analytics::Response.new(400, "Some error")) status = error = nil on_error = Proc.new do |yielded_status, yielded_error| @@ -53,8 +52,6 @@ class Analytics sleep 0.1 # First give thread time to spin-up. sleep 0.01 while worker.is_requesting? - Segment::Analytics::Request::any_instance.unstub(:post) - expect(queue).to be_empty expect(status).to eq(400) expect(error).to eq('Some error') From 5637b407d24670abc7733f5975b2388267517d3d Mon Sep 17 00:00:00 2001 From: Dave Qorashi Date: Thu, 30 Jun 2016 10:37:33 -0700 Subject: [PATCH 2/2] Add logging warns and errors when message queue is full. Add logging features to message enqueuing method. When the queue is 70% full, log warns and if it's full log an error, notifying user. --- lib/segment/analytics/client.rb | 16 ++++++++++++++++ spec/segment/analytics/client_spec.rb | 14 ++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/segment/analytics/client.rb b/lib/segment/analytics/client.rb index 138f819..448021c 100644 --- a/lib/segment/analytics/client.rb +++ b/lib/segment/analytics/client.rb @@ -3,11 +3,13 @@ require 'segment/analytics/utils' require 'segment/analytics/worker' require 'segment/analytics/defaults' +require 'segment/analytics/logging' module Segment class Analytics class Client include Segment::Analytics::Utils + include Segment::Analytics::Logging # public: Creates a new client # @@ -310,9 +312,23 @@ def enqueue(action) ensure_worker_running @queue << action end + queue_logging(queue_full) !queue_full end + # private: Log warns and error when queue is getting full + def queue_logging(is_full) + queue_fullness_percentage = @queue.length.to_f / @max_queue_size + if queue_fullness_percentage >= 0.7 && queue_fullness_percentage < 1.0 + logger.warn("Queue is #{queue_fullness_percentage * 100}% full.") + end + if is_full + msg = 'Queue is full, dropping events. ' \ + 'Please supply :max_queue_size value when initializing the client.' + logger.error(msg) + end + end + # private: Ensures that a string is non-empty # # obj - String|Number that must be non-blank diff --git a/spec/segment/analytics/client_spec.rb b/spec/segment/analytics/client_spec.rb index 251962b..35089a8 100644 --- a/spec/segment/analytics/client_spec.rb +++ b/spec/segment/analytics/client_spec.rb @@ -306,6 +306,20 @@ class Analytics end end end + + context 'logging' do + describe '#enqueue' do + it 'logs warns and errors on queue being full' do + client = Client.new :write_key => WRITE_KEY, max_queue_size: 5 + expect(client.logger).to receive(:warn).with(/Queue is .* full/) + expect(client.logger).to receive(:error).at_least(:once).with(/Queue is full, dropping events/) + + 10.times do + client.track({ :user_id => 'user', :event => "Event" }) + end + end + end + end end end end