Class: Fluent::DdOutput

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

Instance Method Summary collapse

Constructor Details

#initializeDdOutput

Returns a new instance of DdOutput.



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

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

Instance Method Details

#configure(conf) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fluent/plugin/out_dd.rb', line 32

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



47
48
49
# File 'lib/fluent/plugin/out_dd.rb', line 47

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

#shutdownObject



28
29
30
# File 'lib/fluent/plugin/out_dd.rb', line 28

def shutdown
  super
end

#startObject



24
25
26
# File 'lib/fluent/plugin/out_dd.rb', line 24

def start
  super
end

#write(chunk) ⇒ Object



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 51

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

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

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

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

    options = {}
    options['tags'] = [tag] if tag
    options['host'] = host if host
    options['type'] = type if type

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