Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
b1d402f
working on the scheduler
lvilya Aug 16, 2019
68ffd7e
savingBranch
lvilya Aug 22, 2019
888404c
add bin folder to gitignore
lvilya Aug 22, 2019
37c6a3b
remive all bin folder changes"
lvilya Aug 22, 2019
08ad8d9
add tests
lvilya Aug 22, 2019
e980151
remove spec folder
lvilya Aug 22, 2019
9c7aaca
remove unused method
lvilya Sep 4, 2019
49d95da
add the flush flag check
lvilya Sep 4, 2019
b1a7d67
not working here??
lvilya Sep 4, 2019
10aeea0
not working chnges
lvilya Sep 4, 2019
6a3fb05
address the comments and improve the excpetion handling logic
lvilya Sep 5, 2019
6ff863e
add rubycop config file and run it
lvilya Sep 5, 2019
09f725b
change the oprand
lvilya Sep 5, 2019
a95a79d
forgot about one more
lvilya Sep 5, 2019
c75b9c0
address comments
lvilya Sep 27, 2019
f04c058
lints
lvilya Sep 27, 2019
25750ad
change the locking logic ang change linting rules
lvilya Sep 30, 2019
4e04773
add lamda for excpetion handling
lvilya Sep 30, 2019
112d3e8
check lock owning
lvilya Oct 1, 2019
481cc5c
fix the bug and add the lock to the side messages
lvilya Oct 1, 2019
fa4841e
lints
lvilya Oct 1, 2019
60a3707
remove redundant nil
lvilya Oct 1, 2019
207def1
remove the checking
lvilya Oct 2, 2019
1f61ebf
move the falg into lock
lvilya Oct 8, 2019
3024922
remove block
lvilya Oct 9, 2019
48ec1a6
rubocop
lvilya Oct 9, 2019
a827d99
forgot to move another flag swtich into the lock block
lvilya Oct 9, 2019
99b25aa
restore the possibility to pass blocks as messages
lvilya Oct 10, 2019
e1b66ad
change the logic to read easier
lvilya Oct 10, 2019
6c57f91
update rubocop although it didd not have any effect. rubocop is stupid.
lvilya Oct 10, 2019
e1b965d
commit
lvilya Oct 17, 2019
237cff3
sdfsdf
lvilya Oct 17, 2019
74adad2
sdfsdF
lvilya Oct 17, 2019
672f527
fix the dep
lvilya Oct 17, 2019
a9089bc
address the comments
lvilya Oct 22, 2019
b655cfd
remove the stray debug message
lvilya Oct 22, 2019
9522257
gitigone
lvilya Oct 24, 2019
a89ab46
dd
lvilya Oct 24, 2019
1ace81f
dss
lvilya Oct 25, 2019
96024f5
adds tests
lvilya Oct 26, 2019
6ecb59f
rubocop
lvilya Oct 26, 2019
7c518d4
lints
lvilya Oct 26, 2019
7800ad4
remove puts
lvilya Oct 26, 2019
7fc3c4c
gitingone
lvilya Oct 26, 2019
5f156e6
clean
lvilya Oct 26, 2019
4531dc2
working on the scheduler
lvilya Aug 16, 2019
a7b236a
savingBranch
lvilya Aug 22, 2019
4f65eda
the final test is insihed
lvilya Aug 22, 2019
857f4e2
add bin folder to gitignore
lvilya Aug 22, 2019
580406c
remive all bin folder changes"
lvilya Aug 22, 2019
78efc61
add tests
lvilya Aug 22, 2019
108e61c
remove spec folder
lvilya Aug 22, 2019
2637024
address the comments and improve the excpetion handling logic
lvilya Sep 5, 2019
b5bc0a6
add rubycop config file and run it
lvilya Sep 5, 2019
230bf72
address comments
lvilya Sep 27, 2019
f1a67d4
lints
lvilya Sep 27, 2019
2871c0e
change the locking logic ang change linting rules
lvilya Sep 30, 2019
babec9d
check lock owning
lvilya Oct 1, 2019
6b110f3
fix the bug and add the lock to the side messages
lvilya Oct 1, 2019
d25e512
rubocop
lvilya Oct 9, 2019
5c3b7de
forgot to move another flag swtich into the lock block
lvilya Oct 9, 2019
ebcbeb9
commit
lvilya Oct 17, 2019
e840e35
address the comments
lvilya Oct 22, 2019
681c53d
remove the stray debug message
lvilya Oct 22, 2019
54b0839
gitigone
lvilya Oct 24, 2019
12d3803
dd
lvilya Oct 24, 2019
df4943c
dss
lvilya Oct 25, 2019
b60d0e5
adds tests
lvilya Oct 26, 2019
07da391
rubocop
lvilya Oct 26, 2019
48edb37
lints
lvilya Oct 26, 2019
b958c36
remove puts
lvilya Oct 26, 2019
09aeae5
gitingone
lvilya Oct 26, 2019
a12b709
clean
lvilya Oct 26, 2019
0e23733
ddD
lvilya Oct 26, 2019
7c785a9
clean
lvilya Oct 26, 2019
5130fb6
ss
lvilya Oct 26, 2019
7f51d31
sss
lvilya Oct 26, 2019
515336a
ahahahahaha rubocop stupd
lvilya Oct 26, 2019
c3308a3
rubocop and refacyor the test server
lvilya Nov 20, 2019
c2b176d
use send method
lvilya Nov 20, 2019
863acd1
rubocop
lvilya Nov 20, 2019
f528e7d
add one more linting rule
lvilya Nov 26, 2019
bfcdfff
remove the commented libnes and + = replace with +=
Dec 16, 2019
a464953
add the space for = +
Dec 17, 2019
47727f0
changed.
lvilya Dec 19, 2019
ce29df4
resolve conflicts
lvilya Dec 20, 2019
c3acefb
resolve logdna.gemspec changes
lvilya Dec 20, 2019
73caade
conflicts and remove the version.rb file read from a regular file ins…
lvilya Dec 20, 2019
00b7fc6
dont require the version file in gemspec
lvilya Dec 20, 2019
f4fb072
rubocop
lvilya Dec 20, 2019
d182de9
fix the rubocop compalin
lvilya Dec 20, 2019
edbe6ae
will it fix
lvilya Dec 20, 2019
5e78765
remove unrecognized linting rules
lvilya Dec 20, 2019
359ca58
remove .freeze that was added afte merging
lvilya Dec 20, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -20,9 +16,12 @@ 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:
Enabled: true
IndentationWidth: 4
Expand Down Expand Up @@ -56,18 +55,13 @@ 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.
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.
Expand All @@ -80,10 +74,7 @@ Style/RaiseArgs:
Style/SignalException:
EnforcedStyle: only_raise

# Suppressing exceptions can be perfectly fine, and be it to avoid to
# explicitly type nil into the rescue since that's what you want to return,
# or suppressing LoadError for optional dependencies
Lint/SuppressedException:
Style/GuardClause:
Enabled: false

# { ... } for multi-line blocks is okay, follow Weirichs rule instead:
Expand All @@ -110,6 +101,9 @@ Style/SingleLineBlockParams:
Lint/ShadowingOuterLocalVariable:
Enabled: false

Lint/AssignmentInCondition:
Enabled: false

# Check with yard instead.
Style/Documentation:
Enabled: false
Expand All @@ -123,9 +117,26 @@ Naming/BinaryOperatorParameterName:
Lint/Debugger:
Enabled: false

Security/Eval:
Enabled: false
# Style preference
Style/MethodDefParentheses:
Enabled: false

Style/TrailingCommaInHashLiteral:
Enabled: false

Style/IfUnlessModifier:
Enabled: false

Lint/DuplicateMethods:
Enabled: false

Style/RedundantSelf:
Enabled: false

Style/NegatedIf:
Enabled: false

Style/SafeNavigation:
Enabled: false
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
source 'https://rubygems.org'
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in logdna_ruby.gemspec
gemspec
gem "minitest"
22 changes: 10 additions & 12 deletions lib/logdna.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# require 'singleton'
require "logger"
require "socket"
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
Expand All @@ -22,11 +22,13 @@ 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

Expand Down Expand Up @@ -64,7 +66,7 @@ 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")
Expand Down Expand Up @@ -103,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

Expand All @@ -114,20 +116,16 @@ 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

def close
if !@client.nil?
@client.exitout
end
@client&.exitout
end

at_exit do
if !@client.nil?
@client.exitout
end
@client&.exitout
end
end
end
12 changes: 7 additions & 5 deletions lib/logdna/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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 = {})
Expand Down Expand Up @@ -81,7 +84,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)
Expand All @@ -97,9 +100,9 @@ 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 ")
handle_exception.call("The response is not successful #{@response}")
end
@exception_flag = false
rescue SocketError
Expand All @@ -114,7 +117,6 @@ def send_request
end

def flush

if @lock.try_lock
@flush_scheduled = false
if @buffer.any? || @side_messages.any?
Expand All @@ -128,7 +130,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
18 changes: 10 additions & 8 deletions lib/logdna/resources.rb
Original file line number Diff line number Diff line change
@@ -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]
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
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
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])$/
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
4 changes: 3 additions & 1 deletion lib/logdna/version.rb
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module LogDNA
VERSION = '1.3.0'.freeze
VERSION = "1.3.0"
end
35 changes: 18 additions & 17 deletions logdna.gemspec
Original file line number Diff line number Diff line change
@@ -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)
require 'logdna/version'
version = File.open("lib/logdna/version.rb").read.scan(/"([^"]*)"/).first.first

Gem::Specification.new do |spec|
spec.name = 'logdna'
spec.version = LogDNA::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
50 changes: 50 additions & 0 deletions test_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require "socket"

class TestServer
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 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)
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")

eval(data)
end
end
Loading