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

throw exceptions on HTTPTooManyRequests and HTTPServerError so FluentD will retry #1845

Merged
merged 6 commits into from
Mar 26, 2020
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
8 changes: 7 additions & 1 deletion fluentd/fluent-plugin-grafana-loki/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Metrics/BlockLength:
Max: 60
Metrics/MethodLength:
Max: 50
Metrics/LineLength:
Layout/LineLength:
Max: 120
Metrics/ClassLength:
Max: 150
Expand All @@ -25,3 +25,9 @@ RSpec/ExampleLength:
Max: 100
RSpec/MultipleExpectations:
Max: 10
Style/HashEachMethods:
Enabled: true
Style/HashTransformKeys:
Enabled: true
Style/HashTransformValues:
Enabled: true
2 changes: 1 addition & 1 deletion fluentd/fluent-plugin-grafana-loki/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ source 'https://rubygems.org'

gemspec

gem 'fluentd', '1.9.0'
gem 'fluentd', '1.9.3'
gem 'rubocop-rspec'
gem 'simplecov', require: false, group: :test
gem 'test-unit'
46 changes: 26 additions & 20 deletions fluentd/fluent-plugin-grafana-loki/lib/fluent/plugin/out_loki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require 'fluent/env'
require 'fluent/plugin/output'
require 'net/http'
require 'yajl'
Expand All @@ -26,6 +27,8 @@ module Plugin
class LokiOutput < Fluent::Plugin::Output # rubocop:disable Metrics/ClassLength
Fluent::Plugin.register_output('loki', self)

class LogPostError < StandardError; end

helpers :compat_parameters, :record_accessor

attr_accessor :record_accessors
Expand Down Expand Up @@ -131,29 +134,16 @@ def write(chunk)
body = { 'streams' => payload }

# add ingest path to loki url
res = loki_http_request(body)

req = Net::HTTP::Post.new(
@uri.request_uri
)
req.add_field('Content-Type', 'application/json')
req.add_field('X-Scope-OrgID', @tenant) if @tenant
req.body = Yajl.dump(body)
req.basic_auth(@username, @password) if @username

opts = ssl_opts(@uri)
return if res.is_a?(Net::HTTPSuccess)

log.debug "sending #{req.body.length} bytes to loki"
res = Net::HTTP.start(@uri.host, @uri.port, **opts) { |http| http.request(req) }
unless res&.is_a?(Net::HTTPSuccess)
res_summary = if res
"#{res.code} #{res.message} #{res.body}"
else
'res=nil'
end
log.warn "failed to #{req.method} #{@uri} (#{res_summary})"
log.debug Yajl.dump(body)
res_summary = "#{res.code} #{res.message} #{res.body}"
log.warn "failed to write post to #{@uri} (#{res_summary})"
log.debug Yajl.dump(body)

end
# Only retry 429 and 500s
raise(LogPostError, res_summary) if res.is_a?(Net::HTTPTooManyRequests) || res.is_a?(Net::HTTPServerError)
wardbekker marked this conversation as resolved.
Show resolved Hide resolved
end

def ssl_opts(uri)
Expand Down Expand Up @@ -186,6 +176,22 @@ def generic_to_loki(chunk)

private

def loki_http_request(body)
req = Net::HTTP::Post.new(
@uri.request_uri
)
req.add_field('Content-Type', 'application/json')
req.add_field('X-Scope-OrgID', @tenant) if @tenant
req.body = Yajl.dump(body)
req.basic_auth(@username, @password) if @username

opts = ssl_opts(@uri)

log.debug "sending #{req.body.length} bytes to loki"

Net::HTTP.start(@uri.host, @uri.port, **opts) { |http| http.request(req) }
end

def numeric?(val)
!Float(val).nil?
rescue StandardError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,37 @@
expect(res[0]['stream']).to eq('stream' => 'stdout')
6.times { |i| expect(res[0]['values'][i][1]).to eq i.to_s }
end

it 'raises an LogPostError when http request is not successful' do
config = <<-CONF
url https://logs-us-west1.grafana.net
CONF
driver = Fluent::Test::Driver::Output.new(described_class)
driver.configure(config)
lines = [[Time.at(1_546_270_458), { 'message' => 'foobar', 'stream' => 'stdout' }]]

# 200
success = Net::HTTPSuccess.new(1.0, 200, 'OK')
allow(driver.instance).to receive(:loki_http_request) { success }
allow(success).to receive(:body).and_return('fake body')
expect { driver.instance.write(lines) }.not_to raise_error

# 205
success = Net::HTTPSuccess.new(1.0, 205, 'OK')
allow(driver.instance).to receive(:loki_http_request) { success }
allow(success).to receive(:body).and_return('fake body')
expect { driver.instance.write(lines) }.not_to raise_error

# 429
too_many_requests = Net::HTTPTooManyRequests.new(1.0, 429, 'OK')
allow(driver.instance).to receive(:loki_http_request) { too_many_requests }
allow(too_many_requests).to receive(:body).and_return('fake body')
expect { driver.instance.write(lines) }.to raise_error(described_class::LogPostError)

# 505
server_error = Net::HTTPServerError.new(1.0, 505, 'OK')
allow(driver.instance).to receive(:loki_http_request) { server_error }
allow(server_error).to receive(:body).and_return('fake body')
expect { driver.instance.write(lines) }.to raise_error(described_class::LogPostError)
end
end