Class: LogStash::Inputs::Twitter

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/inputs/twitter.rb

Overview

Ingest events from the Twitter Streaming API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#event_generation_error_countObject (readonly)

Returns the value of attribute event_generation_error_count.



14
15
16
# File 'lib/logstash/inputs/twitter.rb', line 14

def event_generation_error_count
  @event_generation_error_count
end

#filter_optionsObject (readonly)

Returns the value of attribute filter_options.



14
15
16
# File 'lib/logstash/inputs/twitter.rb', line 14

def filter_options
  @filter_options
end

Instance Method Details

#registerObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/logstash/inputs/twitter.rb', line 107

def register
  if !@use_samples && ( @keywords.nil? && @follows.nil? && @locations.nil? )
    raise LogStash::ConfigurationError.new("At least one parameter (follows, locations or keywords) must be specified.")
  end

  # 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

  LogStash::Inputs::TwitterPatches.patch

  @rest_client     = Twitter::REST::Client.new       { |c|  configure(c) }
  @stream_client   = Twitter::Streaming::Client.new  { |c|  configure(c) }
  @twitter_options = build_options
end

#run(queue) ⇒ Object



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
# File 'lib/logstash/inputs/twitter.rb', line 123

def run(queue)
  @logger.info("Starting twitter tracking", twitter_options.clone) # need to pass a clone as it modify this var otherwise

  # keep track of the amount of non-specific errors rescued and logged - use in testing to verify no errors.
  # this is because, as yet, we can't have rspec expectations on the logger instance.
  @event_generation_error_count = 0

  begin
    if @use_samples
      @stream_client.sample do |tweet|
        return if stop?
        tweet_processor(queue, tweet)
      end
    else
      @stream_client.filter(twitter_options) do |tweet|
        return if stop?
        tweet_processor(queue, tweet)
      end
    end
  rescue Twitter::Error::TooManyRequests => e
    sleep_for = e.rate_limit.reset_in || @rate_limit_reset_in # 5 minutes default value from config
    @logger.warn("Twitter too many requests error, sleeping for #{sleep_for}s")
    Stud.stoppable_sleep(sleep_for) { stop? }
    retry
  rescue => e
    # if a lot of these errors begin to occur, the repeated retry will result in TooManyRequests errors trapped above.
    @logger.warn("Twitter client error", :message => e.message, :exception => e.class, :backtrace => e.backtrace, :options => @filter_options)
    retry
  end
end

#set_stream_client(client) ⇒ Object



162
163
164
# File 'lib/logstash/inputs/twitter.rb', line 162

def set_stream_client(client)
  @stream_client = client
end

#stopObject

def run



154
155
156
# File 'lib/logstash/inputs/twitter.rb', line 154

def stop
  @stream_client = nil
end

#twitter_optionsObject



158
159
160
# File 'lib/logstash/inputs/twitter.rb', line 158

def twitter_options
  @twitter_options
end