module Fluent::Plugin::Graphite

Public Class Methods

new() click to toggle source
Calls superclass method
# File lib/fluent/plugin/graphite.rb, line 5
def initialize
  super
  require 'graphite-api'
  log.info("Initialize graphite plugin")
end

Public Instance Methods

format_metrics(tag, record) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
post(metric, time) click to toggle source
# 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