From b1d402f6272eccbc5922e24bdaabde0ff277a159 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/94] working on the scheduler --- lib/logdna.rb | 49 ++++++------- lib/logdna/client.rb | 158 ++++++++++++++++++---------------------- lib/logdna/resources.rb | 1 + 3 files changed, 96 insertions(+), 112 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index a3351cd..c9cd191 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -22,23 +22,22 @@ def initialize(key, opts = {}) @level = opts[:level] || "INFO" @env = opts[:env] @meta = opts[:meta] + 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) - 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]}" : "" - 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) - request[:'user-agent'] = opts[:'user-agent'] || "ruby/#{LogDNA::VERSION}" - @client = Logdna::Client.new(request, uri, opts) + @@client = Logdna::Client.new(request, uri, opts) end def default_opts @@ -59,18 +58,16 @@ def level=(value) @level = value end - def log(message = nil, opts = {}) - if message.nil? && block_given? - message = yield - end - if message.nil? - puts "provide either a message or block" + def log(message, opts={}) + if(message.length == 0) + 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 - )) + 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 + })) end Resources::LOG_LEVELS.each do |lvl| @@ -96,10 +93,10 @@ def clear @meta = nil end - def <<(msg = nil, opts = {}) - log(msg, opts.merge( - level: "" - )) + def <<(msg=nil, opts={}) + self.log(msg, opts.merge({ + level: '', + })) end def add(*_arg) @@ -125,8 +122,8 @@ def close end at_exit do - if !@client.nil? - @client.exitout + if defined? @@client and !@@client.nil? + #@@client.exitout() end end end diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index b26434e..f5dbb70 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -1,10 +1,9 @@ -# frozen_string_literal: true - -require "net/http" -require "socket" -require "json" -require "concurrent" -require "date" +require 'net/http' +require 'socket' +require 'json' +require 'concurrent' +require 'thread' +require 'date' module Logdna class Client @@ -18,18 +17,15 @@ 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 - @exception_flag = false + @flush_limit = opts[:flush_size] ||= Resources::FLUSH_BYTE_LIMIT + @flush_interval = opts[:flush_interval] ||= Resources::FLUSH_INTERVAL - @request = request - @retry_timeout = opts[:retry_timeout] || Resources::RETRY_TIMEOUT + @@request = request + @timer_task = false end - def process_message(msg, opts = {}) - processed_message = { + def process_message(msg, opts={}) + processedMessage = { line: msg, app: opts[:app], level: opts[:level], @@ -37,97 +33,87 @@ def process_message(msg, opts = {}) meta: opts[:meta], timestamp: Time.now.to_i, } - processed_message.delete(:meta) if processed_message[:meta].nil? - processed_message + processedMessage.delete(:meta) if processedMessage[:meta].nil? + processedMessage end - def schedule_flush - start_timer = lambda { - sleep(@exception_flag ? @retry_timeout : @flush_interval) - flush if @flush_scheduled - } - thread = Thread.new { start_timer } - thread.join + 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 + end + timer_task.execute + timer_task end def write_to_buffer(msg, opts) + puts 'log received' if @lock.try_lock - processed_message = process_message(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 - schedule_flush unless @flush_scheduled + 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 + @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 - @side_message_lock.synchronize do @side_messages.push(process_message(msg, opts)) - end - end - end - - # This method has to be called with @lock - def send_request - @side_message_lock.synchronize do - @buffer.concat(@side_messages) - @side_messages.clear end - @request.body = { - e: "ls", - ls: @buffer + # 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 - handle_exception = lambda do |message| - puts message - @exception_flag = true - @side_message_lock.synchronize do - @side_messages.concat(@buffer) - end + @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 - 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) - handle_exception.call("The response is not successful ") - end - @exception_flag = false - rescue SocketError - handle_exception.call("Network connectivity issue") - rescue Errno::ECONNREFUSED => e - handle_exception.call("The server is down. #{e.message}") - rescue Timeout::Error => e - handle_exception.call("Timeout error occurred. #{e.message}") - ensure - @buffer.clear + 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 - end - - def flush - if @lock.try_lock - @flush_scheduled = false - if @buffer.any? || @side_messages.any? - send_request - end + begin @lock.unlock - else - schedule_flush + rescue + puts 'Nothing was locked' end end def exitout - flush + puts @timer_task.status + if @buffer.any? + flush() + end puts "Exiting LogDNA logger: Logging remaining messages" end end diff --git a/lib/logdna/resources.rb b/lib/logdna/resources.rb index b1323c4..619edd3 100755 --- a/lib/logdna/resources.rb +++ b/lib/logdna/resources.rb @@ -8,6 +8,7 @@ module Resources MAX_INPUT_LENGTH = 80 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 68ffd7e08cdcbc5e985bc35d2d199594e45eb1e5 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/94] savingBranch --- bin/bundle | 105 ++++++++++++++++++++++++++++++++++++++ bin/htmldiff | 29 +++++++++++ bin/ldiff | 29 +++++++++++ bin/rake | 29 +++++++++++ bin/rspec | 29 +++++++++++ bin/safe_yaml | 29 +++++++++++ lib/logdna.rb | 15 ++++++ lib/logdna/client.rb | 108 +++++++++++++++++++++------------------- lib/logdna/resources.rb | 2 +- spec/logdnaTest.rb | 5 ++ 10 files changed, 329 insertions(+), 51 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 c9cd191..afee4d0 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -115,6 +115,7 @@ def datetime_format(*_arg) false end +<<<<<<< HEAD def close if !@client.nil? @client.exitout @@ -126,5 +127,19 @@ def close #@@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 +>>>>>>> savingBranch end end diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index f5dbb70..df55421 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -19,9 +19,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={}) @@ -38,83 +41,88 @@ 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 - # 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 @lock.try_lock + @request.body = { + e: 'ls', + ls: @buffer.concat(@side_messages), + }.to_json + @timer_task = false + @side_messages.clear - 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 + @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' + 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 +<<<<<<< HEAD puts "Exiting LogDNA logger: Logging remaining messages" +======= + puts "Exiting LogDNA logger: Logging remaining messages" + return +>>>>>>> savingBranch end end end diff --git a/lib/logdna/resources.rb b/lib/logdna/resources.rb index 619edd3..d27094e 100755 --- a/lib/logdna/resources.rb +++ b/lib/logdna/resources.rb @@ -6,7 +6,7 @@ module Resources MAX_REQUEST_TIMEOUT = 300000 MAX_LINE_LENGTH = 32000 MAX_INPUT_LENGTH = 80 - RETRY_TIMEOUT = 60 + 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 888404c20b30ab691da48cb68f5a351cba03c995 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:10:42 -0700 Subject: [PATCH 03/94] 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 37c6a3b3a3c4ef15f8a214d742d2d845fe66fcc1 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:12:21 -0700 Subject: [PATCH 04/94] 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 08ad8d9ebb0b70ea903f7ae750c85017d76120f5 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:42:04 -0700 Subject: [PATCH 05/94] 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 e980151f92548fc7a521f1c8249a719a07acd64c Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:44:04 -0700 Subject: [PATCH 06/94] 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 9c7aacaa8f5d73c77f5de6d7d0c2407b6c134dba Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 4 Sep 2019 12:41:24 -0700 Subject: [PATCH 07/94] 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 df55421..28cfb84 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -24,7 +24,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={}) @@ -40,17 +40,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 49d95da391a50b7eb3a128687efeb5d0abc9efd9 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 4 Sep 2019 12:57:47 -0700 Subject: [PATCH 08/94] 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 28cfb84..77af5ff 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -24,7 +24,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={}) @@ -41,8 +41,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 } @@ -70,7 +71,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 @@ -91,10 +92,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 b1a7d677ff0807d8f1ce510ab7fab4bf158ddad5 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 4 Sep 2019 14:38:54 -0700 Subject: [PATCH 09/94] 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 afee4d0..538ef9d 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -59,12 +59,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 77af5ff..f818d42 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -69,7 +69,6 @@ def write_to_buffer(msg, opts) begin @lock.unlock rescue - puts 'Nothing was locked' end schedule_flush() if !@flush_scheduled else @@ -77,6 +76,7 @@ def write_to_buffer(msg, opts) end def flush + puts (@buffer) return if @buffer.empty? if @lock.try_lock @request.body = { @@ -102,7 +102,6 @@ def flush begin @lock.unlock rescue - puts 'Nothing was locked' end end end From 10aeea035af7e247633f3400c870468b09daf4b3 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 4 Sep 2019 15:01:26 -0700 Subject: [PATCH 10/94] 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 538ef9d..34822dd 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -35,7 +35,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 f818d42..262cbde 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -23,7 +23,6 @@ def initialize(request, uri, opts) @exception_flag = false @request = request - @timer_task = false @retry_timeout = opts[:retry_timeout] ||= Resources::RETRY_TIMEOUT end @@ -65,25 +64,20 @@ 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)) 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 @@ -99,10 +93,7 @@ def flush @flush_scheduled = false @buffer.clear - begin - @lock.unlock - rescue - end + @lock.unlock if @lock.locked? end end From 6a3fb05181c7cfe0b5055c858e9db6404251597b Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 5 Sep 2019 14:21:12 -0700 Subject: [PATCH 11/94] 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 262cbde..ef03f89 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -79,21 +79,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 6ff863ee72e4512807442ffa707dba582b21bfc3 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 5 Sep 2019 16:41:50 -0700 Subject: [PATCH 12/94] add rubycop config file and run it --- .rubocop.yml | 21 ++++++----------- .ruby-version | 1 + lib/logdna.rb | 21 ++++------------- lib/logdna/client.rb | 56 +++++++++++++++++++++----------------------- 4 files changed, 39 insertions(+), 60 deletions(-) create mode 100644 .ruby-version diff --git a/.rubocop.yml b/.rubocop.yml index bd8c31c..c56aab3 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: 100 + Max: 40 # The guiding principle of classes is SRP, SRP can't be accurately measured by LoC Metrics/ClassLength: @@ -22,10 +22,11 @@ Metrics/PerceivedComplexity: # No space makes the method definition shorter and differentiates # from a regular assignment. - -Layout/AccessModifierIndentation: - Enabled: true - IndentationWidth: 4 +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 @@ -56,11 +57,6 @@ Style/CollectionMethods: # inject seems more common in the community. reduce: "inject" -Style/RedundantInterpolation: - Enabled: false - -Style/RescueStandardError: - Enabled: false # Either allow this style or don't. Marking it as safe with parenthesis # is silly. Let's try to live without them for now. @@ -83,7 +79,7 @@ Style/SignalException: # 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/SuppressedException: +Lint/HandleExceptions: Enabled: false # { ... } for multi-line blocks is okay, follow Weirichs rule instead: @@ -126,6 +122,3 @@ Lint/Debugger: # Style preference Style/MethodDefParentheses: Enabled: false - -Style/TrailingCommaInHashLiteral: - Enabled: false diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..24ba9a3 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.7.0 diff --git a/lib/logdna.rb b/lib/logdna.rb index 34822dd..76d677c 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -35,7 +35,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 @@ -59,11 +59,12 @@ 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.encode("UTF-8") + 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 })) @@ -114,19 +115,6 @@ def datetime_format(*_arg) false end -<<<<<<< HEAD - def close - if !@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? @@ -139,6 +127,5 @@ def close # @@client.exitout() # end # end ->>>>>>> savingBranch end end diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index ef03f89..1ccd4de 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -23,7 +23,8 @@ def initialize(request, uri, opts) @exception_flag = false @request = request - @retry_timeout = opts[:retry_timeout] ||= Resources::RETRY_TIMEOUT + @timer_task = false + @backoff_interval = opts[:backoff_period] ||= Resources::BACKOFF_PERIOD end def process_message(msg, opts={}) @@ -39,10 +40,17 @@ 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 - @flush_scheduled = true def start_timer - sleep(@exception_flag ? @retry_timeout : @flush_interval) + sleep(@exception_flag ? @backoff_period : @flush_interval) flush end thread = Thread.new{ start_timer } @@ -64,9 +72,13 @@ def write_to_buffer(msg, opts) @buffer.push(processed_message) self.flush end - @lock.unlock if @lock.locked? - schedule_flush() if !@flush_scheduled + begin + @lock.unlock + rescue + puts 'Nothing was locked' + end + schedule_flush() else @side_messages.push(process_message(msg, opts)) end @@ -78,35 +90,25 @@ def flush e: 'ls', ls: @buffer.concat(@side_messages), }.to_json + @timer_task = false @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" + rescue + puts `Error at the attempt to send the request #{@response.body if @response}` @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 + @buffer.clear + + begin + @lock.unlock + rescue + puts 'Nothing was locked' end end end @@ -115,12 +117,8 @@ def exitout if @buffer.any? flush() end -<<<<<<< HEAD - puts "Exiting LogDNA logger: Logging remaining messages" -======= puts "Exiting LogDNA logger: Logging remaining messages" return ->>>>>>> savingBranch end end end From 09f725bb01153c50629df395d0365e26eb10d9b7 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 5 Sep 2019 16:52:00 -0700 Subject: [PATCH 13/94] 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 1ccd4de..2cc52e3 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -17,8 +17,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 a95a79d2c47440d2a7d89a8fb524d97cb9d39bbc Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 5 Sep 2019 16:52:32 -0700 Subject: [PATCH 14/94] forgot about one more --- lib/logdna/client.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 2cc52e3..7a748e8 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -23,8 +23,7 @@ def initialize(request, uri, opts) @exception_flag = false @request = request - @timer_task = false - @backoff_interval = opts[:backoff_period] ||= Resources::BACKOFF_PERIOD + @retry_timeout = opts[:retry_timeout] ? opts[:retry_timeout] : Resources::RETRY_TIMEOUT end def process_message(msg, opts={}) From c75b9c04cc7cbfbbbb063420697d2a1d570a34dc Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 27 Sep 2019 14:01:23 -0700 Subject: [PATCH 15/94] address comments --- lib/logdna.rb | 33 ++++++++++++++------------------- lib/logdna/client.rb | 19 ++++++++++++++----- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 76d677c..4d6e800 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -25,19 +25,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 @@ -63,9 +66,8 @@ def log(message, opts={}) 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({ + 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 @@ -115,17 +117,10 @@ def datetime_format(*_arg) 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 + 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 7a748e8..8546fc1 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -112,12 +112,21 @@ def flush end end - def exitout - if @buffer.any? - flush() + def flush + @flush_scheduled = false + return if @buffer.empty? && @side_messages.empty? + + if @lock.try_lock + send_request + else + schedule_flush end - puts "Exiting LogDNA logger: Logging remaining messages" - return + end + + def exitout + flush if @buffer.any? || @side_messages.any? + puts "Exiting LogDNA logger: Logging remaining messages" + nil end end end From f04c0586a76e7de5c126e39e453c47d5f6ee0cf9 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 27 Sep 2019 14:05:33 -0700 Subject: [PATCH 16/94] lints --- lib/logdna.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 4d6e800..b5926fb 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -119,7 +119,7 @@ def datetime_format(*_arg) def close if defined? @client and !@client.nil? - @client.exitout() + @client.exitout end end end From 25750add6dfb5950f6de8979aca091a0f9afd3c2 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 30 Sep 2019 15:59:33 -0700 Subject: [PATCH 17/94] change the locking logic ang change linting rules --- .rubocop.yml | 14 ++++--- lib/logdna.rb | 50 ++++++++++++------------- lib/logdna/client.rb | 88 ++++++++++++++++++++++---------------------- 3 files changed, 77 insertions(+), 75 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 b5926fb..aecde8f 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -6,7 +6,6 @@ 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 @@ -26,19 +25,18 @@ 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 ) + 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 @@ -52,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 @@ -61,30 +59,30 @@ 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) + log(msg, opts.merge( + level: lvl + ), &block) end define_method "#{name}?" do - return Resources::LOG_LEVELS[self.level] == lvl if level.is_a? Numeric + return Resources::LOG_LEVELS[level] == lvl if level.is_a? Numeric - self.level == lvl + assign_level == lvl end end @@ -95,10 +93,10 @@ def clear @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) @@ -118,9 +116,11 @@ def datetime_format(*_arg) 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 8546fc1..35f3d67 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -17,13 +17,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,56 +58,60 @@ def start_timer 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 + @buffer.concat(@side_messages) unless @side_messages.empty? + processed_message = process_message(msg, opts) + new_message_size = processed_message.to_s.bytesize - begin - @lock.unlock - rescue - puts 'Nothing was locked' - end - schedule_flush() + @buffer_byte_size += new_message_size + @buffer.push(processed_message) else @side_messages.push(process_message(msg, opts)) end + @lock.unlock if @lock.locked? - def flush - return if @buffer.empty? - if @lock.try_lock + flush if @flush_limit <= @buffer_byte_size + schedule_flush unless @flush_scheduled + end + + def send_request + if !@lock.try_lock + schedule_flush + else @request.body = { - e: 'ls', - ls: @buffer.concat(@side_messages), + 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| + @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 - puts `Error at the attempt to send the request #{@response.body if @response}` + rescue SocketError + print "Network connectivity issue" @exception_flag = true @side_messages.concat(@buffer) - end - @buffer.clear - - begin - @lock.unlock - rescue - puts 'Nothing was locked' + 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 end end @@ -116,11 +120,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 4e04773eae124baa52332e9344bccbba135e5f49 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 30 Sep 2019 16:47:38 -0700 Subject: [PATCH 18/94] 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 35f3d67..e391436 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -82,6 +82,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, @@ -98,17 +105,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 112d3e81c6ef894e8f21f1dbfe7cc45c192993a3 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 30 Sep 2019 18:00:39 -0700 Subject: [PATCH 19/94] 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 e391436..828e5bb 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -67,7 +67,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 @@ -83,7 +83,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) @@ -97,7 +97,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) @@ -112,7 +112,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 481cc5c1fd95b32bff8a4c43cc82d92958d29ac8 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Tue, 1 Oct 2019 12:10:36 -0700 Subject: [PATCH 20/94] fix the bug and add the lock to the side messages --- lib/logdna/client.rb | 56 ++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 828e5bb..08941f1 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -17,6 +17,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 @@ -48,45 +49,49 @@ def create_flush_task end def schedule_flush - def start_timer - sleep(@exception_flag ? @backoff_period : @flush_interval) - flush - end - thread = Thread.new{ start_timer } + @flush_scheduled = true + start_timer = lambda { + sleep(@exception_flag ? @retry_timeout : @flush_interval) + flush if @flush_scheduled + } + thread = Thread.new { start_timer } thread.join end 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_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 @@ -97,7 +102,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) @@ -112,16 +116,18 @@ def send_request handleExcpetion.call("Timeout error occurred. #{e.message}") ensure @buffer.clear - @lock.unlock if @lock.locked? && @lock.owned? end - 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 fa4841e405edc04de0030c8f39396c0ec480ac5e Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Tue, 1 Oct 2019 12:13:11 -0700 Subject: [PATCH 21/94] 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 08941f1..06a8a38 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -76,53 +76,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 60a3707fc02682c8d4bf08d58641b56070e2306d Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Tue, 1 Oct 2019 12:23:39 -0700 Subject: [PATCH 22/94] 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 06a8a38..039de80 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -134,7 +134,6 @@ def flush def exitout flush if @buffer.any? || @side_messages.any? puts "Exiting LogDNA logger: Logging remaining messages" - nil end end end From 207def13fb8bea374896df4a5460b0284d7bd38d Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 2 Oct 2019 12:22:35 -0700 Subject: [PATCH 23/94] 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 039de80..275bffc 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -132,7 +132,7 @@ def flush end def exitout - flush if @buffer.any? || @side_messages.any? + flush puts "Exiting LogDNA logger: Logging remaining messages" end end From 1f61ebf1a9edd4b364859ec76120c4b8e29e4dca Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Tue, 8 Oct 2019 16:20:58 -0700 Subject: [PATCH 24/94] 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 275bffc..da057cc 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -49,7 +49,6 @@ def create_flush_task end def schedule_flush - @flush_scheduled = true start_timer = lambda { sleep(@exception_flag ? @retry_timeout : @flush_interval) flush if @flush_scheduled @@ -64,6 +63,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 @@ -114,6 +114,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 30249225ba9a541dceace085536c03e5a5147d56 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 9 Oct 2019 10:59:56 -0700 Subject: [PATCH 25/94] 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 48ec1a63aff700e991d2d52146799e32a9e32922 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 9 Oct 2019 11:01:23 -0700 Subject: [PATCH 26/94] rubocop --- lib/logdna.rb | 8 ++++---- logdna.gemspec | 2 ++ test.rb | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 test.rb 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 be05c27..b4fb928 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' @@ -15,6 +16,7 @@ Gem::Specification.new do |spec| spec.bindir = 'exe' 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 new file mode 100644 index 0000000..b554d4b --- /dev/null +++ b/test.rb @@ -0,0 +1,56 @@ +require 'require_all' +require_all 'lib' + +options = { hostname: "new_ruby", meta: { :once => { :first => "nested1", :another => "nested2" } } } + +logger1 = Logdna::Ruby.new('Your API Key', options) + +logger1.log('**************** This is the start of test ****************') +logger1.env = 'STAGING' +logger1.app = 'HELLO' +logger1.warn('Warn message with Staging and Hello') +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.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.level = Logger::DEBUG +logger1.log('This is debug message') +logger1.add('this should not be supported') +logger1.fatal('Does this continue as fatal?') +logger1.log('This should be debug') +logger1.level = Logger::WARN +logger1.log('**************** This is the end of test ****************') + +=begin +logger1.level = Logger::WARN +logger1.log('This should be warn') +logger1.trace('This should be trace') +logger1.log('Again warn level') + +logger1.log('Warn level log1') +logger1.info('Info level log1') +logger1.level = Logger::DEBUG +logger1.log('DEBUG log1') + +logger1.app = 'NEW APP NAME' +logger1.env = 'Staging' +logger1.level = 'INFO' + +logger1.level = 'INFO' +logger1.level == Logger::INFO + +logger1.log('are changes all updated') +=end +sleep 3 From a827d995cc5ca639a342ab2bc78bd7e7ed5b0cc4 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 9 Oct 2019 11:11:14 -0700 Subject: [PATCH 27/94] 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 da057cc..8bb9634 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -122,8 +122,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 99b25aa5a9cd748845eb4eb2b161e47ccd537f1a Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 10 Oct 2019 14:27:23 -0700 Subject: [PATCH 28/94] 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 e1b66adc1a377426a4af1d9ccee311f54ab91924 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 10 Oct 2019 14:36:14 -0700 Subject: [PATCH 29/94] 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 6c57f91f26a3aede5057b033db0c03e014902624 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 10 Oct 2019 14:44:58 -0700 Subject: [PATCH 30/94] 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 e1b965d2e98bad661a521c826a35f7ba439eb6c9 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 17 Oct 2019 13:15:07 -0700 Subject: [PATCH 31/94] 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 8bb9634..43fc368 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -86,7 +86,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 @@ -105,17 +105,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 @@ -124,7 +124,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 b4fb928..74c76f2 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 237cff3d96c507c5987cb14f2d39bd953fb76b44 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 17 Oct 2019 13:36:12 -0700 Subject: [PATCH 32/94] sdfsdf --- lib/logdna.rb | 3 +++ lib/logdna/client.rb | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 825207b..80dc4a7 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -21,7 +21,10 @@ def initialize(key, opts = {}) @level = opts[:level] || "INFO" @env = opts[:env] @meta = opts[:meta] +<<<<<<< HEAD +======= +>>>>>>> sdfsdf endpoint = opts[:endpoint] || Resources::ENDPOINT hostname = opts[:hostname] || Socket.gethostname diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 43fc368..8e774d4 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -124,7 +124,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 74adad29370e69f80580e0b06417944a6d084415 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 17 Oct 2019 13:36:52 -0700 Subject: [PATCH 33/94] sdfsdF --- lib/logdna.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 80dc4a7..13485f4 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -126,7 +126,7 @@ def close end end - def at_exit + at_exit if defined?(@client && !@client.nil?) @client.exitout() end From 672f52728d5abdee7382646549f6f3d92d893dca Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 17 Oct 2019 13:46:42 -0700 Subject: [PATCH 34/94] 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 13485f4..e0d152b 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 @@ -121,14 +122,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 a9089bcbb0112c308d706712b77958aceca36a5b Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 21 Oct 2019 17:45:31 -0700 Subject: [PATCH 35/94] 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 e0d152b..58bf441 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -69,6 +69,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 8e774d4..3f4ceeb 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -75,6 +75,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) @@ -108,6 +109,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 @@ -122,14 +124,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 b655cfd340f4641736f765dd2cd0767b50250cac Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 21 Oct 2019 17:55:43 -0700 Subject: [PATCH 36/94] 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 3f4ceeb..fb9a547 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -109,7 +109,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 9522257204697790c24eda7b026bbfe830eae94c Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 24 Oct 2019 12:01:52 -0700 Subject: [PATCH 37/94] 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 a89ab461e0051dd011d30dc65e2c4ca57cf66b8e Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 24 Oct 2019 14:06:25 -0700 Subject: [PATCH 38/94] dd --- lib/logdna.rb | 16 ++++--------- lib/logdna/client.rb | 10 ++++---- test.rb | 56 -------------------------------------------- 3 files changed, 11 insertions(+), 71 deletions(-) delete mode 100644 test.rb diff --git a/lib/logdna.rb b/lib/logdna.rb index 58bf441..6c89e09 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -6,7 +6,6 @@ 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 @@ -22,10 +21,6 @@ def initialize(key, opts = {}) @level = opts[:level] || "INFO" @env = opts[:env] @meta = opts[:meta] -<<<<<<< HEAD - -======= ->>>>>>> sdfsdf endpoint = opts[:endpoint] || Resources::ENDPOINT hostname = opts[:hostname] || Socket.gethostname @@ -69,7 +64,6 @@ 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( @@ -123,14 +117,14 @@ def datetime_format(*_arg) end def close - if !@client.nil? - @client.exitout + if defined?(@client and !@@client.nil?) + @client.exitout() end end - at_exit do - if !@client.nil? - @client.exitout + def at_exit + if defined?(@client && !@client.nil?) + @client.exitout() end end end diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index fb9a547..78c68c0 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -25,6 +25,9 @@ def initialize(request, uri, opts) @request = request @retry_timeout = opts[:retry_timeout] || Resources::RETRY_TIMEOUT + + @internal_logger = Logger.new(STDOUT) + @internal_logger.level = Logger::DEBUG end def process_message(msg, opts={}) @@ -88,7 +91,7 @@ def send_request }.to_json handle_exception = lambda do |message| - puts message + @internal_logger.debug(message) @exception_flag = true @side_message_lock.synchronize do @side_messages.concat(@buffer) @@ -104,7 +107,7 @@ def send_request http.request(@request) end if @response.is_a?(Net::HTTPForbidden) - puts "Please provide a valid ingestion key" + @internal_logger.debug("Please provide a valid ingestion key") elsif !@response.is_a?(Net::HTTPSuccess) handle_exception.call("The response is not successful ") end @@ -123,7 +126,6 @@ def send_request end def flush - if @lock.try_lock @flush_scheduled = false if @buffer.any? || @side_messages.any? @@ -137,7 +139,7 @@ def flush def exitout flush - puts "Exiting LogDNA logger: Logging remaining messages" + @internal_logger.debug("Exiting LogDNA logger: Logging remaining messages") end end end diff --git a/test.rb b/test.rb deleted file mode 100644 index b554d4b..0000000 --- a/test.rb +++ /dev/null @@ -1,56 +0,0 @@ -require 'require_all' -require_all 'lib' - -options = { hostname: "new_ruby", meta: { :once => { :first => "nested1", :another => "nested2" } } } - -logger1 = Logdna::Ruby.new('Your API Key', options) - -logger1.log('**************** This is the start of test ****************') -logger1.env = 'STAGING' -logger1.app = 'HELLO' -logger1.warn('Warn message with Staging and Hello') -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.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.level = Logger::DEBUG -logger1.log('This is debug message') -logger1.add('this should not be supported') -logger1.fatal('Does this continue as fatal?') -logger1.log('This should be debug') -logger1.level = Logger::WARN -logger1.log('**************** This is the end of test ****************') - -=begin -logger1.level = Logger::WARN -logger1.log('This should be warn') -logger1.trace('This should be trace') -logger1.log('Again warn level') - -logger1.log('Warn level log1') -logger1.info('Info level log1') -logger1.level = Logger::DEBUG -logger1.log('DEBUG log1') - -logger1.app = 'NEW APP NAME' -logger1.env = 'Staging' -logger1.level = 'INFO' - -logger1.level = 'INFO' -logger1.level == Logger::INFO - -logger1.log('are changes all updated') -=end -sleep 3 From 1ace81f3a3585cc5e35564ab6f0c87d56f3dfb8e Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 24 Oct 2019 17:19:39 -0700 Subject: [PATCH 39/94] dss --- lib/logdna/client.rb | 4 ++-- spec/client.rb | 39 +++++++++++++++++++++++++++++++++++ spec/logdna.test.rb | 49 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 spec/client.rb create mode 100644 spec/logdna.test.rb diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 78c68c0..8e20d12 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -70,6 +70,7 @@ def write_to_buffer(msg, opts) @lock.unlock flush if @flush_limit <= @buffer_byte_size + schedule_flush unless @flush_scheduled else @side_message_lock.synchronize do @@ -80,6 +81,7 @@ def write_to_buffer(msg, opts) # This method has to be called with @lock def send_request + puts "lll" @side_message_lock.synchronize do @buffer.concat(@side_messages) @side_messages.clear @@ -118,8 +120,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 diff --git a/spec/client.rb b/spec/client.rb new file mode 100644 index 0000000..ff96d3d --- /dev/null +++ b/spec/client.rb @@ -0,0 +1,39 @@ +require_relative "../lib/logdna.rb" +require_relative "../lib/logdna/client.rb" +RSpec.describe "" do + before do + + end + + + let(:uri) {'uri'} + let(:opts) {{:app => "app"}} + let (:options) { + { + :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", + :flush_interval => 1, + :flush_size => 1, + :retry_timeout => 60 + } + } + + it "" do + request = spy("l;;;;;;;") + + # allow(request).to receive(:body) + # allow(request).to receive(:body) + + client = Logdna::Client.new(request, uri, options) + client.write_to_buffer("lllll", {}) + + + expect(request).to receive(:body) + end + +end diff --git a/spec/logdna.test.rb b/spec/logdna.test.rb new file mode 100644 index 0000000..98e6c5b --- /dev/null +++ b/spec/logdna.test.rb @@ -0,0 +1,49 @@ +require_relative "../lib/logdna.rb" +require_relative "../lib/logdna/client.rb" + +RSpec.describe "should call the level named methods and assign the correct method" do + before do + allow(logger).to receive(:log) + end + let (:log_message) {"logging message"} + let (:options) { + { + :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", + :flush_interval => 1, + :flush_size => 5, + :retry_timeout => 60 + } + } + let (:logger) {Logdna::Ruby.new("PPPPPPP", options)} + + it "should call and assignt the level accordingly - debug" do + logger.debug(log_message) + expect(logger).to have_received(:log).with(log_message, {:level=>"DEBUG"}) + end + it "should call and assignt the level accordingly - warn" do + logger.warn(log_message) + expect(logger).to have_received(:log).with(log_message, {:level=>"WARN"}) + end + it "should call and assignt the level accordingly - info" do + logger.info(log_message) + expect(logger).to have_received(:log).with(log_message, {:level=>"INFO"}) + end + it "should call and assignt the level accordingly - fatal" do + logger.fatal(log_message) + expect(logger).to have_received(:log).with(log_message, {:level=>"FATAL"}) + end + it "should call and assignt the level accordingly - error" do + logger.error(log_message) + expect(logger).to have_received(:log).with(log_message, {:level=>"ERROR"}) + end + it "should call and assignt the level accordingly - trace" do + logger.trace(log_message) + expect(logger).to have_received(:log).with(log_message, {:level=>"TRACE"}) + end +end From 96024f54978bbb1760a08efeedc726cd92894652 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 19:37:06 -0700 Subject: [PATCH 40/94] adds tests --- lib/logdna.rb | 9 +++ lib/logdna/client.rb | 3 +- spec/client.rb | 94 ++++++++++++++++----------- test-server.rb | 50 +++++++++++++++ tests.rb | 147 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 264 insertions(+), 39 deletions(-) create mode 100644 test-server.rb create mode 100644 tests.rb diff --git a/lib/logdna.rb b/lib/logdna.rb index 6c89e09..3fde248 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -129,3 +129,12 @@ def at_exit end end end + + +# require 'socket' +# puts "here?" +# loop { +# msg = $stdin.gets +# +# TCPSocket.open("localhost", 2000).puts(msg) +# } diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 8e20d12..d2af6f6 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -81,7 +81,6 @@ def write_to_buffer(msg, opts) # This method has to be called with @lock def send_request - puts "lll" @side_message_lock.synchronize do @buffer.concat(@side_messages) @side_messages.clear @@ -111,7 +110,7 @@ def send_request if @response.is_a?(Net::HTTPForbidden) @internal_logger.debug("Please provide a valid ingestion key") elsif !@response.is_a?(Net::HTTPSuccess) - handle_exception.call("The response is not successful ") + handle_exception.call("The response is not successful #{@response}") end @exception_flag = false rescue SocketError diff --git a/spec/client.rb b/spec/client.rb index ff96d3d..6cfa80c 100644 --- a/spec/client.rb +++ b/spec/client.rb @@ -1,39 +1,59 @@ require_relative "../lib/logdna.rb" require_relative "../lib/logdna/client.rb" -RSpec.describe "" do - before do - - end - - - let(:uri) {'uri'} - let(:opts) {{:app => "app"}} - let (:options) { - { - :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", - :flush_interval => 1, - :flush_size => 1, - :retry_timeout => 60 - } - } - - it "" do - request = spy("l;;;;;;;") - - # allow(request).to receive(:body) - # allow(request).to receive(:body) - - client = Logdna::Client.new(request, uri, options) - client.write_to_buffer("lllll", {}) - - - expect(request).to receive(:body) - end - -end +require 'socket' + +server = TCPServer.new("localhost", 3000) +client = server.accept + +client.write("hello") + + + + + + + + + + +# RSpec.describe "" do +# before do +# +# end +# +# +# # let(:uri) {} +# let(:opts) {{:app => "app"}} +# let (:options) { +# { +# :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", +# :flush_interval => 1, +# :flush_size => 1, +# :retry_timeout => 60 +# } +# } +# +# it "" do +# request = spy("l;;;;;;;") +# response = double(Net::HTTP) +# +# mockUri = double("uri") +# allow(mockUri).to receive(:hostname).and_return("host") +# allow(mockUri).to receive(:port).and_return("port") +# allow(mockUri).to receive(:scheme) +# allow(response).to receive(:start) +# +# client = Logdna::Client.new(request, mockUri, options) +# client.write_to_buffer("lllll", {}) +# +# +# expect(request).to receive(:body) +# end +# +# end diff --git a/test-server.rb b/test-server.rb new file mode 100644 index 0000000..5e2c6a0 --- /dev/null +++ b/test-server.rb @@ -0,0 +1,50 @@ +require 'socket' +class TestServer + attr_accessor :a + def startServer(port) + server = TCPServer.new(port) + puts server + data = '' + + Thread.start(server.accept) { |client| + headers = {} + while line = client.gets.split(' ', 2) + break if line[0] == "" + headers[line[0].chop] = line[1].strip + end + data = client.read(headers["Content-Length"].to_i) + client.puts("HTTP/1.1 200 OK") + client.close + }.join + + return eval(data) + end + + def startServerWithNotFound(port) + server = TCPServer.new(port) + + count = 0 + data = '' + loop do + count = count + 1 + Thread.start(server.accept) { |client| + headers = {} + while line = client.gets.split(' ', 2) + break if line[0] == "" + headers[line[0].chop] = line[1].strip + end + data =+ client.read(headers["Content-Length"].to_i) + if (count < 2) + client.puts("HTTP/1.1 404 Not Found") + else + client.puts("HTTP/1.1 200 OK") + end + client.close + }.join + break if count == 2 + end + return eval(data) +end +end + + diff --git a/tests.rb b/tests.rb new file mode 100644 index 0000000..04fcb2d --- /dev/null +++ b/tests.rb @@ -0,0 +1,147 @@ +require_relative "lib/logdna.rb" +require_relative "lib/logdna/client.rb" +require_relative "test-server.rb" +require "minitest/autorun" +class TestFoo < Minitest::Test + i_suck_and_my_tests_are_order_dependent! + + def get_options(port) + return { + :hostname => 'rubyTestHost', + :ip => '75.10.4.81', + :mac => '00:00:00:a1:2b:cc', + :app => 'rubyApplication', + :level => "INFO", + :env => "PRODUCTION", + :endpoint => "http://localhost:#{port}", + :flush_interval => 1, + :flush_size => 5, + :retry_timeout => 1 + } + end + + @@log_line = "log line" + + def warn_method(port) + options = get_options(port) + logdnaThread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.warn(@@log_line) + end + + serverThread = Thread.start do + server = TestServer.new() + recievedData = server.startServer(port) + + assert_equal(recievedData[:ls][0][:line], @@log_line) + assert_equal(recievedData[:ls][0][:app], options[:app]) + assert_equal(recievedData[:ls][0][:level], "WARN") + assert_equal(recievedData[:ls][0][:env], options[:env]) + end + + logdnaThread.join + serverThread.join + end + + def info_method(port) + options = get_options(port) + logdnaThread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.info(@@log_line) + end + + serverThread = Thread.start do + sor = TestServer.new() + recievedData = sor.startServer(port) + + assert_equal(recievedData[:ls][0][:line], @@log_line) + assert_equal(recievedData[:ls][0][:app], options[:app]) + assert_equal(recievedData[:ls][0][:level], "INFO") + assert_equal(recievedData[:ls][0][:env], options[:env]) + end + + logdnaThread.join + serverThread.join + end + def debug_method(port) + options = get_options(port) + logdnaThread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.debug(@@log_line) + end + + serverThread = Thread.start do + sor = TestServer.new() + recievedData = sor.startServer(port) + + assert_equal(recievedData[:ls][0][:line], @@log_line) + assert_equal(recievedData[:ls][0][:app], options[:app]) + assert_equal(recievedData[:ls][0][:level], "DEBUG") + assert_equal(recievedData[:ls][0][:env], options[:env]) + end + + logdnaThread.join + serverThread.join + end + def fatal_method(port) + options = get_options(port) + logdnaThread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.fatal(@@log_line) + end + + serverThread = Thread.start do + sor = TestServer.new() + recievedData = sor.startServer(port) + + assert_equal(recievedData[:ls][0][:line], @@log_line) + assert_equal(recievedData[:ls][0][:app], options[:app]) + assert_equal(recievedData[:ls][0][:level], "FATAL") + assert_equal(recievedData[:ls][0][:env], options[:env]) + end + + logdnaThread.join + serverThread.join + end + + # Should retry to connect and preserve the failed line + def fatal_method_not_found(port) + second_line = " second line" + options = get_options(port) + logdnaThread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.fatal(@@log_line) + logger.fatal(second_line) + end + + serverThread = Thread.start do + sor = TestServer.new() + data = '' + recievedData = sor.startServerWithNotFound(port) + # The order of recieved lines is unpredictable. + recievedLines = [ + recievedData[:ls][0][:line], + recievedData[:ls][1][:line] + ] + + assert_includes(recievedLines, @@log_line) + assert_includes(recievedLines, second_line) + + assert_equal(recievedData[:ls][0][:app], options[:app]) + assert_equal(recievedData[:ls][0][:level], "FATAL") + assert_equal(recievedData[:ls][0][:env], options[:env]) + end + + logdnaThread.join + serverThread.join + end + + def test_all + warn_method(2000) + info_method(2001) + fatal_method(2002) + debug_method(2003) + fatal_method_not_found(2004) + end +end + From 6ecb59f8c2ebc315a578f1261a8e39b51cdb0fdb Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 19:43:31 -0700 Subject: [PATCH 41/94] rubocop --- Gemfile | 1 + spec/client.rb | 59 ---------- spec/logdna.test.rb | 49 -------- tests.rb | 276 ++++++++++++++++++++++---------------------- 4 files changed, 140 insertions(+), 245 deletions(-) delete mode 100644 spec/client.rb delete mode 100644 spec/logdna.test.rb diff --git a/Gemfile b/Gemfile index bb242f2..293d51e 100755 --- a/Gemfile +++ b/Gemfile @@ -2,3 +2,4 @@ source 'https://rubygems.org' # Specify your gem's dependencies in logdna_ruby.gemspec gemspec +minitest diff --git a/spec/client.rb b/spec/client.rb deleted file mode 100644 index 6cfa80c..0000000 --- a/spec/client.rb +++ /dev/null @@ -1,59 +0,0 @@ -require_relative "../lib/logdna.rb" -require_relative "../lib/logdna/client.rb" -require 'socket' - -server = TCPServer.new("localhost", 3000) -client = server.accept - -client.write("hello") - - - - - - - - - - -# RSpec.describe "" do -# before do -# -# end -# -# -# # let(:uri) {} -# let(:opts) {{:app => "app"}} -# let (:options) { -# { -# :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", -# :flush_interval => 1, -# :flush_size => 1, -# :retry_timeout => 60 -# } -# } -# -# it "" do -# request = spy("l;;;;;;;") -# response = double(Net::HTTP) -# -# mockUri = double("uri") -# allow(mockUri).to receive(:hostname).and_return("host") -# allow(mockUri).to receive(:port).and_return("port") -# allow(mockUri).to receive(:scheme) -# allow(response).to receive(:start) -# -# client = Logdna::Client.new(request, mockUri, options) -# client.write_to_buffer("lllll", {}) -# -# -# expect(request).to receive(:body) -# end -# -# end diff --git a/spec/logdna.test.rb b/spec/logdna.test.rb deleted file mode 100644 index 98e6c5b..0000000 --- a/spec/logdna.test.rb +++ /dev/null @@ -1,49 +0,0 @@ -require_relative "../lib/logdna.rb" -require_relative "../lib/logdna/client.rb" - -RSpec.describe "should call the level named methods and assign the correct method" do - before do - allow(logger).to receive(:log) - end - let (:log_message) {"logging message"} - let (:options) { - { - :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", - :flush_interval => 1, - :flush_size => 5, - :retry_timeout => 60 - } - } - let (:logger) {Logdna::Ruby.new("PPPPPPP", options)} - - it "should call and assignt the level accordingly - debug" do - logger.debug(log_message) - expect(logger).to have_received(:log).with(log_message, {:level=>"DEBUG"}) - end - it "should call and assignt the level accordingly - warn" do - logger.warn(log_message) - expect(logger).to have_received(:log).with(log_message, {:level=>"WARN"}) - end - it "should call and assignt the level accordingly - info" do - logger.info(log_message) - expect(logger).to have_received(:log).with(log_message, {:level=>"INFO"}) - end - it "should call and assignt the level accordingly - fatal" do - logger.fatal(log_message) - expect(logger).to have_received(:log).with(log_message, {:level=>"FATAL"}) - end - it "should call and assignt the level accordingly - error" do - logger.error(log_message) - expect(logger).to have_received(:log).with(log_message, {:level=>"ERROR"}) - end - it "should call and assignt the level accordingly - trace" do - logger.trace(log_message) - expect(logger).to have_received(:log).with(log_message, {:level=>"TRACE"}) - end -end diff --git a/tests.rb b/tests.rb index 04fcb2d..9ced6db 100644 --- a/tests.rb +++ b/tests.rb @@ -1,147 +1,149 @@ +# frozen_string_literal: true + +require "minitest/autorun" + require_relative "lib/logdna.rb" require_relative "lib/logdna/client.rb" require_relative "test-server.rb" -require "minitest/autorun" -class TestFoo < Minitest::Test - i_suck_and_my_tests_are_order_dependent! - - def get_options(port) - return { - :hostname => 'rubyTestHost', - :ip => '75.10.4.81', - :mac => '00:00:00:a1:2b:cc', - :app => 'rubyApplication', - :level => "INFO", - :env => "PRODUCTION", - :endpoint => "http://localhost:#{port}", - :flush_interval => 1, - :flush_size => 5, - :retry_timeout => 1 - } - end - - @@log_line = "log line" - - def warn_method(port) - options = get_options(port) - logdnaThread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.warn(@@log_line) - end - - serverThread = Thread.start do - server = TestServer.new() - recievedData = server.startServer(port) - - assert_equal(recievedData[:ls][0][:line], @@log_line) - assert_equal(recievedData[:ls][0][:app], options[:app]) - assert_equal(recievedData[:ls][0][:level], "WARN") - assert_equal(recievedData[:ls][0][:env], options[:env]) - end - - logdnaThread.join - serverThread.join + +class TestLogDNARuby < Minitest::Test + @@log_line = "log line" + + def get_options(port) + { + hostname: "rubyTestHost", + ip: "75.10.4.81", + mac: "00:00:00:a1:2b:cc", + app: "rubyApplication", + level: "INFO", + env: "PRODUCTION", + endpoint: "http://localhost:#{port}", + flush_interval: 1, + flush_size: 5, + retry_timeout: 1 + } + end + + def warn_method(port) + options = get_options(port) + logdna_thread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.warn(@@log_line) end - - def info_method(port) - options = get_options(port) - logdnaThread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.info(@@log_line) - end - - serverThread = Thread.start do - sor = TestServer.new() - recievedData = sor.startServer(port) - - assert_equal(recievedData[:ls][0][:line], @@log_line) - assert_equal(recievedData[:ls][0][:app], options[:app]) - assert_equal(recievedData[:ls][0][:level], "INFO") - assert_equal(recievedData[:ls][0][:env], options[:env]) - end - - logdnaThread.join - serverThread.join + + server_thread = Thread.start do + server = TestServer.new + recieved_data = server.startServer(port) + + assert_equal(recieved_data[:ls][0][:line], @@log_line) + assert_equal(recieved_data[:ls][0][:app], options[:app]) + assert_equal(recieved_data[:ls][0][:level], "WARN") + assert_equal(recieved_data[:ls][0][:env], options[:env]) end - def debug_method(port) - options = get_options(port) - logdnaThread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.debug(@@log_line) - end - - serverThread = Thread.start do - sor = TestServer.new() - recievedData = sor.startServer(port) - - assert_equal(recievedData[:ls][0][:line], @@log_line) - assert_equal(recievedData[:ls][0][:app], options[:app]) - assert_equal(recievedData[:ls][0][:level], "DEBUG") - assert_equal(recievedData[:ls][0][:env], options[:env]) - end - - logdnaThread.join - serverThread.join + + logdna_thread.join + server_thread.join + end + + def info_method(port) + options = get_options(port) + logdna_thread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.info(@@log_line) end - def fatal_method(port) - options = get_options(port) - logdnaThread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.fatal(@@log_line) - end - - serverThread = Thread.start do - sor = TestServer.new() - recievedData = sor.startServer(port) - - assert_equal(recievedData[:ls][0][:line], @@log_line) - assert_equal(recievedData[:ls][0][:app], options[:app]) - assert_equal(recievedData[:ls][0][:level], "FATAL") - assert_equal(recievedData[:ls][0][:env], options[:env]) - end - - logdnaThread.join - serverThread.join + + server_thread = Thread.start do + sor = TestServer.new + recieved_data = sor.startServer(port) + + assert_equal(recieved_data[:ls][0][:line], @@log_line) + assert_equal(recieved_data[:ls][0][:app], options[:app]) + assert_equal(recieved_data[:ls][0][:level], "INFO") + assert_equal(recieved_data[:ls][0][:env], options[:env]) end - - # Should retry to connect and preserve the failed line - def fatal_method_not_found(port) - second_line = " second line" - options = get_options(port) - logdnaThread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.fatal(@@log_line) - logger.fatal(second_line) - end - - serverThread = Thread.start do - sor = TestServer.new() - data = '' - recievedData = sor.startServerWithNotFound(port) - # The order of recieved lines is unpredictable. - recievedLines = [ - recievedData[:ls][0][:line], - recievedData[:ls][1][:line] - ] - - assert_includes(recievedLines, @@log_line) - assert_includes(recievedLines, second_line) - - assert_equal(recievedData[:ls][0][:app], options[:app]) - assert_equal(recievedData[:ls][0][:level], "FATAL") - assert_equal(recievedData[:ls][0][:env], options[:env]) - end - - logdnaThread.join - serverThread.join + + logdna_thread.join + server_thread.join + end + + def debug_method(port) + options = get_options(port) + logdna_thread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.debug(@@log_line) end - - def test_all - warn_method(2000) - info_method(2001) - fatal_method(2002) - debug_method(2003) - fatal_method_not_found(2004) + + server_thread = Thread.start do + sor = TestServer.new + recieved_data = sor.startServer(port) + + assert_equal(recieved_data[:ls][0][:line], @@log_line) + assert_equal(recieved_data[:ls][0][:app], options[:app]) + assert_equal(recieved_data[:ls][0][:level], "DEBUG") + assert_equal(recieved_data[:ls][0][:env], options[:env]) end -end + logdna_thread.join + server_thread.join + end + + def fatal_method(port) + options = get_options(port) + logdna_thread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.fatal(@@log_line) + end + + server_thread = Thread.start do + sor = TestServer.new + recieved_data = sor.startServer(port) + + assert_equal(recieved_data[:ls][0][:line], @@log_line) + assert_equal(recieved_data[:ls][0][:app], options[:app]) + assert_equal(recieved_data[:ls][0][:level], "FATAL") + assert_equal(recieved_data[:ls][0][:env], options[:env]) + end + + logdna_thread.join + server_thread.join + end + + # Should retry to connect and preserve the failed line + def fatal_method_not_found(port) + second_line = " second line" + options = get_options(port) + logdna_thread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.fatal(@@log_line) + logger.fatal(second_line) + end + + server_thread = Thread.start do + sor = TestServer.new + recieved_data = sor.startServerWithNotFound(port) + # The order of recieved lines is unpredictable. + recieved_lines = [ + recieved_data[:ls][0][:line], + recieved_data[:ls][1][:line] + ] + + assert_includes(recieved_lines, @@log_line) + assert_includes(recieved_lines, second_line) + + assert_equal(recieved_data[:ls][0][:app], options[:app]) + assert_equal(recieved_data[:ls][0][:level], "FATAL") + assert_equal(recieved_data[:ls][0][:env], options[:env]) + end + + logdna_thread.join + server_thread.join + end + + def test_all + warn_method(2000) + info_method(2001) + fatal_method(2002) + debug_method(2003) + fatal_method_not_found(2004) + end +end From 7c518d42e0a115bf3e86868c908f0b7f4a9c9019 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 19:51:31 -0700 Subject: [PATCH 42/94] lints --- .rubocop.yml | 7 +++++-- test-server.rb | 50 ------------------------------------------------ test_server.rb | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ tests.rb | 12 ++++++------ 4 files changed, 63 insertions(+), 58 deletions(-) delete mode 100644 test-server.rb create mode 100644 test_server.rb diff --git a/.rubocop.yml b/.rubocop.yml index 63b7ceb..5a6eab0 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -66,8 +66,6 @@ Style/RescueStandardError: # 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. @@ -110,6 +108,9 @@ Style/SingleLineBlockParams: Lint/ShadowingOuterLocalVariable: Enabled: false +Lint/AssignmentInCondition: + Enabled: false + # Check with yard instead. Style/Documentation: Enabled: false @@ -123,6 +124,8 @@ Naming/BinaryOperatorParameterName: Lint/Debugger: Enabled: false +Security/Eval: + Enabled: false # Style preference Style/MethodDefParentheses: Enabled: false diff --git a/test-server.rb b/test-server.rb deleted file mode 100644 index 5e2c6a0..0000000 --- a/test-server.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'socket' -class TestServer - attr_accessor :a - def startServer(port) - server = TCPServer.new(port) - puts server - data = '' - - Thread.start(server.accept) { |client| - headers = {} - while line = client.gets.split(' ', 2) - break if line[0] == "" - headers[line[0].chop] = line[1].strip - end - data = client.read(headers["Content-Length"].to_i) - client.puts("HTTP/1.1 200 OK") - client.close - }.join - - return eval(data) - end - - def startServerWithNotFound(port) - server = TCPServer.new(port) - - count = 0 - data = '' - loop do - count = count + 1 - Thread.start(server.accept) { |client| - headers = {} - while line = client.gets.split(' ', 2) - break if line[0] == "" - headers[line[0].chop] = line[1].strip - end - data =+ client.read(headers["Content-Length"].to_i) - if (count < 2) - client.puts("HTTP/1.1 404 Not Found") - else - client.puts("HTTP/1.1 200 OK") - end - client.close - }.join - break if count == 2 - end - return eval(data) -end -end - - diff --git a/test_server.rb b/test_server.rb new file mode 100644 index 0000000..76fa18f --- /dev/null +++ b/test_server.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +require "socket" +class TestServer + attr_accessor :a + def start_server(port) + server = TCPServer.new(port) + puts server + data = "" + + Thread.start(server.accept) { |client| + headers = {} + while line = client.gets.split(" ", 2) + break if line[0] == "" + + headers[line[0].chop] = line[1].strip + end + data = client.read(headers["Content-Length"].to_i) + client.puts("HTTP/1.1 200 OK") + client.close + }.join + + eval(data) + end + + def return_not_found_res(port) + server = TCPServer.new(port) + + count = 0 + data = "" + loop do + count += 1 + Thread.start(server.accept) { |client| + headers = {} + while line = client.gets.split(" ", 2) + break if line[0] == "" + + headers[line[0].chop] = line[1].strip + end + data = + client.read(headers["Content-Length"].to_i) + if count < 2 + client.puts("HTTP/1.1 404 Not Found") + else + client.puts("HTTP/1.1 200 OK") + end + client.close + }.join + break if count == 2 + end + eval(data) +end +end diff --git a/tests.rb b/tests.rb index 9ced6db..7aace05 100644 --- a/tests.rb +++ b/tests.rb @@ -4,7 +4,7 @@ require_relative "lib/logdna.rb" require_relative "lib/logdna/client.rb" -require_relative "test-server.rb" +require_relative "test_server.rb" class TestLogDNARuby < Minitest::Test @@log_line = "log line" @@ -33,7 +33,7 @@ def warn_method(port) server_thread = Thread.start do server = TestServer.new - recieved_data = server.startServer(port) + recieved_data = server.start_server(port) assert_equal(recieved_data[:ls][0][:line], @@log_line) assert_equal(recieved_data[:ls][0][:app], options[:app]) @@ -54,7 +54,7 @@ def info_method(port) server_thread = Thread.start do sor = TestServer.new - recieved_data = sor.startServer(port) + recieved_data = sor.start_server(port) assert_equal(recieved_data[:ls][0][:line], @@log_line) assert_equal(recieved_data[:ls][0][:app], options[:app]) @@ -75,7 +75,7 @@ def debug_method(port) server_thread = Thread.start do sor = TestServer.new - recieved_data = sor.startServer(port) + recieved_data = sor.start_server(port) assert_equal(recieved_data[:ls][0][:line], @@log_line) assert_equal(recieved_data[:ls][0][:app], options[:app]) @@ -96,7 +96,7 @@ def fatal_method(port) server_thread = Thread.start do sor = TestServer.new - recieved_data = sor.startServer(port) + recieved_data = sor.start_server(port) assert_equal(recieved_data[:ls][0][:line], @@log_line) assert_equal(recieved_data[:ls][0][:app], options[:app]) @@ -120,7 +120,7 @@ def fatal_method_not_found(port) server_thread = Thread.start do sor = TestServer.new - recieved_data = sor.startServerWithNotFound(port) + recieved_data = sor.return_not_found_res(port) # The order of recieved lines is unpredictable. recieved_lines = [ recieved_data[:ls][0][:line], From 7800ad42b951f755994b0dc3a249deee354dff1a Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 19:52:39 -0700 Subject: [PATCH 43/94] remove puts --- test_server.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test_server.rb b/test_server.rb index 76fa18f..3c7a840 100644 --- a/test_server.rb +++ b/test_server.rb @@ -5,7 +5,6 @@ class TestServer attr_accessor :a def start_server(port) server = TCPServer.new(port) - puts server data = "" Thread.start(server.accept) { |client| From 7fc3c4cf6cac8d7295bd04f658bf33eef9b40eb3 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 19:57:13 -0700 Subject: [PATCH 44/94] 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 From 5f156e6222036709bb1f18a0b9723bc4bdca1d94 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 20:00:12 -0700 Subject: [PATCH 45/94] clean --- lib/logdna.rb | 5 +---- lib/logdna/client.rb | 11 +++++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 3fde248..75651e8 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -1,7 +1,4 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -# require 'singleton' +require 'logger' require "socket" require "uri" require_relative "logdna/client.rb" diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index d2af6f6..fdccbad 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -1,9 +1,8 @@ -require 'net/http' -require 'socket' -require 'json' -require 'concurrent' -require 'thread' -require 'date' +require "net/http" +require "socket" +require "json" +require "concurrent" +require "date" module Logdna class Client From 4531dc2c60183d3b465cf9949a41850506193fc9 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 16 Aug 2019 14:46:46 -0700 Subject: [PATCH 46/94] working on the scheduler --- lib/logdna/client.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index fdccbad..b8507d9 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -76,6 +76,8 @@ def write_to_buffer(msg, opts) @side_messages.push(process_message(msg, opts)) end end + timer_task.execute + timer_task end # This method has to be called with @lock From a7b236a8688d1a4f9c1c2306b044717f108c28f1 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 21 Aug 2019 21:13:04 -0700 Subject: [PATCH 47/94] savingBranch --- bin/bundle | 105 +++++++++++++++++++++++++++++++++++++++++++ bin/htmldiff | 29 ++++++++++++ bin/ldiff | 29 ++++++++++++ bin/rake | 29 ++++++++++++ bin/rspec | 29 ++++++++++++ bin/safe_yaml | 29 ++++++++++++ lib/logdna/client.rb | 2 +- 7 files changed, 251 insertions(+), 1 deletion(-) 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 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/client.rb b/lib/logdna/client.rb index b8507d9..366d18e 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -135,7 +135,7 @@ def flush else schedule_flush end - end + end def exitout flush From 4f65eda085331afce788506aeba92c11d642203e Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:05:07 -0700 Subject: [PATCH 48/94] the final test is insihed --- spec/logdnaTest.rb | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 spec/logdnaTest.rb 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 857f4e2ad1b30d137be37cb3a6f5116af2bc2621 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:10:42 -0700 Subject: [PATCH 49/94] 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 580406cf1c2928e87c604eb05e6feb98a166f182 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:12:21 -0700 Subject: [PATCH 50/94] 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 78efc61914f2515bafa127d5d30dc076db5e3bc3 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:42:04 -0700 Subject: [PATCH 51/94] 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 108e61c63562b9e50537662b6d766a6ce1b2de25 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 22 Aug 2019 15:44:04 -0700 Subject: [PATCH 52/94] 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 2637024cb0a621b43af702265597d0a34172ae2b Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 5 Sep 2019 14:21:12 -0700 Subject: [PATCH 53/94] address the comments and improve the excpetion handling logic --- lib/logdna/client.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 366d18e..83ae726 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -97,6 +97,13 @@ def send_request @exception_flag = true @side_message_lock.synchronize do @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 From b5bc0a63047a549eabc9a5217cc54f1a20a8f156 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 5 Sep 2019 16:41:50 -0700 Subject: [PATCH 54/94] add rubycop config file and run it --- .rubocop.yml | 12 ++++++++++++ lib/logdna/client.rb | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index 5a6eab0..207ebfc 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -5,7 +5,11 @@ 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: +<<<<<<< HEAD Max: 100 +======= + Max: 40 +>>>>>>> add rubycop config file and run it # The guiding principle of classes is SRP, SRP can't be accurately measured by LoC Metrics/ClassLength: @@ -22,10 +26,18 @@ Metrics/PerceivedComplexity: # No space makes the method definition shorter and differentiates # from a regular assignment. +<<<<<<< HEAD Layout/AccessModifierIndentation: Enabled: true IndentationWidth: 4 +======= +Style/Layout: + EnforcedStyle: no_space + EnforcedHashRocketStyle: table + EnforcedColonStyle: table + SpaceBeforeBlockParameters: false +>>>>>>> add rubycop config file and run it # Single quotes being faster is hardly measurable and only affects parse time. # Enforcing double quotes reduces the times where you need to change them diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 83ae726..612dec1 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -36,7 +36,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 } processedMessage.delete(:meta) if processedMessage[:meta].nil? processedMessage From 230bf72c86b9e258ebda835d7f46da34077c1d32 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 27 Sep 2019 14:01:23 -0700 Subject: [PATCH 55/94] address comments --- lib/logdna.rb | 6 ++++++ lib/logdna/client.rb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 75651e8..42e67d3 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -124,6 +124,12 @@ def at_exit @client.exitout() end 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 612dec1..83ae726 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -36,7 +36,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, } processedMessage.delete(:meta) if processedMessage[:meta].nil? processedMessage From f1a67d46ebdb65329f29d590bf2155108a09e45e Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 27 Sep 2019 14:05:33 -0700 Subject: [PATCH 56/94] lints --- lib/logdna.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 42e67d3..191e0c5 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -127,7 +127,7 @@ def at_exit def close if defined? @client and !@client.nil? - @client.exitout() + @client.exitout end end end From 2871c0e5c0198fd7d0a9d53bf2007da71174f507 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 30 Sep 2019 15:59:33 -0700 Subject: [PATCH 57/94] change the locking logic ang change linting rules --- .rubocop.yml | 14 +++++++------- lib/logdna.rb | 10 ++++++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 207ebfc..da2d5aa 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -5,11 +5,15 @@ 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: +<<<<<<< HEAD <<<<<<< HEAD Max: 100 ======= Max: 40 >>>>>>> add rubycop config file and run it +======= + Max: 100 +>>>>>>> change the locking logic ang change linting rules # The guiding principle of classes is SRP, SRP can't be accurately measured by LoC Metrics/ClassLength: @@ -27,17 +31,13 @@ Metrics/PerceivedComplexity: # No space makes the method definition shorter and differentiates # from a regular assignment. <<<<<<< HEAD +<<<<<<< HEAD +======= +>>>>>>> change the locking logic ang change linting rules Layout/AccessModifierIndentation: Enabled: true IndentationWidth: 4 -======= -Style/Layout: - EnforcedStyle: no_space - EnforcedHashRocketStyle: table - EnforcedColonStyle: table - SpaceBeforeBlockParameters: false ->>>>>>> add rubycop config file and run it # Single quotes being faster is hardly measurable and only affects parse time. # Enforcing double quotes reduces the times where you need to change them diff --git a/lib/logdna.rb b/lib/logdna.rb index 191e0c5..c4bdaa7 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -46,7 +46,7 @@ def default_opts } end - def level=(value) + def assign_level=(value) if value.is_a? Numeric @level = Resources::LOG_LEVELS[value] return @@ -126,9 +126,11 @@ def at_exit 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 From babec9d8e00a87a5a967c9d5cfc78688114894eb Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 30 Sep 2019 18:00:39 -0700 Subject: [PATCH 58/94] check lock owning --- 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 83ae726..4d63a5f 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -103,7 +103,7 @@ def send_request @side_messages.concat(@buffer) ensure @buffer.clear - @lock.unlock if @lock.locked? + @lock.unlock if @lock.locked? && @lock.owned? end end From 6b110f3acc0f27f87343959c2535a92b21ead325 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Tue, 1 Oct 2019 12:10:36 -0700 Subject: [PATCH 59/94] fix the bug and add the lock to the side messages --- lib/logdna/client.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 4d63a5f..764d412 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -103,7 +103,6 @@ def send_request @side_messages.concat(@buffer) ensure @buffer.clear - @lock.unlock if @lock.locked? && @lock.owned? end end From d25e512102d95d934d9d9a9a2cdefe1b6092c285 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 9 Oct 2019 11:01:23 -0700 Subject: [PATCH 60/94] rubocop --- lib/logdna.rb | 4 ++++ logdna.gemspec | 1 + 2 files changed, 5 insertions(+) diff --git a/lib/logdna.rb b/lib/logdna.rb index c4bdaa7..152bd0d 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -74,7 +74,11 @@ def log(message = nil, opts = {}) define_method name do |msg = nil, opts = {}, &block| self.log(msg, opts.merge( level: lvl +<<<<<<< HEAD ), &block) +======= + )) +>>>>>>> rubocop end define_method "#{name}?" do diff --git a/logdna.gemspec b/logdna.gemspec index 74c76f2..b4fb928 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' From 5c3b7dea80cf771190d349c96f926cc458e31b0f Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 9 Oct 2019 11:11:14 -0700 Subject: [PATCH 61/94] forgot to move another flag swtich into the lock block --- lib/logdna.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 152bd0d..c4bdaa7 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -74,11 +74,7 @@ def log(message = nil, opts = {}) define_method name do |msg = nil, opts = {}, &block| self.log(msg, opts.merge( level: lvl -<<<<<<< HEAD ), &block) -======= - )) ->>>>>>> rubocop end define_method "#{name}?" do From ebcbeb979e37a7e309a87c7ad78c97216d5cff4e Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 17 Oct 2019 13:15:07 -0700 Subject: [PATCH 62/94] commit --- lib/logdna.rb | 4 +++- logdna.gemspec | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index c4bdaa7..e2951c7 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -130,7 +130,9 @@ def close 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/logdna.gemspec b/logdna.gemspec index b4fb928..74c76f2 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 e840e35a0a79c2084a584f77b0eaf7a9224edbce Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 21 Oct 2019 17:45:31 -0700 Subject: [PATCH 63/94] address the comments --- lib/logdna/client.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 764d412..415058a 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -120,6 +120,7 @@ def send_request handle_exception.call("The response is not successful #{@response}") end @exception_flag = false + p @response rescue SocketError handle_exception.call("Network connectivity issue") rescue Errno::ECONNREFUSED => e From 681c53d22a5fd1f3b616b450f5301def01b0805e Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 21 Oct 2019 17:55:43 -0700 Subject: [PATCH 64/94] 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 415058a..764d412 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -120,7 +120,6 @@ def send_request handle_exception.call("The response is not successful #{@response}") end @exception_flag = false - p @response rescue SocketError handle_exception.call("Network connectivity issue") rescue Errno::ECONNREFUSED => e From 54b0839023cdcee95755c6c5a69be5532d4ccdc4 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 24 Oct 2019 12:01:52 -0700 Subject: [PATCH 65/94] 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 12d38034e119d78abbb3c273d5bef633ebef349b Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 24 Oct 2019 14:06:25 -0700 Subject: [PATCH 66/94] dd --- lib/logdna.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index e2951c7..efbe62f 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -1,3 +1,6 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + require 'logger' require "socket" require "uri" @@ -18,11 +21,14 @@ def initialize(key, opts = {}) @level = opts[:level] || "INFO" @env = opts[:env] @meta = opts[:meta] + @internal_logger = Logger.new(STDOUT) + @internal_logger.level = Logger::DEBUG + 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" + @internal_logger.debug("Hostname or Appname is over #{Resources::MAX_INPUT_LENGTH} characters") return end @@ -60,7 +66,8 @@ def log(message = nil, opts = {}) message = yield end if message.nil? - puts "provide either a message or block" + @internal_logger.debug("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( @@ -98,7 +105,7 @@ def <<(msg = nil, opts = {}) end def add(*_arg) - puts "add not supported in LogDNA logger" + @internal_logger.debug("add not supported in LogDNA logger") false end @@ -109,7 +116,7 @@ def unknown(msg = nil, opts = {}) end def datetime_format(*_arg) - puts "datetime_format not supported in LogDNA logger" + @internal_logger.debug("datetime_format not supported in LogDNA logger") false end From df4943c1257b3284d8afeab44ae65f392bb48ccf Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 24 Oct 2019 17:19:39 -0700 Subject: [PATCH 67/94] dss --- lib/logdna/client.rb | 1 + spec/client.rb | 39 +++++++++++++++++++++++++++++++++++ spec/logdna.test.rb | 49 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 spec/client.rb create mode 100644 spec/logdna.test.rb diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 764d412..c94125a 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -82,6 +82,7 @@ def write_to_buffer(msg, opts) # This method has to be called with @lock def send_request + puts "lll" @side_message_lock.synchronize do @buffer.concat(@side_messages) @side_messages.clear diff --git a/spec/client.rb b/spec/client.rb new file mode 100644 index 0000000..ff96d3d --- /dev/null +++ b/spec/client.rb @@ -0,0 +1,39 @@ +require_relative "../lib/logdna.rb" +require_relative "../lib/logdna/client.rb" +RSpec.describe "" do + before do + + end + + + let(:uri) {'uri'} + let(:opts) {{:app => "app"}} + let (:options) { + { + :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", + :flush_interval => 1, + :flush_size => 1, + :retry_timeout => 60 + } + } + + it "" do + request = spy("l;;;;;;;") + + # allow(request).to receive(:body) + # allow(request).to receive(:body) + + client = Logdna::Client.new(request, uri, options) + client.write_to_buffer("lllll", {}) + + + expect(request).to receive(:body) + end + +end diff --git a/spec/logdna.test.rb b/spec/logdna.test.rb new file mode 100644 index 0000000..98e6c5b --- /dev/null +++ b/spec/logdna.test.rb @@ -0,0 +1,49 @@ +require_relative "../lib/logdna.rb" +require_relative "../lib/logdna/client.rb" + +RSpec.describe "should call the level named methods and assign the correct method" do + before do + allow(logger).to receive(:log) + end + let (:log_message) {"logging message"} + let (:options) { + { + :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", + :flush_interval => 1, + :flush_size => 5, + :retry_timeout => 60 + } + } + let (:logger) {Logdna::Ruby.new("PPPPPPP", options)} + + it "should call and assignt the level accordingly - debug" do + logger.debug(log_message) + expect(logger).to have_received(:log).with(log_message, {:level=>"DEBUG"}) + end + it "should call and assignt the level accordingly - warn" do + logger.warn(log_message) + expect(logger).to have_received(:log).with(log_message, {:level=>"WARN"}) + end + it "should call and assignt the level accordingly - info" do + logger.info(log_message) + expect(logger).to have_received(:log).with(log_message, {:level=>"INFO"}) + end + it "should call and assignt the level accordingly - fatal" do + logger.fatal(log_message) + expect(logger).to have_received(:log).with(log_message, {:level=>"FATAL"}) + end + it "should call and assignt the level accordingly - error" do + logger.error(log_message) + expect(logger).to have_received(:log).with(log_message, {:level=>"ERROR"}) + end + it "should call and assignt the level accordingly - trace" do + logger.trace(log_message) + expect(logger).to have_received(:log).with(log_message, {:level=>"TRACE"}) + end +end From b60d0e5a0bdc8b695a7af1745239975923fdab0c Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 19:37:06 -0700 Subject: [PATCH 68/94] adds tests --- lib/logdna/client.rb | 1 - spec/client.rb | 94 +++++++++------ test-server.rb | 50 ++++++++ tests.rb | 271 +++++++++++++++++++++---------------------- 4 files changed, 241 insertions(+), 175 deletions(-) create mode 100644 test-server.rb diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index c94125a..764d412 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -82,7 +82,6 @@ def write_to_buffer(msg, opts) # This method has to be called with @lock def send_request - puts "lll" @side_message_lock.synchronize do @buffer.concat(@side_messages) @side_messages.clear diff --git a/spec/client.rb b/spec/client.rb index ff96d3d..6cfa80c 100644 --- a/spec/client.rb +++ b/spec/client.rb @@ -1,39 +1,59 @@ require_relative "../lib/logdna.rb" require_relative "../lib/logdna/client.rb" -RSpec.describe "" do - before do - - end - - - let(:uri) {'uri'} - let(:opts) {{:app => "app"}} - let (:options) { - { - :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", - :flush_interval => 1, - :flush_size => 1, - :retry_timeout => 60 - } - } - - it "" do - request = spy("l;;;;;;;") - - # allow(request).to receive(:body) - # allow(request).to receive(:body) - - client = Logdna::Client.new(request, uri, options) - client.write_to_buffer("lllll", {}) - - - expect(request).to receive(:body) - end - -end +require 'socket' + +server = TCPServer.new("localhost", 3000) +client = server.accept + +client.write("hello") + + + + + + + + + + +# RSpec.describe "" do +# before do +# +# end +# +# +# # let(:uri) {} +# let(:opts) {{:app => "app"}} +# let (:options) { +# { +# :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", +# :flush_interval => 1, +# :flush_size => 1, +# :retry_timeout => 60 +# } +# } +# +# it "" do +# request = spy("l;;;;;;;") +# response = double(Net::HTTP) +# +# mockUri = double("uri") +# allow(mockUri).to receive(:hostname).and_return("host") +# allow(mockUri).to receive(:port).and_return("port") +# allow(mockUri).to receive(:scheme) +# allow(response).to receive(:start) +# +# client = Logdna::Client.new(request, mockUri, options) +# client.write_to_buffer("lllll", {}) +# +# +# expect(request).to receive(:body) +# end +# +# end diff --git a/test-server.rb b/test-server.rb new file mode 100644 index 0000000..5e2c6a0 --- /dev/null +++ b/test-server.rb @@ -0,0 +1,50 @@ +require 'socket' +class TestServer + attr_accessor :a + def startServer(port) + server = TCPServer.new(port) + puts server + data = '' + + Thread.start(server.accept) { |client| + headers = {} + while line = client.gets.split(' ', 2) + break if line[0] == "" + headers[line[0].chop] = line[1].strip + end + data = client.read(headers["Content-Length"].to_i) + client.puts("HTTP/1.1 200 OK") + client.close + }.join + + return eval(data) + end + + def startServerWithNotFound(port) + server = TCPServer.new(port) + + count = 0 + data = '' + loop do + count = count + 1 + Thread.start(server.accept) { |client| + headers = {} + while line = client.gets.split(' ', 2) + break if line[0] == "" + headers[line[0].chop] = line[1].strip + end + data =+ client.read(headers["Content-Length"].to_i) + if (count < 2) + client.puts("HTTP/1.1 404 Not Found") + else + client.puts("HTTP/1.1 200 OK") + end + client.close + }.join + break if count == 2 + end + return eval(data) +end +end + + diff --git a/tests.rb b/tests.rb index 7aace05..5fd37d2 100644 --- a/tests.rb +++ b/tests.rb @@ -1,149 +1,146 @@ -# frozen_string_literal: true - -require "minitest/autorun" - require_relative "lib/logdna.rb" require_relative "lib/logdna/client.rb" -require_relative "test_server.rb" - -class TestLogDNARuby < Minitest::Test - @@log_line = "log line" - - def get_options(port) - { - hostname: "rubyTestHost", - ip: "75.10.4.81", - mac: "00:00:00:a1:2b:cc", - app: "rubyApplication", - level: "INFO", - env: "PRODUCTION", - endpoint: "http://localhost:#{port}", - flush_interval: 1, - flush_size: 5, - retry_timeout: 1 - } - end - - def warn_method(port) - options = get_options(port) - logdna_thread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.warn(@@log_line) - end - - server_thread = Thread.start do - server = TestServer.new - recieved_data = server.start_server(port) - - assert_equal(recieved_data[:ls][0][:line], @@log_line) - assert_equal(recieved_data[:ls][0][:app], options[:app]) - assert_equal(recieved_data[:ls][0][:level], "WARN") - assert_equal(recieved_data[:ls][0][:env], options[:env]) - end - - logdna_thread.join - server_thread.join - end - - def info_method(port) - options = get_options(port) - logdna_thread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.info(@@log_line) - end - - server_thread = Thread.start do - sor = TestServer.new - recieved_data = sor.start_server(port) - - assert_equal(recieved_data[:ls][0][:line], @@log_line) - assert_equal(recieved_data[:ls][0][:app], options[:app]) - assert_equal(recieved_data[:ls][0][:level], "INFO") - assert_equal(recieved_data[:ls][0][:env], options[:env]) +require_relative "test-server.rb" +require "minitest/autorun" +class TestFoo < Minitest::Test + i_suck_and_my_tests_are_order_dependent! + + def get_options(port) + return { + :hostname => 'rubyTestHost', + :ip => '75.10.4.81', + :mac => '00:00:00:a1:2b:cc', + :app => 'rubyApplication', + :level => "INFO", + :env => "PRODUCTION", + :endpoint => "http://localhost:#{port}", + :flush_interval => 1, + :flush_size => 5, + :retry_timeout => 1 + } + end + + @@log_line = "log line" + + def warn_method(port) + options = get_options(port) + logdnaThread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.warn(@@log_line) + end + + serverThread = Thread.start do + server = TestServer.new() + recievedData = server.startServer(port) + + assert_equal(recievedData[:ls][0][:line], @@log_line) + assert_equal(recievedData[:ls][0][:app], options[:app]) + assert_equal(recievedData[:ls][0][:level], "WARN") + assert_equal(recievedData[:ls][0][:env], options[:env]) + end + + logdnaThread.join + serverThread.join end - logdna_thread.join - server_thread.join - end - - def debug_method(port) - options = get_options(port) - logdna_thread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.debug(@@log_line) + def info_method(port) + options = get_options(port) + logdnaThread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.info(@@log_line) + end + + serverThread = Thread.start do + sor = TestServer.new() + recievedData = sor.startServer(port) + + assert_equal(recievedData[:ls][0][:line], @@log_line) + assert_equal(recievedData[:ls][0][:app], options[:app]) + assert_equal(recievedData[:ls][0][:level], "INFO") + assert_equal(recievedData[:ls][0][:env], options[:env]) + end + + logdnaThread.join + serverThread.join end - - server_thread = Thread.start do - sor = TestServer.new - recieved_data = sor.start_server(port) - - assert_equal(recieved_data[:ls][0][:line], @@log_line) - assert_equal(recieved_data[:ls][0][:app], options[:app]) - assert_equal(recieved_data[:ls][0][:level], "DEBUG") - assert_equal(recieved_data[:ls][0][:env], options[:env]) + def debug_method(port) + options = get_options(port) + logdnaThread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.debug(@@log_line) + end + + serverThread = Thread.start do + sor = TestServer.new() + recievedData = sor.startServer(port) + + assert_equal(recievedData[:ls][0][:line], @@log_line) + assert_equal(recievedData[:ls][0][:app], options[:app]) + assert_equal(recievedData[:ls][0][:level], "DEBUG") + assert_equal(recievedData[:ls][0][:env], options[:env]) + end + + logdnaThread.join + serverThread.join end - - logdna_thread.join - server_thread.join - end - - def fatal_method(port) - options = get_options(port) - logdna_thread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.fatal(@@log_line) + def fatal_method(port) + options = get_options(port) + logdnaThread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.fatal(@@log_line) + end + + serverThread = Thread.start do + sor = TestServer.new() + recievedData = sor.startServer(port) + + assert_equal(recievedData[:ls][0][:line], @@log_line) + assert_equal(recievedData[:ls][0][:app], options[:app]) + assert_equal(recievedData[:ls][0][:level], "FATAL") + assert_equal(recievedData[:ls][0][:env], options[:env]) + end + + logdnaThread.join + serverThread.join end - server_thread = Thread.start do - sor = TestServer.new - recieved_data = sor.start_server(port) - - assert_equal(recieved_data[:ls][0][:line], @@log_line) - assert_equal(recieved_data[:ls][0][:app], options[:app]) - assert_equal(recieved_data[:ls][0][:level], "FATAL") - assert_equal(recieved_data[:ls][0][:env], options[:env]) + # Should retry to connect and preserve the failed line + def fatal_method_not_found(port) + second_line = " second line" + options = get_options(port) + logdnaThread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.fatal(@@log_line) + logger.fatal(second_line) + end + + serverThread = Thread.start do + sor = TestServer.new() + data = '' + recievedData = sor.startServerWithNotFound(port) + # The order of recieved lines is unpredictable. + recievedLines = [ + recievedData[:ls][0][:line], + recievedData[:ls][1][:line] + ] + + assert_includes(recievedLines, @@log_line) + assert_includes(recievedLines, second_line) + + assert_equal(recievedData[:ls][0][:app], options[:app]) + assert_equal(recievedData[:ls][0][:level], "FATAL") + assert_equal(recievedData[:ls][0][:env], options[:env]) + end + + logdnaThread.join + serverThread.join end - logdna_thread.join - server_thread.join - end - - # Should retry to connect and preserve the failed line - def fatal_method_not_found(port) - second_line = " second line" - options = get_options(port) - logdna_thread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.fatal(@@log_line) - logger.fatal(second_line) + def test_all + warn_method(2000) + info_method(2001) + fatal_method(2002) + debug_method(2003) + fatal_method_not_found(2004) end - - server_thread = Thread.start do - sor = TestServer.new - recieved_data = sor.return_not_found_res(port) - # The order of recieved lines is unpredictable. - recieved_lines = [ - recieved_data[:ls][0][:line], - recieved_data[:ls][1][:line] - ] - - assert_includes(recieved_lines, @@log_line) - assert_includes(recieved_lines, second_line) - - assert_equal(recieved_data[:ls][0][:app], options[:app]) - assert_equal(recieved_data[:ls][0][:level], "FATAL") - assert_equal(recieved_data[:ls][0][:env], options[:env]) - end - - logdna_thread.join - server_thread.join - end - - def test_all - warn_method(2000) - info_method(2001) - fatal_method(2002) - debug_method(2003) - fatal_method_not_found(2004) - end end From 07da391911317f63b87810b7bc78ef0de6523e83 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 19:43:31 -0700 Subject: [PATCH 69/94] rubocop --- spec/client.rb | 59 ---------- spec/logdna.test.rb | 49 -------- tests.rb | 269 ++++++++++++++++++++++---------------------- 3 files changed, 136 insertions(+), 241 deletions(-) delete mode 100644 spec/client.rb delete mode 100644 spec/logdna.test.rb diff --git a/spec/client.rb b/spec/client.rb deleted file mode 100644 index 6cfa80c..0000000 --- a/spec/client.rb +++ /dev/null @@ -1,59 +0,0 @@ -require_relative "../lib/logdna.rb" -require_relative "../lib/logdna/client.rb" -require 'socket' - -server = TCPServer.new("localhost", 3000) -client = server.accept - -client.write("hello") - - - - - - - - - - -# RSpec.describe "" do -# before do -# -# end -# -# -# # let(:uri) {} -# let(:opts) {{:app => "app"}} -# let (:options) { -# { -# :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", -# :flush_interval => 1, -# :flush_size => 1, -# :retry_timeout => 60 -# } -# } -# -# it "" do -# request = spy("l;;;;;;;") -# response = double(Net::HTTP) -# -# mockUri = double("uri") -# allow(mockUri).to receive(:hostname).and_return("host") -# allow(mockUri).to receive(:port).and_return("port") -# allow(mockUri).to receive(:scheme) -# allow(response).to receive(:start) -# -# client = Logdna::Client.new(request, mockUri, options) -# client.write_to_buffer("lllll", {}) -# -# -# expect(request).to receive(:body) -# end -# -# end diff --git a/spec/logdna.test.rb b/spec/logdna.test.rb deleted file mode 100644 index 98e6c5b..0000000 --- a/spec/logdna.test.rb +++ /dev/null @@ -1,49 +0,0 @@ -require_relative "../lib/logdna.rb" -require_relative "../lib/logdna/client.rb" - -RSpec.describe "should call the level named methods and assign the correct method" do - before do - allow(logger).to receive(:log) - end - let (:log_message) {"logging message"} - let (:options) { - { - :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", - :flush_interval => 1, - :flush_size => 5, - :retry_timeout => 60 - } - } - let (:logger) {Logdna::Ruby.new("PPPPPPP", options)} - - it "should call and assignt the level accordingly - debug" do - logger.debug(log_message) - expect(logger).to have_received(:log).with(log_message, {:level=>"DEBUG"}) - end - it "should call and assignt the level accordingly - warn" do - logger.warn(log_message) - expect(logger).to have_received(:log).with(log_message, {:level=>"WARN"}) - end - it "should call and assignt the level accordingly - info" do - logger.info(log_message) - expect(logger).to have_received(:log).with(log_message, {:level=>"INFO"}) - end - it "should call and assignt the level accordingly - fatal" do - logger.fatal(log_message) - expect(logger).to have_received(:log).with(log_message, {:level=>"FATAL"}) - end - it "should call and assignt the level accordingly - error" do - logger.error(log_message) - expect(logger).to have_received(:log).with(log_message, {:level=>"ERROR"}) - end - it "should call and assignt the level accordingly - trace" do - logger.trace(log_message) - expect(logger).to have_received(:log).with(log_message, {:level=>"TRACE"}) - end -end diff --git a/tests.rb b/tests.rb index 5fd37d2..9ced6db 100644 --- a/tests.rb +++ b/tests.rb @@ -1,146 +1,149 @@ +# frozen_string_literal: true + +require "minitest/autorun" + require_relative "lib/logdna.rb" require_relative "lib/logdna/client.rb" require_relative "test-server.rb" -require "minitest/autorun" -class TestFoo < Minitest::Test - i_suck_and_my_tests_are_order_dependent! - - def get_options(port) - return { - :hostname => 'rubyTestHost', - :ip => '75.10.4.81', - :mac => '00:00:00:a1:2b:cc', - :app => 'rubyApplication', - :level => "INFO", - :env => "PRODUCTION", - :endpoint => "http://localhost:#{port}", - :flush_interval => 1, - :flush_size => 5, - :retry_timeout => 1 - } - end - - @@log_line = "log line" - - def warn_method(port) - options = get_options(port) - logdnaThread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.warn(@@log_line) - end - - serverThread = Thread.start do - server = TestServer.new() - recievedData = server.startServer(port) - - assert_equal(recievedData[:ls][0][:line], @@log_line) - assert_equal(recievedData[:ls][0][:app], options[:app]) - assert_equal(recievedData[:ls][0][:level], "WARN") - assert_equal(recievedData[:ls][0][:env], options[:env]) - end - - logdnaThread.join - serverThread.join + +class TestLogDNARuby < Minitest::Test + @@log_line = "log line" + + def get_options(port) + { + hostname: "rubyTestHost", + ip: "75.10.4.81", + mac: "00:00:00:a1:2b:cc", + app: "rubyApplication", + level: "INFO", + env: "PRODUCTION", + endpoint: "http://localhost:#{port}", + flush_interval: 1, + flush_size: 5, + retry_timeout: 1 + } + end + + def warn_method(port) + options = get_options(port) + logdna_thread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.warn(@@log_line) end - def info_method(port) - options = get_options(port) - logdnaThread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.info(@@log_line) - end - - serverThread = Thread.start do - sor = TestServer.new() - recievedData = sor.startServer(port) - - assert_equal(recievedData[:ls][0][:line], @@log_line) - assert_equal(recievedData[:ls][0][:app], options[:app]) - assert_equal(recievedData[:ls][0][:level], "INFO") - assert_equal(recievedData[:ls][0][:env], options[:env]) - end - - logdnaThread.join - serverThread.join + server_thread = Thread.start do + server = TestServer.new + recieved_data = server.startServer(port) + + assert_equal(recieved_data[:ls][0][:line], @@log_line) + assert_equal(recieved_data[:ls][0][:app], options[:app]) + assert_equal(recieved_data[:ls][0][:level], "WARN") + assert_equal(recieved_data[:ls][0][:env], options[:env]) end - def debug_method(port) - options = get_options(port) - logdnaThread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.debug(@@log_line) - end - - serverThread = Thread.start do - sor = TestServer.new() - recievedData = sor.startServer(port) - - assert_equal(recievedData[:ls][0][:line], @@log_line) - assert_equal(recievedData[:ls][0][:app], options[:app]) - assert_equal(recievedData[:ls][0][:level], "DEBUG") - assert_equal(recievedData[:ls][0][:env], options[:env]) - end - - logdnaThread.join - serverThread.join + + logdna_thread.join + server_thread.join + end + + def info_method(port) + options = get_options(port) + logdna_thread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.info(@@log_line) end - def fatal_method(port) - options = get_options(port) - logdnaThread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.fatal(@@log_line) - end - - serverThread = Thread.start do - sor = TestServer.new() - recievedData = sor.startServer(port) - - assert_equal(recievedData[:ls][0][:line], @@log_line) - assert_equal(recievedData[:ls][0][:app], options[:app]) - assert_equal(recievedData[:ls][0][:level], "FATAL") - assert_equal(recievedData[:ls][0][:env], options[:env]) - end - - logdnaThread.join - serverThread.join + + server_thread = Thread.start do + sor = TestServer.new + recieved_data = sor.startServer(port) + + assert_equal(recieved_data[:ls][0][:line], @@log_line) + assert_equal(recieved_data[:ls][0][:app], options[:app]) + assert_equal(recieved_data[:ls][0][:level], "INFO") + assert_equal(recieved_data[:ls][0][:env], options[:env]) end - # Should retry to connect and preserve the failed line - def fatal_method_not_found(port) - second_line = " second line" - options = get_options(port) - logdnaThread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.fatal(@@log_line) - logger.fatal(second_line) - end - - serverThread = Thread.start do - sor = TestServer.new() - data = '' - recievedData = sor.startServerWithNotFound(port) - # The order of recieved lines is unpredictable. - recievedLines = [ - recievedData[:ls][0][:line], - recievedData[:ls][1][:line] - ] - - assert_includes(recievedLines, @@log_line) - assert_includes(recievedLines, second_line) - - assert_equal(recievedData[:ls][0][:app], options[:app]) - assert_equal(recievedData[:ls][0][:level], "FATAL") - assert_equal(recievedData[:ls][0][:env], options[:env]) - end - - logdnaThread.join - serverThread.join + logdna_thread.join + server_thread.join + end + + def debug_method(port) + options = get_options(port) + logdna_thread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.debug(@@log_line) end - def test_all - warn_method(2000) - info_method(2001) - fatal_method(2002) - debug_method(2003) - fatal_method_not_found(2004) + server_thread = Thread.start do + sor = TestServer.new + recieved_data = sor.startServer(port) + + assert_equal(recieved_data[:ls][0][:line], @@log_line) + assert_equal(recieved_data[:ls][0][:app], options[:app]) + assert_equal(recieved_data[:ls][0][:level], "DEBUG") + assert_equal(recieved_data[:ls][0][:env], options[:env]) end + + logdna_thread.join + server_thread.join + end + + def fatal_method(port) + options = get_options(port) + logdna_thread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.fatal(@@log_line) + end + + server_thread = Thread.start do + sor = TestServer.new + recieved_data = sor.startServer(port) + + assert_equal(recieved_data[:ls][0][:line], @@log_line) + assert_equal(recieved_data[:ls][0][:app], options[:app]) + assert_equal(recieved_data[:ls][0][:level], "FATAL") + assert_equal(recieved_data[:ls][0][:env], options[:env]) + end + + logdna_thread.join + server_thread.join + end + + # Should retry to connect and preserve the failed line + def fatal_method_not_found(port) + second_line = " second line" + options = get_options(port) + logdna_thread = Thread.start do + logger = Logdna::Ruby.new("pp", options) + logger.fatal(@@log_line) + logger.fatal(second_line) + end + + server_thread = Thread.start do + sor = TestServer.new + recieved_data = sor.startServerWithNotFound(port) + # The order of recieved lines is unpredictable. + recieved_lines = [ + recieved_data[:ls][0][:line], + recieved_data[:ls][1][:line] + ] + + assert_includes(recieved_lines, @@log_line) + assert_includes(recieved_lines, second_line) + + assert_equal(recieved_data[:ls][0][:app], options[:app]) + assert_equal(recieved_data[:ls][0][:level], "FATAL") + assert_equal(recieved_data[:ls][0][:env], options[:env]) + end + + logdna_thread.join + server_thread.join + end + + def test_all + warn_method(2000) + info_method(2001) + fatal_method(2002) + debug_method(2003) + fatal_method_not_found(2004) + end end From 48edb37a738ba3b88dc6884f645cff51197a69b9 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 19:51:31 -0700 Subject: [PATCH 70/94] lints --- test-server.rb | 50 -------------------------------------------------- test_server.rb | 4 ++++ tests.rb | 12 ++++++------ 3 files changed, 10 insertions(+), 56 deletions(-) delete mode 100644 test-server.rb diff --git a/test-server.rb b/test-server.rb deleted file mode 100644 index 5e2c6a0..0000000 --- a/test-server.rb +++ /dev/null @@ -1,50 +0,0 @@ -require 'socket' -class TestServer - attr_accessor :a - def startServer(port) - server = TCPServer.new(port) - puts server - data = '' - - Thread.start(server.accept) { |client| - headers = {} - while line = client.gets.split(' ', 2) - break if line[0] == "" - headers[line[0].chop] = line[1].strip - end - data = client.read(headers["Content-Length"].to_i) - client.puts("HTTP/1.1 200 OK") - client.close - }.join - - return eval(data) - end - - def startServerWithNotFound(port) - server = TCPServer.new(port) - - count = 0 - data = '' - loop do - count = count + 1 - Thread.start(server.accept) { |client| - headers = {} - while line = client.gets.split(' ', 2) - break if line[0] == "" - headers[line[0].chop] = line[1].strip - end - data =+ client.read(headers["Content-Length"].to_i) - if (count < 2) - client.puts("HTTP/1.1 404 Not Found") - else - client.puts("HTTP/1.1 200 OK") - end - client.close - }.join - break if count == 2 - end - return eval(data) -end -end - - diff --git a/test_server.rb b/test_server.rb index 3c7a840..5fc9604 100644 --- a/test_server.rb +++ b/test_server.rb @@ -5,6 +5,10 @@ class TestServer attr_accessor :a def start_server(port) server = TCPServer.new(port) +<<<<<<< HEAD +======= + puts server +>>>>>>> lints data = "" Thread.start(server.accept) { |client| diff --git a/tests.rb b/tests.rb index 9ced6db..7aace05 100644 --- a/tests.rb +++ b/tests.rb @@ -4,7 +4,7 @@ require_relative "lib/logdna.rb" require_relative "lib/logdna/client.rb" -require_relative "test-server.rb" +require_relative "test_server.rb" class TestLogDNARuby < Minitest::Test @@log_line = "log line" @@ -33,7 +33,7 @@ def warn_method(port) server_thread = Thread.start do server = TestServer.new - recieved_data = server.startServer(port) + recieved_data = server.start_server(port) assert_equal(recieved_data[:ls][0][:line], @@log_line) assert_equal(recieved_data[:ls][0][:app], options[:app]) @@ -54,7 +54,7 @@ def info_method(port) server_thread = Thread.start do sor = TestServer.new - recieved_data = sor.startServer(port) + recieved_data = sor.start_server(port) assert_equal(recieved_data[:ls][0][:line], @@log_line) assert_equal(recieved_data[:ls][0][:app], options[:app]) @@ -75,7 +75,7 @@ def debug_method(port) server_thread = Thread.start do sor = TestServer.new - recieved_data = sor.startServer(port) + recieved_data = sor.start_server(port) assert_equal(recieved_data[:ls][0][:line], @@log_line) assert_equal(recieved_data[:ls][0][:app], options[:app]) @@ -96,7 +96,7 @@ def fatal_method(port) server_thread = Thread.start do sor = TestServer.new - recieved_data = sor.startServer(port) + recieved_data = sor.start_server(port) assert_equal(recieved_data[:ls][0][:line], @@log_line) assert_equal(recieved_data[:ls][0][:app], options[:app]) @@ -120,7 +120,7 @@ def fatal_method_not_found(port) server_thread = Thread.start do sor = TestServer.new - recieved_data = sor.startServerWithNotFound(port) + recieved_data = sor.return_not_found_res(port) # The order of recieved lines is unpredictable. recieved_lines = [ recieved_data[:ls][0][:line], From b958c3642ccd60fc2ac29c401022f0b44425fea7 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 19:52:39 -0700 Subject: [PATCH 71/94] remove puts --- test_server.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/test_server.rb b/test_server.rb index 5fc9604..76fa18f 100644 --- a/test_server.rb +++ b/test_server.rb @@ -5,10 +5,7 @@ class TestServer attr_accessor :a def start_server(port) server = TCPServer.new(port) -<<<<<<< HEAD -======= puts server ->>>>>>> lints data = "" Thread.start(server.accept) { |client| From 09aeae56bf7323ee19e6ce482c761a61de14f2ca Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 19:57:13 -0700 Subject: [PATCH 72/94] 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 From a12b709188488c915a990f5f74df23a43e6acda9 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 20:00:12 -0700 Subject: [PATCH 73/94] clean --- lib/logdna.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index efbe62f..d08501f 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -1,6 +1,3 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - require 'logger' require "socket" require "uri" From 0e237336c7962f9e6b93ed4d16a0fc8e0e9c701b Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 20:38:00 -0700 Subject: [PATCH 74/94] ddD --- .rubocop.yml | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index da2d5aa..efafefa 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -5,15 +5,9 @@ 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: -<<<<<<< HEAD -<<<<<<< HEAD Max: 100 -======= - Max: 40 ->>>>>>> add rubycop config file and run it -======= - Max: 100 ->>>>>>> change the locking logic ang change linting rules + + # The guiding principle of classes is SRP, SRP can't be accurately measured by LoC Metrics/ClassLength: @@ -30,11 +24,6 @@ Metrics/PerceivedComplexity: # No space makes the method definition shorter and differentiates # from a regular assignment. -<<<<<<< HEAD -<<<<<<< HEAD -======= ->>>>>>> change the locking logic ang change linting rules - Layout/AccessModifierIndentation: Enabled: true IndentationWidth: 4 From 7c785a98d2031b93399bd2795f3cab8a2ea47ca1 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 20:44:04 -0700 Subject: [PATCH 75/94] clean --- lib/logdna.rb | 11 +---------- lib/logdna/client.rb | 1 - 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index d08501f..d175037 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -49,7 +49,7 @@ def default_opts } end - def assign_level=(value) + def level=(value) if value.is_a? Numeric @level = Resources::LOG_LEVELS[value] return @@ -140,12 +140,3 @@ def at_exit end end end - - -# require 'socket' -# puts "here?" -# loop { -# msg = $stdin.gets -# -# TCPSocket.open("localhost", 2000).puts(msg) -# } diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 764d412..34f8b43 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -69,7 +69,6 @@ def write_to_buffer(msg, opts) @lock.unlock flush if @flush_limit <= @buffer_byte_size - schedule_flush unless @flush_scheduled else @side_message_lock.synchronize do From 5130fb6a778e8030b3e0fd654a9e5c8472559d05 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 20:59:02 -0700 Subject: [PATCH 76/94] ss --- .rubocop.yml | 12 ++++++++++++ lib/logdna.rb | 8 +++++--- lib/logdna/client.rb | 2 ++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index efafefa..c9c3ae6 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -133,3 +133,15 @@ Style/MethodDefParentheses: Style/TrailingCommaInHashLiteral: Enabled: false + +Style/IfUnlessModifier: + Enabled: false + + Lint/DuplicateMethods: + Enabled: false + + Style/RedundantSelf: + Enabled: false + +Style/NegatedIf: + Enabled: false diff --git a/lib/logdna.rb b/lib/logdna.rb index d175037..3bd6077 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -1,4 +1,6 @@ -require 'logger' +# frozen_string_literal: true + +require "logger" require "socket" require "uri" require_relative "logdna/client.rb" @@ -77,8 +79,8 @@ def log(message = nil, opts = {}) define_method name do |msg = nil, opts = {}, &block| self.log(msg, opts.merge( - level: lvl - ), &block) + level: lvl + ), &block) end define_method "#{name}?" do diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index 34f8b43..b816735 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "net/http" require "socket" require "json" From 7f51d3162016163399e5684c1af20d0824561ecb Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 21:00:06 -0700 Subject: [PATCH 77/94] sss --- .rubocop.yml | 7 +++++-- lib/logdna.rb | 12 ++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c9c3ae6..06ab12a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -137,11 +137,14 @@ Style/TrailingCommaInHashLiteral: Style/IfUnlessModifier: Enabled: false - Lint/DuplicateMethods: +Lint/DuplicateMethods: Enabled: false - Style/RedundantSelf: +Style/RedundantSelf: Enabled: false Style/NegatedIf: Enabled: false + +Style/SafeNavigation: + Enabled: false diff --git a/lib/logdna.rb b/lib/logdna.rb index 3bd6077..910cf52 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -79,8 +79,8 @@ def log(message = nil, opts = {}) define_method name do |msg = nil, opts = {}, &block| self.log(msg, opts.merge( - level: lvl - ), &block) + level: lvl + ), &block) end define_method "#{name}?" do @@ -120,6 +120,7 @@ def datetime_format(*_arg) end def close +<<<<<<< HEAD if defined?(@client and !@@client.nil?) @client.exitout() end @@ -139,6 +140,13 @@ def at_exit if defined?(@client && !@client.nil?) @client.exitout() end +======= + @client&.exitout + end + + at_exit do + @client&.exitout +>>>>>>> sss end end end From 515336a6a4006fc5fee8306f58ffd68da29456b8 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 25 Oct 2019 21:02:29 -0700 Subject: [PATCH 78/94] ahahahahaha rubocop stupd --- lib/logdna.rb | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/lib/logdna.rb b/lib/logdna.rb index 910cf52..c279bd3 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -120,33 +120,11 @@ def datetime_format(*_arg) end def close -<<<<<<< HEAD - if defined?(@client and !@@client.nil?) - @client.exitout() - end - end - - def at_exit - if defined?(@client && !@client.nil?) - @client.exitout() - end - end - - def close - @client.exitout if defined? @client && !@client.nil? - end - - def at_exit - if defined?(@client && !@client.nil?) - @client.exitout() - end -======= @client&.exitout end at_exit do @client&.exitout ->>>>>>> sss end end end From c3308a3a9e8a99d53da740926b64795667b21206 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Tue, 19 Nov 2019 20:20:39 -0800 Subject: [PATCH 79/94] rubocop and refacyor the test server --- .rubocop.yml | 3 +++ Gemfile | 4 +++- Rakefile | 8 ++++++++ lib/logdna/resources.rb | 22 +++++++++++---------- lib/logdna/version.rb | 4 +++- logdna.gemspec | 36 +++++++++++++++++++++++++++++----- test_server.rb | 43 ++++++++++++++++++++--------------------- tests.rb | 22 ++++++++++----------- 8 files changed, 92 insertions(+), 50 deletions(-) create mode 100755 Rakefile diff --git a/.rubocop.yml b/.rubocop.yml index 06ab12a..86d34e6 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -79,6 +79,9 @@ Style/RaiseArgs: Style/SignalException: EnforcedStyle: only_raise +Style/GuardClause: + Enabled: false + # 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 diff --git a/Gemfile b/Gemfile index 293d51e..d6e57eb 100755 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,6 @@ -source 'https://rubygems.org' +# frozen_string_literal: true + +source "https://rubygems.org" # Specify your gem's dependencies in logdna_ruby.gemspec gemspec diff --git a/Rakefile b/Rakefile new file mode 100755 index 0000000..b6ae734 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require "bundler/gem_tasks" +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:spec) + +task default: :spec diff --git a/lib/logdna/resources.rb b/lib/logdna/resources.rb index b1323c4..8d9d91f 100755 --- a/lib/logdna/resources.rb +++ b/lib/logdna/resources.rb @@ -1,15 +1,17 @@ +# frozen_string_literal: true + module Resources - LOG_LEVELS = ['DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL', 'TRACE'].freeze - DEFAULT_REQUEST_HEADER = { 'Content-Type' => 'application/json; charset=UTF-8' }.freeze - DEFAULT_REQUEST_TIMEOUT = 180000 - MS_IN_A_DAY = 86400000 - MAX_REQUEST_TIMEOUT = 300000 - MAX_LINE_LENGTH = 32000 + LOG_LEVELS = %w[DEBUG INFO WARN ERROR FATAL TRACE].freeze + DEFAULT_REQUEST_HEADER = { "Content-Type" => "application/json; charset=UTF-8" }.freeze + DEFAULT_REQUEST_TIMEOUT = 180_000 + MS_IN_A_DAY = 86_400_000 + MAX_REQUEST_TIMEOUT = 300_000 + MAX_LINE_LENGTH = 32_000 MAX_INPUT_LENGTH = 80 RETRY_TIMEOUT = 60 FLUSH_INTERVAL = 0.25 - 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])$/ - IP_ADDR_CHECK = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ + FLUSH_BYTE_LIMIT = 500_000 + ENDPOINT = "https://logs.logdna.com/logs/ingest" + MAC_ADDR_CHECK = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/.freeze + IP_ADDR_CHECK = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.freeze end diff --git a/lib/logdna/version.rb b/lib/logdna/version.rb index 944e6b7..363dd5c 100755 --- a/lib/logdna/version.rb +++ b/lib/logdna/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module LogDNA - VERSION = '1.3.0'.freeze + VERSION = "1.3.0" end diff --git a/logdna.gemspec b/logdna.gemspec index 74c76f2..842adf7 100755 --- a/logdna.gemspec +++ b/logdna.gemspec @@ -1,11 +1,13 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +lib = File.expand_path("lib", __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'logdna/version' +require "logdna/version" Gem::Specification.new do |spec| - spec.name = 'logdna' + spec.name = "logdna" spec.version = LogDNA::VERSION +<<<<<<< HEAD spec.authors = 'Gun Woo Choi, Derek Zhou, Vilya Levitskiy, Muaz Siddiqui' spec.email = 'support@logdna.com' spec.summary = 'LogDNA Ruby logger' @@ -13,11 +15,35 @@ Gem::Specification.new do |spec| spec.license = 'MIT' spec.files = Dir.glob("{lib}/**/*.rb") + %w(LICENSE README.md) spec.bindir = 'exe' +======= + spec.authors = "Gun Woo Choi, Derek Zhou" + spec.email = "support@logdna.com" + + spec.summary = "LogDNA Ruby logger" + spec.homepage = "https://github.com/logdna/ruby" + spec.license = "MIT" + + spec.files = `git ls-files -z`.split("\x0").reject do |f| + f.match(%r{^(test|spec|features)/}) + end + spec.bindir = "exe" +>>>>>>> rubocop and refacyor the test server spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } - spec.require_paths = ['lib'] + spec.require_paths = ["lib"] +<<<<<<< HEAD spec.add_runtime_dependency 'concurrent-ruby', '~> 1.0' spec.add_runtime_dependency 'require_all', '~> 1.4' spec.add_runtime_dependency 'json', '~> 2.0' spec.add_development_dependency 'rubocop', '~> 0.78' +======= + spec.add_runtime_dependency "concurrent-ruby", "~> 1.0" + spec.add_runtime_dependency "json", "~> 2.0" + spec.add_runtime_dependency "require_all", "~> 1.4" + + spec.add_development_dependency "bundler", "~> 1.13" + spec.add_development_dependency "rake", "~> 10.5" + spec.add_development_dependency "rspec", "~> 3.5" + spec.add_development_dependency "webmock", "~> 2.3" +>>>>>>> rubocop and refacyor the test server end diff --git a/test_server.rb b/test_server.rb index 76fa18f..8af9fbe 100644 --- a/test_server.rb +++ b/test_server.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "socket" + class TestServer attr_accessor :a def start_server(port) @@ -23,30 +24,28 @@ def start_server(port) eval(data) end + def accept_logs_and_respond(server, data, res) + Thread.start(server.accept) { |client| + headers = {} + while line = client.gets.split(" ", 2) + break if line[0] == "" + + headers[line[0].chop] = line[1].strip + end + data = + client.read(headers["Content-Length"].to_i) + client.puts(res) + client.close + }.join + + data + end + def return_not_found_res(port) server = TCPServer.new(port) - - count = 0 data = "" - loop do - count += 1 - Thread.start(server.accept) { |client| - headers = {} - while line = client.gets.split(" ", 2) - break if line[0] == "" - - headers[line[0].chop] = line[1].strip - end - data = + client.read(headers["Content-Length"].to_i) - if count < 2 - client.puts("HTTP/1.1 404 Not Found") - else - client.puts("HTTP/1.1 200 OK") - end - client.close - }.join - break if count == 2 - end + accept_logs_and_respond(server, data, "HTTP/1.1 404 Not Found") + data = +accept_logs_and_respond(server, data, "HTTP/1.1 200 OK") + eval(data) -end + end end diff --git a/tests.rb b/tests.rb index 7aace05..2b9ba31 100644 --- a/tests.rb +++ b/tests.rb @@ -7,7 +7,7 @@ require_relative "test_server.rb" class TestLogDNARuby < Minitest::Test - @@log_line = "log line" + LOG_LINE = "log line" def get_options(port) { @@ -28,14 +28,14 @@ def warn_method(port) options = get_options(port) logdna_thread = Thread.start do logger = Logdna::Ruby.new("pp", options) - logger.warn(@@log_line) + logger.warn(LOG_LINE) end server_thread = Thread.start do server = TestServer.new recieved_data = server.start_server(port) - assert_equal(recieved_data[:ls][0][:line], @@log_line) + assert_equal(recieved_data[:ls][0][:line], LOG_LINE) assert_equal(recieved_data[:ls][0][:app], options[:app]) assert_equal(recieved_data[:ls][0][:level], "WARN") assert_equal(recieved_data[:ls][0][:env], options[:env]) @@ -49,14 +49,14 @@ def info_method(port) options = get_options(port) logdna_thread = Thread.start do logger = Logdna::Ruby.new("pp", options) - logger.info(@@log_line) + logger.info(LOG_LINE) end server_thread = Thread.start do sor = TestServer.new recieved_data = sor.start_server(port) - assert_equal(recieved_data[:ls][0][:line], @@log_line) + assert_equal(recieved_data[:ls][0][:line], LOG_LINE) assert_equal(recieved_data[:ls][0][:app], options[:app]) assert_equal(recieved_data[:ls][0][:level], "INFO") assert_equal(recieved_data[:ls][0][:env], options[:env]) @@ -70,14 +70,14 @@ def debug_method(port) options = get_options(port) logdna_thread = Thread.start do logger = Logdna::Ruby.new("pp", options) - logger.debug(@@log_line) + logger.debug(LOG_LINE) end server_thread = Thread.start do sor = TestServer.new recieved_data = sor.start_server(port) - assert_equal(recieved_data[:ls][0][:line], @@log_line) + assert_equal(recieved_data[:ls][0][:line], LOG_LINE) assert_equal(recieved_data[:ls][0][:app], options[:app]) assert_equal(recieved_data[:ls][0][:level], "DEBUG") assert_equal(recieved_data[:ls][0][:env], options[:env]) @@ -91,14 +91,14 @@ def fatal_method(port) options = get_options(port) logdna_thread = Thread.start do logger = Logdna::Ruby.new("pp", options) - logger.fatal(@@log_line) + logger.fatal(LOG_LINE) end server_thread = Thread.start do sor = TestServer.new recieved_data = sor.start_server(port) - assert_equal(recieved_data[:ls][0][:line], @@log_line) + assert_equal(recieved_data[:ls][0][:line], LOG_LINE) assert_equal(recieved_data[:ls][0][:app], options[:app]) assert_equal(recieved_data[:ls][0][:level], "FATAL") assert_equal(recieved_data[:ls][0][:env], options[:env]) @@ -114,7 +114,7 @@ def fatal_method_not_found(port) options = get_options(port) logdna_thread = Thread.start do logger = Logdna::Ruby.new("pp", options) - logger.fatal(@@log_line) + logger.fatal(LOG_LINE) logger.fatal(second_line) end @@ -127,7 +127,7 @@ def fatal_method_not_found(port) recieved_data[:ls][1][:line] ] - assert_includes(recieved_lines, @@log_line) + assert_includes(recieved_lines, LOG_LINE) assert_includes(recieved_lines, second_line) assert_equal(recieved_data[:ls][0][:app], options[:app]) From c2b176d1b0288c5e8e5c56d867164feba1734fe7 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 20 Nov 2019 13:43:06 -0800 Subject: [PATCH 80/94] use send method --- tests.rb | 91 ++++++++++---------------------------------------------- 1 file changed, 16 insertions(+), 75 deletions(-) diff --git a/tests.rb b/tests.rb index 2b9ba31..d46d5e7 100644 --- a/tests.rb +++ b/tests.rb @@ -24,83 +24,24 @@ def get_options(port) } end - def warn_method(port) - options = get_options(port) - logdna_thread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.warn(LOG_LINE) - end - - server_thread = Thread.start do - server = TestServer.new - recieved_data = server.start_server(port) - assert_equal(recieved_data[:ls][0][:line], LOG_LINE) - assert_equal(recieved_data[:ls][0][:app], options[:app]) - assert_equal(recieved_data[:ls][0][:level], "WARN") - assert_equal(recieved_data[:ls][0][:env], options[:env]) - end - logdna_thread.join - server_thread.join - end - - def info_method(port) + def log_level_test(level, port, expectedLevel) options = get_options(port) logdna_thread = Thread.start do logger = Logdna::Ruby.new("pp", options) - logger.info(LOG_LINE) + logger.send(level, LOG_LINE) + # logger.warn(LOG_LINE) end server_thread = Thread.start do - sor = TestServer.new - recieved_data = sor.start_server(port) - - assert_equal(recieved_data[:ls][0][:line], LOG_LINE) - assert_equal(recieved_data[:ls][0][:app], options[:app]) - assert_equal(recieved_data[:ls][0][:level], "INFO") - assert_equal(recieved_data[:ls][0][:env], options[:env]) - end - - logdna_thread.join - server_thread.join - end - - def debug_method(port) - options = get_options(port) - logdna_thread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.debug(LOG_LINE) - end - - server_thread = Thread.start do - sor = TestServer.new - recieved_data = sor.start_server(port) - - assert_equal(recieved_data[:ls][0][:line], LOG_LINE) - assert_equal(recieved_data[:ls][0][:app], options[:app]) - assert_equal(recieved_data[:ls][0][:level], "DEBUG") - assert_equal(recieved_data[:ls][0][:env], options[:env]) - end - - logdna_thread.join - server_thread.join - end - - def fatal_method(port) - options = get_options(port) - logdna_thread = Thread.start do - logger = Logdna::Ruby.new("pp", options) - logger.fatal(LOG_LINE) - end - - server_thread = Thread.start do - sor = TestServer.new - recieved_data = sor.start_server(port) + server = TestServer.new + recieved_data = server.start_server(port) assert_equal(recieved_data[:ls][0][:line], LOG_LINE) assert_equal(recieved_data[:ls][0][:app], options[:app]) - assert_equal(recieved_data[:ls][0][:level], "FATAL") + # assert_equal(recieved_data[:ls][0][:level], "WARN") + assert_equal(recieved_data[:ls][0][:level], expectedLevel) assert_equal(recieved_data[:ls][0][:env], options[:env]) end @@ -109,13 +50,13 @@ def fatal_method(port) end # Should retry to connect and preserve the failed line - def fatal_method_not_found(port) + def fatal_method_not_found(level, port, expectedLevel) second_line = " second line" options = get_options(port) logdna_thread = Thread.start do logger = Logdna::Ruby.new("pp", options) - logger.fatal(LOG_LINE) - logger.fatal(second_line) + logger.send(level, LOG_LINE) + logger.send(level, second_line) end server_thread = Thread.start do @@ -131,7 +72,7 @@ def fatal_method_not_found(port) assert_includes(recieved_lines, second_line) assert_equal(recieved_data[:ls][0][:app], options[:app]) - assert_equal(recieved_data[:ls][0][:level], "FATAL") + assert_equal(recieved_data[:ls][0][:level], expectedLevel) assert_equal(recieved_data[:ls][0][:env], options[:env]) end @@ -140,10 +81,10 @@ def fatal_method_not_found(port) end def test_all - warn_method(2000) - info_method(2001) - fatal_method(2002) - debug_method(2003) - fatal_method_not_found(2004) + log_level_test('warn', 2000, "WARN") + log_level_test('info', 2001, "INFO") + log_level_test('fatal', 2002, "FATAL") + log_level_test('debug', 2003, "DEBUG") + fatal_method_not_found('fatal', 2004, "FATAL") end end From 863acd1e4b8cb371622257091842b198723fc598 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Wed, 20 Nov 2019 13:44:30 -0800 Subject: [PATCH 81/94] rubocop --- tests.rb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/tests.rb b/tests.rb index d46d5e7..279eceb 100644 --- a/tests.rb +++ b/tests.rb @@ -24,9 +24,7 @@ def get_options(port) } end - - - def log_level_test(level, port, expectedLevel) + def log_level_test(level, port, expected_level) options = get_options(port) logdna_thread = Thread.start do logger = Logdna::Ruby.new("pp", options) @@ -41,7 +39,7 @@ def log_level_test(level, port, expectedLevel) assert_equal(recieved_data[:ls][0][:line], LOG_LINE) assert_equal(recieved_data[:ls][0][:app], options[:app]) # assert_equal(recieved_data[:ls][0][:level], "WARN") - assert_equal(recieved_data[:ls][0][:level], expectedLevel) + assert_equal(recieved_data[:ls][0][:level], expected_level) assert_equal(recieved_data[:ls][0][:env], options[:env]) end @@ -50,7 +48,7 @@ def log_level_test(level, port, expectedLevel) end # Should retry to connect and preserve the failed line - def fatal_method_not_found(level, port, expectedLevel) + def fatal_method_not_found(level, port, expected_level) second_line = " second line" options = get_options(port) logdna_thread = Thread.start do @@ -72,7 +70,7 @@ def fatal_method_not_found(level, port, expectedLevel) assert_includes(recieved_lines, second_line) assert_equal(recieved_data[:ls][0][:app], options[:app]) - assert_equal(recieved_data[:ls][0][:level], expectedLevel) + assert_equal(recieved_data[:ls][0][:level], expected_level) assert_equal(recieved_data[:ls][0][:env], options[:env]) end @@ -81,10 +79,10 @@ def fatal_method_not_found(level, port, expectedLevel) end def test_all - log_level_test('warn', 2000, "WARN") - log_level_test('info', 2001, "INFO") - log_level_test('fatal', 2002, "FATAL") - log_level_test('debug', 2003, "DEBUG") - fatal_method_not_found('fatal', 2004, "FATAL") + log_level_test("warn", 2000, "WARN") + log_level_test("info", 2001, "INFO") + log_level_test("fatal", 2002, "FATAL") + log_level_test("debug", 2003, "DEBUG") + fatal_method_not_found("fatal", 2004, "FATAL") end end From f528e7da2dc4be77a15719dfd39611bd9dfc411f Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Mon, 25 Nov 2019 17:22:26 -0800 Subject: [PATCH 82/94] add one more linting rule --- .rubocop.yml | 3 ++- test_server.rb | 1 - tests.rb | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 86d34e6..7bfbbcf 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -127,7 +127,8 @@ Naming/BinaryOperatorParameterName: # also they'll fail CI anyway Lint/Debugger: Enabled: false - +Lint/UnusedLocalVariable: + Enabled: true Security/Eval: Enabled: false # Style preference diff --git a/test_server.rb b/test_server.rb index 8af9fbe..21f3e92 100644 --- a/test_server.rb +++ b/test_server.rb @@ -3,7 +3,6 @@ require "socket" class TestServer - attr_accessor :a def start_server(port) server = TCPServer.new(port) puts server diff --git a/tests.rb b/tests.rb index 279eceb..4d6aa55 100644 --- a/tests.rb +++ b/tests.rb @@ -35,10 +35,8 @@ def log_level_test(level, port, expected_level) server_thread = Thread.start do server = TestServer.new recieved_data = server.start_server(port) - assert_equal(recieved_data[:ls][0][:line], LOG_LINE) assert_equal(recieved_data[:ls][0][:app], options[:app]) - # assert_equal(recieved_data[:ls][0][:level], "WARN") assert_equal(recieved_data[:ls][0][:level], expected_level) assert_equal(recieved_data[:ls][0][:env], options[:env]) end From bfcdfffa2bd4d01a577db29dc90629b1f2d11361 Mon Sep 17 00:00:00 2001 From: Vilya Levitskiy Date: Mon, 16 Dec 2019 15:57:16 -0800 Subject: [PATCH 83/94] remove the commented libnes and + = replace with += --- test_server.rb | 2 +- tests.rb | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/test_server.rb b/test_server.rb index 21f3e92..edee3b2 100644 --- a/test_server.rb +++ b/test_server.rb @@ -31,7 +31,7 @@ def accept_logs_and_respond(server, data, res) headers[line[0].chop] = line[1].strip end - data = + client.read(headers["Content-Length"].to_i) + data =+ client.read(headers["Content-Length"].to_i) client.puts(res) client.close }.join diff --git a/tests.rb b/tests.rb index 4d6aa55..5eba214 100644 --- a/tests.rb +++ b/tests.rb @@ -29,7 +29,6 @@ def log_level_test(level, port, expected_level) logdna_thread = Thread.start do logger = Logdna::Ruby.new("pp", options) logger.send(level, LOG_LINE) - # logger.warn(LOG_LINE) end server_thread = Thread.start do From a4649537def49368c192251c7c464c3fbc968d52 Mon Sep 17 00:00:00 2001 From: Vilya Levitskiy Date: Mon, 16 Dec 2019 16:13:39 -0800 Subject: [PATCH 84/94] add the space for = + --- .rubocop.yml | 8 ++++---- Gemfile | 2 +- logdna.gemspec | 3 +-- test_server.rb | 5 ++--- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 7bfbbcf..077ad86 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -57,11 +57,11 @@ Style/CollectionMethods: # inject seems more common in the community. reduce: "inject" -Style/UnneededInterpolation: - Enabled: flase +Style/RedundantInterpolation: + Enabled: false Style/RescueStandardError: - Enabled: flase + Enabled: false # Either allow this style or don't. Marking it as safe with parenthesis # is silly. Let's try to live without them for now. @@ -85,7 +85,7 @@ Style/GuardClause: # 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: +Lint/SuppressedException: Enabled: false # { ... } for multi-line blocks is okay, follow Weirichs rule instead: diff --git a/Gemfile b/Gemfile index d6e57eb..5ff7bb8 100755 --- a/Gemfile +++ b/Gemfile @@ -4,4 +4,4 @@ source "https://rubygems.org" # Specify your gem's dependencies in logdna_ruby.gemspec gemspec -minitest +gem "minitest" diff --git a/logdna.gemspec b/logdna.gemspec index 842adf7..865a849 100755 --- a/logdna.gemspec +++ b/logdna.gemspec @@ -41,9 +41,8 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency "json", "~> 2.0" spec.add_runtime_dependency "require_all", "~> 1.4" - spec.add_development_dependency "bundler", "~> 1.13" + spec.add_development_dependency "bundler", "~> 2.1.0" spec.add_development_dependency "rake", "~> 10.5" - spec.add_development_dependency "rspec", "~> 3.5" spec.add_development_dependency "webmock", "~> 2.3" >>>>>>> rubocop and refacyor the test server end diff --git a/test_server.rb b/test_server.rb index edee3b2..3a37e24 100644 --- a/test_server.rb +++ b/test_server.rb @@ -31,7 +31,7 @@ def accept_logs_and_respond(server, data, res) headers[line[0].chop] = line[1].strip end - data =+ client.read(headers["Content-Length"].to_i) + data = + client.read(headers["Content-Length"].to_i) client.puts(res) client.close }.join @@ -43,8 +43,7 @@ def return_not_found_res(port) server = TCPServer.new(port) data = "" accept_logs_and_respond(server, data, "HTTP/1.1 404 Not Found") - data = +accept_logs_and_respond(server, data, "HTTP/1.1 200 OK") - + data = + accept_logs_and_respond(server, data, "HTTP/1.1 200 OK") eval(data) end end From 47727f0bda16ae08db1c783423fd4bea9ab01647 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Thu, 19 Dec 2019 10:51:40 -0800 Subject: [PATCH 85/94] changed. --- logdna.gemspec | 20 ++------------------ test_server.rb | 5 +++-- 2 files changed, 5 insertions(+), 20 deletions(-) diff --git a/logdna.gemspec b/logdna.gemspec index 865a849..595f5e3 100755 --- a/logdna.gemspec +++ b/logdna.gemspec @@ -7,15 +7,6 @@ require "logdna/version" Gem::Specification.new do |spec| spec.name = "logdna" spec.version = LogDNA::VERSION -<<<<<<< HEAD - spec.authors = 'Gun Woo Choi, Derek Zhou, Vilya Levitskiy, Muaz Siddiqui' - spec.email = 'support@logdna.com' - spec.summary = 'LogDNA Ruby logger' - spec.homepage = 'https://github.com/logdna/ruby' - spec.license = 'MIT' - spec.files = Dir.glob("{lib}/**/*.rb") + %w(LICENSE README.md) - spec.bindir = 'exe' -======= spec.authors = "Gun Woo Choi, Derek Zhou" spec.email = "support@logdna.com" @@ -27,22 +18,15 @@ Gem::Specification.new do |spec| f.match(%r{^(test|spec|features)/}) end spec.bindir = "exe" ->>>>>>> rubocop and refacyor the test server spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] -<<<<<<< HEAD - spec.add_runtime_dependency 'concurrent-ruby', '~> 1.0' - spec.add_runtime_dependency 'require_all', '~> 1.4' - spec.add_runtime_dependency 'json', '~> 2.0' - spec.add_development_dependency 'rubocop', '~> 0.78' -======= spec.add_runtime_dependency "concurrent-ruby", "~> 1.0" spec.add_runtime_dependency "json", "~> 2.0" spec.add_runtime_dependency "require_all", "~> 1.4" - spec.add_development_dependency "bundler", "~> 2.1.0" + spec.add_development_dependency "bundler", "~> 1.13" spec.add_development_dependency "rake", "~> 10.5" + spec.add_development_dependency "rspec", "~> 3.5" spec.add_development_dependency "webmock", "~> 2.3" ->>>>>>> rubocop and refacyor the test server end diff --git a/test_server.rb b/test_server.rb index 3a37e24..101e96a 100644 --- a/test_server.rb +++ b/test_server.rb @@ -31,7 +31,7 @@ def accept_logs_and_respond(server, data, res) headers[line[0].chop] = line[1].strip end - data = + client.read(headers["Content-Length"].to_i) + data += client.read(headers["Content-Length"].to_i) client.puts(res) client.close }.join @@ -43,7 +43,8 @@ def return_not_found_res(port) server = TCPServer.new(port) data = "" accept_logs_and_respond(server, data, "HTTP/1.1 404 Not Found") - data = + accept_logs_and_respond(server, data, "HTTP/1.1 200 OK") + data += accept_logs_and_respond(server, data, "HTTP/1.1 200 OK") + eval(data) end end From ce29df49af9e3936762cd645be99687b72c61116 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 20 Dec 2019 14:02:56 -0800 Subject: [PATCH 86/94] resolve conflicts --- lib/logdna/client.rb | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/lib/logdna/client.rb b/lib/logdna/client.rb index b816735..29290af 100755 --- a/lib/logdna/client.rb +++ b/lib/logdna/client.rb @@ -31,8 +31,8 @@ def initialize(request, uri, opts) @internal_logger.level = Logger::DEBUG end - def process_message(msg, opts={}) - processedMessage = { + def process_message(msg, opts = {}) + processed_message = { line: msg, app: opts[:app], level: opts[:level], @@ -40,16 +40,8 @@ def process_message(msg, opts={}) meta: opts[:meta], timestamp: Time.now.to_i, } - processedMessage.delete(:meta) if processedMessage[:meta].nil? - 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 + processed_message.delete(:meta) if processed_message[:meta].nil? + processed_message end def schedule_flush @@ -77,8 +69,6 @@ def write_to_buffer(msg, opts) @side_messages.push(process_message(msg, opts)) end end - timer_task.execute - timer_task end # This method has to be called with @lock @@ -98,12 +88,6 @@ def send_request @exception_flag = true @side_message_lock.synchronize do @side_messages.concat(@buffer) - rescue Timeout::Error => e - puts "Timeout error occurred. #{e.message}" - @exception_flag = true - @side_messages.concat(@buffer) - ensure - @buffer.clear end end @@ -142,7 +126,7 @@ def flush else schedule_flush end - end + end def exitout flush From c3acefbeaabae983c7cae31987eb2a0b8ae64cac Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 20 Dec 2019 14:09:05 -0800 Subject: [PATCH 87/94] resolve logdna.gemspec changes --- logdna.gemspec | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/logdna.gemspec b/logdna.gemspec index 595f5e3..be05c27 100755 --- a/logdna.gemspec +++ b/logdna.gemspec @@ -1,32 +1,22 @@ -# frozen_string_literal: true - -lib = File.expand_path("lib", __dir__) +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require "logdna/version" +require 'logdna/version' Gem::Specification.new do |spec| - spec.name = "logdna" + spec.name = 'logdna' spec.version = LogDNA::VERSION - spec.authors = "Gun Woo Choi, Derek Zhou" - spec.email = "support@logdna.com" - - spec.summary = "LogDNA Ruby logger" - spec.homepage = "https://github.com/logdna/ruby" - spec.license = "MIT" - - spec.files = `git ls-files -z`.split("\x0").reject do |f| - f.match(%r{^(test|spec|features)/}) - end - spec.bindir = "exe" + spec.authors = 'Gun Woo Choi, Derek Zhou, Vilya Levitskiy, Muaz Siddiqui' + spec.email = 'support@logdna.com' + spec.summary = 'LogDNA Ruby logger' + spec.homepage = 'https://github.com/logdna/ruby' + spec.license = 'MIT' + spec.files = Dir.glob("{lib}/**/*.rb") + %w(LICENSE README.md) + spec.bindir = 'exe' 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 "json", "~> 2.0" - spec.add_runtime_dependency "require_all", "~> 1.4" - - spec.add_development_dependency "bundler", "~> 1.13" - spec.add_development_dependency "rake", "~> 10.5" - spec.add_development_dependency "rspec", "~> 3.5" - spec.add_development_dependency "webmock", "~> 2.3" + 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' + spec.add_development_dependency 'rubocop', '~> 0.78' end From 73caade8b63117ced0ac38bba827ddd35fd5b597 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 20 Dec 2019 14:46:02 -0800 Subject: [PATCH 88/94] conflicts and remove the version.rb file read from a regular file instead --- .rubocop.yml | 2 -- .ruby-version | 1 - Rakefile | 8 -------- lib/logdna.rb | 4 +++- lib/logdna/version.rb | 5 ----- logdna.gemspec | 8 +++++--- version | 1 + 7 files changed, 9 insertions(+), 20 deletions(-) delete mode 100644 .ruby-version delete mode 100755 Rakefile delete mode 100755 lib/logdna/version.rb create mode 100644 version diff --git a/.rubocop.yml b/.rubocop.yml index 077ad86..74c1101 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -7,8 +7,6 @@ Metrics/LineLength: Metrics/MethodLength: Max: 100 - - # The guiding principle of classes is SRP, SRP can't be accurately measured by LoC Metrics/ClassLength: Max: 1500 diff --git a/.ruby-version b/.ruby-version deleted file mode 100644 index 24ba9a3..0000000 --- a/.ruby-version +++ /dev/null @@ -1 +0,0 @@ -2.7.0 diff --git a/Rakefile b/Rakefile deleted file mode 100755 index b6ae734..0000000 --- a/Rakefile +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -require "bundler/gem_tasks" -require "rspec/core/rake_task" - -RSpec::Core::RakeTask.new(:spec) - -task default: :spec diff --git a/lib/logdna.rb b/lib/logdna.rb index c279bd3..5eb5a0f 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -5,6 +5,7 @@ require "uri" require_relative "logdna/client.rb" require_relative "logdna/resources.rb" + module Logdna class ValidURLRequired < ArgumentError; end class MaxLengthExceeded < ArgumentError; end @@ -38,7 +39,8 @@ def initialize(key, opts = {}) request = Net::HTTP::Post.new(uri.request_uri, "Content-Type" => "application/json") request.basic_auth("username", key) - + request[:'user-agent'] = opts[:'user-agent'] || "ruby/#{File.open("version").read().strip()}" + @client = Logdna::Client.new(request, uri, opts) end diff --git a/lib/logdna/version.rb b/lib/logdna/version.rb deleted file mode 100755 index 363dd5c..0000000 --- a/lib/logdna/version.rb +++ /dev/null @@ -1,5 +0,0 @@ -# frozen_string_literal: true - -module LogDNA - VERSION = "1.3.0" -end diff --git a/logdna.gemspec b/logdna.gemspec index be05c27..f4eeebb 100755 --- a/logdna.gemspec +++ b/logdna.gemspec @@ -1,17 +1,19 @@ # coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'logdna/version' +version = File.open("version").read() + +puts(Dir.glob("{lib}/**/*.rb") + %w(LICENSE README.md version)) Gem::Specification.new do |spec| spec.name = 'logdna' - spec.version = LogDNA::VERSION + spec.version = version spec.authors = 'Gun Woo Choi, Derek Zhou, Vilya Levitskiy, Muaz Siddiqui' spec.email = 'support@logdna.com' spec.summary = 'LogDNA Ruby logger' spec.homepage = 'https://github.com/logdna/ruby' spec.license = 'MIT' - spec.files = Dir.glob("{lib}/**/*.rb") + %w(LICENSE README.md) + spec.files = Dir.glob("{lib}/**/*.rb") + %w(LICENSE README.md version) spec.bindir = 'exe' spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] diff --git a/version b/version new file mode 100644 index 0000000..f0bb29e --- /dev/null +++ b/version @@ -0,0 +1 @@ +1.3.0 From 00b7fc6c24973aaf5016ce0cbe49c1f70e203681 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 20 Dec 2019 15:18:51 -0800 Subject: [PATCH 89/94] dont require the version file in gemspec --- lib/logdna.rb | 4 ++-- lib/logdna/version.rb | 5 +++++ logdna.gemspec | 6 ++---- 3 files changed, 9 insertions(+), 6 deletions(-) create mode 100644 lib/logdna/version.rb diff --git a/lib/logdna.rb b/lib/logdna.rb index 5eb5a0f..36589dd 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -5,6 +5,7 @@ require "uri" require_relative "logdna/client.rb" require_relative "logdna/resources.rb" +require_relative "logdna/version.rb" module Logdna class ValidURLRequired < ArgumentError; end @@ -39,8 +40,7 @@ def initialize(key, opts = {}) request = Net::HTTP::Post.new(uri.request_uri, "Content-Type" => "application/json") request.basic_auth("username", key) - request[:'user-agent'] = opts[:'user-agent'] || "ruby/#{File.open("version").read().strip()}" - + request[:'user-agent'] = opts[:'user-agent'] || "ruby/#{LogDNA::VERSION}" @client = Logdna::Client.new(request, uri, opts) end diff --git a/lib/logdna/version.rb b/lib/logdna/version.rb new file mode 100644 index 0000000..73955e8 --- /dev/null +++ b/lib/logdna/version.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +module LogDNA + VERSION = "1.3.0" +end diff --git a/logdna.gemspec b/logdna.gemspec index f4eeebb..f10e085 100755 --- a/logdna.gemspec +++ b/logdna.gemspec @@ -1,9 +1,7 @@ # coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -version = File.open("version").read() - -puts(Dir.glob("{lib}/**/*.rb") + %w(LICENSE README.md version)) +version = File.open("lib/logdna/version.rb").read().scan(/"([^"]*)"/).first.first Gem::Specification.new do |spec| spec.name = 'logdna' @@ -13,7 +11,7 @@ Gem::Specification.new do |spec| spec.summary = 'LogDNA Ruby logger' spec.homepage = 'https://github.com/logdna/ruby' spec.license = 'MIT' - spec.files = Dir.glob("{lib}/**/*.rb") + %w(LICENSE README.md version) + spec.files = Dir.glob("{lib}/**/*.rb") + %w(LICENSE README.md) spec.bindir = 'exe' spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] From f4fb072a49ebc17f87cf99c3427fae7609fc799e Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 20 Dec 2019 15:19:22 -0800 Subject: [PATCH 90/94] rubocop --- lib/logdna/version.rb | 2 +- logdna.gemspec | 35 ++++++++++++++++++----------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/logdna/version.rb b/lib/logdna/version.rb index 73955e8..363dd5c 100644 --- a/lib/logdna/version.rb +++ b/lib/logdna/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module LogDNA - VERSION = "1.3.0" + VERSION = "1.3.0" end diff --git a/logdna.gemspec b/logdna.gemspec index f10e085..b0cdea2 100755 --- a/logdna.gemspec +++ b/logdna.gemspec @@ -1,22 +1,23 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +lib = File.expand_path("lib", __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -version = File.open("lib/logdna/version.rb").read().scan(/"([^"]*)"/).first.first +version = File.open("lib/logdna/version.rb").read.scan(/"([^"]*)"/).first.first Gem::Specification.new do |spec| - spec.name = 'logdna' - spec.version = version - spec.authors = 'Gun Woo Choi, Derek Zhou, Vilya Levitskiy, Muaz Siddiqui' - spec.email = 'support@logdna.com' - spec.summary = 'LogDNA Ruby logger' - spec.homepage = 'https://github.com/logdna/ruby' - spec.license = 'MIT' - spec.files = Dir.glob("{lib}/**/*.rb") + %w(LICENSE README.md) - spec.bindir = 'exe' + spec.name = "logdna" + spec.version = version + spec.authors = "Gun Woo Choi, Derek Zhou, Vilya Levitskiy, Muaz Siddiqui" + spec.email = "support@logdna.com" + spec.summary = "LogDNA Ruby logger" + spec.homepage = "https://github.com/logdna/ruby" + spec.license = "MIT" + spec.files = Dir.glob("{lib}/**/*.rb") + %w[LICENSE README.md] + spec.bindir = "exe" 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' - spec.add_development_dependency 'rubocop', '~> 0.78' + spec.require_paths = ["lib"] + spec.add_runtime_dependency "concurrent-ruby", "~> 1.0" + spec.add_runtime_dependency "json", "~> 2.0" + spec.add_runtime_dependency "require_all", "~> 1.4" + spec.add_development_dependency "rubocop", "~> 0.78" end From d182de9b44cadfde441226ffae0d3e48237b1716 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 20 Dec 2019 15:27:25 -0800 Subject: [PATCH 91/94] fix the rubocop compalin --- .rubocop.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 74c1101..2ef3f39 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,7 +1,3 @@ -# 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: @@ -20,6 +16,10 @@ Metrics/CyclomaticComplexity: Metrics/PerceivedComplexity: Max: 40 +# Commonly used screens these days easily fit more than 80 characters. +Layout/LineLength: + Max: 120 + # No space makes the method definition shorter and differentiates # from a regular assignment. Layout/AccessModifierIndentation: From edbe6aef190720d7d1d9395e48f04d3ee9221264 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 20 Dec 2019 15:32:08 -0800 Subject: [PATCH 92/94] will it fix --- .rubocop.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 2ef3f39..38eca8a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -125,8 +125,10 @@ Naming/BinaryOperatorParameterName: # also they'll fail CI anyway Lint/Debugger: Enabled: false + Lint/UnusedLocalVariable: Enabled: true + Security/Eval: Enabled: false # Style preference From 5e78765409f9c2bb3661e7beff083ef10967f77c Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 20 Dec 2019 15:35:57 -0800 Subject: [PATCH 93/94] remove unrecognized linting rules --- .rubocop.yml | 12 ------------ lib/logdna.rb | 1 - 2 files changed, 13 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 38eca8a..deaad11 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -55,9 +55,6 @@ Style/CollectionMethods: # inject seems more common in the community. reduce: "inject" -Style/RedundantInterpolation: - Enabled: false - Style/RescueStandardError: Enabled: false @@ -80,12 +77,6 @@ Style/SignalException: Style/GuardClause: Enabled: false -# 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/SuppressedException: - 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: @@ -126,9 +117,6 @@ Naming/BinaryOperatorParameterName: Lint/Debugger: Enabled: false -Lint/UnusedLocalVariable: - Enabled: true - Security/Eval: Enabled: false # Style preference diff --git a/lib/logdna.rb b/lib/logdna.rb index 36589dd..4781277 100755 --- a/lib/logdna.rb +++ b/lib/logdna.rb @@ -24,7 +24,6 @@ def initialize(key, opts = {}) @meta = opts[:meta] @internal_logger = Logger.new(STDOUT) @internal_logger.level = Logger::DEBUG - endpoint = opts[:endpoint] || Resources::ENDPOINT hostname = opts[:hostname] || Socket.gethostname From 359ca58ef1c14e85a1cb91e440edeb3c42706e58 Mon Sep 17 00:00:00 2001 From: vilyapilya <13.veter@gmail.com> Date: Fri, 20 Dec 2019 15:41:01 -0800 Subject: [PATCH 94/94] remove .freeze that was added afte merging --- lib/logdna/resources.rb | 8 ++++---- version | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) delete mode 100644 version diff --git a/lib/logdna/resources.rb b/lib/logdna/resources.rb index 8d9d91f..eceb23b 100755 --- a/lib/logdna/resources.rb +++ b/lib/logdna/resources.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true module Resources - LOG_LEVELS = %w[DEBUG INFO WARN ERROR FATAL TRACE].freeze - DEFAULT_REQUEST_HEADER = { "Content-Type" => "application/json; charset=UTF-8" }.freeze + LOG_LEVELS = %w[DEBUG INFO WARN ERROR FATAL TRACE] + DEFAULT_REQUEST_HEADER = { "Content-Type" => "application/json; charset=UTF-8" } DEFAULT_REQUEST_TIMEOUT = 180_000 MS_IN_A_DAY = 86_400_000 MAX_REQUEST_TIMEOUT = 300_000 @@ -12,6 +12,6 @@ module Resources FLUSH_INTERVAL = 0.25 FLUSH_BYTE_LIMIT = 500_000 ENDPOINT = "https://logs.logdna.com/logs/ingest" - MAC_ADDR_CHECK = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/.freeze - IP_ADDR_CHECK = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.freeze + MAC_ADDR_CHECK = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/ + IP_ADDR_CHECK = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ end diff --git a/version b/version deleted file mode 100644 index f0bb29e..0000000 --- a/version +++ /dev/null @@ -1 +0,0 @@ -1.3.0