Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/segment/analytics/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if this should just be a warn, not error (it is recoverable after all).

end
end

# private: Ensures that a string is non-empty
#
# obj - String|Number that must be non-blank
Expand Down
2 changes: 1 addition & 1 deletion lib/segment/analytics/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')}"
Expand Down
14 changes: 14 additions & 0 deletions spec/segment/analytics/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions spec/segment/analytics/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 << {}
Expand All @@ -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|
Expand All @@ -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')
Expand Down