-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fluentd: Fix bug that caused lines to be dropped when containing non utf-8 characters #5107
Changes from 3 commits
80735f6
7fd4c40
a830c85
badc04d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
/coverage/ | ||
/.rspec_status | ||
/Gemfile.lock | ||
Gemfile.lock | ||
.rspec_status | ||
# rbenv | ||
.ruby-version | ||
# bundler | ||
.bundle/ | ||
vendor/ | ||
# simplecov | ||
coverage/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM ruby:2.6 as build | ||
FROM ruby:2.7.5 as build | ||
|
||
ENV DEBIAN_FRONTEND=noninteractive | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
gem install bundler | ||
bundle install | ||
ruby --version | ||
echo "" | ||
ruby -S gem install bundler --version 2.3.4 | ||
ruby -S bundle config set --local path $(pwd)/vendor/bundle | ||
ruby -S bundle install |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
ruby -S bundle exec rspec |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -278,6 +278,8 @@ def record_to_line(record) | |
when :key_value | ||
formatted_labels = [] | ||
record.each do |k, v| | ||
# Remove non UTF-8 characters by force-encoding the string and replacing said chars with empty string | ||
v = v.encode('utf-8', invalid: :replace, replace: '') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we replace with another special character may be ? Not sure if we can use empty string , it will not tell users there was an issue there ? WDYT ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would � work ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can let Ruby decide how to replace the invalid characters, like so:
|
||
# Escape double quotes and backslashes by prefixing them with a backslash | ||
v = v.to_s.gsub(%r{(["\\])}, '\\\\\1') | ||
if v.include?(' ') || v.include?('=') | ||
|
@@ -292,7 +294,6 @@ def record_to_line(record) | |
line | ||
end | ||
|
||
# | ||
# convert a line to loki line with labels | ||
def line_to_loki(record) | ||
chunk_labels = {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
� rest of line |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,12 +50,12 @@ | |
driver = Fluent::Test::Driver::Output.new(described_class) | ||
driver.configure(config) | ||
content = File.readlines('spec/gems/fluent/plugin/data/syslog2') | ||
single_chunk = [Time.at(1_546_270_458), content] | ||
payload = driver.instance.generic_to_loki([single_chunk]) | ||
body = { 'streams': payload } | ||
expect(body[:streams][0]['stream'].empty?).to eq true | ||
expect(body[:streams][0]['values'].count).to eq 1 | ||
expect(body[:streams][0]['values'][0][0]).to eq '1546270458000000000' | ||
chunk = [Time.at(1_546_270_458), content[0]] | ||
payload = driver.instance.generic_to_loki([chunk]) | ||
expect(payload[0]['stream'].empty?).to eq true | ||
expect(payload[0]['values'].count).to eq 1 | ||
expect(payload[0]['values'][0][0]).to eq '1546270458000000000' | ||
expect(payload[0]['values'][0][1]).to eq content[0] | ||
end | ||
|
||
it 'converts syslog output with extra labels to loki output' do | ||
|
@@ -66,12 +66,12 @@ | |
driver = Fluent::Test::Driver::Output.new(described_class) | ||
driver.configure(config) | ||
content = File.readlines('spec/gems/fluent/plugin/data/syslog2') | ||
single_chunk = [Time.at(1_546_270_458), content] | ||
payload = driver.instance.generic_to_loki([single_chunk]) | ||
body = { 'streams': payload } | ||
expect(body[:streams][0]['stream']).to eq('env' => 'test') | ||
expect(body[:streams][0]['values'].count).to eq 1 | ||
expect(body[:streams][0]['values'][0][0]).to eq '1546270458000000000' | ||
chunk = [Time.at(1_546_270_458), content[0]] | ||
payload = driver.instance.generic_to_loki([chunk]) | ||
expect(payload[0]['stream']).to eq('env' => 'test') | ||
expect(payload[0]['values'].count).to eq 1 | ||
expect(payload[0]['values'][0][0]).to eq '1546270458000000000' | ||
expect(payload[0]['values'][0][1]).to eq content[0] | ||
end | ||
|
||
it 'converts multiple syslog output lines to loki output' do | ||
|
@@ -84,11 +84,12 @@ | |
line1 = [Time.at(1_546_270_458), content[0]] | ||
line2 = [Time.at(1_546_270_460), content[1]] | ||
payload = driver.instance.generic_to_loki([line1, line2]) | ||
body = { 'streams': payload } | ||
expect(body[:streams][0]['stream'].empty?).to eq true | ||
expect(body[:streams][0]['values'].count).to eq 2 | ||
expect(body[:streams][0]['values'][0][0]).to eq '1546270458000000000' | ||
expect(body[:streams][0]['values'][1][0]).to eq '1546270460000000000' | ||
expect(payload[0]['stream'].empty?).to eq true | ||
expect(payload[0]['values'].count).to eq 2 | ||
expect(payload[0]['values'][0][0]).to eq '1546270458000000000' | ||
expect(payload[0]['values'][0][1]).to eq content[0] | ||
expect(payload[0]['values'][1][0]).to eq '1546270460000000000' | ||
expect(payload[0]['values'][1][1]).to eq content[1] | ||
end | ||
|
||
it 'converts multiple syslog output lines with extra labels to loki output' do | ||
|
@@ -102,11 +103,27 @@ | |
line1 = [Time.at(1_546_270_458), content[0]] | ||
line2 = [Time.at(1_546_270_460), content[1]] | ||
payload = driver.instance.generic_to_loki([line1, line2]) | ||
body = { 'streams': payload } | ||
expect(body[:streams][0]['stream']).to eq('env' => 'test') | ||
expect(body[:streams][0]['values'].count).to eq 2 | ||
expect(body[:streams][0]['values'][0][0]).to eq '1546270458000000000' | ||
expect(body[:streams][0]['values'][1][0]).to eq '1546270460000000000' | ||
expect(payload[0]['stream']).to eq('env' => 'test') | ||
expect(payload[0]['values'].count).to eq 2 | ||
expect(payload[0]['values'][0][0]).to eq '1546270458000000000' | ||
expect(payload[0]['values'][0][1]).to eq content[0] | ||
expect(payload[0]['values'][1][0]).to eq '1546270460000000000' | ||
expect(payload[0]['values'][1][1]).to eq content[1] | ||
end | ||
|
||
it 'removed non utf-8 characters from log lines' do | ||
config = <<-CONF | ||
url https://logs-us-west1.grafana.net | ||
CONF | ||
driver = Fluent::Test::Driver::Output.new(described_class) | ||
driver.configure(config) | ||
content = File.readlines('spec/gems/fluent/plugin/data/non_utf8.log') | ||
chunk = [Time.at(1_546_270_458), {'message'=>content[0], 'stream'=>'stdout'}] | ||
payload = driver.instance.generic_to_loki([chunk]) | ||
expect(payload[0]['stream'].empty?).to eq true | ||
expect(payload[0]['values'].count).to eq 1 | ||
expect(payload[0]['values'][0][0]).to eq '1546270458000000000' | ||
expect(payload[0]['values'][0][1]).to eq 'message=" rest of line" stream=stdout' | ||
end | ||
|
||
it 'formats record hash as key_value' do | ||
|
@@ -122,7 +139,7 @@ | |
expect(body[:streams][0]['stream'].empty?).to eq true | ||
expect(body[:streams][0]['values'].count).to eq 1 | ||
expect(body[:streams][0]['values'][0][0]).to eq '1546270458000000000' | ||
expect(body[:streams][0]['values'][0][1]).to eq 'message="' + content[0] + '" stream="stdout"' | ||
expect(body[:streams][0]['values'][0][1]).to eq 'message="' + content[0] + '" stream=stdout' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test was failing since c298514793ddf49ec8d15972b35cdae5f225b5d2 |
||
end | ||
|
||
it 'formats record hash as json' do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 this will do !