Class: Fluent::DdOutput

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

Instance Method Summary collapse

Constructor Details

#initializeDdOutput

Returns a new instance of DdOutput.



12
13
14
15
16
# File 'lib/fluent/plugin/out_dd.rb', line 12

def initialize
  super
  require 'dogapi'
  require 'socket'
end

Instance Method Details

#configure(conf) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fluent/plugin/out_dd.rb', line 26

def configure(conf)
  super

  unless @dd_api_key
    raise Fluent::ConfigError, '`dd_api_key` is required'
  end

  unless @host
    @host = %x[hostname -f 2> /dev/null].strip
    @host = Socket.gethostname if @host.empty?
  end

  @dog = Dogapi::Client.new(@dd_api_key, nil, @host)
end

#format(tag, time, record) ⇒ Object



41
42
43
# File 'lib/fluent/plugin/out_dd.rb', line 41

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

#shutdownObject



22
23
24
# File 'lib/fluent/plugin/out_dd.rb', line 22

def shutdown
  super
end

#startObject



18
19
20
# File 'lib/fluent/plugin/out_dd.rb', line 18

def start
  super
end

#write(chunk) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fluent/plugin/out_dd.rb', line 45

def write(chunk)
  enum = chunk.to_enum(:msgpack_each)

  enum.select {|tag, time, record|
    unless record['metric']
      log.warn("`metric` key does not exist: #{[tag, time, record].inspect}")
    end

    record['metric']
  }.chunk {|tag, time, record|
    dd_tag = record['tag']

    if not dd_tag and @use_fluentd_tag_for_datadog_tag
      dd_tag = tag
    end

    [dd_tag] + record.values_at('metric', 'host', 'type')
  }.each {|i, records|
    tag, metric, host, type = i

    points = records.map do |tag, time, record|
      time = Time.at(time)
      value = record['value']
      [time, value]
    end

    options = {}
    options[:tags] = tag.split(',').map {|i| i.strip } if tag
    options[:host] = host if host
    options[:type] = type if type

    @dog.emit_points(metric, points, options)
  }
end