Class: LogStash::Outputs::GraphTastic

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

Overview

A plugin for a newly developed Java/Spring Metrics application I didn’t really want to code this project but I couldn’t find a respectable alternative that would also run on any Windows machine - which is the problem and why I am not going with Graphite and statsd. This application provides multiple integration options so as to make its use under your network requirements possible. This includes a REST option that is always enabled for your use in case you want to write a small script to send the occasional metric data.

Find GraphTastic here : github.com/NickPadilla/GraphTastic

Constant Summary

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

#flushMetricsObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/logstash/outputs/graphtastic.rb', line 119

def flushMetrics()
  begin
    if @integration.downcase == "tcp"
      flushViaTCP()
    elsif @integration.downcase == "rmi"
      flushViaRMI() 
    elsif @integration.downcase == "udp"
      flushViaUDP()
    elsif @integration.downcase == "rest"
      flushViaREST()
    else
      @logger.error("GraphTastic Not Able To Find Correct Integration - Nothing Sent - Integration Type : ", :@integration => @integration)
    end
    @batch.clear
  rescue
    @logger.error("*******ERROR :  #{$!}")
    @logger.info("*******Attempting #{@retry} out of #{@retries}")
    while @retry < @retries
      @retry = @retry + 1
      flushMetrics()
    end
  end
end

#flushViaRESTObject

send metrics via REST



153
154
155
156
157
158
159
160
# File 'lib/logstash/outputs/graphtastic.rb', line 153

def flushViaREST()
  request = Net::HTTP::Put.new("/#{@context}/addMetric/#{@batch.join(',')}")
  response = @http.request(request)
  if response == 'ERROR'
    raise 'Error happend when sending metric to GraphTastic using REST!'
  end
  @logger.debug("GraphTastic Sent Message Using REST : #{@batch.join(',')}", :response => response.inspect)    
end

#flushViaRMIObject

send metrics via RMI



163
164
165
166
167
168
169
# File 'lib/logstash/outputs/graphtastic.rb', line 163

def flushViaRMI()
  if RUBY_ENGINE != "jruby"
     raise Exception.new("LogStash::Outputs::GraphTastic# JRuby is needed for RMI to work!")
  end
  @remote.insertMetrics(@batch.join(','))
  @logger.debug("GraphTastic Sent Message Using RMI : #{@batch.join(',')}")
end

#flushViaTCPObject

send metrics via tcp



172
173
174
175
176
177
178
179
180
181
# File 'lib/logstash/outputs/graphtastic.rb', line 172

def flushViaTCP()
  # to correctly read the line we need to ensure we send \r\n at the end of every message.
  if @port.nil?
    @port = 1299
  end
  tcpsocket = TCPSocket.open(@host, @port)
  tcpsocket.send(@batch.join(',')+"\r\n", 0)
  tcpsocket.close
  @logger.debug("GraphTastic Sent Message Using TCP : #{@batch.join(',')}")
end

#flushViaUDPObject

send metrics via udp



144
145
146
147
148
149
150
# File 'lib/logstash/outputs/graphtastic.rb', line 144

def flushViaUDP()
  if @port.nil?
   @port = 1399
  end
  udpsocket.send(@batch.join(','), 0, @host, @port)
  @logger.debug("GraphTastic Sent Message Using UDP : #{@batch.join(',')}")
end

#postMetric(name, metric, timestamp) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/logstash/outputs/graphtastic.rb', line 110

def postMetric(name, metric, timestamp)
  message = name+","+metric+","+timestamp.to_s
  if @batch.length < @batch_number
    @batch.push(message)
  else
    flushMetrics()      
  end    
end

#receive(event) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/logstash/outputs/graphtastic.rb', line 95

def receive(event)
  return unless output?(event)
  # Set Intersection - returns a new array with the items that are the same between the two
  if !@tags.empty? && (event["tags"] & @tags).size == 0
     # Skip events that have no tags in common with what we were configured
     @logger.debug("No Tags match for GraphTastic Output!")
     return
  end
  @retry = 1
  @logger.debug("Event found for GraphTastic!", :tags => @tags, :event => event)
  @metrics.each do |name, metric|
    postMetric(event.sprintf(name),event.sprintf(metric),(event["@timestamp"]*1000))# unix_timestamp is what I need in seconds - multiply by 1000 to make milliseconds.
  end
end

#registerObject



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
# File 'lib/logstash/outputs/graphtastic.rb', line 67

def register
  @batch = []
  begin
    if @integration.downcase == "rmi"
      if RUBY_ENGINE != "jruby"
         raise Exception.new("LogStash::Outputs::GraphTastic# JRuby is needed for RMI to work!")
      end
      require "java"
      if @port.nil?
        @port = 1199
      end
      registry = java.rmi.registry.LocateRegistry.getRegistry(@host, @port);
      @remote = registry.lookup("RmiMetricService")
    elsif @integration.downcase == "rest"
      require "net/http"         
      if @port.nil?
        @port = 8080
        gem "mail" #outputs/email, # License: MIT License
      end
      @http = Net::HTTP.new(@host, @port)
    end
    @logger.info("GraphTastic Output Successfully Registered! Using #{@integration} Integration!")
  rescue 
    @logger.error("*******ERROR :  #{$!}")
  end
end

#udpsocketObject



183
# File 'lib/logstash/outputs/graphtastic.rb', line 183

def udpsocket; @socket ||= UDPSocket.new end