Class: LogStash::Codecs::Graphite

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

Overview

This codec will encode and decode Graphite formated lines.

Constant Summary collapse

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

Constants included from LogStash::Config::Mixin

LogStash::Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from LogStash::Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Instance Method Summary collapse

Methods inherited from Base

#clone, #flush, #on_event, #teardown

Methods included from LogStash::Config::Mixin

#config_init, included

Methods inherited from Plugin

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

Constructor Details

#initialize(params = {}) ⇒ Graphite

Returns a new instance of Graphite.



57
58
59
60
# File 'lib/logstash/codecs/graphite.rb', line 57

def initialize(params={})
  super(params)
  @lines = LogStash::Codecs::Line.new
end

Instance Method Details

#decode(data) ⇒ Object



63
64
65
66
67
68
# File 'lib/logstash/codecs/graphite.rb', line 63

def decode(data)
  @lines.decode(data) do |event|
    name, value, time = event["message"].split(" ")
    yield LogStash::Event.new(name => value.to_f, "@timestamp" => Time.at(time.to_i).gmtime)
  end # @lines.decode
end

#encode(event) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/logstash/codecs/graphite.rb', line 80

def encode(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 # data.to_hash.each
  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 # @metrics.each
  end # if @fields_are_metrics

  if messages.empty?
    @logger.debug("Message is empty, not emiting anything.", :messages => messages)
  else
    message = messages.join("\n") + "\n"
    @logger.debug("Emiting carbon messages", :messages => messages)

    @on_event.call(message)
  end # if messages.empty?
end