Module: Fluent::Plugin::Graphite

Included in:
GraphiteFilter, GraphiteOutput
Defined in:
lib/fluent/plugin/graphite.rb

Instance Method Summary collapse

Instance Method Details

#format_metrics(tag, record) ⇒ Object



45
46
47
48
49
50
# File 'lib/fluent/plugin/graphite.rb', line 45

def format_metrics(tag, record)
  metrics = {}
  key = @monitoring_key + "." + @prefix + "." + tag
  metrics[key] = 1
  metrics
end

#init_client(log_level, host, port) ⇒ Object



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

def init_client(log_level, host, port)
  options = {
    # Required: valid URI {udp,tcp}://host:port/?timeout=seconds
    graphite: "tcp://#{host}:#{port}",

    # Optional: results are aggregated in 60 seconds slices ( default is 60 )
    slice: 30,

    # Optional: send to graphite every 60 seconds ( default is 0 - direct send )
    interval: 30,

    # Optional: set the max age in seconds for records reanimation ( default is 12 hours )
    cache: 4 * 60 * 60,
  }
  @client = GraphiteAPI.new options
  init_logger(log_level)
  log.info("Starting graphite client")
end

#init_logger(log_level) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fluent/plugin/graphite.rb', line 11

def init_logger(log_level)
  if log_level == 'debug'
    GraphiteAPI::Logger.init level: :debug
  end
  if log_level == 'warn'
    GraphiteAPI::Logger.init level: :warn
  end
  if log_level == 'info'
    GraphiteAPI::Logger.init level: :info
  end
  if log_level == 'error'
    GraphiteAPI::Logger.init level: :error
  end
end

#initializeObject



5
6
7
8
9
# File 'lib/fluent/plugin/graphite.rb', line 5

def initialize
  super
  require 'graphite-api'
  log.info("Initialize graphite plugin")
end

#post(metric, time) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/fluent/plugin/graphite.rb', line 53

def post(metric, time)
  trial ||= 1
  @client.metrics(metric, time)
  log.debug("Sending metric: #{metric}")
rescue Errno::ETIMEDOUT
  # after long periods with nothing emitted, the connection will be closed and result in timeout
  if trial <= @max_retries
    log.warn "out_graphite: connection timeout to #{@host}:#{@port}. Reconnecting... "
    trial += 1
    ::Graphite.init_client(@log_level, @host, @port)
    retry
  else
    log.error "out_graphite: ERROR: connection timeout to #{@host}:#{@port}. Exceeded max_retries #{@max_retries}"
  end
rescue Errno::ECONNREFUSED
  log.warn "out_graphite: connection refused by #{@host}:#{@port}"
rescue SocketError => se
  log.warn "out_graphite: socket error by #{@host}:#{@port} :#{se}"
rescue StandardError => e
  log.error "out_graphite: ERROR: #{e}"
end