Class: LogStash::Outputs::Librato

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

Instance Method Summary collapse

Instance Method Details

#receive(event) ⇒ Object



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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/logstash/outputs/librato.rb', line 104

def receive(event)
  # TODO (lusis)
  # batch and flush

  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 = LogStash::Json.dump(metrics_event)
      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 = LogStash::Json.dump(annotation_event)
      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



92
93
94
95
96
97
98
99
100
101
# File 'lib/logstash/outputs/librato.rb', line 92

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