From 85cf794f71500b8a1e80cc891f92904b76db4333 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 16 Aug 2019 14:46:46 -0700 Subject: [PATCH 01/40] working on the scheduler --- lib/logdna.rb | 61 ++++---------- lib/logdna/client.rb | 183 ++++++++++++++++------------------------ lib/logdna/resources.rb | 2 +- 3 files changed, 87 insertions(+), 159 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 0899917..c9aca7d 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -20,50 +20,24 @@ def initialize(key, opts={}) @level = opts[:level] || 'INFO' @env = opts[:env] @meta = opts[:meta] - @@client = nil unless defined? @@client - + endpoint = opts[:endpoint] || Resources::ENDPOINT hostname = opts[:hostname] || Socket.gethostname ip = opts.key?(:ip) ? "&ip=#{opts[:ip]}" : '' mac = opts.key?(:mac) ? "&mac=#{opts[:mac]}" : '' url = "#{endpoint}?hostname=#{hostname}#{mac}#{ip}" + uri = URI(url) - begin - if (hostname.size > Resources::MAX_INPUT_LENGTH || @app.size > Resources::MAX_INPUT_LENGTH ) - raise MaxLengthExceeded.new - end - rescue MaxLengthExceeded => e + if (hostname.size > Resources::MAX_INPUT_LENGTH || @app.size > Resources::MAX_INPUT_LENGTH ) puts "Hostname or Appname is over #{Resources::MAX_INPUT_LENGTH} characters" - handle_exception(e) - return - end - - begin - uri = URI(url) - rescue URI::ValidURIRequired => e - puts "Invalid URL Endpoint: #{url}" - handle_exception(e) - return end - begin - request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json') - request.basic_auth 'username', key - rescue => e - handle_exception(e) - return - end + request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json') + request.basic_auth 'username', key @@client = Logdna::Client.new(request, uri, opts) end - def handle_exception(e) - exception_message = e.message - exception_backtrace = e.backtrace - # NOTE: should log with Ruby logger? - puts exception_message - end - def default_opts { app: @app, @@ -82,14 +56,16 @@ def level=(value) @level = value end - def log(msg=nil, opts={}) - loggerExist? - message = msg - message = yield if msg.nil? && block_given? - @response = @@client.buffer(message, default_opts.merge(opts).merge({ + def log(message, opts={}) + if(message.length == 0) + puts "Your logline cannot be empty" + return + end + message = message.to_s unless message.instance_of? String + message = message.encode("UTF-8") + @@client.write_to_buffer(message, default_opts.merge(opts).merge({ timestamp: (Time.now.to_f * 1000).to_i - })) - 'Saved' + })) end Resources::LOG_LEVELS.each do |lvl| @@ -114,13 +90,6 @@ def clear @meta = nil end - def loggerExist? - if @@client.nil? - puts "Logger Not Initialized Yet" - close - end - end - def <<(msg=nil, opts={}) self.log(msg, opts.merge({ level: '', @@ -152,7 +121,7 @@ def close at_exit do if defined? @@client and !@@client.nil? - @@client.exitout() + #@@client.exitout() end end end diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index cd102b2..0b8f0b4 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -3,6 +3,7 @@ require 'json' require 'concurrent' require 'thread' +require 'date' module Logdna class Client @@ -11,37 +12,21 @@ def initialize(request, uri, opts) @uri = uri # NOTE: buffer is in memory - @buffer = StringIO.new - @messages = [] - @buffer_over_limit = false + @buffer = [] + @buffer_byte_size = 0 - @side_buffer = StringIO.new @side_messages = [] @lock = Mutex.new - @task = nil - - # NOTE: the byte limit only affects the message, not the entire message_hash - @actual_byte_limit = opts[:flushbyte] ||= Resources::FLUSH_BYTE_LIMIT - @actual_flush_interval = opts[:flushtime] ||= Resources::FLUSH_INTERVAL + @flush_limit = opts[:flush_size] ||= Resources::FLUSH_BYTE_LIMIT + @flush_interval = opts[:flush_interval] ||= Resources::FLUSH_INTERVAL @@request = request + @timer_task = false end - def encode_message(msg) - msg = msg.to_s unless msg.instance_of? String - - begin - msg = msg.encode("UTF-8") - rescue Encoding::UndefinedConversionError => e - # NOTE: should this be raised or handled silently? - # raise e - end - msg - end - - def message_hash(msg, opts={}) - obj = { + def process_message(msg, opts={}) + processedMessage = { line: msg, app: opts[:app], level: opts[:level], @@ -49,112 +34,86 @@ def message_hash(msg, opts={}) meta: opts[:meta], timestamp: Time.now.to_i, } - obj.delete(:meta) if obj[:meta].nil? - obj + processedMessage.delete(:meta) if processedMessage[:meta].nil? + processedMessage end def create_flush_task - return @task unless @task.nil? or !@task.running? - - t = Concurrent::TimerTask.new(execution_interval: @actual_flush_interval, timeout_interval: Resources::TIMER_OUT) do |task| - if @messages.any? - # keep running if there are queued messages, but don't flush - # because the buffer is being flushed due to being over the limit - unless @buffer_over_limit - flush() - end - else - # no messages means we can kill the task - task.kill - end - end - t.execute - end - - def check_side_buffer - return if @side_buffer.size == 0 - - @buffer.write(@side_buffer.string) - @side_buffer.truncate(0) - queued_side_messages = @side_messages - @side_messages = [] - queued_side_messages.each { |message_hash_obj| @messages.push(message_hash_obj) } - end - - - # this should always be running synchronously within this thread - def buffer(msg, opts) - buffer_size = write_to_buffer(msg, opts) - unless buffer_size.nil? - process_buffer(buffer_size) + puts "calls" + timer_task = Concurrent::TimerTask.new(execution_interval: @flush_interval, timeout_interval: Resources::TIMER_OUT) do |task| + puts 'executing' + self.flush end + timer_task.execute + timer_task end def write_to_buffer(msg, opts) - return if msg.nil? - msg = encode_message(msg) - - if @lock.locked? - @side_buffer.write(msg) - @side_messages.push(message_hash(msg, opts)) - return - end - - check_side_buffer - buffer_size = @buffer.write(msg) - @messages.push(message_hash(msg, opts)) - buffer_size - end - - def queue_to_buffer(queue=@queue) - next_object = queue.shift - write_to_buffer(next_object[:msg], next_object[:opts]) - end + puts 'log received' + if @lock.try_lock + if !@side_messages.empty? + @buffer.concat(@side_messages) + end - def process_buffer(buffer_size) - if buffer_size > @actual_byte_limit - @buffer_over_limit = true - flush() - @buffer_over_limit = false + if @timer_task == false + @timer_task = Thread.new { self.create_flush_task } + @timer_task.join + puts "inside if block" + puts @timer_task.status + end + puts "in buffer method" + puts @timer_task.status + processes_message = process_message(msg, opts) + new_message_size = processes_message.to_s.bytesize + @buffer_byte_size += new_message_size + + if @flush_limit > (new_message_size + @buffer_byte_size) + @buffer.push(processes_message) + else + puts "calls from here?" + @buffer.push(processes_message) + self.flush + end else - @task = create_flush_task + @side_messages.push(process_message(msg, opts)) end end # this should be running synchronously if @buffer_over_limit i.e. called from self.buffer # else asynchronously through @task - def flush() - if defined? @@request and !@@request.nil? - request_messages = [] - @lock.synchronize do - request_messages = @messages - @buffer.truncate(0) - @messages = [] - end - return if request_messages.empty? - - real = { - e: 'ls', - ls: request_messages, - }.to_json - - @@request.body = real - @response = Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: @uri.scheme == 'https') do |http| - http.request(@@request) - end - - # don't kill @task if this was executed from self.buffer - # don't kill @task if there are queued messages - unless @buffer_over_limit || @messages.any? || @task.nil? - @task.shutdown - @task.kill - end + def flush + puts "in flush method" + puts @timer_task.status + return if @buffer.empty? + @@request.body = { + e: 'ls', + ls: @buffer, + }.to_json + + @buffer.clear + # @timer_task.shutdown if !@timer_task.nil? + puts 'log is flushed' + @response = Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: @uri.scheme == 'https') do |http| + http.request(@@request) + end + + if @response.code != '200' + @buffer.concat(@@request.ls) + @@request.body = nil + # check what is request and clear it if still contains data + puts `Error at the attempt to send the request #{@response.body}` + end + + begin + @lock.unlock + rescue + puts 'Nothing was locked' end end - def exitout() - check_side_buffer - if @messages.any? + def exitout + puts @timer_task.status + if @buffer.any? flush() end puts "Exiting LogDNA logger: Logging remaining messages" diff --git a/lib/logdna/resources.rb b/lib/logdna/resources.rb index 66d6c16..2d6c1d9 100755 --- a/lib/logdna/resources.rb +++ b/lib/logdna/resources.rb @@ -7,7 +7,7 @@ module Resources MAX_LINE_LENGTH = 32000 MAX_INPUT_LENGTH = 80 FLUSH_INTERVAL = 0.25 - TIMER_OUT = 15 + TIMER_OUT = 120 FLUSH_BYTE_LIMIT = 500000 ENDPOINT = 'https://logs.logdna.com/logs/ingest'.freeze MAC_ADDR_CHECK = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/ From 5f4be72d080c9d198a0ac2339165de9eb81f6bca Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 21 Aug 2019 21:13:04 -0700 Subject: [PATCH 02/40] savingBranch --- bin/bundle | 105 ++++++++++++++++++++++++++++++++++++++ bin/htmldiff | 29 +++++++++++ bin/ldiff | 29 +++++++++++ bin/rake | 29 +++++++++++ bin/rspec | 29 +++++++++++ bin/safe_yaml | 29 +++++++++++ lib/logdna.rb | 22 ++++---- lib/logdna/client.rb | 109 +++++++++++++++++++++------------------- lib/logdna/resources.rb | 1 + spec/logdnaTest.rb | 5 ++ 10 files changed, 323 insertions(+), 64 deletions(-) create mode 100755 bin/bundle create mode 100755 bin/htmldiff create mode 100755 bin/ldiff create mode 100755 bin/rake create mode 100755 bin/rspec create mode 100755 bin/safe_yaml create mode 100644 spec/logdnaTest.rb diff --git a/bin/bundle b/bin/bundle new file mode 100755 index 0000000..524dfd3 --- /dev/null +++ b/bin/bundle @@ -0,0 +1,105 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'bundle' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +require "rubygems" + +m = Module.new do + module_function + + def invoked_as_script? + File.expand_path($0) == File.expand_path(__FILE__) + end + + def env_var_version + ENV["BUNDLER_VERSION"] + end + + def cli_arg_version + return unless invoked_as_script? # don't want to hijack other binstubs + return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update` + bundler_version = nil + update_index = nil + ARGV.each_with_index do |a, i| + if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN + bundler_version = a + end + next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/ + bundler_version = $1 || ">= 0.a" + update_index = i + end + bundler_version + end + + def gemfile + gemfile = ENV["BUNDLE_GEMFILE"] + return gemfile if gemfile && !gemfile.empty? + + File.expand_path("../../Gemfile", __FILE__) + end + + def lockfile + lockfile = + case File.basename(gemfile) + when "gems.rb" then gemfile.sub(/\.rb$/, gemfile) + else "#{gemfile}.lock" + end + File.expand_path(lockfile) + end + + def lockfile_version + return unless File.file?(lockfile) + lockfile_contents = File.read(lockfile) + return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/ + Regexp.last_match(1) + end + + def bundler_version + @bundler_version ||= begin + env_var_version || cli_arg_version || + lockfile_version || "#{Gem::Requirement.default}.a" + end + end + + def load_bundler! + ENV["BUNDLE_GEMFILE"] ||= gemfile + + # must dup string for RG < 1.8 compatibility + activate_bundler(bundler_version.dup) + end + + def activate_bundler(bundler_version) + if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new("2.0") + bundler_version = "< 2" + end + gem_error = activation_error_handling do + gem "bundler", bundler_version + end + return if gem_error.nil? + require_error = activation_error_handling do + require "bundler/version" + end + return if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION)) + warn "Activating bundler (#{bundler_version}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`" + exit 42 + end + + def activation_error_handling + yield + nil + rescue StandardError, LoadError => e + e + end +end + +m.load_bundler! + +if m.invoked_as_script? + load Gem.bin_path("bundler", "bundle") +end diff --git a/bin/htmldiff b/bin/htmldiff new file mode 100755 index 0000000..091820c --- /dev/null +++ b/bin/htmldiff @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'htmldiff' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +require "pathname" +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", + Pathname.new(__FILE__).realpath) + +bundle_binstub = File.expand_path("../bundle", __FILE__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("diff-lcs", "htmldiff") diff --git a/bin/ldiff b/bin/ldiff new file mode 100755 index 0000000..073e19f --- /dev/null +++ b/bin/ldiff @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'ldiff' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +require "pathname" +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", + Pathname.new(__FILE__).realpath) + +bundle_binstub = File.expand_path("../bundle", __FILE__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("diff-lcs", "ldiff") diff --git a/bin/rake b/bin/rake new file mode 100755 index 0000000..9275675 --- /dev/null +++ b/bin/rake @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'rake' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +require "pathname" +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", + Pathname.new(__FILE__).realpath) + +bundle_binstub = File.expand_path("../bundle", __FILE__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("rake", "rake") diff --git a/bin/rspec b/bin/rspec new file mode 100755 index 0000000..a6c7852 --- /dev/null +++ b/bin/rspec @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'rspec' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +require "pathname" +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", + Pathname.new(__FILE__).realpath) + +bundle_binstub = File.expand_path("../bundle", __FILE__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("rspec-core", "rspec") diff --git a/bin/safe_yaml b/bin/safe_yaml new file mode 100755 index 0000000..1345fa9 --- /dev/null +++ b/bin/safe_yaml @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'safe_yaml' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +require "pathname" +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", + Pathname.new(__FILE__).realpath) + +bundle_binstub = File.expand_path("../bundle", __FILE__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("safe_yaml", "safe_yaml") diff --git a/lib/logdna.rb b/lib/logdna.rb index c9aca7d..cdbd166 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -113,16 +113,16 @@ def datetime_format(*arg) end - def close - if defined? @@client and !@@client.nil? - @@client.exitout() - end - end - - at_exit do - if defined? @@client and !@@client.nil? - #@@client.exitout() - end - end + # def close + # if defined? @@client and !@@client.nil? + # @@client.exitout() + # end + # end + # + # at_exit do + # if defined? @@client and !@@client.nil? + # @@client.exitout() + # end + # end end end diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 0b8f0b4..99a4be8 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -20,9 +20,12 @@ def initialize(request, uri, opts) @lock = Mutex.new @flush_limit = opts[:flush_size] ||= Resources::FLUSH_BYTE_LIMIT @flush_interval = opts[:flush_interval] ||= Resources::FLUSH_INTERVAL + @flush_scheduled = false + @exception_flag = false - @@request = request + @request = request @timer_task = false + @backoff_interval = opts[:backoff_period] ||= Resources::BACKOFF_PERIOD end def process_message(msg, opts={}) @@ -39,84 +42,84 @@ def process_message(msg, opts={}) end def create_flush_task - puts "calls" - timer_task = Concurrent::TimerTask.new(execution_interval: @flush_interval, timeout_interval: Resources::TIMER_OUT) do |task| - puts 'executing' - self.flush + timer_task = Concurrent::TimerTask.new(execution_interval: @flush_interval, timeout_interval: Resources::TIMER_OUT) do |task| + puts 'executing' + self.flush + end + timer_task.execute + end + + def schedule_flush + def start_timer + sleep(@exception_flag ? @backoff_period : @flush_interval) + flush end - timer_task.execute - timer_task + thread = Thread.new{ start_timer } + thread.join end def write_to_buffer(msg, opts) - puts 'log received' if @lock.try_lock if !@side_messages.empty? @buffer.concat(@side_messages) end - - if @timer_task == false - @timer_task = Thread.new { self.create_flush_task } - @timer_task.join - puts "inside if block" - puts @timer_task.status - end - puts "in buffer method" - puts @timer_task.status - processes_message = process_message(msg, opts) - new_message_size = processes_message.to_s.bytesize + processed_message = process_message(msg, opts) + new_message_size = processed_message.to_s.bytesize @buffer_byte_size += new_message_size if @flush_limit > (new_message_size + @buffer_byte_size) - @buffer.push(processes_message) + @buffer.push(processed_message) else - puts "calls from here?" - @buffer.push(processes_message) + @buffer.push(processed_message) self.flush end + + begin + @lock.unlock + rescue + puts 'Nothing was locked' + end + schedule_flush() else @side_messages.push(process_message(msg, opts)) end end - # this should be running synchronously if @buffer_over_limit i.e. called from self.buffer - # else asynchronously through @task def flush - puts "in flush method" - puts @timer_task.status return if @buffer.empty? - @@request.body = { - e: 'ls', - ls: @buffer, - }.to_json - - @buffer.clear - # @timer_task.shutdown if !@timer_task.nil? - puts 'log is flushed' - @response = Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: @uri.scheme == 'https') do |http| - http.request(@@request) - end - - if @response.code != '200' - @buffer.concat(@@request.ls) - @@request.body = nil - # check what is request and clear it if still contains data - puts `Error at the attempt to send the request #{@response.body}` - end - - begin - @lock.unlock - rescue - puts 'Nothing was locked' + if @lock.try_lock + @request.body = { + e: 'ls', + ls: @buffer.concat(@side_messages), + }.to_json + @timer_task = false + @side_messages.clear + + begin + @response = Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: @uri.scheme == 'https') do |http| + http.request(@request) + end + @exception_flag = false + rescue + puts `Error at the attempt to send the request #{@response.body if @response}` + @exception_flag = true + @side_messages.concat(@buffer) + end + @buffer.clear + + begin + @lock.unlock + rescue + puts 'Nothing was locked' + end end - end + end - def exitout - puts @timer_task.status + def exitout if @buffer.any? flush() end - puts "Exiting LogDNA logger: Logging remaining messages" + puts "Exiting LogDNA logger: Logging remaining messages" return end end diff --git a/lib/logdna/resources.rb b/lib/logdna/resources.rb index 2d6c1d9..d27094e 100755 --- a/lib/logdna/resources.rb +++ b/lib/logdna/resources.rb @@ -6,6 +6,7 @@ module Resources MAX_REQUEST_TIMEOUT = 300000 MAX_LINE_LENGTH = 32000 MAX_INPUT_LENGTH = 80 + BACKOFF_PERIOD = 60 FLUSH_INTERVAL = 0.25 TIMER_OUT = 120 FLUSH_BYTE_LIMIT = 500000 diff --git a/spec/logdnaTest.rb b/spec/logdnaTest.rb new file mode 100644 index 0000000..f7e2ee9 --- /dev/null +++ b/spec/logdnaTest.rb @@ -0,0 +1,5 @@ +describe "test" do + it "should work" do + + end +end From 4590d4e43da447c28f41509962b14120908de704 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:05:07 -0700 Subject: [PATCH 03/40] the final test is insihed --- lib/logdna.rb | 13 ------------- spec/logdnaTest.rb | 5 ----- 2 files changed, 18 deletions(-) delete mode 100644 spec/logdnaTest.rb diff --git a/lib/logdna.rb b/lib/logdna.rb index cdbd166..2801cd8 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -111,18 +111,5 @@ def datetime_format(*arg) puts "datetime_format not supported in LogDNA logger" return false end - - - # def close - # if defined? @@client and !@@client.nil? - # @@client.exitout() - # end - # end - # - # at_exit do - # if defined? @@client and !@@client.nil? - # @@client.exitout() - # end - # end end end diff --git a/spec/logdnaTest.rb b/spec/logdnaTest.rb deleted file mode 100644 index f7e2ee9..0000000 --- a/spec/logdnaTest.rb +++ /dev/null @@ -1,5 +0,0 @@ -describe "test" do - it "should work" do - - end -end From ef2c34638d6804a1db1b16dca025810c005eacd5 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:10:42 -0700 Subject: [PATCH 04/40] add bin folder to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index dd39a76..ed04e2a 100755 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ /tmp/ *.gem .DS_Store +/bin/ From e3b90ca186984492cf7ee2e9b4abb01c3a0c4fc7 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:12:21 -0700 Subject: [PATCH 05/40] remive all bin folder changes" --- bin/bundle | 105 -------------------------------------------------- bin/htmldiff | 29 -------------- bin/ldiff | 29 -------------- bin/rake | 29 -------------- bin/rspec | 29 -------------- bin/safe_yaml | 29 -------------- 6 files changed, 250 deletions(-) delete mode 100755 bin/bundle delete mode 100755 bin/htmldiff delete mode 100755 bin/ldiff delete mode 100755 bin/rake delete mode 100755 bin/rspec delete mode 100755 bin/safe_yaml diff --git a/bin/bundle b/bin/bundle deleted file mode 100755 index 524dfd3..0000000 --- a/bin/bundle +++ /dev/null @@ -1,105 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -# -# This file was generated by Bundler. -# -# The application 'bundle' is installed as part of a gem, and -# this file is here to facilitate running it. -# - -require "rubygems" - -m = Module.new do - module_function - - def invoked_as_script? - File.expand_path($0) == File.expand_path(__FILE__) - end - - def env_var_version - ENV["BUNDLER_VERSION"] - end - - def cli_arg_version - return unless invoked_as_script? # don't want to hijack other binstubs - return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update` - bundler_version = nil - update_index = nil - ARGV.each_with_index do |a, i| - if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN - bundler_version = a - end - next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/ - bundler_version = $1 || ">= 0.a" - update_index = i - end - bundler_version - end - - def gemfile - gemfile = ENV["BUNDLE_GEMFILE"] - return gemfile if gemfile && !gemfile.empty? - - File.expand_path("../../Gemfile", __FILE__) - end - - def lockfile - lockfile = - case File.basename(gemfile) - when "gems.rb" then gemfile.sub(/\.rb$/, gemfile) - else "#{gemfile}.lock" - end - File.expand_path(lockfile) - end - - def lockfile_version - return unless File.file?(lockfile) - lockfile_contents = File.read(lockfile) - return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/ - Regexp.last_match(1) - end - - def bundler_version - @bundler_version ||= begin - env_var_version || cli_arg_version || - lockfile_version || "#{Gem::Requirement.default}.a" - end - end - - def load_bundler! - ENV["BUNDLE_GEMFILE"] ||= gemfile - - # must dup string for RG < 1.8 compatibility - activate_bundler(bundler_version.dup) - end - - def activate_bundler(bundler_version) - if Gem::Version.correct?(bundler_version) && Gem::Version.new(bundler_version).release < Gem::Version.new("2.0") - bundler_version = "< 2" - end - gem_error = activation_error_handling do - gem "bundler", bundler_version - end - return if gem_error.nil? - require_error = activation_error_handling do - require "bundler/version" - end - return if require_error.nil? && Gem::Requirement.new(bundler_version).satisfied_by?(Gem::Version.new(Bundler::VERSION)) - warn "Activating bundler (#{bundler_version}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_version}'`" - exit 42 - end - - def activation_error_handling - yield - nil - rescue StandardError, LoadError => e - e - end -end - -m.load_bundler! - -if m.invoked_as_script? - load Gem.bin_path("bundler", "bundle") -end diff --git a/bin/htmldiff b/bin/htmldiff deleted file mode 100755 index 091820c..0000000 --- a/bin/htmldiff +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -# -# This file was generated by Bundler. -# -# The application 'htmldiff' is installed as part of a gem, and -# this file is here to facilitate running it. -# - -require "pathname" -ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", - Pathname.new(__FILE__).realpath) - -bundle_binstub = File.expand_path("../bundle", __FILE__) - -if File.file?(bundle_binstub) - if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ - load(bundle_binstub) - else - abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. -Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") - end -end - -require "rubygems" -require "bundler/setup" - -load Gem.bin_path("diff-lcs", "htmldiff") diff --git a/bin/ldiff b/bin/ldiff deleted file mode 100755 index 073e19f..0000000 --- a/bin/ldiff +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -# -# This file was generated by Bundler. -# -# The application 'ldiff' is installed as part of a gem, and -# this file is here to facilitate running it. -# - -require "pathname" -ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", - Pathname.new(__FILE__).realpath) - -bundle_binstub = File.expand_path("../bundle", __FILE__) - -if File.file?(bundle_binstub) - if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ - load(bundle_binstub) - else - abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. -Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") - end -end - -require "rubygems" -require "bundler/setup" - -load Gem.bin_path("diff-lcs", "ldiff") diff --git a/bin/rake b/bin/rake deleted file mode 100755 index 9275675..0000000 --- a/bin/rake +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -# -# This file was generated by Bundler. -# -# The application 'rake' is installed as part of a gem, and -# this file is here to facilitate running it. -# - -require "pathname" -ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", - Pathname.new(__FILE__).realpath) - -bundle_binstub = File.expand_path("../bundle", __FILE__) - -if File.file?(bundle_binstub) - if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ - load(bundle_binstub) - else - abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. -Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") - end -end - -require "rubygems" -require "bundler/setup" - -load Gem.bin_path("rake", "rake") diff --git a/bin/rspec b/bin/rspec deleted file mode 100755 index a6c7852..0000000 --- a/bin/rspec +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -# -# This file was generated by Bundler. -# -# The application 'rspec' is installed as part of a gem, and -# this file is here to facilitate running it. -# - -require "pathname" -ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", - Pathname.new(__FILE__).realpath) - -bundle_binstub = File.expand_path("../bundle", __FILE__) - -if File.file?(bundle_binstub) - if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ - load(bundle_binstub) - else - abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. -Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") - end -end - -require "rubygems" -require "bundler/setup" - -load Gem.bin_path("rspec-core", "rspec") diff --git a/bin/safe_yaml b/bin/safe_yaml deleted file mode 100755 index 1345fa9..0000000 --- a/bin/safe_yaml +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -# -# This file was generated by Bundler. -# -# The application 'safe_yaml' is installed as part of a gem, and -# this file is here to facilitate running it. -# - -require "pathname" -ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", - Pathname.new(__FILE__).realpath) - -bundle_binstub = File.expand_path("../bundle", __FILE__) - -if File.file?(bundle_binstub) - if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ - load(bundle_binstub) - else - abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. -Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") - end -end - -require "rubygems" -require "bundler/setup" - -load Gem.bin_path("safe_yaml", "safe_yaml") From e4300735900668a7ce9b5355d82f94a33a82fcda Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:42:04 -0700 Subject: [PATCH 06/40] add tests --- spec/client_spec.rb | 18 ++++++++ spec/spec_helper.rb | 100 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 spec/client_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/spec/client_spec.rb b/spec/client_spec.rb new file mode 100644 index 0000000..3542885 --- /dev/null +++ b/spec/client_spec.rb @@ -0,0 +1,18 @@ +require 'logdna' + +describe "Client initialization" do + it "no options provided should use the default settings" do + opts = { + :hostname=>"rubyTestHost", + :ip=>"75.10.4.81", + :mac=>"00:00:00:a1:2b:cc", + :app=>"rubyApplication", + :level=>"INFO", + :env=>"PRODUCTION", + :endpoint=>"https://logs.logdna.com/logs/ingest" + } + client = Logdna::Client.new('request', 'test.com', opts) + puts client + #client.uri.should == 'test.com' + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..251aa51 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,100 @@ +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # This setting enables warnings. It's recommended, but in some cases may + # be too noisy due to issues in dependencies. + config.warnings = true + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end From 523280466be723fbdf2726dd81e04cb96437e1c5 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:44:04 -0700 Subject: [PATCH 07/40] remove spec folder --- spec/client_spec.rb | 18 -------- spec/spec_helper.rb | 100 -------------------------------------------- 2 files changed, 118 deletions(-) delete mode 100644 spec/client_spec.rb delete mode 100644 spec/spec_helper.rb diff --git a/spec/client_spec.rb b/spec/client_spec.rb deleted file mode 100644 index 3542885..0000000 --- a/spec/client_spec.rb +++ /dev/null @@ -1,18 +0,0 @@ -require 'logdna' - -describe "Client initialization" do - it "no options provided should use the default settings" do - opts = { - :hostname=>"rubyTestHost", - :ip=>"75.10.4.81", - :mac=>"00:00:00:a1:2b:cc", - :app=>"rubyApplication", - :level=>"INFO", - :env=>"PRODUCTION", - :endpoint=>"https://logs.logdna.com/logs/ingest" - } - client = Logdna::Client.new('request', 'test.com', opts) - puts client - #client.uri.should == 'test.com' - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb deleted file mode 100644 index 251aa51..0000000 --- a/spec/spec_helper.rb +++ /dev/null @@ -1,100 +0,0 @@ -# This file was generated by the `rspec --init` command. Conventionally, all -# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. -# The generated `.rspec` file contains `--require spec_helper` which will cause -# this file to always be loaded, without a need to explicitly require it in any -# files. -# -# Given that it is always loaded, you are encouraged to keep this file as -# light-weight as possible. Requiring heavyweight dependencies from this file -# will add to the boot time of your test suite on EVERY test run, even for an -# individual file that may not need all of that loaded. Instead, consider making -# a separate helper file that requires the additional dependencies and performs -# the additional setup, and require it from the spec files that actually need -# it. -# -# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration -RSpec.configure do |config| - # rspec-expectations config goes here. You can use an alternate - # assertion/expectation library such as wrong or the stdlib/minitest - # assertions if you prefer. - config.expect_with :rspec do |expectations| - # This option will default to `true` in RSpec 4. It makes the `description` - # and `failure_message` of custom matchers include text for helper methods - # defined using `chain`, e.g.: - # be_bigger_than(2).and_smaller_than(4).description - # # => "be bigger than 2 and smaller than 4" - # ...rather than: - # # => "be bigger than 2" - expectations.include_chain_clauses_in_custom_matcher_descriptions = true - end - - # rspec-mocks config goes here. You can use an alternate test double - # library (such as bogus or mocha) by changing the `mock_with` option here. - config.mock_with :rspec do |mocks| - # Prevents you from mocking or stubbing a method that does not exist on - # a real object. This is generally recommended, and will default to - # `true` in RSpec 4. - mocks.verify_partial_doubles = true - end - - # This option will default to `:apply_to_host_groups` in RSpec 4 (and will - # have no way to turn it off -- the option exists only for backwards - # compatibility in RSpec 3). It causes shared context metadata to be - # inherited by the metadata hash of host groups and examples, rather than - # triggering implicit auto-inclusion in groups with matching metadata. - config.shared_context_metadata_behavior = :apply_to_host_groups - -# The settings below are suggested to provide a good initial experience -# with RSpec, but feel free to customize to your heart's content. -=begin - # This allows you to limit a spec run to individual examples or groups - # you care about by tagging them with `:focus` metadata. When nothing - # is tagged with `:focus`, all examples get run. RSpec also provides - # aliases for `it`, `describe`, and `context` that include `:focus` - # metadata: `fit`, `fdescribe` and `fcontext`, respectively. - config.filter_run_when_matching :focus - - # Allows RSpec to persist some state between runs in order to support - # the `--only-failures` and `--next-failure` CLI options. We recommend - # you configure your source control system to ignore this file. - config.example_status_persistence_file_path = "spec/examples.txt" - - # Limits the available syntax to the non-monkey patched syntax that is - # recommended. For more details, see: - # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ - # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ - # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode - config.disable_monkey_patching! - - # This setting enables warnings. It's recommended, but in some cases may - # be too noisy due to issues in dependencies. - config.warnings = true - - # Many RSpec users commonly either run the entire suite or an individual - # file, and it's useful to allow more verbose output when running an - # individual spec file. - if config.files_to_run.one? - # Use the documentation formatter for detailed output, - # unless a formatter has already been configured - # (e.g. via a command-line flag). - config.default_formatter = "doc" - end - - # Print the 10 slowest examples and example groups at the - # end of the spec run, to help surface which specs are running - # particularly slow. - config.profile_examples = 10 - - # Run specs in random order to surface order dependencies. If you find an - # order dependency and want to debug it, you can fix the order by providing - # the seed, which is printed after each run. - # --seed 1234 - config.order = :random - - # Seed global randomization in this process using the `--seed` CLI option. - # Setting this allows you to use `--seed` to deterministically reproduce - # test failures related to randomization by passing the same `--seed` value - # as the one that triggered the failure. - Kernel.srand config.seed -=end -end From 1258ed4b7d4ba747cae32351aac0c2c15d58970e Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 4 Sep 2019 12:41:24 -0700 Subject: [PATCH 08/40] remove unused method --- lib/logdna/client.rb | 12 ++---------- lib/logdna/resources.rb | 3 +-- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 99a4be8..d85cd2c 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -25,7 +25,7 @@ def initialize(request, uri, opts) @request = request @timer_task = false - @backoff_interval = opts[:backoff_period] ||= Resources::BACKOFF_PERIOD + @backoff_interval = opts[:RETRY_TIMEOUT] ||= Resources::RETRY_TIMEOUT end def process_message(msg, opts={}) @@ -41,17 +41,9 @@ def process_message(msg, opts={}) processedMessage end - def create_flush_task - timer_task = Concurrent::TimerTask.new(execution_interval: @flush_interval, timeout_interval: Resources::TIMER_OUT) do |task| - puts 'executing' - self.flush - end - timer_task.execute - end - def schedule_flush def start_timer - sleep(@exception_flag ? @backoff_period : @flush_interval) + sleep(@exception_flag ? @RETRY_TIMEOUT : @flush_interval) flush end thread = Thread.new{ start_timer } diff --git a/lib/logdna/resources.rb b/lib/logdna/resources.rb index d27094e..b1323c4 100755 --- a/lib/logdna/resources.rb +++ b/lib/logdna/resources.rb @@ -6,9 +6,8 @@ module Resources MAX_REQUEST_TIMEOUT = 300000 MAX_LINE_LENGTH = 32000 MAX_INPUT_LENGTH = 80 - BACKOFF_PERIOD = 60 + RETRY_TIMEOUT = 60 FLUSH_INTERVAL = 0.25 - TIMER_OUT = 120 FLUSH_BYTE_LIMIT = 500000 ENDPOINT = 'https://logs.logdna.com/logs/ingest'.freeze MAC_ADDR_CHECK = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/ From bfef45f988f2f253d06e2891507592e703d6c111 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 4 Sep 2019 12:57:47 -0700 Subject: [PATCH 09/40] add the flush flag check --- lib/logdna/client.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index d85cd2c..f7eb5f0 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -25,7 +25,7 @@ def initialize(request, uri, opts) @request = request @timer_task = false - @backoff_interval = opts[:RETRY_TIMEOUT] ||= Resources::RETRY_TIMEOUT + @retry_timeout = opts[:retry_timeout] ||= Resources::RETRY_TIMEOUT end def process_message(msg, opts={}) @@ -42,8 +42,9 @@ def process_message(msg, opts={}) end def schedule_flush + @flush_scheduled = true def start_timer - sleep(@exception_flag ? @RETRY_TIMEOUT : @flush_interval) + sleep(@exception_flag ? @retry_timeout : @flush_interval) flush end thread = Thread.new{ start_timer } @@ -71,7 +72,7 @@ def write_to_buffer(msg, opts) rescue puts 'Nothing was locked' end - schedule_flush() + schedule_flush() if !@flush_scheduled else @side_messages.push(process_message(msg, opts)) end @@ -93,10 +94,11 @@ def flush end @exception_flag = false rescue - puts `Error at the attempt to send the request #{@response.body if @response}` + p "Error at the attempt to send the request #{@response.body if @response}" @exception_flag = true @side_messages.concat(@buffer) end + @flush_scheduled = false @buffer.clear begin From 0a7712752c89717d35248c9462d2d5b663410006 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 4 Sep 2019 14:38:54 -0700 Subject: [PATCH 10/40] not working here?? --- lib/logdna.rb | 5 ++--- lib/logdna/client.rb | 3 +-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 2801cd8..d829d90 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -57,12 +57,11 @@ def level=(value) end def log(message, opts={}) - if(message.length == 0) + if (message.length == 0) puts "Your logline cannot be empty" return end - message = message.to_s unless message.instance_of? String - message = message.encode("UTF-8") + message = message.to_s.encode("UTF-8") @@client.write_to_buffer(message, default_opts.merge(opts).merge({ timestamp: (Time.now.to_f * 1000).to_i })) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index f7eb5f0..6199eb8 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -70,7 +70,6 @@ def write_to_buffer(msg, opts) begin @lock.unlock rescue - puts 'Nothing was locked' end schedule_flush() if !@flush_scheduled else @@ -79,6 +78,7 @@ def write_to_buffer(msg, opts) end def flush + puts (@buffer) return if @buffer.empty? if @lock.try_lock @request.body = { @@ -104,7 +104,6 @@ def flush begin @lock.unlock rescue - puts 'Nothing was locked' end end end From ad1636d5b0e1fe7cb9b3f7d8d1da6e52da60bbb1 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 4 Sep 2019 15:01:26 -0700 Subject: [PATCH 11/40] not working chnges --- lib/logdna.rb | 2 +- lib/logdna/client.rb | 13 ++----------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index d829d90..4495142 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -33,7 +33,7 @@ def initialize(key, opts={}) end request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json') - request.basic_auth 'username', key + request.basic_auth('username', key) @@client = Logdna::Client.new(request, uri, opts) end diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 6199eb8..de070f7 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -24,7 +24,6 @@ def initialize(request, uri, opts) @exception_flag = false @request = request - @timer_task = false @retry_timeout = opts[:retry_timeout] ||= Resources::RETRY_TIMEOUT end @@ -66,11 +65,8 @@ def write_to_buffer(msg, opts) @buffer.push(processed_message) self.flush end + @lock.unlock if @lock.locked? - begin - @lock.unlock - rescue - end schedule_flush() if !@flush_scheduled else @side_messages.push(process_message(msg, opts)) @@ -78,14 +74,12 @@ def write_to_buffer(msg, opts) end def flush - puts (@buffer) return if @buffer.empty? if @lock.try_lock @request.body = { e: 'ls', ls: @buffer.concat(@side_messages), }.to_json - @timer_task = false @side_messages.clear begin @@ -101,10 +95,7 @@ def flush @flush_scheduled = false @buffer.clear - begin - @lock.unlock - rescue - end + @lock.unlock if @lock.locked? end end From e314f62e78b27b5433a2f8bbe85a5ed78c6b69b6 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 5 Sep 2019 14:21:12 -0700 Subject: [PATCH 12/40] address the comments and improve the excpetion handling logic --- lib/logdna/client.rb | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index de070f7..d1a0f4d 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -81,21 +81,35 @@ def flush ls: @buffer.concat(@side_messages), }.to_json @side_messages.clear + @flush_scheduled = false begin @response = Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: @uri.scheme == 'https') do |http| http.request(@request) end + + if(@response.is_a?(Net::HTTPForbidden)) + p "Please provide a valid ingestion key" + elsif(!@response.is_a?(Net::HTTPSuccess)) + p "The response is not successful #{}" + end @exception_flag = false - rescue - p "Error at the attempt to send the request #{@response.body if @response}" + rescue SocketError + p "Network connectivity issue" + @exception_flag = true + @side_messages.concat(@buffer) + rescue Errno::ECONNREFUSED => e + puts "The server is down. #{e.message}" @exception_flag = true @side_messages.concat(@buffer) + rescue Timeout::Error => e + puts "Timeout error occurred. #{e.message}" + @exception_flag = true + @side_messages.concat(@buffer) + ensure + @buffer.clear + @lock.unlock if @lock.locked? end - @flush_scheduled = false - @buffer.clear - - @lock.unlock if @lock.locked? end end From 00d652c33d274b3ad7aaefe9d4b7d518b7f864e6 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 5 Sep 2019 16:41:50 -0700 Subject: [PATCH 13/40] add rubycop config file and run it --- .rubocop.yml | 124 +++++++++++++++++++++++++++++++++++ .ruby-version | 2 +- lib/logdna/client.rb | 152 ++++++++++++++++++++++--------------------- 3 files changed, 204 insertions(+), 74 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..c56aab3 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,124 @@ +# Commonly used screens these days easily fit more than 80 characters. +Metrics/LineLength: + Max: 120 + +# Too short methods lead to extraction of single-use methods, which can make +# the code easier to read (by naming things), but can also clutter the class +Metrics/MethodLength: + Max: 40 + +# The guiding principle of classes is SRP, SRP can't be accurately measured by LoC +Metrics/ClassLength: + Max: 1500 + +Metrics/AbcSize: + Max: 40 + +Metrics/CyclomaticComplexity: + Max: 40 + +Metrics/PerceivedComplexity: + Max: 40 + +# No space makes the method definition shorter and differentiates +# from a regular assignment. +Style/Layout: + EnforcedStyle: no_space + EnforcedHashRocketStyle: table + EnforcedColonStyle: table + SpaceBeforeBlockParameters: false + +# Single quotes being faster is hardly measurable and only affects parse time. +# Enforcing double quotes reduces the times where you need to change them +# when introducing an interpolation. Use single quotes only if their semantics +# are needed. +Style/StringLiterals: + EnforcedStyle: double_quotes + +# We do not need to support Ruby 1.9, so this is good to use. +Style/SymbolArray: + Enabled: true + +# Mixing the styles looks just silly. +Style/HashSyntax: + EnforcedStyle: ruby19_no_mixed_keys + +# has_key? and has_value? are far more readable than key? and value? +Style/PreferredHashMethods: + Enabled: false + +# String#% is by far the least verbose and only object oriented variant. +Style/FormatString: + EnforcedStyle: percent + +Style/CollectionMethods: + Enabled: true + PreferredMethods: + # inject seems more common in the community. + reduce: "inject" + + +# Either allow this style or don't. Marking it as safe with parenthesis +# is silly. Let's try to live without them for now. +Style/ParenthesesAroundCondition: + AllowSafeAssignment: false +Lint/AssignmentInCondition: + AllowSafeAssignment: false + +# A specialized exception class will take one or more arguments and construct the message from it. +# So both variants make sense. +Style/RaiseArgs: + Enabled: false + +# Fail is an alias of raise. Avoid aliases, it's more cognitive load for no gain. +# The argument that fail should be used to abort the program is wrong too, +# there's Kernel#abort for that. +Style/SignalException: + EnforcedStyle: only_raise + +# Suppressing exceptions can be perfectly fine, and be it to avoid to +# explicitly type nil into the rescue since that's what you want to return, +# or suppressing LoadError for optional dependencies +Lint/HandleExceptions: + Enabled: false + +# { ... } for multi-line blocks is okay, follow Weirichs rule instead: +# https://web.archive.org/web/20140221124509/http://onestepback.org/index.cgi/Tech/Ruby/BraceVsDoEnd.rdoc +Style/BlockDelimiters: + Enabled: false + +# do / end blocks should be used for side effects, +# methods that run a block for side effects and have +# a useful return value are rare, assign the return +# value to a local variable for those cases. +Style/MethodCalledOnDoEndBlock: + Enabled: true + +# Enforcing the names of variables? To single letter ones? Just no. +Style/SingleLineBlockParams: + Enabled: false + +# Shadowing outer local variables with block parameters is often useful +# to not reinvent a new name for the same thing, it highlights the relation +# between the outer variable and the parameter. The cases where it's actually +# confusing are rare, and usually bad for other reasons already, for example +# because the method is too long. +Lint/ShadowingOuterLocalVariable: + Enabled: false + +# Check with yard instead. +Style/Documentation: + Enabled: false + +# This is just silly. Calling the argument `other` in all cases makes no sense. +Naming/BinaryOperatorParameterName: + Enabled: false + +# There are valid cases, for example debugging Cucumber steps, +# also they'll fail CI anyway +Lint/Debugger: + Enabled: false + +# Style preference +Style/MethodDefParentheses: + Enabled: false diff --git a/.ruby-version b/.ruby-version index ccbccc3..24ba9a3 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.2.0 +2.7.0 diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index d1a0f4d..3d0d5b8 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -1,13 +1,13 @@ -require 'net/http' -require 'socket' -require 'json' -require 'concurrent' -require 'thread' -require 'date' +# frozen_string_literal: true + +require "net/http" +require "socket" +require "json" +require "concurrent" +require "date" module Logdna class Client - def initialize(request, uri, opts) @uri = uri @@ -27,98 +27,104 @@ def initialize(request, uri, opts) @retry_timeout = opts[:retry_timeout] ||= Resources::RETRY_TIMEOUT end - def process_message(msg, opts={}) - processedMessage = { + def process_message(msg, opts = {}) + processed_message = { line: msg, app: opts[:app], level: opts[:level], env: opts[:env], meta: opts[:meta], - timestamp: Time.now.to_i, + timestamp: Time.now.to_i } - processedMessage.delete(:meta) if processedMessage[:meta].nil? - processedMessage + processed_message.delete(:meta) if processed_message[:meta].nil? + processed_message end def schedule_flush @flush_scheduled = true - def start_timer + start_timer = lambda { sleep(@exception_flag ? @retry_timeout : @flush_interval) flush - end - thread = Thread.new{ start_timer } + } + thread = Thread.new { start_timer } thread.join end def write_to_buffer(msg, opts) if @lock.try_lock - if !@side_messages.empty? - @buffer.concat(@side_messages) - end - processed_message = process_message(msg, opts) - new_message_size = processed_message.to_s.bytesize - @buffer_byte_size += new_message_size - - if @flush_limit > (new_message_size + @buffer_byte_size) - @buffer.push(processed_message) - else - @buffer.push(processed_message) - self.flush - end - @lock.unlock if @lock.locked? - - schedule_flush() if !@flush_scheduled + @buffer.concat(@side_messages) unless @side_messages.empty? + processed_message = process_message(msg, opts) + new_message_size = processed_message.to_s.bytesize + + @buffer_byte_size += new_message_size + @buffer.push(processed_message) + + @lock.unlock if @lock.locked? + + flush if @flush_limit <= @buffer_byte_size + + schedule_flush unless @flush_scheduled else - @side_messages.push(process_message(msg, opts)) + @side_messages.push(process_message(msg, opts)) + end + end + + def send_request + @request.body = { + e: "ls", + ls: @buffer.concat(@side_messages) + }.to_json + @side_messages.clear + + begin + @response = Net::HTTP.start( + @uri.hostname, + @uri.port, + use_ssl: @uri.scheme == "https" + ) do |http| + http.request(@request) + end + + if @response.is_a?(Net::HTTPForbidden) + puts "Please provide a valid ingestion key" + elsif !@response.is_a?(Net::HTTPSuccess) + puts "The response is not successful " + end + + @exception_flag = false + rescue SocketError + puts "Network connectivity issue" + @exception_flag = true + @side_messages.concat(@buffer) + rescue Errno::ECONNREFUSED => e + puts "The server is down. #{e.message}" + @exception_flag = true + @side_messages.concat(@buffer) + rescue Timeout::Error => e + puts "Timeout error occurred. #{e.message}" + @exception_flag = true + @side_messages.concat(@buffer) + ensure + @buffer.clear + @lock.unlock if @lock.locked? end end def flush + @flush_scheduled = false return if @buffer.empty? + if @lock.try_lock - @request.body = { - e: 'ls', - ls: @buffer.concat(@side_messages), - }.to_json - @side_messages.clear - @flush_scheduled = false - - begin - @response = Net::HTTP.start(@uri.hostname, @uri.port, use_ssl: @uri.scheme == 'https') do |http| - http.request(@request) - end - - if(@response.is_a?(Net::HTTPForbidden)) - p "Please provide a valid ingestion key" - elsif(!@response.is_a?(Net::HTTPSuccess)) - p "The response is not successful #{}" - end - @exception_flag = false - rescue SocketError - p "Network connectivity issue" - @exception_flag = true - @side_messages.concat(@buffer) - rescue Errno::ECONNREFUSED => e - puts "The server is down. #{e.message}" - @exception_flag = true - @side_messages.concat(@buffer) - rescue Timeout::Error => e - puts "Timeout error occurred. #{e.message}" - @exception_flag = true - @side_messages.concat(@buffer) - ensure - @buffer.clear - @lock.unlock if @lock.locked? - end + send_request + else + schedule_flush end - end + end - def exitout - if @buffer.any? - flush() - end - puts "Exiting LogDNA logger: Logging remaining messages" - return + def exitout + flush if @buffer.any? + puts "Exiting LogDNA logger: Logging remaining messages" + nil end end end From 25b7176f7ca05564b227d0e114283330f05244af Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 5 Sep 2019 16:52:00 -0700 Subject: [PATCH 14/40] change the oprand --- lib/logdna/client.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 3d0d5b8..c574ee8 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -18,8 +18,8 @@ def initialize(request, uri, opts) @side_messages = [] @lock = Mutex.new - @flush_limit = opts[:flush_size] ||= Resources::FLUSH_BYTE_LIMIT - @flush_interval = opts[:flush_interval] ||= Resources::FLUSH_INTERVAL + @flush_limit = opts[:flush_size] ? opts[:flush_size] : Resources::FLUSH_BYTE_LIMIT + @flush_interval = opts[:flush_interval] ? opts[:flush_interval] : Resources::FLUSH_INTERVAL @flush_scheduled = false @exception_flag = false From ca85153ef0da3ff91724901f1b273ba3bfe5ee90 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 5 Sep 2019 16:52:32 -0700 Subject: [PATCH 15/40] forgot about one more --- lib/logdna/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index c574ee8..1cce734 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -24,7 +24,7 @@ def initialize(request, uri, opts) @exception_flag = false @request = request - @retry_timeout = opts[:retry_timeout] ||= Resources::RETRY_TIMEOUT + @retry_timeout = opts[:retry_timeout] ? opts[:retry_timeout] : Resources::RETRY_TIMEOUT end def process_message(msg, opts = {}) From 88f86ccc0679c677cd0dd234b8906c6d8cedbe33 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 27 Sep 2019 14:01:23 -0700 Subject: [PATCH 16/40] address comments --- lib/logdna.rb | 19 ++++++++++++++----- lib/logdna/client.rb | 6 +++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 4495142..75cacbc 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -23,19 +23,22 @@ def initialize(key, opts={}) endpoint = opts[:endpoint] || Resources::ENDPOINT hostname = opts[:hostname] || Socket.gethostname + + if (hostname.size > Resources::MAX_INPUT_LENGTH || @app.size > Resources::MAX_INPUT_LENGTH ) + puts "Hostname or Appname is over #{Resources::MAX_INPUT_LENGTH} characters" + return + end + ip = opts.key?(:ip) ? "&ip=#{opts[:ip]}" : '' mac = opts.key?(:mac) ? "&mac=#{opts[:mac]}" : '' url = "#{endpoint}?hostname=#{hostname}#{mac}#{ip}" uri = URI(url) - if (hostname.size > Resources::MAX_INPUT_LENGTH || @app.size > Resources::MAX_INPUT_LENGTH ) - puts "Hostname or Appname is over #{Resources::MAX_INPUT_LENGTH} characters" - end request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json') request.basic_auth('username', key) - @@client = Logdna::Client.new(request, uri, opts) + @client = Logdna::Client.new(request, uri, opts) end def default_opts @@ -62,7 +65,7 @@ def log(message, opts={}) return end message = message.to_s.encode("UTF-8") - @@client.write_to_buffer(message, default_opts.merge(opts).merge({ + @client.write_to_buffer(message, default_opts.merge(opts).merge({ timestamp: (Time.now.to_f * 1000).to_i })) end @@ -110,5 +113,11 @@ def datetime_format(*arg) puts "datetime_format not supported in LogDNA logger" return false end + + def close + if defined? @client and !@client.nil? + @client.exitout() + end + end end end diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 1cce734..e92f783 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -34,7 +34,7 @@ def process_message(msg, opts = {}) level: opts[:level], env: opts[:env], meta: opts[:meta], - timestamp: Time.now.to_i + timestamp: Time.now.to_i, } processed_message.delete(:meta) if processed_message[:meta].nil? processed_message @@ -112,7 +112,7 @@ def send_request def flush @flush_scheduled = false - return if @buffer.empty? + return if @buffer.empty? && @side_messages.empty? if @lock.try_lock send_request @@ -122,7 +122,7 @@ def flush end def exitout - flush if @buffer.any? + flush if @buffer.any? || @side_messages.any? puts "Exiting LogDNA logger: Logging remaining messages" nil end From 78e24c66d60c164d30c803d2bce3831d2822e2f3 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 27 Sep 2019 14:05:33 -0700 Subject: [PATCH 17/40] lints --- lib/logdna.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 75cacbc..1aec3b7 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -116,7 +116,7 @@ def datetime_format(*arg) def close if defined? @client and !@client.nil? - @client.exitout() + @client.exitout end end end From a8c3ab1e3d7d5e920a2b4a96db9cce5545fa1425 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 30 Sep 2019 15:59:33 -0700 Subject: [PATCH 18/40] change the locking logic ang change linting rules --- .rubocop.yml | 14 ++++--- lib/logdna.rb | 89 ++++++++++++++++++++-------------------- lib/logdna/client.rb | 96 +++++++++++++++++++++----------------------- 3 files changed, 100 insertions(+), 99 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c56aab3..cbec8f3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -5,7 +5,7 @@ Metrics/LineLength: # Too short methods lead to extraction of single-use methods, which can make # the code easier to read (by naming things), but can also clutter the class Metrics/MethodLength: - Max: 40 + Max: 100 # The guiding principle of classes is SRP, SRP can't be accurately measured by LoC Metrics/ClassLength: @@ -22,11 +22,10 @@ Metrics/PerceivedComplexity: # No space makes the method definition shorter and differentiates # from a regular assignment. -Style/Layout: - EnforcedStyle: no_space - EnforcedHashRocketStyle: table - EnforcedColonStyle: table - SpaceBeforeBlockParameters: false + +Layout/AccessModifierIndentation: + Enabled: true + IndentationWidth: 4 # Single quotes being faster is hardly measurable and only affects parse time. # Enforcing double quotes reduces the times where you need to change them @@ -122,3 +121,6 @@ Lint/Debugger: # Style preference Style/MethodDefParentheses: Enabled: false + +Style/TrailingCommaInHashLiteral: + Enabled: false diff --git a/lib/logdna.rb b/lib/logdna.rb index 1aec3b7..aecde8f 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -1,10 +1,11 @@ #!/usr/bin/env ruby -# encoding: utf-8 +# frozen_string_literal: true + # require 'singleton' -require 'socket' -require 'uri' -require_relative 'logdna/client.rb' -require_relative 'logdna/resources.rb' +require "socket" +require "uri" +require_relative "logdna/client.rb" +require_relative "logdna/resources.rb" module Logdna class ValidURLRequired < ArgumentError; end class MaxLengthExceeded < ArgumentError; end @@ -15,28 +16,27 @@ class Ruby < ::Logger Logger::TRACE = 5 attr_accessor :level, :app, :env, :meta - def initialize(key, opts={}) - @app = opts[:app] || 'default' - @level = opts[:level] || 'INFO' + def initialize(key, opts = {}) + @app = opts[:app] || "default" + @level = opts[:level] || "INFO" @env = opts[:env] @meta = opts[:meta] endpoint = opts[:endpoint] || Resources::ENDPOINT hostname = opts[:hostname] || Socket.gethostname - if (hostname.size > Resources::MAX_INPUT_LENGTH || @app.size > Resources::MAX_INPUT_LENGTH ) + if hostname.size > Resources::MAX_INPUT_LENGTH || @app.size > Resources::MAX_INPUT_LENGTH puts "Hostname or Appname is over #{Resources::MAX_INPUT_LENGTH} characters" return end - ip = opts.key?(:ip) ? "&ip=#{opts[:ip]}" : '' - mac = opts.key?(:mac) ? "&mac=#{opts[:mac]}" : '' + ip = opts.key?(:ip) ? "&ip=#{opts[:ip]}" : "" + mac = opts.key?(:mac) ? "&mac=#{opts[:mac]}" : "" url = "#{endpoint}?hostname=#{hostname}#{mac}#{ip}" uri = URI(url) - - request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json') - request.basic_auth('username', key) + request = Net::HTTP::Post.new(uri.request_uri, "Content-Type" => "application/json") + request.basic_auth("username", key) @client = Logdna::Client.new(request, uri, opts) end @@ -50,7 +50,7 @@ def default_opts } end - def level=(value) + def assign_level=(value) if value.is_a? Numeric @level = Resources::LOG_LEVELS[value] return @@ -59,65 +59,68 @@ def level=(value) @level = value end - def log(message, opts={}) - if (message.length == 0) + def log(message, opts = {}) + if message.empty? puts "Your logline cannot be empty" return end message = message.to_s.encode("UTF-8") - @client.write_to_buffer(message, default_opts.merge(opts).merge({ - timestamp: (Time.now.to_f * 1000).to_i - })) + @client.write_to_buffer(message, default_opts.merge(opts).merge( + timestamp: (Time.now.to_f * 1000).to_i + )) end Resources::LOG_LEVELS.each do |lvl| name = lvl.downcase - define_method name do |msg=nil, opts={}, &block| - self.log(msg, opts.merge({ - level: lvl, - }), &block) + define_method name do |msg = nil, opts = {}, &block| + log(msg, opts.merge( + level: lvl + ), &block) end define_method "#{name}?" do - return Resources::LOG_LEVELS[self.level] == lvl if self.level.is_a? Numeric - self.level == lvl + return Resources::LOG_LEVELS[level] == lvl if level.is_a? Numeric + + assign_level == lvl end end def clear - @app = 'default' - @level = 'INFO' + @app = "default" + @level = "INFO" @env = nil @meta = nil end - def <<(msg=nil, opts={}) - self.log(msg, opts.merge({ - level: '', - })) + def <<(msg = nil, opts = {}) + log(msg, opts.merge( + level: "" + )) end - def add(*arg) + def add(*_arg) puts "add not supported in LogDNA logger" - return false + false end - def unknown(msg=nil, opts={}) - self.log(msg, opts.merge({ - level: 'UNKNOWN', - })) + def unknown(msg = nil, opts = {}) + log(msg, opts.merge( + level: "UNKNOWN" + )) end - def datetime_format(*arg) + def datetime_format(*_arg) puts "datetime_format not supported in LogDNA logger" - return false + false end def close - if defined? @client and !@client.nil? - @client.exitout - end + @client.exitout if defined? @client && !@client.nil? + end + + def at_exit + @client.exitout if defined? @client && !@client.nil? end end end diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index e92f783..116f350 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -18,13 +18,13 @@ def initialize(request, uri, opts) @side_messages = [] @lock = Mutex.new - @flush_limit = opts[:flush_size] ? opts[:flush_size] : Resources::FLUSH_BYTE_LIMIT - @flush_interval = opts[:flush_interval] ? opts[:flush_interval] : Resources::FLUSH_INTERVAL + @flush_limit = opts[:flush_size] || Resources::FLUSH_BYTE_LIMIT + @flush_interval = opts[:flush_interval] || Resources::FLUSH_INTERVAL @flush_scheduled = false @exception_flag = false @request = request - @retry_timeout = opts[:retry_timeout] ? opts[:retry_timeout] : Resources::RETRY_TIMEOUT + @retry_timeout = opts[:retry_timeout] || Resources::RETRY_TIMEOUT end def process_message(msg, opts = {}) @@ -58,55 +58,55 @@ def write_to_buffer(msg, opts) @buffer_byte_size += new_message_size @buffer.push(processed_message) - - @lock.unlock if @lock.locked? - - flush if @flush_limit <= @buffer_byte_size - - schedule_flush unless @flush_scheduled else @side_messages.push(process_message(msg, opts)) end + @lock.unlock if @lock.locked? + + flush if @flush_limit <= @buffer_byte_size + schedule_flush unless @flush_scheduled end def send_request - @request.body = { - e: "ls", - ls: @buffer.concat(@side_messages) - }.to_json - @side_messages.clear - - begin - @response = Net::HTTP.start( - @uri.hostname, - @uri.port, - use_ssl: @uri.scheme == "https" - ) do |http| - http.request(@request) - end - - if @response.is_a?(Net::HTTPForbidden) - puts "Please provide a valid ingestion key" - elsif !@response.is_a?(Net::HTTPSuccess) - puts "The response is not successful " + if !@lock.try_lock + schedule_flush + else + @request.body = { + e: "ls", + ls: @buffer.concat(@side_messages) + }.to_json + @side_messages.clear + begin + @response = Net::HTTP.start( + @uri.hostname, + @uri.port, + use_ssl: @uri.scheme == "https" + ) do |http| + http.request(@request) + end + + if @response.is_a?(Net::HTTPForbidden) + puts "Please provide a valid ingestion key" + elsif !@response.is_a?(Net::HTTPSuccess) + puts "The response is not successful " + end + @exception_flag = false + rescue SocketError + print "Network connectivity issue" + @exception_flag = true + @side_messages.concat(@buffer) + rescue Errno::ECONNREFUSED => e + print "The server is down. #{e.message}" + @exception_flag = true + @side_messages.concat(@buffer) + rescue Timeout::Error => e + print "Timeout error occurred. #{e.message}" + @exception_flag = true + @side_messages.concat(@buffer) + ensure + @buffer.clear + @lock.unlock if @lock.locked? end - - @exception_flag = false - rescue SocketError - puts "Network connectivity issue" - @exception_flag = true - @side_messages.concat(@buffer) - rescue Errno::ECONNREFUSED => e - puts "The server is down. #{e.message}" - @exception_flag = true - @side_messages.concat(@buffer) - rescue Timeout::Error => e - puts "Timeout error occurred. #{e.message}" - @exception_flag = true - @side_messages.concat(@buffer) - ensure - @buffer.clear - @lock.unlock if @lock.locked? end end @@ -114,11 +114,7 @@ def flush @flush_scheduled = false return if @buffer.empty? && @side_messages.empty? - if @lock.try_lock - send_request - else - schedule_flush - end + send_request end def exitout From a594976078d970a73fda1a906886e86d7c45e46c Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 30 Sep 2019 16:47:38 -0700 Subject: [PATCH 19/40] add lamda for excpetion handling --- lib/logdna/client.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 116f350..f3075c3 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -76,6 +76,13 @@ def send_request ls: @buffer.concat(@side_messages) }.to_json @side_messages.clear + + handleExcpetion = lambda(message) do + puts message + @exception_flag = true + @side_messages.concat(@buffer) + end + begin @response = Net::HTTP.start( @uri.hostname, @@ -92,17 +99,11 @@ def send_request end @exception_flag = false rescue SocketError - print "Network connectivity issue" - @exception_flag = true - @side_messages.concat(@buffer) + handleExcpetion.call("Network connectivity issue") rescue Errno::ECONNREFUSED => e - print "The server is down. #{e.message}" - @exception_flag = true - @side_messages.concat(@buffer) + handleExcpetion.call("The server is down. #{e.message}") rescue Timeout::Error => e - print "Timeout error occurred. #{e.message}" - @exception_flag = true - @side_messages.concat(@buffer) + handleExcpetion.call("Timeout error occurred. #{e.message}") ensure @buffer.clear @lock.unlock if @lock.locked? From 984b4c3aaeef262d99296db854b51bb8f88df1da Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 30 Sep 2019 18:00:39 -0700 Subject: [PATCH 20/40] check lock owning --- lib/logdna/client.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index f3075c3..2df809a 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -61,7 +61,7 @@ def write_to_buffer(msg, opts) else @side_messages.push(process_message(msg, opts)) end - @lock.unlock if @lock.locked? + @lock.unlock if @lock.locked? && @lock.owned? flush if @flush_limit <= @buffer_byte_size schedule_flush unless @flush_scheduled @@ -77,7 +77,7 @@ def send_request }.to_json @side_messages.clear - handleExcpetion = lambda(message) do + handleExcpetion = lambda do |message| puts message @exception_flag = true @side_messages.concat(@buffer) @@ -91,7 +91,7 @@ def send_request ) do |http| http.request(@request) end - + if @response.is_a?(Net::HTTPForbidden) puts "Please provide a valid ingestion key" elsif !@response.is_a?(Net::HTTPSuccess) @@ -106,7 +106,7 @@ def send_request handleExcpetion.call("Timeout error occurred. #{e.message}") ensure @buffer.clear - @lock.unlock if @lock.locked? + @lock.unlock if @lock.locked? && @lock.owned? end end end From 14727ebeda0d8e860912d978f20f9e1cc45bc61f Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Tue, 1 Oct 2019 12:10:36 -0700 Subject: [PATCH 21/40] fix the bug and add the lock to the side messages --- lib/logdna/client.rb | 47 ++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 2df809a..aa34015 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -18,6 +18,7 @@ def initialize(request, uri, opts) @side_messages = [] @lock = Mutex.new + @side_message_lock = Mutex.new @flush_limit = opts[:flush_size] || Resources::FLUSH_BYTE_LIMIT @flush_interval = opts[:flush_interval] || Resources::FLUSH_INTERVAL @flush_scheduled = false @@ -44,7 +45,7 @@ def schedule_flush @flush_scheduled = true start_timer = lambda { sleep(@exception_flag ? @retry_timeout : @flush_interval) - flush + flush if @flush_scheduled } thread = Thread.new { start_timer } thread.join @@ -52,35 +53,38 @@ def schedule_flush def write_to_buffer(msg, opts) if @lock.try_lock - @buffer.concat(@side_messages) unless @side_messages.empty? processed_message = process_message(msg, opts) new_message_size = processed_message.to_s.bytesize - - @buffer_byte_size += new_message_size @buffer.push(processed_message) + @buffer_byte_size += new_message_size + @lock.unlock + + flush if @flush_limit <= @buffer_byte_size + schedule_flush unless @flush_scheduled else - @side_messages.push(process_message(msg, opts)) + @side_message_lock.synchronize do + @side_messages.push(process_message(msg, opts)) + end end - @lock.unlock if @lock.locked? && @lock.owned? - - flush if @flush_limit <= @buffer_byte_size - schedule_flush unless @flush_scheduled end def send_request - if !@lock.try_lock - schedule_flush - else + @side_message_lock.synchronize do + @buffer.concat(@side_messages) + @side_messages.clear + end + @request.body = { e: "ls", - ls: @buffer.concat(@side_messages) + ls: @buffer }.to_json - @side_messages.clear handleExcpetion = lambda do |message| puts message @exception_flag = true - @side_messages.concat(@buffer) + @side_message_lock.synchronize do + @side_messages.concat(@buffer) + end end begin @@ -91,7 +95,6 @@ def send_request ) do |http| http.request(@request) end - if @response.is_a?(Net::HTTPForbidden) puts "Please provide a valid ingestion key" elsif !@response.is_a?(Net::HTTPSuccess) @@ -106,16 +109,18 @@ def send_request handleExcpetion.call("Timeout error occurred. #{e.message}") ensure @buffer.clear - @lock.unlock if @lock.locked? && @lock.owned? end - end end def flush @flush_scheduled = false - return if @buffer.empty? && @side_messages.empty? - - send_request + if @lock.try_lock + return if @buffer.empty? && @side_messages.empty? + send_request + @lock.unlock + else + schedule_flush + end end def exitout From 4395fd7cb255bc7b46338c9a83dc12af41371a8f Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Tue, 1 Oct 2019 12:13:11 -0700 Subject: [PATCH 22/40] lints --- lib/logdna/client.rb | 73 ++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index aa34015..7bafe29 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -69,53 +69,54 @@ def write_to_buffer(msg, opts) end def send_request + @side_message_lock.synchronize do + @buffer.concat(@side_messages) + @side_messages.clear + end + + @request.body = { + e: "ls", + ls: @buffer + }.to_json + + handle_excpetion = lambda do |message| + puts message + @exception_flag = true @side_message_lock.synchronize do - @buffer.concat(@side_messages) - @side_messages.clear + @side_messages.concat(@buffer) end + end - @request.body = { - e: "ls", - ls: @buffer - }.to_json - - handleExcpetion = lambda do |message| - puts message - @exception_flag = true - @side_message_lock.synchronize do - @side_messages.concat(@buffer) - end + begin + @response = Net::HTTP.start( + @uri.hostname, + @uri.port, + use_ssl: @uri.scheme == "https" + ) do |http| + http.request(@request) end - - begin - @response = Net::HTTP.start( - @uri.hostname, - @uri.port, - use_ssl: @uri.scheme == "https" - ) do |http| - http.request(@request) - end - if @response.is_a?(Net::HTTPForbidden) - puts "Please provide a valid ingestion key" - elsif !@response.is_a?(Net::HTTPSuccess) - puts "The response is not successful " - end - @exception_flag = false - rescue SocketError - handleExcpetion.call("Network connectivity issue") - rescue Errno::ECONNREFUSED => e - handleExcpetion.call("The server is down. #{e.message}") - rescue Timeout::Error => e - handleExcpetion.call("Timeout error occurred. #{e.message}") - ensure - @buffer.clear + if @response.is_a?(Net::HTTPForbidden) + puts "Please provide a valid ingestion key" + elsif !@response.is_a?(Net::HTTPSuccess) + puts "The response is not successful " end + @exception_flag = false + rescue SocketError + handle_excpetion.call("Network connectivity issue") + rescue Errno::ECONNREFUSED => e + handle_excpetion.call("The server is down. #{e.message}") + rescue Timeout::Error => e + handle_excpetion.call("Timeout error occurred. #{e.message}") + ensure + @buffer.clear + end end def flush @flush_scheduled = false if @lock.try_lock return if @buffer.empty? && @side_messages.empty? + send_request @lock.unlock else From 068bc3d09c98ba4f8d5c352ac4a7883bb082d924 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Tue, 1 Oct 2019 12:23:39 -0700 Subject: [PATCH 23/40] remove redundant nil --- lib/logdna/client.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 7bafe29..23d4b49 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -127,7 +127,6 @@ def flush def exitout flush if @buffer.any? || @side_messages.any? puts "Exiting LogDNA logger: Logging remaining messages" - nil end end end From ad965d4b6a6f8cb96deb87dd81ccbdd930cfbf92 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 2 Oct 2019 12:22:35 -0700 Subject: [PATCH 24/40] remove the checking --- lib/logdna/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 23d4b49..b7ab086 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -125,7 +125,7 @@ def flush end def exitout - flush if @buffer.any? || @side_messages.any? + flush puts "Exiting LogDNA logger: Logging remaining messages" end end From 30eaaba2d751d800dd1a95b97e86d1470daa6431 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Tue, 8 Oct 2019 16:20:58 -0700 Subject: [PATCH 25/40] move the falg into lock --- lib/logdna.rb | 18 +++++++++--------- lib/logdna/client.rb | 4 +++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index aecde8f..ab36eb6 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -50,7 +50,7 @@ def default_opts } end - def assign_level=(value) + def level=(value) if value.is_a? Numeric @level = Resources::LOG_LEVELS[value] return @@ -59,15 +59,15 @@ def assign_level=(value) @level = value end - def log(message, opts = {}) - if message.empty? - puts "Your logline cannot be empty" - return + def log(message = nil, opts = {}) + yield if block_given? && message == nil + + if message != nil && !message.empty? + message = message.to_s.encode("UTF-8") + @client.write_to_buffer(message, default_opts.merge(opts).merge( + timestamp: (Time.now.to_f * 1000).to_i + )) end - message = message.to_s.encode("UTF-8") - @client.write_to_buffer(message, default_opts.merge(opts).merge( - timestamp: (Time.now.to_f * 1000).to_i - )) end Resources::LOG_LEVELS.each do |lvl| diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index b7ab086..5500e85 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -42,7 +42,6 @@ def process_message(msg, opts = {}) end def schedule_flush - @flush_scheduled = true start_timer = lambda { sleep(@exception_flag ? @retry_timeout : @flush_interval) flush if @flush_scheduled @@ -57,6 +56,7 @@ def write_to_buffer(msg, opts) new_message_size = processed_message.to_s.bytesize @buffer.push(processed_message) @buffer_byte_size += new_message_size + @flush_scheduled = true @lock.unlock flush if @flush_limit <= @buffer_byte_size @@ -107,6 +107,8 @@ def send_request handle_excpetion.call("The server is down. #{e.message}") rescue Timeout::Error => e handle_excpetion.call("Timeout error occurred. #{e.message}") + rescue + handle_excpetion.call("#{e.message}") ensure @buffer.clear end From d94bbca7ef8292013baea8e8527020d32d1f7c81 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 9 Oct 2019 10:59:56 -0700 Subject: [PATCH 26/40] remove block --- lib/logdna.rb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index ab36eb6..bb54928 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -60,29 +60,27 @@ def level=(value) end def log(message = nil, opts = {}) - yield if block_given? && message == nil + return if message.nil? || message.empty? - if message != nil && !message.empty? - message = message.to_s.encode("UTF-8") - @client.write_to_buffer(message, default_opts.merge(opts).merge( - timestamp: (Time.now.to_f * 1000).to_i - )) - end + message = message.to_s.encode("UTF-8") + @client.write_to_buffer(message, default_opts.merge(opts).merge( + timestamp: (Time.now.to_f * 1000).to_i + )) end Resources::LOG_LEVELS.each do |lvl| name = lvl.downcase - define_method name do |msg = nil, opts = {}, &block| - log(msg, opts.merge( + define_method name do |msg = nil, opts = {}| + self.log(msg, opts.merge( level: lvl - ), &block) + )) end define_method "#{name}?" do return Resources::LOG_LEVELS[level] == lvl if level.is_a? Numeric - assign_level == lvl + self.level == lvl end end From d8e4ad30d50212378a0effb4e0ae113b79f57422 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 9 Oct 2019 11:01:23 -0700 Subject: [PATCH 27/40] rubocop --- lib/logdna.rb | 8 ++++---- logdna.gemspec | 2 +- test.rb | 19 +++---------------- 3 files changed, 8 insertions(+), 21 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index bb54928..94a7c6c 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -64,8 +64,8 @@ def log(message = nil, opts = {}) message = message.to_s.encode("UTF-8") @client.write_to_buffer(message, default_opts.merge(opts).merge( - timestamp: (Time.now.to_f * 1000).to_i - )) + timestamp: (Time.now.to_f * 1000).to_i + )) end Resources::LOG_LEVELS.each do |lvl| @@ -73,8 +73,8 @@ def log(message = nil, opts = {}) define_method name do |msg = nil, opts = {}| self.log(msg, opts.merge( - level: lvl - )) + level: lvl + )) end define_method "#{name}?" do diff --git a/logdna.gemspec b/logdna.gemspec index 89ebac0..76fbdad 100755 --- a/logdna.gemspec +++ b/logdna.gemspec @@ -1,4 +1,5 @@ # coding: utf-8 + lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'logdna/version' @@ -20,7 +21,6 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_runtime_dependency 'concurrent-ruby', '~> 1.0' spec.add_runtime_dependency 'require_all', '~> 1.4' spec.add_runtime_dependency 'json', '~> 2.0' diff --git a/test.rb b/test.rb index 358082e..b554d4b 100644 --- a/test.rb +++ b/test.rb @@ -1,9 +1,7 @@ require 'require_all' require_all 'lib' - -options = {hostname: "new_ruby", meta:{:once => {:first => "nested1", :another => "nested2"}}} - +options = { hostname: "new_ruby", meta: { :once => { :first => "nested1", :another => "nested2" } } } logger1 = Logdna::Ruby.new('Your API Key', options) @@ -14,21 +12,18 @@ logger1.clear logger1.log('Is everything back to normal?') - logger1.log('Testing env app name change using log') logger1.env = 'PRODUCTION' logger1.app = 'CHANGED' logger1.log('This should be stage = PRODUCTION and appname = CHANGED') logger1.log('Testing env app name change using other messages') - -logger1.error('This is error message with env = DEVELOPMENT and appname = NIHAO', {:env => 'DEVELOPMENT', :app => 'NIHAO'}) +logger1.error('This is error message with env = DEVELOPMENT and appname = NIHAO', { :env => 'DEVELOPMENT', :app => 'NIHAO' }) logger1.log('Should not stay as DEVELOPMENT and NIHAO') logger1.env = 'DEVELOPMENT' logger1.app = 'NIHAO' logger1.log('Now should be DEVELOPMENT and NIHAO') -logger1.log('Logging metadata in trace level', {:meta => {:once => {:first => "nested1", :another => "nested2"}}, :level => "TRACE"}) - +logger1.log('Logging metadata in trace level', { :meta => { :once => { :first => "nested1", :another => "nested2" } }, :level => "TRACE" }) logger1.level = Logger::DEBUG logger1.log('This is debug message') @@ -38,7 +33,6 @@ logger1.level = Logger::WARN logger1.log('**************** This is the end of test ****************') - =begin logger1.level = Logger::WARN logger1.log('This should be warn') @@ -54,16 +48,9 @@ logger1.env = 'Staging' logger1.level = 'INFO' - - logger1.level = 'INFO' logger1.level == Logger::INFO - logger1.log('are changes all updated') =end sleep 3 - - - - From 690fa672e9955ab6dbd72e53cfd724b78a4e950a Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 9 Oct 2019 11:11:14 -0700 Subject: [PATCH 28/40] forgot to move another flag swtich into the lock block --- lib/logdna/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 5500e85..47d6316 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -115,8 +115,8 @@ def send_request end def flush - @flush_scheduled = false if @lock.try_lock + @flush_scheduled = false return if @buffer.empty? && @side_messages.empty? send_request From 86cc04344c3fd152df735b7c39e7715032905c8a Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 10 Oct 2019 14:27:23 -0700 Subject: [PATCH 29/40] restore the possibility to pass blocks as messages --- lib/logdna.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 94a7c6c..def14ad 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -60,8 +60,9 @@ def level=(value) end def log(message = nil, opts = {}) - return if message.nil? || message.empty? - + if (message.nil?) + block_given? ? message = yield : return + end message = message.to_s.encode("UTF-8") @client.write_to_buffer(message, default_opts.merge(opts).merge( timestamp: (Time.now.to_f * 1000).to_i @@ -71,10 +72,10 @@ def log(message = nil, opts = {}) Resources::LOG_LEVELS.each do |lvl| name = lvl.downcase - define_method name do |msg = nil, opts = {}| + define_method name do |msg = nil, opts = {}, &block| self.log(msg, opts.merge( level: lvl - )) + ), &block) end define_method "#{name}?" do From 9f86f724457b7b39c8d175d7600ce6ac7a44a232 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 10 Oct 2019 14:36:14 -0700 Subject: [PATCH 30/40] change the logic to read easier --- lib/logdna.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index def14ad..2d4f4af 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -60,8 +60,10 @@ def level=(value) end def log(message = nil, opts = {}) - if (message.nil?) - block_given? ? message = yield : return + if (message.nil? && block_given?) + message = yield + elsif message.nil? + puts "provide either a message or block" end message = message.to_s.encode("UTF-8") @client.write_to_buffer(message, default_opts.merge(opts).merge( From 443f8d0d0ee22f0a72a6eec9c3bdced41671b90a Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 10 Oct 2019 14:44:58 -0700 Subject: [PATCH 31/40] update rubocop although it didd not have any effect. rubocop is stupid. --- .rubocop.yml | 5 +++++ lib/logdna.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index cbec8f3..63b7ceb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -56,6 +56,11 @@ Style/CollectionMethods: # inject seems more common in the community. reduce: "inject" +Style/UnneededInterpolation: + Enabled: flase + +Style/RescueStandardError: + Enabled: flase # Either allow this style or don't. Marking it as safe with parenthesis # is silly. Let's try to live without them for now. diff --git a/lib/logdna.rb b/lib/logdna.rb index 2d4f4af..e6821ba 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -60,7 +60,7 @@ def level=(value) end def log(message = nil, opts = {}) - if (message.nil? && block_given?) + if message.nil? && block_given? message = yield elsif message.nil? puts "provide either a message or block" From cdb6ced7f231d03b5ff1cdf41d632e3ac8338100 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 17 Oct 2019 13:15:07 -0700 Subject: [PATCH 32/40] commit --- lib/logdna.rb | 13 +++++++++---- lib/logdna/client.rb | 17 ++++++++++------- logdna.gemspec | 1 - 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index e6821ba..825207b 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -62,7 +62,8 @@ def level=(value) def log(message = nil, opts = {}) if message.nil? && block_given? message = yield - elsif message.nil? + end + if message.nil? puts "provide either a message or block" end message = message.to_s.encode("UTF-8") @@ -81,7 +82,7 @@ def log(message = nil, opts = {}) end define_method "#{name}?" do - return Resources::LOG_LEVELS[level] == lvl if level.is_a? Numeric + return Resources::LOG_LEVELS[self.level] == lvl if level.is_a? Numeric self.level == lvl end @@ -117,11 +118,15 @@ def datetime_format(*_arg) end def close - @client.exitout if defined? @client && !@client.nil? + if defined?(@client and !@@client.nil?) + @client.exitout() + end end def at_exit - @client.exitout if defined? @client && !@client.nil? + if defined?(@client && !@client.nil?) + @client.exitout() + end end end end diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 47d6316..05a11fa 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -79,7 +79,7 @@ def send_request ls: @buffer }.to_json - handle_excpetion = lambda do |message| + handle_exception = lambda do |message| puts message @exception_flag = true @side_message_lock.synchronize do @@ -98,17 +98,17 @@ def send_request if @response.is_a?(Net::HTTPForbidden) puts "Please provide a valid ingestion key" elsif !@response.is_a?(Net::HTTPSuccess) - puts "The response is not successful " + handle_exception.call("The response is not successful ") end @exception_flag = false rescue SocketError - handle_excpetion.call("Network connectivity issue") + handle_exception.call("Network connectivity issue") rescue Errno::ECONNREFUSED => e - handle_excpetion.call("The server is down. #{e.message}") + handle_exception.call("The server is down. #{e.message}") rescue Timeout::Error => e - handle_excpetion.call("Timeout error occurred. #{e.message}") + handle_exception.call("Timeout error occurred. #{e.message}") rescue - handle_excpetion.call("#{e.message}") + handle_exception.call("#{e.message}") ensure @buffer.clear end @@ -117,7 +117,10 @@ def send_request def flush if @lock.try_lock @flush_scheduled = false - return if @buffer.empty? && @side_messages.empty? + if (@buffer.empty? && @side_messages.empty?) + @lock.unlock + return + end send_request @lock.unlock diff --git a/logdna.gemspec b/logdna.gemspec index 76fbdad..1769c6e 100755 --- a/logdna.gemspec +++ b/logdna.gemspec @@ -1,5 +1,4 @@ # coding: utf-8 - lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'logdna/version' From e66c44b189faf30119bf053eef9c8fe3139247a0 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 17 Oct 2019 13:36:12 -0700 Subject: [PATCH 33/40] sdfsdf --- lib/logdna.rb | 5 ----- lib/logdna/client.rb | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index e73b16b..d12d8e4 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -21,11 +21,6 @@ def initialize(key, opts = {}) @level = opts[:level] || "INFO" @env = opts[:env] @meta = opts[:meta] -<<<<<<< HEAD -======= - @@client = nil unless defined? @@client ->>>>>>> master - endpoint = opts[:endpoint] || Resources::ENDPOINT hostname = opts[:hostname] || Socket.gethostname diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 05a11fa..bfdc222 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -117,7 +117,7 @@ def send_request def flush if @lock.try_lock @flush_scheduled = false - if (@buffer.empty? && @side_messages.empty?) + if @buffer.empty? && @side_messages.empty? @lock.unlock return end From 4316bf4959b1ae9bbd04610775458bb2f8ffabd1 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 17 Oct 2019 13:36:52 -0700 Subject: [PATCH 34/40] sdfsdF --- lib/logdna.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index d12d8e4..77a5cd5 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -122,7 +122,7 @@ def close end end - def at_exit + at_exit if defined?(@client && !@client.nil?) @client.exitout() end From 5a2ecaf5831ad39d5b73b9178e13c1f524d036f9 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 17 Oct 2019 13:46:42 -0700 Subject: [PATCH 35/40] fix the dep --- lib/logdna.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 77a5cd5..583e37e 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -6,6 +6,7 @@ require "uri" require_relative "logdna/client.rb" require_relative "logdna/resources.rb" +require_relative "logdna/version.rb" module Logdna class ValidURLRequired < ArgumentError; end class MaxLengthExceeded < ArgumentError; end @@ -117,14 +118,14 @@ def datetime_format(*_arg) end def close - if defined?(@client and !@@client.nil?) - @client.exitout() + if !@client.nil? + @client.exitout end end - at_exit - if defined?(@client && !@client.nil?) - @client.exitout() + at_exit do + if !@client.nil? + @client.exitout end end end From 1ac174b012d6ef3cb9ea37893e2e67392788f08d Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 21 Oct 2019 17:45:31 -0700 Subject: [PATCH 36/40] address the comments --- lib/logdna.rb | 1 + lib/logdna/client.rb | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 583e37e..a3351cd 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -65,6 +65,7 @@ def log(message = nil, opts = {}) end if message.nil? puts "provide either a message or block" + return end message = message.to_s.encode("UTF-8") @client.write_to_buffer(message, default_opts.merge(opts).merge( diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index bfdc222..d339399 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -68,6 +68,7 @@ def write_to_buffer(msg, opts) end end + # This method has to be called with @lock def send_request @side_message_lock.synchronize do @buffer.concat(@side_messages) @@ -101,6 +102,7 @@ def send_request handle_exception.call("The response is not successful ") end @exception_flag = false + p @response rescue SocketError handle_exception.call("Network connectivity issue") rescue Errno::ECONNREFUSED => e @@ -115,14 +117,12 @@ def send_request end def flush + if @lock.try_lock @flush_scheduled = false - if @buffer.empty? && @side_messages.empty? - @lock.unlock - return + if @buffer.any? || @side_messages.any? + send_request end - - send_request @lock.unlock else schedule_flush From 9d30f80c12e8767b1d445f76eeae241a6e2bdf30 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 21 Oct 2019 17:55:43 -0700 Subject: [PATCH 37/40] remove the stray debug message --- lib/logdna/client.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index d339399..4cb697f 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -102,7 +102,6 @@ def send_request handle_exception.call("The response is not successful ") end @exception_flag = false - p @response rescue SocketError handle_exception.call("Network connectivity issue") rescue Errno::ECONNREFUSED => e From 40484a22567d18e682727b5de116df6fdfc13d67 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 24 Oct 2019 12:01:52 -0700 Subject: [PATCH 38/40] gitigone --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ed04e2a..500ff0f 100755 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ *.gem .DS_Store /bin/ +package-lock.json From 2de8895dee387c7075571284505c2b9591982b21 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 24 Oct 2019 17:19:56 -0700 Subject: [PATCH 39/40] remove the block --- lib/logdna/client.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 4cb697f..b26434e 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -108,8 +108,6 @@ def send_request handle_exception.call("The server is down. #{e.message}") rescue Timeout::Error => e handle_exception.call("Timeout error occurred. #{e.message}") - rescue - handle_exception.call("#{e.message}") ensure @buffer.clear end From c9a07ffa440aba16c73cd063754faafad1de4b91 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 19:54:25 -0700 Subject: [PATCH 40/40] gitingone --- .gitignore | 2 -- 1 file changed, 2 deletions(-) diff --git a/.gitignore b/.gitignore index 500ff0f..dd39a76 100755 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,3 @@ /tmp/ *.gem .DS_Store -/bin/ -package-lock.json