Class: LogStash::Outputs::Amqp

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/amqp.rb

Constant Summary collapse

MQTYPES =
[ "fanout", "direct", "topic" ]

Instance Attribute Summary

Attributes inherited from Base

#logger

Instance Method Summary collapse

Constructor Details

#initialize(url, config = {}, &block) ⇒ Amqp

Returns a new instance of Amqp.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/logstash/outputs/amqp.rb', line 11

def initialize(url, config={}, &block)
  super

  @mq = nil
  @bulk_prefix = nil

  # Handle path /<vhost>/<type>/<name> or /<type>/<name>
  # vhost allowed to contain slashes
  if @url.path =~ %r{^/((.*)/)?([^/]+)/([^/]+)}
    unused, @vhost, @mqtype, @name = $~.captures
  else
    raise "amqp urls must have a path of /<type>/name or /vhost/<type>/name where <type> is #{MQTYPES.join(", ")}"
  end

  if !MQTYPES.include?(@mqtype)
    raise "Invalid type '#{@mqtype}' must be one of #{MQTYPES.join(", ")}"
  end
end

Instance Method Details

#receive(event) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/logstash/outputs/amqp.rb', line 64

def receive(event)
  @logger.debug(["Sending event", { :url => @url, :event => event }])
  if @bulk_prefix
    @target.publish(@bulk_prefix + event.to_json + "\n")
  else
    @target.publish(event.to_json)
  end
end

#receive_raw(raw) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/logstash/outputs/amqp.rb', line 75

def receive_raw(raw)
  if @target == nil
    raise "had trouble registering AMQP URL #{@url.to_s}, @target is nil"
  end

  @target.publish(raw)
end

#registerObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/logstash/outputs/amqp.rb', line 31

def register
  @logger.info("Registering output #{@url}")
  query_args = @url.query ? CGI.parse(@url.query) : {}
  amqpsettings = {
    :vhost => (@vhost or "/"),
    :host => @url.host,
    :port => (@url.port or 5672),
  }
  amqpsettings[:user] = @url.user if @url.user
  amqpsettings[:pass] = @url.password if @url.password
  amqpsettings[:logging] = query_args.include? "debug"
  @logger.debug("Connecting with AMQP settings #{amqpsettings.inspect} to set up #{@mqtype.inspect} exchange #{@name.inspect}")
  @amqp = AMQP.connect(amqpsettings)
  @mq = MQ.new(@amqp)
  @target = nil

  if @urlopts.include? "es_index" and @urlopts.include? "es_type"
    @bulk_prefix = { "index" => { "_index" => @urlopts["es_index"], "_type" => @urlopts["es_type"] } }.to_json + "\n"
    @logger.debug "Preparing ElasticSearch bulk API header for injection: #{@bulk_prefix.inspect}"
  end

  @durable = @urlopts["durable"] ? true : false
  case @mqtype
    when "fanout"
      @target = @mq.fanout(@name, :durable => @durable)
    when "direct"
      @target = @mq.direct(@name, :durable => @durable)
    when "topic"
      @target = @mq.topic(@name, :durable => @durable)
  end # case @mqtype
end