Class: LogStash::Filters::CacheRedis

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/filters/cache_redis.rb

Instance Method Summary collapse

Instance Method Details

#filter(event) ⇒ Object



89
90
91
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
135
136
137
138
139
140
141
142
# File 'lib/logstash/filters/cache_redis.rb', line 89

def filter(event)

    # TODO: Maybe refactor the interface into a more flexible one with two
    #       main configs 'cmd' & 'args'. Then it would be possible to eliminate
    #       all if clauses and replace it through one hashmap call, where
    #       the hashmap would be a mapping from 'cmd' -> <cmd_function_ref>
    #       E.q.: cmds.fetch(event.get(@llen), &method(:cmd_not_found_err))

    begin
        @redis ||= connect

        if @llen
            event.set(@target, @redis.llen(event.get(@llen)))
        end

        if @rpush
            @redis.rpush(event.get(@rpush), event.get(@source))
        end

        if @rpushnx
            key = event.get(@rpushnx)
            begin
                @lock_manager ||= connect_lockmanager
                @lock_manager.lock!("lock_#{key}", @lock_timeout) do
                    @redis.rpush(key, event.get(@source)) unless @redis.exists(key)
                end
            rescue Redlock::LockError => e
                @logger.warn("Failed to lock section 'rpushnx' for key: #{key}",
                             :event => event, :exception => e)
                @lock_manager = nil
                retry
            end
        end

        if @rpop
            event.set(@target, @redis.rpop(event.get(@rpop)))
        end

        if @lget
            event.set(@target, @redis.lrange(event.get(@lget), 0, -1))
        end

    rescue => e
        @logger.warn("Failed to send event to Redis", :event => event,
                     :exception => e, :backtrace => e.backtrace)
        sleep @reconnect_interval
        @redis = nil
        @lock_manager = nil
        retry
    end

    # filter_matched should go in the last line of our successful code
    filter_matched(event)
end

#registerObject



79
80
81
82
83
84
85
86
# File 'lib/logstash/filters/cache_redis.rb', line 79

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