Module: LogStash::Outputs::RabbitMQ::BunnyImpl
- Included in:
- LogStash::Outputs::RabbitMQ
- Defined in:
- lib/logstash/outputs/rabbitmq/bunny.rb
Instance Method Summary collapse
-
#connect ⇒ Object
Implementation.
- #declare_exchange ⇒ Object
- #publish_serialized(message, key = @key) ⇒ Object
- #receive(event) ⇒ Object
-
#register ⇒ Object
API.
- #teardown ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#connect ⇒ Object
Implementation
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 128 129 130 |
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 75 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 || @logger.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.}. Will attempt to reconnect in #{n} seconds...", :exception => e, :backtrace => e.backtrace) return if terminating? sleep n retry end end |
#declare_exchange ⇒ Object
132 133 134 135 136 |
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 132 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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 36 def publish_serialized(, key = @key) begin if @x @x.publish(, :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.}. 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
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 22 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 LogStash::Json::GeneratorError => e @logger.warn("Trouble converting event to JSON", :exception => e, :event => event) end end |
#register ⇒ Object
API
12 13 14 15 16 17 18 19 |
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 12 def register require "bunny" @logger.info("Registering output", :plugin => self) connect declare_exchange end |
#teardown ⇒ Object
62 63 64 65 66 67 |
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 62 def teardown @conn.close if @conn && @conn.open? @conn = nil finished end |
#to_s ⇒ Object
58 59 60 |
# File 'lib/logstash/outputs/rabbitmq/bunny.rb', line 58 def to_s return "amqp://#{@user}@#{@host}:#{@port}#{@vhost}/#{@exchange_type}/#{@exchange}\##{@key}" end |