Class: LogStash::Outputs::Graphite
- Inherits:
-
Base
- Object
- Base
- LogStash::Outputs::Graphite
- 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
-
#connect ⇒ Object
def register.
-
#construct_metric_name(metric) ⇒ Object
def connect.
- #receive(event) ⇒ Object
- #register ⇒ Object
Instance Method Details
#connect ⇒ Object
def register
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/logstash/outputs/graphite.rb', line 78 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
90 91 92 93 94 95 96 |
# File 'lib/logstash/outputs/graphite.rb', line 90 def construct_metric_name(metric) if @metrics_format return @metrics_format.gsub(METRIC_PLACEHOLDER, metric) end metric end |
#receive(event) ⇒ Object
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 143 144 |
# File 'lib/logstash/outputs/graphite.rb', line 99 def receive(event) return unless output?(event) # Graphite message format: metric value timestamp\n = [] = 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)} << "#{construct_metric_name(metric)} #{event.sprintf(value.to_s).to_f} #{}" 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)} << "#{construct_metric_name(event.sprintf(metric))} #{event.sprintf(value).to_f} #{}" end end if .empty? @logger.debug("Message is empty, not sending anything to Graphite", :messages => , :host => @host, :port => @port) else = .join("\n") @logger.debug("Sending carbon 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() 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 |
#register ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/logstash/outputs/graphite.rb', line 65 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 |