Class: LogStash::Outputs::OpentsdbUsingFilterMetrics

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/opentsdb_using_filter_metrics.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#connectObject

def register



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/logstash/outputs/opentsdb_using_filter_metrics.rb', line 34

def connect
  # TODO(sissel): Test error cases. Catch exceptions. Find fortune and glory.
  begin
    @socket = TCPSocket.new(@host, @port)
    @logger.info("Connected to opentsdb server")
  rescue Errno::ECONNREFUSED => e
    @logger.warn("Connection refused to opentsdb server, sleeping...",
                 :host => @host, :port => @port)
    sleep(2)
    retry
  end
end

#receive(event) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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/outputs/opentsdb_using_filter_metrics.rb', line 48

def receive(event)


  # Opentsdb message format: put metric timestamp value tagname=tagvalue tag2=value2\n

  # Catch exceptions like ECONNRESET and friends, reconnect on failure.

  event_hash = event.to_hash
  tags = Array.new

  event_hash.each do |h_key, h_value|
    tags = h_key.split(@tag_separator)
    if tags.length > 1
      metric_name = @metric_name
      metric_value = h_value["count"]
      tags << "host" << hostname

      begin
        name = metric_name
        value = metric_value

        # The first part of the message
        message = ['put',
                   event.sprintf(name),
                   event.sprintf("%{+%s}"),
                   event.sprintf(value),
        ].join(" ")

        # If we have have tags we need to add it to the message
        event_tags = []
        unless tags.nil?
          Hash[*tags.flatten].each do |tag_name,tag_value|
            # Interprete variables if neccesary
            real_tag_name = event.sprintf(tag_name)
            real_tag_value =  event.sprintf(tag_value)
            event_tags << [real_tag_name , real_tag_value ].join('=')
          end
          message+=' '+event_tags.join(' ')
        end

        # TODO(sissel): Test error cases. Catch exceptions. Find fortune and glory.
        tries = 3
        begin
          @socket.puts(message)
        rescue Errno::EPIPE, Errno::ECONNRESET => e
          sleep(1)
          tries -= 1
          if tries > 0
            connect
            retry
          else
            @logger.warn("Connection to opentsdb server died",
                         :exception => e, :host => @host, :port => @port, :query => message)
          end
        end

        # TODO(sissel): resend on failure
        # TODO(sissel): Make 'resend on failure' tunable; sometimes it's OK to
        # drop metrics.
      end # @metrics.each
    end # tags.length > 1
  end # event_hash.each


end

#registerObject



30
31
32
# File 'lib/logstash/outputs/opentsdb_using_filter_metrics.rb', line 30

def register
  connect
end