logstash-output-loki-tenants 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 468a3f120f85bb99690179384555a9cc4d0b090f027fa6abdbc2e40200973964
4
- data.tar.gz: 58532c550dff9474933c6df25b635586d0fd745014753babf8dfaf300e653939
3
+ metadata.gz: 3f63ff2d7beb2921dd555f62616813a045cca6dff4d0ca44ab290f38b4ff593f
4
+ data.tar.gz: 3d5864cb9c48e62b563c609c55d4b59e75976b5ce9542d17cc9d6ca7ac3c29ad
5
5
  SHA512:
6
- metadata.gz: 45db85447deeda78d894384604a1dd470b1b873d5ea854c6ab26390bdbfdc33640aaef7833af7aa66188ed96ad174414a85e0b90ad5ed186c0bbd7ac21ed4932
7
- data.tar.gz: '005299b0c0541b42806f35ae6d0aa7f55967066f8851642c9ec791f58b2d5387065c4600e51bc91fb7830c082abddbf14de51c5c9a82e3ca9d806c6a896de5e1'
6
+ metadata.gz: c37c14272d5241ee466626eced3e9fe22ed85a8b38dd25e67e000c39e70cea5c49aefb354465cb8068b2b504afd73d8c2ce4bcf60086ecb4b03961aa4a671c23
7
+ data.tar.gz: 8eccc504913b8d3e62edeb2fc5cd57034511e50b523af3ec820646214d474e2a19f57e6ef03c3693d237d96022b750e9e84caa8737c0894cffcef8f09e39b385
data/README.md CHANGED
@@ -1,4 +1,21 @@
1
- # Contributing to Loki Logstash Output Plugin
1
+ # Fork of Loki Logstash Output Plugin
2
+
3
+ Added features:
4
+
5
+ * split batches by the tenant attribute. Use ‘default’ if attribute not set.
6
+ * add X-Scope-OrgID header based on ‘tenant' message field.
7
+ * DO not set header if 'tenant’ attribute is empty.
8
+
9
+ Available from <https://rubygems.org/gems/logstash-output-loki-tenants>
10
+
11
+ ## Building and pushing gem
12
+ 1. `gem build logstash-output-loki.gemspec`
13
+ 2. Push desired build version `gem push logstash-output-loki-tenants-{VERSION}.gem`
14
+ - In case of massage 'Repushing of gem versions is not allowed.' Raise the plugin version in logstash-output-loki.gemspec
15
+ - Rebuild the plugin
16
+ - Push proper version
17
+
18
+ ## Contributing to Loki Logstash Output Plugin
2
19
 
3
20
  For information about how to use this plugin see this [documentation](../../docs/sources/clients/logstash/_index.md).
4
21
 
@@ -69,7 +69,7 @@ class LogStash::Outputs::Loki < LogStash::Outputs::Base
69
69
  @logger.info("Loki output plugin", :class => self.class.name)
70
70
 
71
71
  # initialize Queue and Mutex
72
- @entries = Queue.new
72
+ @entries = Queue.new
73
73
  @mutex = Mutex.new
74
74
  @stop = false
75
75
 
@@ -88,10 +88,10 @@ class LogStash::Outputs::Loki < LogStash::Outputs::Base
88
88
 
89
89
  end
90
90
 
91
- def batch(tenant = 'fake')
91
+ def batch(tenant = 'default')
92
92
  return nil if @batches.nil?
93
93
  return @batches[tenant] if !tenant.nil? && !tenant.empty? && @batches.key?(tenant)
94
- return @batches['fake'] if @batches.key?('fake')
94
+ return @batches['default'] if @batches.key?('default')
95
95
  return nil
96
96
  end
97
97
 
@@ -100,13 +100,13 @@ class LogStash::Outputs::Loki < LogStash::Outputs::Base
100
100
  @mutex.synchronize do
101
101
  return if @stop
102
102
  end
103
-
103
+
104
104
  e = @entries.deq
105
105
  return if e.nil?
106
106
 
107
107
  tenant = nil
108
108
  tenant = e.labels['tenant'] if !e.labels.nil? && e.labels.key?('tenant')
109
- tenant = 'fake' if tenant.nil? or tenant.empty?
109
+ tenant = 'default' if tenant.nil? or tenant.empty?
110
110
 
111
111
  @mutex.synchronize do
112
112
  if !add_entry_to_batch(e, tenant)
@@ -190,12 +190,12 @@ class LogStash::Outputs::Loki < LogStash::Outputs::Base
190
190
 
191
191
  # Add an entry to the current batch returns false if the batch is full
192
192
  # and the entry can't be added.
193
- def add_entry_to_batch(e, tenant = 'fake')
193
+ def add_entry_to_batch(e, tenant = 'default')
194
194
  line = e.entry['line']
195
195
  # we don't want to send empty lines.
196
196
  return true if line.to_s.strip.empty?
197
197
 
198
- tenant = 'fake' if tenant.nil? or tenant.empty?
198
+ tenant = 'default' if tenant.nil? or tenant.empty?
199
199
 
200
200
  if @batches.nil?
201
201
  @batches = Hash.new
@@ -214,8 +214,8 @@ class LogStash::Outputs::Loki < LogStash::Outputs::Base
214
214
  return true
215
215
  end
216
216
 
217
- def is_batch_expired(tenant = 'fake')
218
- tenant = 'fake' if tenant.nil? or tenant.empty?
217
+ def is_batch_expired(tenant = 'default')
218
+ tenant = 'default' if tenant.nil? or tenant.empty?
219
219
  return !@batches.nil? && @batches.key?(tenant) && @batches[tenant].age() >= @batch_wait
220
220
  end
221
221
 
@@ -231,8 +231,8 @@ class LogStash::Outputs::Loki < LogStash::Outputs::Base
231
231
 
232
232
  def close
233
233
  @entries.close
234
- @mutex.synchronize do
235
- @stop = true
234
+ @mutex.synchronize do
235
+ @stop = true
236
236
  end
237
237
  @batch_wait_thread.join
238
238
  @batch_size_thread.join
@@ -245,7 +245,7 @@ class LogStash::Outputs::Loki < LogStash::Outputs::Base
245
245
  @batches = nil
246
246
  end
247
247
 
248
- def send(batch, tenant = 'fake')
248
+ def send(batch, tenant = 'default')
249
249
  payload = batch.to_json
250
250
  res = loki_http_request(payload, tenant)
251
251
  if res.is_a?(Net::HTTPSuccess)
@@ -255,12 +255,12 @@ class LogStash::Outputs::Loki < LogStash::Outputs::Base
255
255
  end
256
256
  end
257
257
 
258
- def loki_http_request(payload, tenant = 'fake')
258
+ def loki_http_request(payload, tenant = 'default')
259
259
  req = Net::HTTP::Post.new(
260
260
  @uri.request_uri
261
261
  )
262
262
  req.add_field('Content-Type', 'application/json')
263
- if !tenant.nil? && !tenant.empty? && !tenant.eql?('fake')
263
+ if !tenant.nil? && !tenant.empty? && !tenant.eql?('default')
264
264
  req.add_field('X-Scope-OrgID', tenant)
265
265
  elsif !@tenant_id.nil? && !@tenant_id.empty?
266
266
  req.add_field('X-Scope-OrgID', @tenant_id)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'logstash-output-loki-tenants'
3
- s.version = '1.0.3'
3
+ s.version = '1.0.4'
4
4
  s.authors = ['Co.brick']
5
5
  s.email = ['aiops.dev@cobrick.com']
6
6
 
@@ -35,9 +35,10 @@ describe LogStash::Outputs::Loki do
35
35
  plugin = LogStash::Plugin.lookup("output", "loki").new(simple_loki_config)
36
36
  expect(plugin.batch).to eql nil
37
37
  expect(plugin.add_entry_to_batch(entry, "a")).to eql true
38
- expect(plugin.add_entry_to_batch(entry, "b")).to eql true
38
+ expect(plugin.add_entry_to_batch(entry, "fake")).to eql true
39
39
  expect(plugin.add_entry_to_batch(entry, nil)).to eql true
40
40
  expect(plugin.add_entry_to_batch(entry, "")).to eql true
41
+ expect(plugin.add_entry_to_batch(entry, "default")).to eql true
41
42
  expect(plugin.batches.keys.length).to eq 3
42
43
  end
43
44
 
@@ -58,6 +59,20 @@ describe LogStash::Outputs::Loki do
58
59
  expect(plugin.batch.streams[lbs.to_s]['entries'].length).to eq 2
59
60
  expect(plugin.batch.streams[lbs.to_s]['labels']).to eq lbs
60
61
  expect(plugin.batch.size_bytes).to eq 14
62
+ expect(plugin.batch("default").size_bytes).to eq 14
63
+ end
64
+
65
+ it 'should add entry to tenant fake' do
66
+ plugin = LogStash::Plugin.lookup("output", "loki").new(simple_loki_config)
67
+ expect(plugin.batch("fake")).to eql nil
68
+ expect(plugin.add_entry_to_batch(entry, "fake")).to eql true
69
+ expect(plugin.add_entry_to_batch(entry, "fake")).to eql true
70
+ expect(plugin.batch("fake")).not_to be_nil
71
+ expect(plugin.batch).to be_nil
72
+ expect(plugin.batch("fake").streams.length).to eq 1
73
+ expect(plugin.batch("fake").streams[lbs.to_s]['entries'].length).to eq 2
74
+ expect(plugin.batch("fake").streams[lbs.to_s]['labels']).to eq lbs
75
+ expect(plugin.batch("fake").size_bytes).to eq 14
61
76
  end
62
77
 
63
78
  it 'should not add if full' do
@@ -97,6 +112,15 @@ describe LogStash::Outputs::Loki do
97
112
  sleep(1)
98
113
  expect(loki.is_batch_expired).to be true
99
114
  end
115
+ it 'should expire if old different tenants' do
116
+ loki = LogStash::Outputs::Loki.new(simple_loki_config.merge!({'batch_wait'=>0.5}))
117
+ expect(loki.add_entry_to_batch(entry, "fake")).to eql true
118
+ sleep(1)
119
+ expect(loki.add_entry_to_batch(entry, "custom")).to eql true
120
+ expect(loki.is_batch_expired("fake")).to be true
121
+ expect(loki.is_batch_expired("custom")).to be false
122
+ expect(loki.is_batch_expired).to be false
123
+ end
100
124
  end
101
125
 
102
126
  context 'channel' do
@@ -174,6 +198,30 @@ describe LogStash::Outputs::Loki do
174
198
  loki.send(b, "custom")
175
199
  expect(post).to have_been_requested.times(1)
176
200
  end
201
+ it 'should send message tenant fake' do
202
+ conf = {
203
+ 'url'=>'http://localhost:3100/loki/api/v1/push',
204
+ 'username' => 'foo',
205
+ 'password' => 'bar',
206
+ 'tenant_id' => 'custom'
207
+ }
208
+ loki = LogStash::Outputs::Loki.new(conf)
209
+ loki.register
210
+ b = Batch.new(entry)
211
+ post = stub_request(:post, "http://localhost:3100/loki/api/v1/push").with(
212
+ basic_auth: ['foo', 'bar'],
213
+ body: b.to_json,
214
+ headers:{
215
+ 'Content-Type' => 'application/json' ,
216
+ 'User-Agent' => 'loki-logstash',
217
+ 'X-Scope-OrgID'=>'fake',
218
+ 'Accept'=>'*/*',
219
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
220
+ }
221
+ )
222
+ loki.send(b, "fake")
223
+ expect(post).to have_been_requested.times(1)
224
+ end
177
225
  it 'should send credentials' do
178
226
  conf = {
179
227
  'url'=>'http://localhost:3100/loki/api/v1/push',
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-output-loki-tenants
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Co.brick
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-25 00:00:00.000000000 Z
11
+ date: 2021-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement