Class: LogStash::Outputs::Redis

Inherits:
Base show all
Includes:
Stud::Buffer
Defined in:
lib/logstash/outputs/redis.rb

Overview

send events to a redis database using RPUSH

For more information about redis, see <redis.io/>

Constant Summary

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#handle, #handle_worker, #initialize, #worker_setup, #workers_not_supported

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Outputs::Base

Instance Method Details

#congestion_check(key) ⇒ Object

def receive



177
178
179
180
181
182
183
184
185
186
# File 'lib/logstash/outputs/redis.rb', line 177

def congestion_check(key)
  return if @congestion_threshold == 0
  if (Time.now.to_i - @congestion_check_times[key]) >= @congestion_interval # Check congestion only if enough time has passed since last check.
    while @redis.llen(key) > @congestion_threshold # Don't push event to redis key which has reached @congestion_threshold.
      @logger.warn? and @logger.warn("Redis key size has hit a congestion threshold #{@congestion_threshold} suspending output for #{@congestion_interval} seconds")
      sleep @congestion_interval
    end
    @congestion_check_time = Time.now.to_i
  end
end

#flush(events, key, teardown = false) ⇒ Object

called from Stud::Buffer#buffer_flush when there are events to flush



189
190
191
192
193
194
195
# File 'lib/logstash/outputs/redis.rb', line 189

def flush(events, key, teardown=false)
  @redis ||= connect
  # we should not block due to congestion on teardown
  # to support this Stud::Buffer#buffer_flush should pass here the :final boolean value.
  congestion_check(key) unless teardown
  @redis.rpush(key, events)
end

#on_flush_error(e) ⇒ Object

called from Stud::Buffer#buffer_flush when an error occurs



197
198
199
200
201
202
203
204
# File 'lib/logstash/outputs/redis.rb', line 197

def on_flush_error(e)
  @logger.warn("Failed to send backlog of events to redis",
    :identity => identity,
    :exception => e,
    :backtrace => e.backtrace
  )
  @redis = connect
end

#receive(event) ⇒ Object

def register



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
# File 'lib/logstash/outputs/redis.rb', line 136

def receive(event)
  return unless output?(event)

  if @batch and @data_type == 'list' # Don't use batched method for pubsub.
    # Stud::Buffer
    buffer_receive(event.to_json, event.sprintf(@key))
    return
  end

  key = event.sprintf(@key)
  # TODO(sissel): We really should not drop an event, but historically
  # we have dropped events that fail to be converted to json.
  # TODO(sissel): Find a way to continue passing events through even
  # if they fail to convert properly.
  begin
    payload = event.to_json
  rescue Encoding::UndefinedConversionError, ArgumentError
    puts "FAILUREENCODING"
    @logger.error("Failed to convert event to JSON. Invalid UTF-8, maybe?",
                  :event => event.inspect)
    return
  end

  begin
    @redis ||= connect
    if @data_type == 'list'
      congestion_check(key)
      @redis.rpush(key, payload)
    else
      @redis.publish(key, payload)
    end
  rescue => e
    @logger.warn("Failed to send event to redis", :event => event,
                 :identity => identity, :exception => e,
                 :backtrace => e.backtrace)
    sleep @reconnect_interval
    @redis = nil
    retry
  end
end

#registerObject



92
93
94
95
96
97
98
99
100
101
102
103
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
# File 'lib/logstash/outputs/redis.rb', line 92

def register
  require 'redis'

  # TODO remove after setting key and data_type to true
  if @queue
    if @key or @data_type
      raise RuntimeError.new(
        "Cannot specify queue parameter and key or data_type"
      )
    end
    @key = @queue
    @data_type = 'list'
  end

  if not @key or not @data_type
    raise RuntimeError.new(
      "Must define queue, or key and data_type parameters"
    )
  end
  # end TODO


  if @batch
    if @data_type != "list"
      raise RuntimeError.new(
        "batch is not supported with data_type #{@data_type}"
      )
    end
    buffer_initialize(
      :max_items => @batch_events,
      :max_interval => @batch_timeout,
      :logger => @logger
    )
  end

  @redis = nil
  if @shuffle_hosts
      @host.shuffle!
  end
  @host_idx = 0

  @congestion_check_times = Hash.new { |h,k| h[k] = Time.now.to_i - @congestion_interval }
end

#teardownObject



206
207
208
209
210
211
212
213
214
# File 'lib/logstash/outputs/redis.rb', line 206

def teardown
  if @batch
    buffer_flush(:final => true)
  end
  if @data_type == 'channel' and @redis
    @redis.quit
    @redis = nil
  end
end