Skip to content

Commit

Permalink
add URI validation (#1603)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarokkk authored Jan 29, 2020
1 parent 0adad82 commit c793d7d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
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.4.0'
gem 'fluentd', '1.9.0'
gem 'rubocop-rspec'
gem 'simplecov', require: false, group: :test
gem 'test-unit'
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.7'
spec.version = '1.2.8'
spec.authors = %w[woodsaj briangann cyriltovena]
spec.email = ['[email protected]', '[email protected]', '[email protected]']

Expand All @@ -28,5 +28,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake', '~> 12.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_runtime_dependency 'fluentd', ['>= 0.14.10', '< 2']
spec.add_runtime_dependency 'fluentd', ['>=1.9.0', '< 2']
end
14 changes: 8 additions & 6 deletions fluentd/fluent-plugin-grafana-loki/lib/fluent/plugin/out_loki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

require 'fluent/plugin/output'
require 'net/http'
require 'uri'
require 'yajl'
require 'time'

Expand Down Expand Up @@ -73,6 +72,10 @@ class LokiOutput < Fluent::Plugin::Output # rubocop:disable Metrics/ClassLength
def configure(conf)
compat_parameters_convert(conf, :buffer)
super
@uri = URI.parse(@url + '/loki/api/v1/push')
unless @uri.kind_of?(URI::HTTP) or @uri.kind_of?(URI::HTTPS)
raise Fluent::ConfigError, "url parameter must be valid HTTP"
end
@record_accessors = {}
conf.elements.select { |element| element.name == 'label' }.each do |element|
element.each_pair do |k, v|
Expand Down Expand Up @@ -127,27 +130,26 @@ def write(chunk)
body = { 'streams' => payload }

# add ingest path to loki url
uri = URI.parse(url + '/loki/api/v1/push')

req = Net::HTTP::Post.new(
uri.request_uri
@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)
opts = ssl_opts(@uri)

log.debug "sending #{req.body.length} bytes to loki"
res = Net::HTTP.start(uri.hostname, uri.port, **opts) { |http| http.request(req) }
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.warn "failed to #{req.method} #{@uri} (#{res_summary})"
log.warn Yajl.dump(body)

end
Expand Down

0 comments on commit c793d7d

Please sign in to comment.