lib/fluent/plugin/out_newrelic.rb in fluent-plugin-newrelic-1.0.1 vs lib/fluent/plugin/out_newrelic.rb in fluent-plugin-newrelic-1.1.1

- old
+ new

@@ -25,16 +25,13 @@ class ConnectionFailure < StandardError end Fluent::Plugin.register_output('newrelic', self) helpers :thread - config_param :api_key, :string + config_param :api_key, :string, :default => nil config_param :base_uri, :string, :default => "https://log-api.newrelic.com/log/v1" - config_param :retry_seconds, :integer, :default => 5 - config_param :max_delay, :integer, :default => 30 - config_param :retries, :integer, :default => 5 - config_param :concurrent_requests, :integer, :default => 1 + config_param :license_key, :string, :default => nil DEFAULT_BUFFER_TYPE = 'memory'.freeze config_section :buffer do config_set_default :@type, DEFAULT_BUFFER_TYPE @@ -49,18 +46,25 @@ true end def configure(conf) super + if @api_key.nil? && @license_key.nil? + raise Fluent::ConfigError.new("'api_key' or `license_key` parameter is required") + end # create initial sockets hash and socket based on config param @end_point = URI.parse(@base_uri) + auth = { + @api_key.nil? ? 'X-License_key' : 'X-Insert-Key' => + @api_key.nil? ? @license_key : @api_key + } @header = { - 'X-Insert-Key' => @api_key, 'X-Event-Source' => 'logs', 'Content-Encoding' => 'gzip' - }.freeze + }.merge(auth) + .freeze end def package_record(record, timestamp) packaged = { 'timestamp' => timestamp, @@ -104,47 +108,25 @@ end io = StringIO.new gzip = Zlib::GzipWriter.new(io) gzip << [payload].to_json gzip.close - attempt_send(io.string, 0) + send(io.string) end - - def should_retry?(attempt) - attempt < @retries - end - - def was_successful?(response) - 200 <= response.code.to_i && response.code.to_i < 300 - end - - def sleep_duration(attempt) - if attempt == 0 - return 0 + + def handle_response(response) + if !(200 <= response.code.to_i && response.code.to_i < 300) + log.error("Response was " + response.code + " " + response.body) end - - [@max_delay, (2 ** (attempt - 1)) * @retry_seconds].min end - def sleep_on_retry(attempt) - duration = sleep_duration(attempt) - if duration > 0 - sleep duration - end - end - - def attempt_send(payload, attempt) - sleep_on_retry(attempt) - attempt_send(payload, attempt + 1) unless was_successful?(send(payload)) if should_retry?(attempt) - end - def send(payload) http = Net::HTTP.new(@end_point.host, 443) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER request = Net::HTTP::Post.new(@end_point.request_uri, @header) request.body = payload - http.request(request) + handle_response(http.request(request)) end end end end