Class: LogStash::Outputs::Graphite

Inherits:
Base show all
Defined in:
lib/logstash/outputs/graphite.rb

Overview

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.

Constant Summary collapse

EXCLUDE_ALWAYS =
[ "@timestamp", "@version" ]
DEFAULT_METRICS_FORMAT =
"*"
METRIC_PLACEHOLDER =
"*"

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#handle, #handle_worker, #initialize, #worker_setup, #workers_not_supported

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Outputs::Base

Instance Method Details

#connectObject

def register



76
77
78
79
80
81
82
83
84
85
86
# 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) ⇒ Object

def connect



88
89
90
91
92
93
94
# 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) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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

#registerObject



63
64
65
66
67
68
69
70
71
72
73
74
# 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