Class: Fluent::GraphiteOutput

Inherits:
Output
  • Object
show all
Includes:
HandleTagNameMixin, Mixin::RewriteTagName
Defined in:
lib/fluent/plugin/out_graphite.rb

Instance Method Summary collapse

Constructor Details

#initializeGraphiteOutput

Returns a new instance of GraphiteOutput.



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

def initialize
  super
  require 'graphite-api'
end

Instance Method Details

#configure(conf) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fluent/plugin/out_graphite.rb', line 30

def configure(conf)
  super

  if !['prefix', 'suffix', 'ignore'].include?(@tag_for)
    raise Fluent::ConfigError, 'out_graphite: can specify to tag_for only prefix, suffix or ignore'
  end

  if !@name_keys && !@name_key_pattern
    raise Fluent::ConfigError, 'out_graphite: missing both of name_keys and name_key_pattern'
  end
  if @name_keys && @name_key_pattern
    raise Fluent::ConfigError, 'out_graphite: cannot specify both of name_keys and name_key_pattern'
  end

  if @name_keys
    @name_keys = @name_keys.split(',')
  end
  if @name_key_pattern
    @name_key_pattern = Regexp.new(@name_key_pattern)
  end
end

#emit(tag, es, chain) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/fluent/plugin/out_graphite.rb', line 52

def emit(tag, es, chain)
  es.each do |time, record|
    emit_tag = tag.dup
    filter_record(emit_tag, time, record)
    next unless metrics = format_metrics(emit_tag, record)

    # implemented to immediate call post method in this loop, because graphite-api.gem has the buffers.
    post(metrics, time)
  end

  chain.next
end

#format_metrics(tag, record) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fluent/plugin/out_graphite.rb', line 65

def format_metrics(tag, record)
  filtered_record = if @name_keys
                      record.select { |k,v| @name_keys.include?(k) }
                    else # defined @name_key_pattern
                      record.select { |k,v| @name_key_pattern.match(k) }
                    end

  return nil if filtered_record.empty?

  metrics = {}
  tag = tag.sub(/\.$/, '') # may include a dot at the end of the emit_tag fluent-mixin-rewrite-tag-name returns. remove it.
  filtered_record.each do |k, v|
    key = case @tag_for
          when 'ignore' then k
          when 'prefix' then tag + '.' + k
          when 'suffix' then k + '.' + tag
          end

    key = key.gsub(/(\s|\/)+/, '_') # cope with in the case of containing symbols or spaces in the key of the record like in_dstat.
    metrics[key] = v.to_f
  end
  metrics
end

#post(metrics, time) ⇒ Object



89
90
91
92
93
# File 'lib/fluent/plugin/out_graphite.rb', line 89

def post(metrics, time)
  @client.metrics(metrics, time)
rescue Errno::ECONNREFUSED
  log.warn "out_graphite: connection refused by #{@host}:#{@port}"
end

#startObject



25
26
27
28
# File 'lib/fluent/plugin/out_graphite.rb', line 25

def start
  super
  @client = GraphiteAPI.new(graphite: "#{@host}:#{@port}")
end