=begin

  Copyright (C) 2012 Patrik Antonsson

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
  under the License.

=end

class LogglyOutputBuffred < Fluent::BufferedOutput
  class ConnectionFailureError < StandardError; end
  Fluent::Plugin.register_output('loggly_buffered', self)
  config_param :loggly_url, :string, :default => nil
  config_param :output_include_time, :bool, :default => true  # Recommended
  config_param :time_precision_digits, :integer, :default => 0
  # overriding default flush_interval (60 sec) from Fluent::BufferedOutput
  config_param :flush_interval, :time, default: 1

  unless method_defined?(:log)
    define_method("log") { $log }
  end

  def configure(conf)
    super
    $log.debug "Configured loggly url: #{@loggly_url}"
  end

  def start
    super
    require 'net/http/persistent'
    @http = Net::HTTP::Persistent.new 'fluentd-plugin-loggly', :ENV
    @http.headers['Content-Type'] = 'text'
  end

  def pick_uri(record)
    # if kubernetes pod has loggly url as annotation, use it
    if record.dig('kubernetes', 'annotations', 'solarwinds_io/loggly_url')
      url = record['kubernetes']['annotations']['solarwinds_io/loggly_url']
      # else if kubernetes namespace has papertrail destination as annotation, use it
    elsif record.dig('kubernetes', 'namespace_annotations', 'solarwinds_io/loggly_url')
      url = record['kubernetes']['namespace_annotations']['solarwinds_io/loggly_url']
      # else use pre-configured destination
    else
      url = @loggly_url
    end
    URI url
  end

  def shutdown
    super
  end

  def format(tag, time, record)
    if time.is_a?(Integer)
      [tag, time, record].to_msgpack
    else
      Fluent::Engine.msgpack_factory.packer.write([tag, time, record]).to_s
    end
  end

  def write(chunk)
    chunk.msgpack_each {|tag,time,record|
      records = []
      record['timestamp'] ||= Time.at(time).iso8601(@time_precision_digits) if @output_include_time
      records.push(Yajl::Encoder.encode(record))
      uri = pick_uri(record)
      $log.debug "#{records.length} records sent"
      post = Net::HTTP::Post.new uri.path
      post.body = records.join("\n")
      begin
        response = @http.request uri, post
        $log.debug "HTTP Response code #{response.code}"
        if response.code != '200'
          err_msg = "Received HTTP #{response.code} from #{uri}: #{response.body}"
          raise ConnectionFailureError, err_msg
        end
      rescue => e
        err_msg = "Error posting to #{uri}: #{e}"
        # reset http client here
        $log.error "Recreating Net::HTTP::Persistent instance due to #{e.class}"
        @http = Net::HTTP::Persistent.new 'fluentd-plugin-loggly', :ENV
        @http.headers['Content-Type'] = 'text'
        raise ConnectionFailureError, err_msg, e.backtrace
      end
    }

  end
end