lib/logstash/outputs/riemann.rb in logstash-output-riemann-0.1.4 vs lib/logstash/outputs/riemann.rb in logstash-output-riemann-0.2.0
- old
+ new
@@ -105,27 +105,34 @@
@client = Riemann::Client.new(:host => @host, :port => @port)
end # def register
public
def map_fields(parent, fields)
- fields.each {|key, val|
- if !key.start_with?("@")
- field = parent.nil? ? key : parent + '.' + key
- contents = val
- if contents.is_a?(Hash)
- map_fields(field, contents)
- else
- @my_event[field.to_sym] = contents
- end
+ this_level = Hash.new
+ fields.each do |key, contents|
+ next if key.start_with?("@")
+ field = parent.nil? ? key : "#{parent}.#{key}"
+ if contents.is_a?(Hash)
+ this_level.merge! map_fields(field, contents)
+ else
+ this_level[field.to_sym] = contents
end
- }
+ end
+ return this_level
end
public
def receive(event)
return unless output?(event)
+ r_event = build_riemann_formatted_event(event)
+
+ @logger.debug("Riemann event: ", :riemann_event => r_event)
+ send_to_riemann(r_event)
+ end # def receive
+
+ def build_riemann_formatted_event(event)
# Let's build us an event, shall we?
r_event = Hash.new
r_event[:host] = event.sprintf(@sender)
# riemann doesn't handle floats so we reduce the precision here
r_event[:time] = event["@timestamp"].to_i
@@ -138,20 +145,23 @@
r_event[key.to_sym] = event.sprintf(val)
end
end
end
if @map_fields == true
- @my_event = Hash.new
- map_fields(nil, event)
- r_event.merge!(@my_event) {|key, val1, val2| val1}
+ r_event.merge! map_fields(nil, event.to_hash)
end
r_event[:tags] = event["tags"] if event["tags"].is_a?(Array)
- @logger.debug("Riemann event: ", :riemann_event => r_event)
+
+ return r_event
+ end
+
+ def send_to_riemann(riemann_formatted_event)
begin
proto_client = @client.instance_variable_get("@#{@protocol}")
@logger.debug("Riemann client proto: #{proto_client.to_s}")
- proto_client << r_event
+ proto_client << riemann_formatted_event
rescue Exception => e
@logger.debug("Unhandled exception", :error => e)
end
- end # def receive
+ end # def send_to_riemann
+
end # class LogStash::Outputs::Riemann