Module: LogStash::Outputs::RabbitMQ::BunnyImpl

Included in:
LogStash::Outputs::RabbitMQ
Defined in:
lib/logstash/outputs/rabbitmq/bunny.rb

Instance Method Summary collapse

Instance Method Details

#connectObject

Implementation



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 72

def connect
  @vhost       ||= Bunny::DEFAULT_HOST
  # 5672. Will be switched to 5671 by Bunny if TLS is enabled.
  @port        ||= AMQ::Protocol::DEFAULT_PORT
  @routing_key ||= "#"

  @settings = {
    :vhost => @vhost,
    :host  => @host,
    :port  => @port,
    :automatically_recover => false
  }
  @settings[:user]      = @user || Bunny::DEFAULT_USER
  @settings[:pass]      = if @password
                            @password.value
                          else
                            Bunny::DEFAULT_PASSWORD
                          end

  @settings[:log_level] = if @debug
                            :debug
                          else
                            :error
                          end

  @settings[:tls]        = @ssl if @ssl
  @settings[:verify_ssl] = @verify_ssl if @verify_ssl

  proto                  = if @ssl
                             "amqp"
                           else
                             "amqps"
                           end
  @connection_url        = "#{proto}://#{@user}@#{@host}:#{@port}#{vhost}/#{@queue}"

  begin
    @conn = Bunny.new(@settings)

    @logger.debug("Connecting to RabbitMQ. Settings: #{@settings.inspect}, queue: #{@queue.inspect}")
    return if terminating?
    @conn.start

    @ch = @conn.create_channel
    @logger.info("Connected to RabbitMQ at #{@settings[:host]}")
  rescue Bunny::NetworkFailure, Bunny::ConnectionClosedError, Bunny::ConnectionLevelException, Bunny::TCPConnectionFailed => e
    n = Bunny::Session::DEFAULT_NETWORK_RECOVERY_INTERVAL * 2

    @logger.error("RabbitMQ connection error: #{e.message}. Will attempt to reconnect in #{n} seconds...",
                  :exception => e,
                  :backtrace => e.backtrace)
    return if terminating?

    sleep n
    retry
  end
end

#declare_exchangeObject



129
130
131
132
133
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 129

def declare_exchange
  @logger.debug("Declaring an exchange", :name => @exchange, :type => @exchange_type,
                :durable => @durable)
  @x = @ch.exchange(@exchange, :type => @exchange_type.to_sym, :durable => @durable)
end

#publish_serialized(message, key = @key) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 33

def publish_serialized(message, key = @key)
  begin
    if @x
      @x.publish(message, :persistent => @persistent, :routing_key => key)
    else
      @logger.warn("Tried to send a message, but not connected to RabbitMQ yet.")
    end
  rescue Bunny::NetworkFailure, Bunny::ConnectionClosedError, Bunny::ConnectionLevelException, Bunny::TCPConnectionFailed => e
    n = Bunny::Session::DEFAULT_NETWORK_RECOVERY_INTERVAL * 2

    @logger.error("RabbitMQ connection error: #{e.message}. Will attempt to reconnect in #{n} seconds...",
                  :exception => e,
                  :backtrace => e.backtrace)
    return if terminating?

    sleep n
    connect
    declare_exchange
    retry
  end
end

#receive(event) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 19

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

  @logger.debug("Sending event", :destination => to_s, :event => event, :key => key)
  key = event.sprintf(@key) if @key

  begin
    publish_serialized(event.to_json, key)
  rescue JSON::GeneratorError => e
    @logger.warn("Trouble converting event to JSON", :exception => e,
                 :event => event)
  end
end

#registerObject

API



9
10
11
12
13
14
15
16
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 9

def register
  require "bunny"

  @logger.info("Registering output", :plugin => self)

  connect
  declare_exchange
end

#teardownObject



59
60
61
62
63
64
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 59

def teardown
  @conn.close if @conn && @conn.open?
  @conn = nil

  finished
end

#to_sObject



55
56
57
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 55

def to_s
  return "amqp://#{@user}@#{@host}:#{@port}#{@vhost}/#{@exchange_type}/#{@exchange}\##{@key}"
end