coralogix_logger 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +7 -6
- data/lib/constants.rb +10 -4
- data/lib/coralogix_logger.rb +5 -2
- data/lib/httpsender.rb +27 -23
- data/lib/manager.rb +30 -9
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 444f4d0977a5a0d4a0111d7e5f5a7a4ed3428d2d
|
4
|
+
data.tar.gz: b43e3f213458541180e6e14782ac5d93f3457bf9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e648ae6b4703093c37be7ef9db79018fbef19b97e93748e3b32cfedf95f0234e51cd6742e47f710ed57b9fbdc93c8f0e3b2017bb50e3f75ccf21e5e0910a19b4
|
7
|
+
data.tar.gz: abab7b3b0313cb6c206929437a99822919cd2fcec39f4d4f21d05b936394c9baaf7946ade6ff55f48a819bfbe293029fd7b8a61e6427830892a8f53cbaa1ac9f
|
data/README.md
CHANGED
@@ -33,17 +33,18 @@ You must provide the following four variables when creating a Coralogix logger i
|
|
33
33
|
APP_NAME = "MyTestApp"
|
34
34
|
SUB_SYSTEM = "BL"
|
35
35
|
|
36
|
+
# Configure Coralogix SDK. You need to define it only once per process.
|
36
37
|
Coralogix::CoralogixLogger.configure(PRIVATE_KEY, APP_NAME, SUB_SYSTEM)
|
37
38
|
|
38
|
-
#The common practice is to get an instance of the logger in each class and setting the logger name to the class name.
|
39
|
-
logger
|
39
|
+
# The common practice is to get an instance of the logger in each class and setting the logger name to the class name.
|
40
|
+
# logger name will be used as category unless specified otherwise.
|
41
|
+
logger = Coralogix::CoralogixLogger.get_logger("my class")
|
40
42
|
|
41
|
-
#Send "Hello World!" message with severity verbose.
|
43
|
+
# Send "Hello World!" message with severity verbose.
|
42
44
|
logger.log(Coralogix::Severity::VERBOSE, "Hello World!")
|
43
45
|
|
44
|
-
#Additional options
|
45
|
-
#Severity
|
46
|
-
#If category is not specified, logger name will be used instead
|
46
|
+
# Additional options
|
47
|
+
# Severity and message parameters are mandatory. The rest of the parameters are optional.
|
47
48
|
logger.log(Coralogix::Severity::DEBUG, "Hello World!", category: "my category")
|
48
49
|
logger.log(Coralogix::Severity::INFO, "Hello World!", category: "my category", className: "my class")
|
49
50
|
logger.log(Coralogix::Severity::WARNING, "Hello World!", category: "my category", className: "my class", methodName: "my method")
|
data/lib/constants.rb
CHANGED
@@ -27,19 +27,19 @@ module Coralogix
|
|
27
27
|
|
28
28
|
|
29
29
|
#Coralogix logs url
|
30
|
-
CORALOGIX_LOG_URL = "https://
|
30
|
+
CORALOGIX_LOG_URL = "https://api.coralogix.com:443/api/v1/logs"
|
31
31
|
|
32
32
|
#Coralogix time delat url
|
33
|
-
CORALOGIX_TIME_DELTA_URL = "https://
|
33
|
+
CORALOGIX_TIME_DELTA_URL = "https://api.coralogix.com:443/sdk/v1/time"
|
34
34
|
|
35
35
|
#Default private key
|
36
36
|
FAILED_PRIVATE_KEY = "9626c7dd-8174-5015-a3fe-5572e042b6d9"
|
37
37
|
|
38
38
|
#Default application name
|
39
|
-
|
39
|
+
NO_APP_NAME = "NO_APP_NAME"
|
40
40
|
|
41
41
|
#Default subsystem name
|
42
|
-
|
42
|
+
NO_SUB_SYSTEM = "NO_SUB_NAME"
|
43
43
|
|
44
44
|
#Default log file name
|
45
45
|
LOG_FILE_NAME = "coralogix.sdk.log"
|
@@ -53,4 +53,10 @@ module Coralogix
|
|
53
53
|
#Interval between failed http post requests
|
54
54
|
HTTP_SEND_RETRY_INTERVAL = 2
|
55
55
|
|
56
|
+
# Coralogix category
|
57
|
+
CORALOGIX_CATEGORY = 'CORALOGIX'
|
58
|
+
|
59
|
+
# Sync time update interval
|
60
|
+
SYNC_TIME_UPDATE_INTERVAL = 5 #minutes
|
61
|
+
|
56
62
|
end
|
data/lib/coralogix_logger.rb
CHANGED
@@ -61,6 +61,9 @@ module Coralogix
|
|
61
61
|
# @param sub_system - sub system name
|
62
62
|
# @return [boolean] return a true or false.
|
63
63
|
def self.configure private_key, app_name, sub_system
|
64
|
+
private_key = private_key.strip.empty? ? FAILED_PRIVATE_KEY : private_key
|
65
|
+
app_name = app_name.strip.empty? ? NO_APP_NAME : app_name
|
66
|
+
sub_system = sub_system.strip.empty? ? NO_SUB_SYSTEM : sub_system
|
64
67
|
LoggerManager.configure(:privateKey => private_key, :applicationName => app_name, :subsystemName => sub_system) unless LoggerManager.configured
|
65
68
|
end
|
66
69
|
|
@@ -72,7 +75,7 @@ module Coralogix
|
|
72
75
|
# @param className - log class name
|
73
76
|
# @param methodName - log method name
|
74
77
|
# @param threadId - log thread id
|
75
|
-
def log severity, message, category: @category, className: "", methodName: "", threadId:
|
78
|
+
def log severity, message, category: @category, className: "", methodName: "", threadId: Thread.current.object_id.to_s
|
76
79
|
LoggerManager.add_logline message, severity, category, :className => className, :methodName => methodName, :threadId => threadId
|
77
80
|
end
|
78
81
|
|
@@ -81,7 +84,7 @@ module Coralogix
|
|
81
84
|
# For instance, for info severity it will create a method:
|
82
85
|
# def info message, category: @category, className: "", methodName: "", threadId: ""
|
83
86
|
SEVERITIES.keys.each do |severity|
|
84
|
-
define_method("#{severity}") do |message, category: @category, className: "", methodName: "", threadId:
|
87
|
+
define_method("#{severity}") do |message, category: @category, className: "", methodName: "", threadId: Thread.current.object_id.to_s|
|
85
88
|
LoggerManager.add_logline message, SEVERITIES["#{__method__}".to_sym], category, :className => className, :methodName => methodName, :threadId => threadId
|
86
89
|
end
|
87
90
|
end
|
data/lib/httpsender.rb
CHANGED
@@ -6,13 +6,13 @@ require 'openssl'
|
|
6
6
|
require 'benchmark'
|
7
7
|
require 'date'
|
8
8
|
require 'time'
|
9
|
-
|
10
9
|
module Coralogix
|
11
10
|
# @private
|
12
11
|
module CoralogixHTTPSender
|
13
12
|
TICKS_IN_SECOND = 10**7
|
14
13
|
@initialized = false
|
15
|
-
|
14
|
+
@mutex = Mutex.new
|
15
|
+
|
16
16
|
def CoralogixHTTPSender.disable_proxy value
|
17
17
|
@disable_proxy = value
|
18
18
|
end
|
@@ -33,7 +33,9 @@ module Coralogix
|
|
33
33
|
@http.keep_alive_timeout = 10
|
34
34
|
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
35
35
|
@http.read_timeout = HTTP_TIMEOUT # seconds
|
36
|
+
|
36
37
|
@req = Net::HTTP::Post.new(@uri.path, 'Content-Type' => 'application/json')
|
38
|
+
#@req_get = Net::HTTP::Get.new URI(CORALOGIX_TIME_DELTA_URL)
|
37
39
|
@initialized = true
|
38
40
|
rescue Exception => e
|
39
41
|
DebugLogger.error e.message
|
@@ -73,29 +75,31 @@ module Coralogix
|
|
73
75
|
#
|
74
76
|
# @return [float] - time delta
|
75
77
|
def CoralogixHTTPSender.get_time_sync
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
if res.is_a?(Net::HTTPSuccess) && !res.body.to_s.empty?
|
82
|
-
#Get server ticks from 1970
|
83
|
-
server_ticks = res.body.to_i.to_s # Relative to 1970
|
84
|
-
#Take the first 13 digits
|
85
|
-
server_ticks = server_ticks[0..12]
|
86
|
-
#Convert the ticks to utc time
|
87
|
-
server_time = Time.parse(Time.at(server_ticks.to_i / 1000.to_f).strftime('%H:%M:%S.%L')).utc
|
88
|
-
local_time = Time.now.utc
|
78
|
+
@mutex.synchronize do
|
79
|
+
self.initialize unless @initialized
|
80
|
+
begin
|
81
|
+
DebugLogger.info "Syncing time with coralogix server"
|
82
|
+
res = @http.get(CORALOGIX_TIME_DELTA_URL)
|
89
83
|
|
90
|
-
|
91
|
-
|
92
|
-
|
84
|
+
if res.is_a?(Net::HTTPSuccess) && !res.body.to_s.empty?
|
85
|
+
#Get server ticks from 1970
|
86
|
+
server_ticks = res.body.to_i.to_s # Relative to 1970
|
87
|
+
#Take the first 13 digits
|
88
|
+
server_ticks = server_ticks[0..12]
|
89
|
+
#Convert the ticks to utc time
|
90
|
+
server_time = Time.parse(Time.at(server_ticks.to_i / 1000.to_f).strftime('%H:%M:%S.%L')).utc
|
91
|
+
local_time = Time.now.utc
|
92
|
+
|
93
|
+
time_delta = (server_time - local_time) * 1000.0
|
94
|
+
DebugLogger.info "Updating time delta to: #{time_delta}"
|
95
|
+
return true, time_delta
|
96
|
+
end
|
97
|
+
return false, 0
|
98
|
+
rescue Exception => e
|
99
|
+
DebugLogger.error e.message
|
100
|
+
DebugLogger.error e.backtrace.inspect
|
101
|
+
return false, 0
|
93
102
|
end
|
94
|
-
return false, 0
|
95
|
-
rescue Exception => e
|
96
|
-
DebugLogger.error e.message
|
97
|
-
DebugLogger.error e.backtrace.inspect
|
98
|
-
return false, 0
|
99
103
|
end
|
100
104
|
end
|
101
105
|
end
|
data/lib/manager.rb
CHANGED
@@ -15,7 +15,7 @@ module Coralogix
|
|
15
15
|
def self.initialize
|
16
16
|
@buffer = []
|
17
17
|
@buffer_size = 0
|
18
|
-
@bulk_template = {:privateKey => FAILED_PRIVATE_KEY, :applicationName =>
|
18
|
+
@bulk_template = {:privateKey => FAILED_PRIVATE_KEY, :applicationName => NO_APP_NAME, :subsystemName => NO_SUB_SYSTEM}
|
19
19
|
@mutex = Mutex.new
|
20
20
|
@time_delta_last_update = 0
|
21
21
|
@time_delta = 0
|
@@ -32,12 +32,27 @@ module Coralogix
|
|
32
32
|
def self.configure **args
|
33
33
|
begin
|
34
34
|
@bulk_template = args.merge({:computerName => `hostname`.strip})
|
35
|
-
configured
|
36
|
-
|
37
|
-
|
35
|
+
DebugLogger.info "Successfully configured Coralogix logger."
|
36
|
+
@configured = true
|
37
|
+
self.update_time_delta_interval
|
38
|
+
self.add_logline "The Application Name #{@bulk_template[:applicationName]} and Subsystem Name #{@bulk_template[:subsystemName]} from the Ruby SDK, version #{self.version?} has started to send data.", Severity::INFO, CORALOGIX_CATEGORY
|
39
|
+
rescue Exception => e
|
40
|
+
DebugLogger.error e.message
|
41
|
+
DebugLogger.error e.backtrace.inspect
|
42
|
+
@configured = false
|
38
43
|
end
|
44
|
+
return @configured
|
39
45
|
end
|
40
46
|
|
47
|
+
def self.version?
|
48
|
+
begin
|
49
|
+
Gem.loaded_specs['coralogix_logger'].version.to_s
|
50
|
+
rescue Exception => e
|
51
|
+
DebugLogger.error e.message
|
52
|
+
DebugLogger.error e.backtrace.inspect
|
53
|
+
return '0.0.0'
|
54
|
+
end
|
55
|
+
end
|
41
56
|
# Add a log line to our buffer.
|
42
57
|
#
|
43
58
|
# @param message - The logs message. This is a must parameter.
|
@@ -55,10 +70,8 @@ module Coralogix
|
|
55
70
|
#Combine a logentry from the must parameters together with the optional one.
|
56
71
|
new_entry = {:text => message, :timestamp => Time.now.utc.to_f * 1000 + @time_delta, :severity => severity, :category => category}.merge(args)
|
57
72
|
@buffer << new_entry
|
58
|
-
#
|
73
|
+
#Update the buffer size to reflect the new size.
|
59
74
|
@buffer_size+=new_entry.to_json.bytesize
|
60
|
-
else
|
61
|
-
DebugLogger.warn "Buffer is full. Ignoring current message."
|
62
75
|
end
|
63
76
|
end
|
64
77
|
rescue Exception => e
|
@@ -77,12 +90,17 @@ module Coralogix
|
|
77
90
|
@mutex.synchronize do
|
78
91
|
# Total buffer size
|
79
92
|
size = @buffer.size
|
93
|
+
return unless size > 0
|
80
94
|
|
81
95
|
# If the size is bigger than the maximum allowed chunk size then split it by half.
|
82
96
|
# Keep splitting it until the size is less than MAX_LOG_CHUNK_SIZE
|
83
|
-
while @buffer.take(size).join(",").bytesize > MAX_LOG_CHUNK_SIZE
|
97
|
+
while (@buffer.take(size).join(",").bytesize > MAX_LOG_CHUNK_SIZE) || (size == 0)
|
84
98
|
size=size/2;
|
85
99
|
end
|
100
|
+
|
101
|
+
# We must take at leat one value. If the first message is bigger than MAX_LOG_CHUNK_SIZE
|
102
|
+
# we need to take it anyway.
|
103
|
+
size = size > 0 ? size : 1
|
86
104
|
|
87
105
|
DebugLogger.info "Checking buffer size. Total log entries is: #{size}"
|
88
106
|
@bulk_template[:logEntries] = @buffer.shift(size)
|
@@ -90,6 +108,9 @@ module Coralogix
|
|
90
108
|
# Extract from the buffer size the total amount of the logs we removed from the buffer
|
91
109
|
@buffer_size-= (@bulk_template[:logEntries].to_json.bytesize - 2 - size-1)
|
92
110
|
|
111
|
+
# Make sure we are always positive
|
112
|
+
@buffer_size = @buffer_size >= 0 ? @buffer_size : 0
|
113
|
+
|
93
114
|
DebugLogger.info "Bufer size after removal is: #{@buffer.join(",").bytesize}"
|
94
115
|
end
|
95
116
|
CoralogixHTTPSender.send_request(@bulk_template) unless @bulk_template[:logEntries].empty?
|
@@ -103,7 +124,7 @@ module Coralogix
|
|
103
124
|
def self.update_time_delta_interval
|
104
125
|
begin
|
105
126
|
#If more than 5 seconds passed from the last sync update
|
106
|
-
if ((DateTime.now.strftime('%Q').to_i - @time_delta_last_update) / 1000) >=
|
127
|
+
if ((DateTime.now.strftime('%Q').to_i - @time_delta_last_update) / 1000) >= (60 * SYNC_TIME_UPDATE_INTERVAL) #5 minuts
|
107
128
|
res, _time_delta = CoralogixHTTPSender.get_time_sync
|
108
129
|
if res
|
109
130
|
@time_delta = _time_delta
|