Class: RedisLogstash::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_logstash/socket.rb

Constant Summary collapse

@@socket =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSocket

Returns a new instance of Socket.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/redis_logstash/socket.rb', line 19

def initialize
  self.options = ParseConfig.get[:redis]

  host = options[:host] || '127.0.0.1'
  port = options[:port] || 6379
  password = options[:password] || nil

  self.redis_key = options[:key] || 'logstash'
  self.redis = password ? ::Redis.new(host: host, port: port, password: password) : ::Redis.new(host: host, port: port)

end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/redis_logstash/socket.rb', line 6

def options
  @options
end

#redisObject

Returns the value of attribute redis.



6
7
8
# File 'lib/redis_logstash/socket.rb', line 6

def redis
  @redis
end

#redis_keyObject

Returns the value of attribute redis_key.



6
7
8
# File 'lib/redis_logstash/socket.rb', line 6

def redis_key
  @redis_key
end

Class Method Details

.getObject



9
10
11
# File 'lib/redis_logstash/socket.rb', line 9

def get
  @@socket ||= new
end

.write(json) ⇒ Object



13
14
15
# File 'lib/redis_logstash/socket.rb', line 13

def write(json)
  get.push(json)
end

Instance Method Details

#push(json) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/redis_logstash/socket.rb', line 31

def push(json)
  begin
    unless redis.rpush(redis_key, Logger.gzip({type: options[:type], logs: json.to_json}.to_json))
      raise "could not send event to redis"
    end
  rescue ::Redis::InheritedError
    redis.client.connect
    retry
  end
end