Skip to content

Commit

Permalink
monkey patch twitter gem to trap json parsing exceptions and use our …
Browse files Browse the repository at this point in the history
…own json parser

Fixes elastic#1450
  • Loading branch information
colinsurprenant authored and jordansissel committed Sep 27, 2014
1 parent 10ac156 commit a78f6ba
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/logstash/inputs/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "logstash/namespace"
require "logstash/timestamp"
require "logstash/util"
require "logstash/json"

# Read events from the twitter streaming api.
class LogStash::Inputs::Twitter < LogStash::Inputs::Base
Expand Down Expand Up @@ -54,6 +55,24 @@ class LogStash::Inputs::Twitter < LogStash::Inputs::Base
public
def register
require "twitter"

# monkey patch twitter gem to ignore json parsing error.
# at the same time, use our own json parser
# this has been tested with a specific gem version, raise if not the same
raise("Invalid Twitter gem") unless Twitter::Version.to_s == "5.0.0.rc.1"
Twitter::Streaming::Response.module_eval do
def on_body(data)
@tokenizer.extract(data).each do |line|
next if line.empty?
begin
@block.call(LogStash::Json.load(line, :symbolize_keys => true))
rescue LogStash::Json::ParserError
# silently ignore json parsing errors
end
end
end
end

@client = Twitter::Streaming::Client.new do |c|
c.consumer_key = @consumer_key
c.consumer_secret = @consumer_secret.value
Expand Down

0 comments on commit a78f6ba

Please sign in to comment.