Class: LogStash::Outputs::Librato

Inherits:
Base show all
Defined in:
lib/logstash/outputs/librato.rb

Constant Summary

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#handle, #handle_worker, #initialize, #worker_setup, #workers_not_supported

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Outputs::Base

Instance Method Details

#receive(event) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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

#registerObject



60
61
62
63
64
65
66
67
68
69
# 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