class LogStash::Outputs::Graphite

This output allows you to pull metrics from your logs and ship them to graphite. Graphite is an open source tool for storing and graphing metrics.

An example use case: At loggly, some of our applications emit aggregated stats in the logs every 10 seconds. Using the grok filter and this output, I can capture the metric values from the logs and emit them to graphite.

Constants

DEFAULT_METRICS_FORMAT
EXCLUDE_ALWAYS
METRIC_PLACEHOLDER

Public Instance Methods

connect() click to toggle source
# File lib/logstash/outputs/graphite.rb, line 76
def connect
  # TODO(sissel): Test error cases. Catch exceptions. Find fortune and glory.
  begin
    @socket = TCPSocket.new(@host, @port)
  rescue Errno::ECONNREFUSED => e
    @logger.warn("Connection refused to graphite server, sleeping...",
                 :host => @host, :port => @port)
    sleep(@reconnect_interval)
    retry
  end
end
construct_metric_name(metric) click to toggle source
# File lib/logstash/outputs/graphite.rb, line 88
def construct_metric_name(metric)
  if @metrics_format
    return @metrics_format.gsub(METRIC_PLACEHOLDER, metric)
  end

  metric
end
receive(event) click to toggle source
# File lib/logstash/outputs/graphite.rb, line 97
def receive(event)
  return unless output?(event)

  # Graphite message format: metric value timestamp\n

  messages = []
  timestamp = event.sprintf("%{+%s}")

  if @fields_are_metrics
    @logger.debug("got metrics event", :metrics => event.to_hash)
    event.to_hash.each do |metric,value|
      next if EXCLUDE_ALWAYS.include?(metric)
      next unless @include_metrics.empty? || @include_metrics.any? { |regexp| metric.match(regexp) }
      next if @exclude_metrics.any? {|regexp| metric.match(regexp)}
      messages << "#{construct_metric_name(metric)} #{event.sprintf(value.to_s).to_f} #{timestamp}"
    end
  else
    @metrics.each do |metric, value|
      @logger.debug("processing", :metric => metric, :value => value)
      metric = event.sprintf(metric)
      next unless @include_metrics.any? {|regexp| metric.match(regexp)}
      next if @exclude_metrics.any? {|regexp| metric.match(regexp)}
      messages << "#{construct_metric_name(event.sprintf(metric))} #{event.sprintf(value).to_f} #{timestamp}"
    end
  end

  if messages.empty?
    @logger.debug("Message is empty, not sending anything to graphite", :messages => messages, :host => @host, :port => @port)
  else
    message = messages.join("\n")
    @logger.debug("Sending carbon messages", :messages => messages, :host => @host, :port => @port)

    # Catch exceptions like ECONNRESET and friends, reconnect on failure.
    # TODO(sissel): Test error cases. Catch exceptions. Find fortune and glory.
    begin
      @socket.puts(message)
    rescue Errno::EPIPE, Errno::ECONNRESET => e
      @logger.warn("Connection to graphite server died",
                   :exception => e, :host => @host, :port => @port)
      sleep(@reconnect_interval)
      connect
      retry if @resend_on_failure
    end
  end

end
register() click to toggle source
# File lib/logstash/outputs/graphite.rb, line 63
def register
  @include_metrics.collect!{|regexp| Regexp.new(regexp)}
  @exclude_metrics.collect!{|regexp| Regexp.new(regexp)}

  if @metrics_format && !@metrics_format.include?(METRIC_PLACEHOLDER)
    @logger.warn("metrics_format does not include placeholder #{METRIC_PLACEHOLDER} .. falling back to default format: #{DEFAULT_METRICS_FORMAT.inspect}")

    @metrics_format = DEFAULT_METRICS_FORMAT
  end

  connect
end