logstash_writer 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.editorconfig +7 -0
- data/lib/logstash_writer.rb +71 -42
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0dddb7bf146e8698d1bdebfb0deb5b3de55be252787b7d8a888624e4c63e97b4
|
4
|
+
data.tar.gz: 988d074dcdf5023010d0f480f15431d721a8dfdaf20c527433e72c7d7e9238c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da8f334ad24b00e5dba960654235bab9cfe738efaed27986fa76d09730a8889b447f9db5722b370eb9a4a2c8ccca389ef28512887c315aec38546a98a01aee39
|
7
|
+
data.tar.gz: ccc715f7bc30a718d6c6383b5ee76f2da186687440a8b1e6efee3aa72793bbe8975aa50a9124aa480d525e3dcb6fbde7f5272915706d06a1804fe361ebc5af4a
|
data/.editorconfig
ADDED
data/lib/logstash_writer.rb
CHANGED
@@ -74,19 +74,19 @@ class LogstashWriter
|
|
74
74
|
@server_name, @logger, @backlog = server_name, logger, backlog
|
75
75
|
|
76
76
|
@metrics = {
|
77
|
-
received:
|
78
|
-
sent:
|
79
|
-
queue_size:
|
80
|
-
dropped:
|
77
|
+
received: metrics_registry.counter(:"#{metrics_prefix}_events_received_total", "The number of logstash events which have been submitted for delivery"),
|
78
|
+
sent: metrics_registry.counter(:"#{metrics_prefix}_events_written_total", "The number of logstash events which have been delivered to the logstash server"),
|
79
|
+
queue_size: metrics_registry.gauge(:"#{metrics_prefix}_queue_size", "The number of events currently in the queue to be sent"),
|
80
|
+
dropped: metrics_registry.counter(:"#{metrics_prefix}_events_dropped_total", "The number of events which have been dropped from the queue"),
|
81
81
|
|
82
|
-
lag:
|
82
|
+
lag: metrics_registry.gauge(:"#{metrics_prefix}_last_sent_event_time_seconds", "When the last event successfully sent to logstash was originally received"),
|
83
83
|
|
84
|
-
connected:
|
85
|
-
connect_exception:
|
86
|
-
write_exception:
|
84
|
+
connected: metrics_registry.gauge(:"#{metrics_prefix}_connected_to_server", "Boolean flag indicating whether we are currently connected to a logstash server"),
|
85
|
+
connect_exception: metrics_registry.counter(:"#{metrics_prefix}_connect_exceptions_total", "The number of exceptions that have occurred whilst attempting to connect to a logstash server"),
|
86
|
+
write_exception: metrics_registry.counter(:"#{metrics_prefix}_write_exceptions_total", "The number of exceptions that have occurred whilst attempting to write an event to a logstash server"),
|
87
87
|
|
88
88
|
write_loop_exception: metrics_registry.counter(:"#{metrics_prefix}_write_loop_exceptions_total", "The number of exceptions that have occurred in the writing loop"),
|
89
|
-
write_loop_ok:
|
89
|
+
write_loop_ok: metrics_registry.gauge(:"#{metrics_prefix}_write_loop_ok", "Boolean flag indicating whether the writing loop is currently operating correctly, or is in a post-apocalyptic hellscape of never-ending exceptions"),
|
90
90
|
}
|
91
91
|
|
92
92
|
@metrics[:lag].set({}, 0)
|
@@ -136,6 +136,33 @@ class LogstashWriter
|
|
136
136
|
nil
|
137
137
|
end
|
138
138
|
|
139
|
+
# Send events.
|
140
|
+
#
|
141
|
+
# Does not return until `#shutdown` is called (in another thread).
|
142
|
+
#
|
143
|
+
def run
|
144
|
+
@queue_mutex.synchronize do
|
145
|
+
@terminate = false
|
146
|
+
end
|
147
|
+
|
148
|
+
write_loop
|
149
|
+
end
|
150
|
+
|
151
|
+
# Tell the LogstashWriter to flush its queue and terminate operation.
|
152
|
+
#
|
153
|
+
# Returns immediately.
|
154
|
+
#
|
155
|
+
def shutdown
|
156
|
+
#:nocov:
|
157
|
+
@worker_mutex.synchronize do
|
158
|
+
@queue_mutex.synchronize do
|
159
|
+
@terminate = true
|
160
|
+
@queue_cv.signal
|
161
|
+
end
|
162
|
+
end
|
163
|
+
#:nocov:
|
164
|
+
end
|
165
|
+
|
139
166
|
# Start sending events.
|
140
167
|
#
|
141
168
|
# This method will return almost immediately, and actual event
|
@@ -143,11 +170,14 @@ class LogstashWriter
|
|
143
170
|
#
|
144
171
|
# @return [NilClass]
|
145
172
|
#
|
146
|
-
def
|
173
|
+
def start!
|
147
174
|
@worker_mutex.synchronize do
|
148
175
|
if @worker_thread.nil?
|
176
|
+
@queue_mutex.synchronize do
|
177
|
+
@terminate = false
|
178
|
+
end
|
179
|
+
|
149
180
|
@worker_thread = Thread.new do
|
150
|
-
Thread.current.name = "LogstashWriter"
|
151
181
|
write_loop
|
152
182
|
end
|
153
183
|
end
|
@@ -164,18 +194,19 @@ class LogstashWriter
|
|
164
194
|
#
|
165
195
|
# @return [NilClass]
|
166
196
|
#
|
167
|
-
def stop
|
197
|
+
def stop!
|
168
198
|
@worker_mutex.synchronize do
|
169
199
|
if @worker_thread
|
170
|
-
@
|
171
|
-
|
200
|
+
@queue_mutex.synchronize do
|
201
|
+
@terminate = true
|
202
|
+
@queue_cv.signal
|
203
|
+
end
|
172
204
|
begin
|
173
|
-
@worker_thread.join
|
205
|
+
@worker_thread.join unless @worker_thread == Thread.current
|
174
206
|
rescue Exception => ex
|
175
207
|
@logger.error("LogstashWriter") { (["Worker thread terminated with exception: #{ex.message} (#{ex.class})"] + ex.backtrace).join("\n ") }
|
176
208
|
end
|
177
209
|
@worker_thread = nil
|
178
|
-
@socket_mutex.synchronize { (@current_target.close; @current_target = nil) if @current_target }
|
179
210
|
end
|
180
211
|
end
|
181
212
|
|
@@ -210,45 +241,43 @@ class LogstashWriter
|
|
210
241
|
#
|
211
242
|
def write_loop
|
212
243
|
error_wait = INITIAL_RETRY_WAIT
|
244
|
+
Thread.current.name = "LogstashWriter"
|
213
245
|
|
214
|
-
|
215
|
-
|
216
|
-
event = nil
|
246
|
+
until @terminate do
|
247
|
+
event = nil
|
217
248
|
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
end
|
223
|
-
|
224
|
-
if @queue.empty? && @terminate
|
225
|
-
@terminate = false
|
226
|
-
throw :terminate
|
227
|
-
end
|
228
|
-
|
229
|
-
event = @queue.shift
|
249
|
+
begin
|
250
|
+
@queue_mutex.synchronize do
|
251
|
+
while @queue.empty? && !@terminate
|
252
|
+
@queue_cv.wait(@queue_mutex)
|
230
253
|
end
|
231
254
|
|
255
|
+
event = @queue.shift
|
256
|
+
end
|
257
|
+
|
258
|
+
if event
|
232
259
|
current_target do |t|
|
233
260
|
t.socket.puts event[:content].to_json
|
234
261
|
stat_sent(t.to_s, event[:arrival_timestamp])
|
235
262
|
@metrics[:write_loop_ok].set({}, 1)
|
236
263
|
error_wait = INITIAL_RETRY_WAIT
|
237
264
|
end
|
238
|
-
rescue StandardError => ex
|
239
|
-
@logger.error("LogstashWriter") { (["Exception in write_loop: #{ex.message} (#{ex.class})"] + ex.backtrace).join("\n ") }
|
240
|
-
@queue_mutex.synchronize { @queue.unshift(event) if event }
|
241
|
-
@metrics[:write_loop_exception].increment(class: ex.class.to_s)
|
242
|
-
@metrics[:write_loop_ok].set({}, 0)
|
243
|
-
sleep error_wait
|
244
|
-
# Increase the error wait timeout for next time, up to a maximum
|
245
|
-
# interval of about 60 seconds
|
246
|
-
error_wait *= 1.1
|
247
|
-
error_wait = 60 if error_wait > 60
|
248
|
-
error_wait += rand / 0.5
|
249
265
|
end
|
266
|
+
rescue StandardError => ex
|
267
|
+
@logger.error("LogstashWriter") { (["Exception in write_loop: #{ex.message} (#{ex.class})"] + ex.backtrace).join("\n ") }
|
268
|
+
@queue_mutex.synchronize { @queue.unshift(event) if event }
|
269
|
+
@metrics[:write_loop_exception].increment(class: ex.class.to_s)
|
270
|
+
@metrics[:write_loop_ok].set({}, 0)
|
271
|
+
sleep error_wait
|
272
|
+
# Increase the error wait timeout for next time, up to a maximum
|
273
|
+
# interval of about 60 seconds
|
274
|
+
error_wait *= 1.1
|
275
|
+
error_wait = 60 if error_wait > 60
|
276
|
+
error_wait += rand / 0.5
|
250
277
|
end
|
251
278
|
end
|
279
|
+
|
280
|
+
force_disconnect!
|
252
281
|
end
|
253
282
|
|
254
283
|
# Yield a Target connected to the server we currently believe to be
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logstash_writer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Palmer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: prometheus-client
|
@@ -202,6 +202,7 @@ executables: []
|
|
202
202
|
extensions: []
|
203
203
|
extra_rdoc_files: []
|
204
204
|
files:
|
205
|
+
- ".editorconfig"
|
205
206
|
- ".gitignore"
|
206
207
|
- ".rubocop.yml"
|
207
208
|
- ".travis.yml"
|
@@ -230,8 +231,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
230
231
|
- !ruby/object:Gem::Version
|
231
232
|
version: '0'
|
232
233
|
requirements: []
|
233
|
-
|
234
|
-
rubygems_version: 2.7.7
|
234
|
+
rubygems_version: 3.0.1
|
235
235
|
signing_key:
|
236
236
|
specification_version: 4
|
237
237
|
summary: Opinionated logstash event pump
|