logstash-output-odyssey 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e7d0d7561934efc8341e8393a1ed4795db3b0ec330cc0e9a3842b3d2ebe9b22e
4
+ data.tar.gz: d6b635bb01d61a8826b3c9edf3720237c29128d1c19f75fe82b0225fcbc4fbf2
5
+ SHA512:
6
+ metadata.gz: 8d69c4c6c073c0fb8bb0462e11ef7faaee3d068cc7245fbc7505c2bd8cbebf4d51970560b2ca73d76b84eb357aa90f16e24f8a9135ff39fe2f1cf4442089a7a1
7
+ data.tar.gz: a2970c892f7acba6594de07d8831962e3e8bdeff8c687f0cc92608105ca92d089aaca6ed52cdca7efe581837380c8a8bf3a306877a911264a5fb9442ea23c02b
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ logstash_path = ENV["LOGSTASH_PATH"] || "./logstash"
6
+
7
+ if Dir.exist?(logstash_path)
8
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
9
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
10
+ else
11
+ raise 'missing logstash vendoring'
12
+ end
13
+
14
+ gem "webmock", "~> 3.8"
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # Contributing to Loki Logstash Output Plugin
2
+
3
+ For information about how to use this plugin see this [documentation](../../docs/sources/clients/logstash/_index.md).
4
+
5
+ ## Install dependencies
6
+
7
+ First, make sure you have JDK version `8` or `11` installed and you have set the `JAVA_HOME` environment variable.
8
+
9
+ You need to setup JRuby environment to build this plugin. Refer https://github.com/rbenv/rbenv for setting up your rbenv environment.
10
+
11
+ After setting up `rbenv`. Install JRuby
12
+
13
+ ```bash
14
+ rbenv install jruby-9.2.10.0
15
+ rbenv local jruby-9.2.10.0
16
+ ```
17
+
18
+ Check that the environment is configured
19
+
20
+ ```bash
21
+ ruby --version
22
+ jruby 9.2.10
23
+ ```
24
+
25
+ You should make sure you are running `jruby` and not `ruby`. If the command `ruby --version` still shows `ruby` and not `jruby`, check that PATH contains `$HOME/.rbenv/shims` and `$HOME/.rbenv/bin`. Also verify that you have this in your bash profile:
26
+
27
+ ```bash
28
+ export PATH="$HOME/.rbenv/bin:$PATH"
29
+ eval "$(rbenv init -)"
30
+ ```
31
+
32
+ Then install bundler:
33
+
34
+ ```bash
35
+ gem install bundler:2.1.4
36
+ ```
37
+
38
+ Follow those instructions to [install logstash](https://www.elastic.co/guide/en/logstash/current/installing-logstash.html) before moving to the next section.
39
+
40
+ ## Build and test the plugin
41
+
42
+ ### Install required packages
43
+
44
+ ```bash
45
+ git clone git@github.com:elastic/logstash.git
46
+ cd logstash
47
+ git checkout tags/v7.16.1
48
+ export LOGSTASH_PATH="$(pwd)"
49
+ export GEM_PATH="$LOGSTASH_PATH/vendor/bundle/jruby/2.5.0"
50
+ export GEM_HOME="$LOGSTASH_PATH/vendor/bundle/jruby/2.5.0"
51
+ ./gradlew assemble
52
+ cd ..
53
+ ruby -S bundle config set --local path "$LOGSTASH_PATH/vendor/bundle"
54
+ ruby -S bundle install
55
+ ruby -S bundle exec rake vendor
56
+ ```
57
+
58
+ ### Build the plugin
59
+
60
+ ```bash
61
+ gem build logstash-output-loki.gemspec
62
+ ```
63
+
64
+ ### Test
65
+
66
+ ```bash
67
+ ruby -S bundle exec rspec
68
+ ```
69
+
70
+ Alternatively if you don't want to install JRuby. Enter inside logstash-loki container.
71
+
72
+ ```bash
73
+ docker build -t logstash-loki ./
74
+ docker run -v $(pwd)/spec:/home/logstash/spec -it --rm --entrypoint /bin/sh logstash-loki
75
+ bundle exec rspec
76
+ ```
77
+
78
+ ## Install plugin to local logstash
79
+
80
+ ```bash
81
+ bin/logstash-plugin install --no-verify --local logstash-output-loki-1.0.0.gem
82
+ ```
83
+
84
+ ## Send sample event and check plugin is working
85
+
86
+ ```bash
87
+ bin/logstash -f loki.conf
88
+ ```
@@ -0,0 +1,70 @@
1
+ require 'time'
2
+
3
+ module Loki
4
+ class Batch
5
+ attr_reader :streams
6
+ def initialize(e)
7
+ @bytes = 0
8
+ @createdAt = Time.now
9
+ @streams = {}
10
+ add(e)
11
+ end
12
+
13
+ def size_bytes
14
+ return @bytes
15
+ end
16
+
17
+ def add(e)
18
+ # puts "Batch add()"
19
+ @bytes = @bytes + e.entry['line'].length
20
+
21
+ # Append the entry to an already existing stream (if any)
22
+ labels = e.labels.sort.to_h
23
+ labelkey = labels.to_s
24
+ if @streams.has_key?(labelkey)
25
+ # puts "Batch add() - existing stream"
26
+ stream = @streams[labelkey]
27
+ stream['entries'].append(e.entry)
28
+ return
29
+ else
30
+ # puts "Batch add() - new stream"
31
+ # Add the entry as a new stream
32
+ @streams[labelkey] = {
33
+ "labels" => labels,
34
+ "entries" => [e.entry],
35
+ }
36
+ end
37
+ end
38
+
39
+ def size_bytes_after(line)
40
+ return @bytes + line.length
41
+ end
42
+
43
+ def age()
44
+ return Time.now - @createdAt
45
+ end
46
+
47
+ def to_json
48
+ streams = []
49
+ @streams.each { |_ , stream|
50
+ streams.append(build_stream(stream))
51
+ }
52
+ return {"streams"=>streams}.to_json
53
+ end
54
+
55
+ def build_stream(stream)
56
+ values = []
57
+ stream['entries'].each { |entry|
58
+ values.append([
59
+ entry['ts'].to_s,
60
+ entry['line'],
61
+ entry['metadata']
62
+ ])
63
+ }
64
+ return {
65
+ 'stream'=>stream['labels'],
66
+ 'values' => values
67
+ }
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,52 @@
1
+ module Loki
2
+ def to_ns(s)
3
+ (s.to_f * (10**9)).to_i
4
+ end
5
+ class Entry
6
+ include Loki
7
+ attr_reader :labels, :entry
8
+ def initialize(event,message_field,include_fields,metadata_fields)
9
+
10
+ @metadata = {}
11
+ event.to_hash.each { |key,value|
12
+ next if key.start_with?('@')
13
+ next if value.is_a?(Hash)
14
+ next if metadata_fields.length() > 0 and not metadata_fields.include?(key)
15
+ @metadata[key] = value.to_s
16
+ }
17
+
18
+ @entry = {
19
+ "ts" => to_ns(event.get("@timestamp")),
20
+ "line" => event.get(message_field).to_s,
21
+ "metadata" => @metadata
22
+ }
23
+ event = event.clone()
24
+ event.remove(message_field)
25
+ event.remove("@timestamp")
26
+
27
+ @labels = {}
28
+ event.to_hash.each { |key,value|
29
+ next if key.start_with?('@')
30
+ next if value.is_a?(Hash)
31
+ next if include_fields.length() > 0 and not include_fields.include?(key)
32
+ @labels[key] = value.to_s
33
+ }
34
+
35
+ # @metadata = {}
36
+ # event.to_hash.each { |key,value|
37
+ # next if key.start_with?('@')
38
+ # next if value.is_a?(Hash)
39
+ # next if metadata_fields.length() > 0 and not metadata_fields.include?(key)
40
+ # @metadata[key] = value.to_s
41
+ # }
42
+
43
+ # puts "Entry block"
44
+ # puts message_field
45
+ # puts include_fields
46
+ # puts event
47
+ # puts @entry
48
+ # puts @labels
49
+ # puts @metadata
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,275 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+ require "logstash/outputs/loki/entry"
4
+ require "logstash/outputs/loki/batch"
5
+ require "logstash/namespace"
6
+ require 'net/http'
7
+ require 'time'
8
+ require 'uri'
9
+ require 'json'
10
+
11
+ class LogStash::Outputs::Loki < LogStash::Outputs::Base
12
+ include Loki
13
+ config_name "loki"
14
+
15
+ ## 'A single instance of the Output will be shared among the pipeline worker threads'
16
+ concurrency :single
17
+
18
+ ## 'Loki URL'
19
+ config :url, :validate => :string, :required => true
20
+
21
+ ## 'BasicAuth credentials'
22
+ config :username, :validate => :string, :required => false
23
+ config :password, :validate => :string, secret: true, :required => false
24
+
25
+ ## 'Client certificate'
26
+ config :cert, :validate => :path, :required => false
27
+ config :key, :validate => :path, :required => false
28
+
29
+ ## 'TLS'
30
+ config :ca_cert, :validate => :path, :required => false
31
+
32
+ ## 'Disable server certificate verification'
33
+ config :insecure_skip_verify, :validate => :boolean, :default => false, :required => false
34
+
35
+ ## 'Loki Tenant ID'
36
+ config :tenant_id, :validate => :string, :required => false
37
+
38
+ ## 'Maximum batch size to accrue before pushing to loki. Defaults to 102400 bytes'
39
+ config :batch_size, :validate => :number, :default => 102400, :required => false
40
+
41
+ ## 'Interval in seconds to wait before pushing a batch of records to loki. Defaults to 1 second'
42
+ config :batch_wait, :validate => :number, :default => 1, :required => false
43
+
44
+ ## 'Log line field to pick from logstash. Defaults to "message"'
45
+ config :message_field, :validate => :string, :default => "message", :required => false
46
+
47
+ ## 'Backoff configuration. Initial backoff time between retries. Default 1s'
48
+ config :min_delay, :validate => :number, :default => 1, :required => false
49
+
50
+ ## 'An array of fields to map to labels, if defined only fields in this list will be mapped.'
51
+ config :include_fields, :validate => :array, :default => [], :required => false
52
+
53
+ ## 'An array of fields to map to labels, if defined only fields in this list will be mapped.'
54
+ config :metadata_fields, :validate => :array, :default => [], :required => false
55
+
56
+ ## 'Backoff configuration. Maximum backoff time between retries. Default 300s'
57
+ config :max_delay, :validate => :number, :default => 300, :required => false
58
+
59
+ ## 'Backoff configuration. Maximum number of retries to do'
60
+ config :retries, :validate => :number, :default => 10, :required => false
61
+
62
+ attr_reader :batch
63
+ public
64
+ def register
65
+ @uri = URI.parse(@url)
66
+ unless @uri.is_a?(URI::HTTP) || @uri.is_a?(URI::HTTPS)
67
+ raise LogStash::ConfigurationError, "url parameter must be valid HTTP, currently '#{@url}'"
68
+ end
69
+
70
+ if @min_delay > @max_delay
71
+ raise LogStash::ConfigurationError, "Min delay should be less than Max delay, currently 'Min delay is #{@min_delay} and Max delay is #{@max_delay}'"
72
+ end
73
+
74
+ @logger.info("Loki output plugin", :class => self.class.name)
75
+
76
+ # initialize Queue and Mutex
77
+ @entries = Queue.new
78
+ @mutex = Mutex.new
79
+ @stop = false
80
+
81
+ # create nil batch object.
82
+ @batch = nil
83
+
84
+ # validate certs
85
+ if ssl_cert?
86
+ load_ssl
87
+ validate_ssl_key
88
+ end
89
+
90
+ # start batch_max_wait and batch_max_size threads
91
+ @batch_wait_thread = Thread.new{max_batch_wait()}
92
+ @batch_size_thread = Thread.new{max_batch_size()}
93
+ end
94
+
95
+ def max_batch_size
96
+ loop do
97
+ @mutex.synchronize do
98
+ return if @stop
99
+ end
100
+
101
+ e = @entries.deq
102
+ return if e.nil?
103
+
104
+ @mutex.synchronize do
105
+ if !add_entry_to_batch(e)
106
+ @logger.debug("Max batch_size is reached. Sending batch to loki")
107
+ send(@batch)
108
+ @batch = Batch.new(e)
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ def max_batch_wait
115
+ # minimum wait frequency is 10 milliseconds
116
+ min_wait_checkfrequency = 1/100
117
+ max_wait_checkfrequency = @batch_wait
118
+ if max_wait_checkfrequency < min_wait_checkfrequency
119
+ max_wait_checkfrequency = min_wait_checkfrequency
120
+ end
121
+
122
+ loop do
123
+ @mutex.synchronize do
124
+ return if @stop
125
+ end
126
+
127
+ sleep(max_wait_checkfrequency)
128
+ if is_batch_expired
129
+ @mutex.synchronize do
130
+ @logger.debug("Max batch_wait time is reached. Sending batch to loki")
131
+ send(@batch)
132
+ @batch = nil
133
+ end
134
+ end
135
+ end
136
+ end
137
+
138
+ def ssl_cert?
139
+ !@key.nil? && !@cert.nil?
140
+ end
141
+
142
+ def load_ssl
143
+ @cert = OpenSSL::X509::Certificate.new(File.read(@cert)) if @cert
144
+ @key = OpenSSL::PKey.read(File.read(@key)) if @key
145
+ end
146
+
147
+ def validate_ssl_key
148
+ if !@key.is_a?(OpenSSL::PKey::RSA) && !@key.is_a?(OpenSSL::PKey::DSA)
149
+ raise LogStash::ConfigurationError, "Unsupported private key type '#{@key.class}''"
150
+ end
151
+ end
152
+
153
+ def ssl_opts(uri)
154
+ opts = {
155
+ use_ssl: uri.scheme == 'https'
156
+ }
157
+
158
+ # disable server certificate verification
159
+ if @insecure_skip_verify
160
+ opts = opts.merge(
161
+ verify_mode: OpenSSL::SSL::VERIFY_NONE
162
+ )
163
+ end
164
+
165
+ if !@cert.nil? && !@key.nil?
166
+ opts = opts.merge(
167
+ verify_mode: OpenSSL::SSL::VERIFY_PEER,
168
+ cert: @cert,
169
+ key: @key
170
+ )
171
+ end
172
+
173
+ unless @ca_cert.nil?
174
+ opts = opts.merge(
175
+ ca_file: @ca_cert
176
+ )
177
+ end
178
+ opts
179
+ end
180
+
181
+ # Add an entry to the current batch returns false if the batch is full
182
+ # and the entry can't be added.
183
+ def add_entry_to_batch(e)
184
+ line = e.entry['line']
185
+ # we don't want to send empty lines.
186
+ return true if line.to_s.strip.empty?
187
+
188
+ if @batch.nil?
189
+ @batch = Batch.new(e)
190
+ return true
191
+ end
192
+
193
+ if @batch.size_bytes_after(line) > @batch_size
194
+ return false
195
+ end
196
+ @batch.add(e)
197
+ return true
198
+ end
199
+
200
+ def is_batch_expired
201
+ return !@batch.nil? && @batch.age() >= @batch_wait
202
+ end
203
+
204
+ ## Receives logstash events
205
+ public
206
+ def receive(event)
207
+ # puts "receive event"
208
+ @entries << Entry.new(event, @message_field, @include_fields, @metadata_fields)
209
+ end
210
+
211
+ def close
212
+ @entries.close
213
+ @mutex.synchronize do
214
+ @stop = true
215
+ end
216
+ @batch_wait_thread.join
217
+ @batch_size_thread.join
218
+
219
+ # if by any chance we still have a forming batch, we need to send it.
220
+ send(@batch) if !@batch.nil?
221
+ @batch = nil
222
+ end
223
+
224
+ def send(batch)
225
+ payload = batch.to_json
226
+ res = loki_http_request(payload)
227
+ if res.is_a?(Net::HTTPSuccess)
228
+ @logger.debug("Successfully pushed data to loki")
229
+ else
230
+ @logger.debug("failed payload", :payload => payload)
231
+ end
232
+ end
233
+
234
+ def loki_http_request(payload)
235
+ req = Net::HTTP::Post.new(
236
+ @uri.request_uri
237
+ )
238
+ req.add_field('Content-Type', 'application/json')
239
+ req.add_field('X-Scope-OrgID', @tenant_id) if @tenant_id
240
+ req['User-Agent']= 'loki-logstash'
241
+ req.basic_auth(@username, @password) if @username
242
+ req.body = payload
243
+
244
+ # puts "loki_http_request()"
245
+ # puts payload
246
+
247
+ opts = ssl_opts(@uri)
248
+
249
+ @logger.debug("sending #{req.body.length} bytes to loki")
250
+ retry_count = 0
251
+ delay = @min_delay
252
+ begin
253
+ res = Net::HTTP.start(@uri.host, @uri.port, **opts) { |http|
254
+ http.request(req)
255
+ }
256
+ return res if !res.nil? && res.code.to_i != 429 && res.code.to_i.div(100) != 5
257
+ raise StandardError.new res
258
+ rescue StandardError => e
259
+ retry_count += 1
260
+ @logger.warn("Failed to send batch, attempt: #{retry_count}/#{@retries}", :error_inspect => e.inspect, :error => e)
261
+ if retry_count < @retries
262
+ sleep delay
263
+ if delay * 2 <= @max_delay
264
+ delay = delay * 2
265
+ else
266
+ delay = @max_delay
267
+ end
268
+ retry
269
+ else
270
+ @logger.error("Failed to send batch", :error_inspect => e.inspect, :error => e)
271
+ return res
272
+ end
273
+ end
274
+ end
275
+ end
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-output-odyssey'
3
+ s.version = '0.0.1'
4
+ s.authors = ['Chris Foster']
5
+ s.email = ['chris.foster78@gmail.com']
6
+
7
+ s.summary = 'Output plugin for Odyssey'
8
+ s.description = 'Output plugin for Odyssey'
9
+ s.homepage = 'https://en.wikipedia.org/wiki/Homer'
10
+ s.license = 'Apache-2.0'
11
+ s.require_paths = ["lib"]
12
+
13
+ # Files
14
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile']
15
+ # Tests
16
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
17
+
18
+ # Special flag to let us know this is actually a logstash plugin
19
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "output" }
20
+
21
+ # Gem dependencies
22
+ #
23
+ s.add_runtime_dependency "logstash-core-plugin-api", ">= 1.60", "<= 2.99"
24
+ s.add_runtime_dependency "logstash-codec-plain", "3.1.0"
25
+ s.add_development_dependency 'logstash-devutils', "2.0.2"
26
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/outputs/loki"
4
+ require "logstash/codecs/plain"
5
+ require "logstash/event"
6
+ require "net/http"
7
+ include Loki
8
+
9
+ describe Loki::Entry do
10
+ context 'test entry generation' do
11
+ let (:event) {
12
+ LogStash::Event.new(
13
+ {
14
+ 'message' => 'hello',
15
+ '@metadata' => {'foo'=>'bar'},
16
+ '@version' => '1',
17
+ 'foo' => 5,
18
+ 'agent' => 'filebeat',
19
+ 'log' => {
20
+ 'file' =>
21
+ {'@path' => '/path/to/file.log'},
22
+ },
23
+ 'host' => '172.0.0.1',
24
+ '@timestamp' => Time.now
25
+ }
26
+ )
27
+ }
28
+
29
+ it 'labels extracted should not contains object and metadata or timestamp' do
30
+ entry = Entry.new(event,"message", [])
31
+ expect(entry.labels).to eql({ 'agent' => 'filebeat', 'host' => '172.0.0.1', 'foo'=>'5'})
32
+ expect(entry.entry['ts']).to eql to_ns(event.get("@timestamp"))
33
+ expect(entry.entry['line']).to eql 'hello'
34
+ end
35
+
36
+ it 'labels extracted should only contain allowlisted labels' do
37
+ entry = Entry.new(event, "message", %w[agent foo])
38
+ expect(entry.labels).to eql({ 'agent' => 'filebeat', 'foo'=>'5'})
39
+ expect(entry.entry['ts']).to eql to_ns(event.get("@timestamp"))
40
+ expect(entry.entry['line']).to eql 'hello'
41
+ end
42
+ end
43
+
44
+ context 'test batch generation with label order' do
45
+ let (:entries) {[
46
+ Entry.new(LogStash::Event.new({"message"=>"foobuzz","buzz"=>"bar","cluster"=>"us-central1","@timestamp"=>Time.at(1)}),"message", []),
47
+ Entry.new(LogStash::Event.new({"log"=>"foobar","bar"=>"bar","@timestamp"=>Time.at(2)}),"log", []),
48
+ Entry.new(LogStash::Event.new({"cluster"=>"us-central1","message"=>"foobuzz","buzz"=>"bar","@timestamp"=>Time.at(3)}),"message", []),
49
+
50
+ ]}
51
+ let (:expected) {
52
+ {"streams" => [
53
+ {"stream"=> {"buzz"=>"bar","cluster"=>"us-central1"}, "values" => [[to_ns(Time.at(1)).to_s,"foobuzz"],[to_ns(Time.at(3)).to_s,"foobuzz"]]},
54
+ {"stream"=> {"bar"=>"bar"}, "values"=>[[to_ns(Time.at(2)).to_s,"foobar"]]},
55
+ ] }
56
+ }
57
+
58
+ it 'to_json' do
59
+ @batch = Loki::Batch.new(entries.first)
60
+ entries.drop(1).each { |e| @batch.add(e)}
61
+ expect(JSON.parse(@batch.to_json)).to eql expected
62
+ end
63
+ end
64
+
65
+
66
+ end
@@ -0,0 +1,263 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/outputs/loki"
4
+ require "logstash/codecs/plain"
5
+ require "logstash/event"
6
+ require "net/http"
7
+ require 'webmock/rspec'
8
+ include Loki
9
+
10
+ describe LogStash::Outputs::Loki do
11
+
12
+ let (:simple_loki_config) { {'url' => 'http://localhost:3100'} }
13
+
14
+ context 'when initializing' do
15
+ it "should register" do
16
+ loki = LogStash::Plugin.lookup("output", "loki").new(simple_loki_config)
17
+ expect { loki.register }.to_not raise_error
18
+ end
19
+
20
+ it 'should populate loki config with default or initialized values' do
21
+ loki = LogStash::Outputs::Loki.new(simple_loki_config)
22
+ expect(loki.url).to eql 'http://localhost:3100'
23
+ expect(loki.tenant_id).to eql nil
24
+ expect(loki.batch_size).to eql 102400
25
+ expect(loki.batch_wait).to eql 1
26
+ end
27
+ end
28
+
29
+ context 'when adding en entry to the batch' do
30
+ let (:simple_loki_config) {{'url' => 'http://localhost:3100'}}
31
+ let (:entry) {Entry.new(LogStash::Event.new({"message"=>"foobuzz","buzz"=>"bar","cluster"=>"us-central1","@timestamp"=>Time.at(1)}),"message", [])}
32
+ let (:lbs) {{"buzz"=>"bar","cluster"=>"us-central1"}.sort.to_h}
33
+ let (:include_loki_config) {{ 'url' => 'http://localhost:3100', 'include_fields' => ["cluster"] }}
34
+ let (:include_entry) {Entry.new(LogStash::Event.new({"message"=>"foobuzz","buzz"=>"bar","cluster"=>"us-central1","@timestamp"=>Time.at(1)}),"message", ["cluster"])}
35
+ let (:include_lbs) {{"cluster"=>"us-central1"}.sort.to_h}
36
+
37
+ it 'should not add empty line' do
38
+ plugin = LogStash::Plugin.lookup("output", "loki").new(simple_loki_config)
39
+ emptyEntry = Entry.new(LogStash::Event.new({"message"=>"foobuzz","buzz"=>"bar","cluster"=>"us-central1","@timestamp"=>Time.at(1)}),"foo", [])
40
+ expect(plugin.add_entry_to_batch(emptyEntry)).to eql true
41
+ expect(plugin.batch).to eql nil
42
+ end
43
+
44
+ it 'should add entry' do
45
+ plugin = LogStash::Plugin.lookup("output", "loki").new(simple_loki_config)
46
+ expect(plugin.batch).to eql nil
47
+ expect(plugin.add_entry_to_batch(entry)).to eql true
48
+ expect(plugin.add_entry_to_batch(entry)).to eql true
49
+ expect(plugin.batch).not_to be_nil
50
+ expect(plugin.batch.streams.length).to eq 1
51
+ expect(plugin.batch.streams[lbs.to_s]['entries'].length).to eq 2
52
+ expect(plugin.batch.streams[lbs.to_s]['labels']).to eq lbs
53
+ expect(plugin.batch.size_bytes).to eq 14
54
+ end
55
+
56
+ it 'should only allowed labels defined in include_fields' do
57
+ plugin = LogStash::Plugin.lookup("output", "loki").new(include_loki_config)
58
+ expect(plugin.batch).to eql nil
59
+ expect(plugin.add_entry_to_batch(include_entry)).to eql true
60
+ expect(plugin.add_entry_to_batch(include_entry)).to eql true
61
+ expect(plugin.batch).not_to be_nil
62
+ expect(plugin.batch.streams.length).to eq 1
63
+ expect(plugin.batch.streams[include_lbs.to_s]['entries'].length).to eq 2
64
+ expect(plugin.batch.streams[include_lbs.to_s]['labels']).to eq include_lbs
65
+ expect(plugin.batch.size_bytes).to eq 14
66
+ end
67
+
68
+ it 'should not add if full' do
69
+ plugin = LogStash::Plugin.lookup("output", "loki").new(simple_loki_config.merge!({'batch_size'=>10}))
70
+ expect(plugin.batch).to eql nil
71
+ expect(plugin.add_entry_to_batch(entry)).to eql true # first entry is fine.
72
+ expect(plugin.batch).not_to be_nil
73
+ expect(plugin.batch.streams.length).to eq 1
74
+ expect(plugin.batch.streams[lbs.to_s]['entries'].length).to eq 1
75
+ expect(plugin.batch.streams[lbs.to_s]['labels']).to eq lbs
76
+ expect(plugin.batch.size_bytes).to eq 7
77
+ expect(plugin.add_entry_to_batch(entry)).to eql false # second entry goes over the limit.
78
+ expect(plugin.batch).not_to be_nil
79
+ expect(plugin.batch.streams.length).to eq 1
80
+ expect(plugin.batch.streams[lbs.to_s]['entries'].length).to eq 1
81
+ expect(plugin.batch.streams[lbs.to_s]['labels']).to eq lbs
82
+ expect(plugin.batch.size_bytes).to eq 7
83
+ end
84
+ end
85
+
86
+ context 'batch expiration' do
87
+ let (:entry) {Entry.new(LogStash::Event.new({"message"=>"foobuzz","buzz"=>"bar","cluster"=>"us-central1","@timestamp"=>Time.at(1)}),"message", [])}
88
+
89
+ it 'should not expire if empty' do
90
+ loki = LogStash::Outputs::Loki.new(simple_loki_config.merge!({'batch_wait'=>0.5}))
91
+ sleep(1)
92
+ expect(loki.is_batch_expired).to be false
93
+ end
94
+ it 'should not expire batch if not old' do
95
+ loki = LogStash::Outputs::Loki.new(simple_loki_config.merge!({'batch_wait'=>0.5}))
96
+ expect(loki.add_entry_to_batch(entry)).to eql true
97
+ expect(loki.is_batch_expired).to be false
98
+ end
99
+ it 'should expire if old' do
100
+ loki = LogStash::Outputs::Loki.new(simple_loki_config.merge!({'batch_wait'=>0.5}))
101
+ expect(loki.add_entry_to_batch(entry)).to eql true
102
+ sleep(1)
103
+ expect(loki.is_batch_expired).to be true
104
+ end
105
+ end
106
+
107
+ context 'channel' do
108
+ let (:event) {LogStash::Event.new({"message"=>"foobuzz","buzz"=>"bar","cluster"=>"us-central1","@timestamp"=>Time.at(1)})}
109
+
110
+ it 'should send entry if batch size reached with no tenant' do
111
+ loki = LogStash::Outputs::Loki.new(simple_loki_config.merge!({'batch_wait'=>0.5,'batch_size'=>10}))
112
+ loki.register
113
+ sent = Queue.new
114
+ allow(loki).to receive(:send) do |batch|
115
+ Thread.new do
116
+ sent << batch
117
+ end
118
+ end
119
+ loki.receive(event)
120
+ loki.receive(event)
121
+ sent.deq
122
+ sent.deq
123
+ loki.close
124
+ end
125
+ it 'should send entry while closing' do
126
+ loki = LogStash::Outputs::Loki.new(simple_loki_config.merge!({'batch_wait'=>10,'batch_size'=>10}))
127
+ loki.register
128
+ sent = Queue.new
129
+ allow(loki).to receive(:send) do | batch|
130
+ Thread.new do
131
+ sent << batch
132
+ end
133
+ end
134
+ loki.receive(event)
135
+ loki.close
136
+ sent.deq
137
+ end
138
+ it 'should send entry when batch is expiring' do
139
+ loki = LogStash::Outputs::Loki.new(simple_loki_config.merge!({'batch_wait'=>0.5,'batch_size'=>10}))
140
+ loki.register
141
+ sent = Queue.new
142
+ allow(loki).to receive(:send) do | batch|
143
+ Thread.new do
144
+ sent << batch
145
+ end
146
+ end
147
+ loki.receive(event)
148
+ sent.deq
149
+ sleep(0.01) # Adding a minimal sleep. In few cases @batch=nil might happen after evaluating for nil
150
+ expect(loki.batch).to be_nil
151
+ loki.close
152
+ end
153
+ end
154
+
155
+ context 'http requests' do
156
+ let (:entry) {Entry.new(LogStash::Event.new({"message"=>"foobuzz","buzz"=>"bar","cluster"=>"us-central1","@timestamp"=>Time.at(1)}),"message", [])}
157
+
158
+ it 'should send credentials' do
159
+ conf = {
160
+ 'url'=>'http://localhost:3100/loki/api/v1/push',
161
+ 'username' => 'foo',
162
+ 'password' => 'bar',
163
+ 'tenant_id' => 't'
164
+ }
165
+ loki = LogStash::Outputs::Loki.new(conf)
166
+ loki.register
167
+ b = Batch.new(entry)
168
+ post = stub_request(:post, "http://localhost:3100/loki/api/v1/push").with(
169
+ basic_auth: ['foo', 'bar'],
170
+ body: b.to_json,
171
+ headers:{
172
+ 'Content-Type' => 'application/json' ,
173
+ 'User-Agent' => 'loki-logstash',
174
+ 'X-Scope-OrgID'=>'t',
175
+ 'Accept'=>'*/*',
176
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
177
+ }
178
+ )
179
+ loki.send(b)
180
+ expect(post).to have_been_requested.times(1)
181
+ end
182
+
183
+ it 'should not send credentials' do
184
+ conf = {
185
+ 'url'=>'http://foo.com/loki/api/v1/push',
186
+ }
187
+ loki = LogStash::Outputs::Loki.new(conf)
188
+ loki.register
189
+ b = Batch.new(entry)
190
+ post = stub_request(:post, "http://foo.com/loki/api/v1/push").with(
191
+ body: b.to_json,
192
+ headers:{
193
+ 'Content-Type' => 'application/json' ,
194
+ 'User-Agent' => 'loki-logstash',
195
+ 'Accept'=>'*/*',
196
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
197
+ }
198
+ )
199
+ loki.send(b)
200
+ expect(post).to have_been_requested.times(1)
201
+ end
202
+ it 'should retry 500' do
203
+ conf = {
204
+ 'url'=>'http://foo.com/loki/api/v1/push',
205
+ 'retries' => 3,
206
+ }
207
+ loki = LogStash::Outputs::Loki.new(conf)
208
+ loki.register
209
+ b = Batch.new(entry)
210
+ post = stub_request(:post, "http://foo.com/loki/api/v1/push").with(
211
+ body: b.to_json,
212
+ ).to_return(status: [500, "Internal Server Error"])
213
+ loki.send(b)
214
+ loki.close
215
+ expect(post).to have_been_requested.times(3)
216
+ end
217
+ it 'should retry 429' do
218
+ conf = {
219
+ 'url'=>'http://foo.com/loki/api/v1/push',
220
+ 'retries' => 2,
221
+ }
222
+ loki = LogStash::Outputs::Loki.new(conf)
223
+ loki.register
224
+ b = Batch.new(entry)
225
+ post = stub_request(:post, "http://foo.com/loki/api/v1/push").with(
226
+ body: b.to_json,
227
+ ).to_return(status: [429, "stop spamming"])
228
+ loki.send(b)
229
+ loki.close
230
+ expect(post).to have_been_requested.times(2)
231
+ end
232
+ it 'should not retry 400' do
233
+ conf = {
234
+ 'url'=>'http://foo.com/loki/api/v1/push',
235
+ 'retries' => 11,
236
+ }
237
+ loki = LogStash::Outputs::Loki.new(conf)
238
+ loki.register
239
+ b = Batch.new(entry)
240
+ post = stub_request(:post, "http://foo.com/loki/api/v1/push").with(
241
+ body: b.to_json,
242
+ ).to_return(status: [400, "bad request"])
243
+ loki.send(b)
244
+ loki.close
245
+ expect(post).to have_been_requested.times(1)
246
+ end
247
+ it 'should retry exception' do
248
+ conf = {
249
+ 'url'=>'http://foo.com/loki/api/v1/push',
250
+ 'retries' => 11,
251
+ }
252
+ loki = LogStash::Outputs::Loki.new(conf)
253
+ loki.register
254
+ b = Batch.new(entry)
255
+ post = stub_request(:post, "http://foo.com/loki/api/v1/push").with(
256
+ body: b.to_json,
257
+ ).to_raise("some error").then.to_return(status: [200, "fine !"])
258
+ loki.send(b)
259
+ loki.close
260
+ expect(post).to have_been_requested.times(2)
261
+ end
262
+ end
263
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-output-odyssey
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Foster
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash-core-plugin-api
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.60'
20
+ - - "<="
21
+ - !ruby/object:Gem::Version
22
+ version: '2.99'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.60'
30
+ - - "<="
31
+ - !ruby/object:Gem::Version
32
+ version: '2.99'
33
+ - !ruby/object:Gem::Dependency
34
+ name: logstash-codec-plain
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '='
38
+ - !ruby/object:Gem::Version
39
+ version: 3.1.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '='
45
+ - !ruby/object:Gem::Version
46
+ version: 3.1.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: logstash-devutils
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 2.0.2
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - '='
59
+ - !ruby/object:Gem::Version
60
+ version: 2.0.2
61
+ description: Output plugin for Odyssey
62
+ email:
63
+ - chris.foster78@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - Gemfile
69
+ - README.md
70
+ - lib/logstash/outputs/loki.rb
71
+ - lib/logstash/outputs/loki/batch.rb
72
+ - lib/logstash/outputs/loki/entry.rb
73
+ - logstash-output-loki.gemspec
74
+ - spec/outputs/loki/entry_spec.rb
75
+ - spec/outputs/loki_spec.rb
76
+ homepage: https://en.wikipedia.org/wiki/Homer
77
+ licenses:
78
+ - Apache-2.0
79
+ metadata:
80
+ logstash_plugin: 'true'
81
+ logstash_group: output
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 3.0.3.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Output plugin for Odyssey
101
+ test_files:
102
+ - spec/outputs/loki/entry_spec.rb
103
+ - spec/outputs/loki_spec.rb