Skip to content

Commit

Permalink
Separated out the building/sending of events
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
jhitze authored and jordansissel committed Aug 4, 2015
1 parent 3103223 commit 7ffb0c5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
22 changes: 16 additions & 6 deletions lib/logstash/outputs/riemann.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ def map_fields(parent, fields)
def receive(event)
return unless output?(event)

r_event = build_reimann_formatted_event(event)

@logger.debug("Riemann event: ", :riemann_event => r_event)
send_to_riemann(r_event)
end # def receive

def build_reimann_formatted_event(event)
# Let's build us an event, shall we?
r_event = Hash.new
r_event[:host] = event.sprintf(@sender)
Expand All @@ -141,18 +148,21 @@ def receive(event)
end
end
if @map_fields == true
@my_event = Hash.new
map_fields(nil, event.to_hash)
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
12 changes: 11 additions & 1 deletion spec/outputs/riemann_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@
end
end
end
end

context "build_reimann_formatted_event" do
it "will return symboled hash with at least :host, :time, and :description" do
data = {"message"=>"hello", "node_info" => {"name" => "node1", "status" => "up"}, "@version"=>"1", "@timestamp"=>"2015-06-03T23:34:54.076Z", "host"=>"vagrant-ubuntu-trusty-64"}
expected_data = {:time=>1433374494,:message =>"hello", :description =>"hello", :host =>"vagrant-ubuntu-trusty-64", :"node_info.name" => "node1", :"node_info.status" => "up"}
event = LogStash::Event.new data
output = LogStash::Plugin.lookup("output", "riemann").new("map_fields" => "true")
expect(output.build_reimann_formatted_event(event)).to eq expected_data
end
end
end

0 comments on commit 7ffb0c5

Please sign in to comment.