Class: Semlog::RabbitNotifier

Inherits:
Object
  • Object
show all
Defined in:
lib/semlog/rabbit_notifier.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:, vhost:, exchange_name:, user:, pw:) ⇒ RabbitNotifier

Returns a new instance of RabbitNotifier.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/semlog/rabbit_notifier.rb', line 14

def initialize host:, port:, vhost:, exchange_name:, user:, pw:
  @connection = nil
  @host = host
  @port = port
  @vhost = vhost
  @exhange_name = exchange_name
  @user = user
  @pw = pw

  $logger.debug "RabbitNotifier>host: #{@host} port #{@port} vhost #{@vhost} "

  @default_options = {}
  @default_options['version'] = "1.1"
  @default_options['host'] ||= Socket.gethostname
  @default_options['level'] ||= Semlog::UNKNOWN
  @default_options['facility'] ||= 'RabbitNotifier'

end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



12
13
14
# File 'lib/semlog/rabbit_notifier.rb', line 12

def connection
  @connection
end

#default_optionsObject

Returns the value of attribute default_options.



12
13
14
# File 'lib/semlog/rabbit_notifier.rb', line 12

def default_options
  @default_options
end

#exhange_nameObject

Returns the value of attribute exhange_name.



12
13
14
# File 'lib/semlog/rabbit_notifier.rb', line 12

def exhange_name
  @exhange_name
end

#hostObject

Returns the value of attribute host.



12
13
14
# File 'lib/semlog/rabbit_notifier.rb', line 12

def host
  @host
end

#portObject

Returns the value of attribute port.



12
13
14
# File 'lib/semlog/rabbit_notifier.rb', line 12

def port
  @port
end

#pwObject

Returns the value of attribute pw.



12
13
14
# File 'lib/semlog/rabbit_notifier.rb', line 12

def pw
  @pw
end

#userObject

Returns the value of attribute user.



12
13
14
# File 'lib/semlog/rabbit_notifier.rb', line 12

def user
  @user
end

#vhostObject

Returns the value of attribute vhost.



12
13
14
# File 'lib/semlog/rabbit_notifier.rb', line 12

def vhost
  @vhost
end

Instance Method Details

#channelObject



68
69
70
71
# File 'lib/semlog/rabbit_notifier.rb', line 68

def channel
  connect
  @channel ||= @connection.create_channel
end

#connectObject



33
34
35
36
37
38
39
# File 'lib/semlog/rabbit_notifier.rb', line 33

def connect
  unless @connection
    @connection = Bunny.new(:host => @host, :vhost => @vhost, :user => @user, :password => @pw)
    @connection.start
  end
  @connection
end

#data_to_json(data) ⇒ Object



60
61
62
# File 'lib/semlog/rabbit_notifier.rb', line 60

def data_to_json(data)
  Oj.dump(gelfify(data))
end

#exchangeObject



73
74
75
# File 'lib/semlog/rabbit_notifier.rb', line 73

def exchange
  @exchange ||= channel.fanout(exhange_name, durable: true)
end

#gelfify(data) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/semlog/rabbit_notifier.rb', line 41

def gelfify(data)
  gdata = @default_options.dup
  data.keys.each do |key|
    value, key_s = data[key], key.to_s
    if ['host', 'level', 'version', 'short_message', 'full_message', 'timestamp', 'facility', 'line', 'file'].index(key_s)
      gdata[key_s] = value
    elsif key_s == 'action'
      gdata["_application_action"] = value
    elsif key_s == 'id'
      gdata["_application_id"] = value
    elsif key_s[0] != '_'
      gdata["_#{key_s}"] = value
    else
      gdata[key_s] = value
    end
  end
  gdata
end

#notify!(data) ⇒ Object



64
65
66
# File 'lib/semlog/rabbit_notifier.rb', line 64

def notify!(data)
  exchange.publish(data_to_json(data))
end