Skip to content

Commit

Permalink
logstash code refactor and doc improvements (#2410)
Browse files Browse the repository at this point in the history
* logstash code refactor and doc improvements

* added back gradlew assemble
  • Loading branch information
adityacs authored Jul 24, 2020
1 parent dd68651 commit 43f2ef3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
10 changes: 6 additions & 4 deletions cmd/logstash/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Contributing to Loki Logstash Output Plugin

For information about hwo to use this plugin see this [documentation](../../docs/clients/logstash/README.md).
For information about how to use this plugin see this [documentation](../../docs/sources/clients/logstash/_index.md).

## Install dependencies

First you need to setup JRuby environment to build this plugin. Refer https://github.com/rbenv/rbenv for setting up your rbenv environment.
First, make sure you have JDK version `8` or `11` installed and you have set the `JAVA_HOME` environment variable.

You need to setup JRuby environment to build this plugin. Refer https://github.com/rbenv/rbenv for setting up your rbenv environment.

After setting up `rbenv`. Install JRuby

Expand All @@ -20,7 +22,7 @@ ruby --version
jruby 9.2.10
```

You should use make sure you are running jruby and not ruby. If the command below 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:
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:

```bash
export PATH="$HOME/.rbenv/bin:$PATH"
Expand All @@ -32,7 +34,7 @@ Then install bundler

Follow those instructions to [install logstash](https://www.elastic.co/guide/en/logstash/current/installing-logstash.html) before moving to the next section.

## Install dependencies and Build plugin
## Build and test the plugin

### Install required packages

Expand Down
29 changes: 20 additions & 9 deletions cmd/logstash/lib/logstash/outputs/loki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class LogStash::Outputs::Loki < LogStash::Outputs::Base
## 'TLS'
config :ca_cert, :validate => :path, :required => false

## 'Disable server certificate verification'
config :insecure_skip_verify, :validate => :boolean, :default => false, :required => false

## 'Loki Tenant ID'
config :tenant_id, :validate => :string, :required => false

Expand All @@ -45,8 +48,8 @@ class LogStash::Outputs::Loki < LogStash::Outputs::Base
## 'Backoff configuration. Initial backoff time between retries. Default 1s'
config :min_delay, :validate => :number, :default => 1, :required => false

## 'Backoff configuration. Maximum backoff time between retries. Default 300s'
config :max_delay, :validate => :number, :default => 300, :required => false
## 'Backoff configuration. Maximum backoff time between retries. Default 300s'
config :max_delay, :validate => :number, :default => 300, :required => false

## 'Backoff configuration. Maximum number of retries to do'
config :retries, :validate => :number, :default => 10, :required => false
Expand Down Expand Up @@ -102,6 +105,13 @@ def ssl_opts(uri)
use_ssl: uri.scheme == 'https'
}

# disable server certificate verification
if @insecure_skip_verify
opts = opts.merge(
verify_mode: OpenSSL::SSL::VERIFY_NONE
)
end

if !@cert.nil? && !@key.nil?
opts = opts.merge(
verify_mode: OpenSSL::SSL::VERIFY_PEER,
Expand All @@ -119,7 +129,8 @@ def ssl_opts(uri)
end

def run()
min_wait_checkfrequency = 1/100 #1 millisecond
# minimum wait frequency is 1 millisecond
min_wait_checkfrequency = 1/100
max_wait_checkfrequency = @batch_wait / 10
if max_wait_checkfrequency < min_wait_checkfrequency
max_wait_checkfrequency = min_wait_checkfrequency
Expand All @@ -139,7 +150,7 @@ def run()
end
}
s.take(@max_wait_check) {
# Send batch if max wait time has been reached
# send batch if max wait time has been reached
if is_batch_expired
@logger.debug("Max batch_wait time is reached. Sending batch to loki")
send(@batch)
Expand All @@ -150,7 +161,7 @@ def run()
end
end

# add an entry to the current batch return false if the batch is full
# Add an entry to the current batch returns false if the batch is full
# and the entry can't be added.
def add_entry_to_batch(e)
line = e.entry['line']
Expand Down Expand Up @@ -222,13 +233,13 @@ def loki_http_request(payload)
raise StandardError.new res
rescue StandardError => e
retry_count += 1
@logger.warn("Failed to send batch attempt: #{retry_count}/#{@retries}", :error_inspect => e.inspect, :error => e)
@logger.warn("Failed to send batch, attempt: #{retry_count}/#{@retries}", :error_inspect => e.inspect, :error => e)
if retry_count < @retries
sleep delay
if (delay * 2 - delay) > @max_delay
delay = delay
else
if delay * 2 <= @max_delay
delay = delay * 2
else
delay = @max_delay
end
retry
else
Expand Down
4 changes: 2 additions & 2 deletions cmd/logstash/logstash-output-loki.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'logstash-output-loki'
s.version = '1.0.1'
s.name = 'logstash-output-loki'
s.version = '1.0.2'
s.authors = ['Aditya C S','Cyril Tovena']
s.email = ['aditya.gnu@gmail.com','cyril.tovena@grafana.com']

Expand Down
2 changes: 2 additions & 0 deletions cmd/logstash/loki.conf
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ output {
# key => /path/to/key.key

# ca_cert => /path/to/ca.pem

# insecure_skip_verify => false
}
}
5 changes: 5 additions & 0 deletions docs/sources/clients/logstash/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ output {
[ca_cert => path | default = nil | required=false]
[insecure_skip_verify => boolean | default = fasle | required=false]
}
}
```
Expand Down Expand Up @@ -229,6 +230,10 @@ Loki is a multi-tenant log storage platform and all requests sent must include a

Specify a pair of client certificate and private key with `cert` and `key` if a reverse proxy with client certificate verification is configured in front of Loki. `ca_cert` can also be specified if the server uses custom certificate authority.

### insecure_skip_verify

A flag to disable server certificate verification. By default it is set to `false`.

### Full configuration example

```conf
Expand Down

0 comments on commit 43f2ef3

Please sign in to comment.