Skip to content
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

Fix encoding error in fluentd client #5163

Merged
merged 2 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Main

* [5163](https://github.com/grafana/loki/pull/5163) **chaudum** Fix regression in fluentd plugin introduced with #5107 that caused `NoMethodError` when parsing non-string values of log lines.
* [5144](https://github.com/grafana/loki/pull/5144) **dannykopping** Ruler: fix remote write basic auth credentials.
* [5107](https://github.com/grafana/loki/pull/5107) **chaudum** Fix bug in fluentd plugin that caused log lines containing non UTF-8 characters to be dropped.
* [5091](https://github.com/grafana/loki/pull/5091) **owen-d**: Changes `ingester.concurrent-flushes` default to 32
Expand Down
2 changes: 1 addition & 1 deletion clients/cmd/fluentd/fluent-plugin-grafana-loki.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $LOAD_PATH.push File.expand_path('lib', __dir__)

Gem::Specification.new do |spec|
spec.name = 'fluent-plugin-grafana-loki'
spec.version = '1.2.17'
spec.version = '1.2.18'
spec.authors = %w[woodsaj briangann cyriltovena]
spec.email = ['[email protected]', '[email protected]', '[email protected]']

Expand Down
4 changes: 3 additions & 1 deletion clients/cmd/fluentd/lib/fluent/plugin/out_loki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ def record_to_line(record)
formatted_labels = []
record.each do |k, v|
# Remove non UTF-8 characters by force-encoding the string
v = v.encode('utf-8', invalid: :replace)
if v.is_a?(String)
v = v.encode('utf-8', invalid: :replace)
end
# Escape double quotes and backslashes by prefixing them with a backslash
v = v.to_s.gsub(%r{(["\\])}, '\\\\\1')
if v.include?(' ') || v.include?('=')
Expand Down
24 changes: 20 additions & 4 deletions clients/cmd/fluentd/spec/gems/fluent/plugin/loki_output_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,29 @@
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'}]
content = File.readlines('spec/gems/fluent/plugin/data/non_utf8.log')[0]
chunk = [Time.at(1_546_270_458), {'message'=>content, 'number': 1.2345, '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'
expect(payload[0]['values'][0][0]).to eq "1546270458000000000"
expect(payload[0]['values'][0][1]).to eq "message=\"� rest of line\" number=1.2345 stream=stdout"
end

it 'handle non utf-8 characters from log lines in json format' do
config = <<-CONF
url https://logs-us-west1.grafana.net
line_format json
CONF
driver = Fluent::Test::Driver::Output.new(described_class)
driver.configure(config)
content = File.readlines('spec/gems/fluent/plugin/data/non_utf8.log')[0]
chunk = [Time.at(1_546_270_458), {'message'=>content, 'number': 1.2345, '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\":\"\xC1 rest of line\",\"number\":1.2345,\"stream\":\"stdout\"}"
end

it 'formats record hash as key_value' do
Expand Down