class LogStash::Outputs::Librato

Public Instance Methods

receive(event) click to toggle source
# File lib/logstash/outputs/librato.rb, line 72
def receive(event)
  # TODO (lusis)
  # batch and flush
  return unless output?(event)

  metrics_event = Hash.new
  unless @gauge.size == 0
    g_hash = Hash[*@gauge.collect{|k,v| [k,event.sprintf(v)]}.flatten]
    g_hash.each do |k,v|
      g_hash[k] = v.to_f if k=="value"
    end
    g_hash['measure_time'] = event["@timestamp"].to_i unless g_hash['measure_time']
    @logger.warn("Gauges hash", :data => g_hash)
    metrics_event['gauges'] = Array.new 
    metrics_event['gauges'] << g_hash
    @logger.warn("Metrics hash", :data => metrics_event)
  end
  unless @counter.size == 0
    c_hash = Hash[*@counter.collect{|k,v| [k,event.sprintf(v)]}.flatten]
    c_hash.each do |k,v|
      c_hash[k] = v.to_f if k=="value"
    end
    c_hash['measure_time'] = event["@timestamp"].to_i unless c_hash['measure_time']
    @logger.warn("Counters hash", :data => c_hash)
    metrics_event['counters'] = Array.new
    metrics_event['counters'] << c_hash
    @logger.warn("Metrics hash", :data => metrics_event)
  end
 
  # TODO (lusis)
  # Clean this mess up
  unless metrics_event.size == 0
    request = Net::HTTP::Post.new(@uri.path+"metrics")
    request.basic_auth(@account_id, @api_token)
    
    begin
      request.body = metrics_event.to_json
      request.add_field("Content-Type", 'application/json')
      response = @client.request(request)
      @logger.warn("Librato convo", :request => request.inspect, :response => response.inspect)
      raise unless response.code == '200'
    rescue Exception => e
      @logger.warn("Unhandled exception", :request => request.inspect, :response => response.inspect, :exception => e.inspect)
    end
  end

  unless @annotation.size == 0
    annotation_hash = Hash.new
    annotation_hash['annotations'] = Array.new
    @logger.warn("Original Annotation", :data => @annotation)
    annotation_event = Hash[*@annotation.collect{|k,v| [event.sprintf(k),event.sprintf(v)]}.flatten]
    @logger.warn("Annotation event", :data => annotation_event)
    
    annotation_path = "#{@uri.path}annotations/#{annotation_event['name']}"
    @logger.warn("Annotation path", :data => annotation_path)
    request = Net::HTTP::Post.new(annotation_path)
    request.basic_auth(@account_id, @api_token)
    annotation_event.delete('name')
    annotation_event['start_time'] = event["@timestamp"].to_i unless annotation_event['start_time']
    annotation_event['end_time'] = event["@timestamp"].to_i unless annotation_event['end_time']
    annotation_hash['annotations'] << annotation_event
    @logger.warn("Annotation event", :data => annotation_event)

    begin
      request.body = annotation_event.to_json
      request.add_field("Content-Type", 'application/json')
      response = @client.request(request)
      @logger.warn("Librato convo", :request => request.inspect, :response => response.inspect)
      raise unless response.code == '201'
    rescue Exception => e
      @logger.warn("Unhandled exception", :request => request.inspect, :response => response.inspect, :exception => e.inspect)
    end
  end
end
register() click to toggle source
# File lib/logstash/outputs/librato.rb, line 60
def register
  require "net/https"
  require "uri"
  @url = "https://metrics-api.librato.com/v1/"
  @uri = URI.parse(@url)
  @client = Net::HTTP.new(@uri.host, @uri.port)
  @client.use_ssl = true
  @client.verify_mode = OpenSSL::SSL::VERIFY_NONE
  
end