Class: LogStash::Outputs::Graphite

Inherits:
Base
  • Object
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: Some applications emit aggregated stats in the logs every 10 seconds. Using the grok filter and this output, it is possible to capture the metric values from the logs and emit them to Graphite.

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#connectObject

def register



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/logstash/outputs/graphite.rb', line 83

def connect
  # TODO(sissel): Test error cases. Catch exceptions. Find fortune and glory. Retire to yak farm.
  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



95
96
97
98
99
100
101
# File 'lib/logstash/outputs/graphite.rb', line 95

def construct_metric_name(metric)
  if @metrics_format
    return @metrics_format.gsub(METRIC_PLACEHOLDER, metric)
  end

  metric
end

#receive(event) ⇒ Object



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
143
144
145
146
147
148
149
# File 'lib/logstash/outputs/graphite.rb', line 104

def receive(event)
  return unless output?(event)

  # Graphite message format: metric value timestamp\n

  messages = []
  timestamp = event[@timestamp_field].to_i

  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, IOError => 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



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/logstash/outputs/graphite.rb', line 70

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