Class: Fluent::AmqpOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_amqp.rb

Instance Method Summary collapse

Constructor Details

#initializeAmqpOutput

Returns a new instance of AmqpOutput.



19
20
21
22
23
# File 'lib/fluent/plugin/out_amqp.rb', line 19

def initialize(*)
  super
  require "bunny"
  require "yajl"
end

Instance Method Details

#configure(conf) ⇒ Object

This method is called before starting. ‘conf’ is a Hash that includes configuration parameters. If the configuration is invalid, raise Fluent::ConfigError.

Raises:

  • (Fluent::ConfigError)


28
29
30
31
32
33
34
35
# File 'lib/fluent/plugin/out_amqp.rb', line 28

def configure(conf)
  super

  raise Fluent::ConfigError, "missing host infromation" unless @host
  raise Fluent::ConfigError, "missing exchange" unless @exchange

  @exchange_name = @exchange
end

#format(tag, time, record) ⇒ Object



57
58
59
# File 'lib/fluent/plugin/out_amqp.rb', line 57

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#shutdownObject

This method is called when shutting down. Shutdown the thread and close sockets or files here.



52
53
54
55
# File 'lib/fluent/plugin/out_amqp.rb', line 52

def shutdown
  super
  @amqp_conn && @amqp_conn.stop
end

#startObject

This method is called when starting. Open sockets or files here.



39
40
41
42
43
44
45
46
47
48
# File 'lib/fluent/plugin/out_amqp.rb', line 39

def start
  super

  begin
    get_or_create_exchange
  rescue => e
    $log.error "AMQP error", error: e.to_s
    $log.warn_backtrace e.backtrace
  end
end

#write(chunk) ⇒ Object

This method is called every flush interval. Write the buffer chunk to files or databases here. ‘chunk’ is a buffer chunk that includes multiple formatted events. You can use ‘data = chunk.read’ to get all events and ‘chunk.open {|io| … }’ to get IO objects.



66
67
68
69
70
71
# File 'lib/fluent/plugin/out_amqp.rb', line 66

def write(chunk)
  chunk.msgpack_each do |(tag, time, record)|
    event = @payload_only ? record : { "key" => tag, "timestamp" => time, "payload" => record }
    get_or_create_exchange.publish Yajl.dump(event), routing_key: tag, content_type: @content_type
  end
end