class LogStash::Outputs::HoneycombJSONBatch

Constants

VERSION

Public Instance Methods

close() click to toggle source
# File lib/logstash/outputs/honeycomb_json_batch.rb, line 51
def close
  client.close
end
multi_receive(events) click to toggle source
# File lib/logstash/outputs/honeycomb_json_batch.rb, line 55
def multi_receive(events)
  events.each_slice(@flush_size) do |chunk|
    documents = []
    chunk.each do |event|
      data = event.to_hash()
      timestamp = data.delete("@timestamp")
      doc = { "time" => timestamp, "data" => data }
      if samplerate = data.delete("@samplerate")
        doc["samplerate"] = samplerate.to_i
      end
      documents.push(doc)
    end
    make_request(documents)
  end
end
register() click to toggle source
# File lib/logstash/outputs/honeycomb_json_batch.rb, line 33
def register
  @total = 0
  @total_failed = 0
  if @api_host.nil?
    @api_host = "https://api.honeycomb.io"
  elsif !@api_host.start_with? "http"
    @api_host = "http://#{ @api_host }"
  end
  @api_host = @api_host.chomp

  @dataset = URI::encode(@dataset)

  logger.info("Initialized honeycomb_json_batch with settings",
    :api_host => @api_host,
    :headers => request_headers,
    :retry_individual => @retry_individual)
end

Private Instance Methods

log_failure(message, opts) click to toggle source

This is split into a separate method mostly to help testing

# File lib/logstash/outputs/honeycomb_json_batch.rb, line 145
def log_failure(message, opts)
  @logger.error("[Honeycomb Batch Output Failure] #{message}", opts)
end
make_request(documents) click to toggle source
# File lib/logstash/outputs/honeycomb_json_batch.rb, line 73
def make_request(documents)
  body = LogStash::Json.dump(documents)

  url = "#{@api_host}/1/batch/#{@dataset}"
  request = client.post(url, {
    :body => body,
    :headers => request_headers
  })

  request.on_success do |response|
    if response.code >= 200 && response.code < 300
      @total = @total + documents.length
      @logger.debug("Successfully submitted batch",
        :num_docs => documents.length,
        :response_code => response.code,
        :total => @total,
        :thread_id => Thread.current.object_id,
        :time => Time::now.utc)
    else
      if documents.length > 1 && @retry_individual
        if statuses = JSON.parse(response.body).values.first
          statuses.each_with_index do |status, i|
            code = status["status"]
            if code == nil
              @logger.warn("Status code missing in response: #{status}")
              next
            elsif code >= 200 && code < 300
              next
            end
            make_request([documents[i]])
          end
        end
      else
        @total_failed += documents.length
        log_failure(
            "Encountered non-200 HTTP code #{response.code}",
            :response_code => response.code,
            :url => url,
            :response_body => response.body,
            :num_docs => documents.length,
            :retry_individual => @retry_individual,
            :total_failed => @total_failed)
      end
    end
  end

  request.on_failure do |exception|
    @total_failed += documents.length
    log_failure("Could not access URL",
      :url => url,
      :method => @http_method,
      :body => body,
      :headers => request_headers,
      :message => exception.message,
      :class => exception.class.name,
      :backtrace => exception.backtrace,
      :total_failed => @total_failed
    )
  end

  @logger.debug("Submitting batch",
        :num_docs => documents.length,
        :total => @total,
        :thread_id => Thread.current.object_id,
        :time => Time::now.utc)
  request.call

rescue Exception => e
  log_failure("Got totally unexpected exception #{e.message}", :docs => documents.length)
end
request_headers() click to toggle source
# File lib/logstash/outputs/honeycomb_json_batch.rb, line 149
def request_headers()
  {
    "Content-Type" => "application/json",
    "X-Honeycomb-Team" => @write_key,
    "X-Plugin-Version" => VERSION
  }
end