Class: TraceDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/trace_dispatcher.rb

Overview

Simplistic, asynchronous trace dispatcher. Pulls traces off the TraceQueue and sends them along to Spring Insight over REST as JSON

Requires ActiveSupport::JSON

The rails JSON is used here due to incompatibilities
when using the non-rails JSON inside of Rails
ActiveSupport::JSON overrides JSON behaviour in wierd
ways

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, user, pass) ⇒ TraceDispatcher

Returns a new instance of TraceDispatcher.



23
24
25
26
27
28
29
# File 'lib/agent/trace_dispatcher.rb', line 23

def initialize(url, user, pass)
  uri = URI.parse(url)
  @host = uri.host
  @port = uri.port
  @user = user
  @pass = pass
end

Class Method Details

.convert_to_json(trace) ⇒ Object



62
63
64
# File 'lib/agent/trace_dispatcher.rb', line 62

def self.convert_to_json(trace)
  trace.to_json
end

Instance Method Details

#dispatch(host, port, user, pass, traceJSON) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/agent/trace_dispatcher.rb', line 70

def dispatch(host, port, user, pass, traceJSON)
  puts traceJSON
  path="/services/traces/rest/dispatch"
  headers = {'Content-Type' =>'application/json'}
  http = Net::HTTP.new(host, port)
  req = Net::HTTP::Put.new(path,headers)
  req.basic_auth user, pass
  req.body = traceJSON
  res, data = http.request(req)
  puts data
end

#pollObject



52
53
54
55
56
57
58
59
60
# File 'lib/agent/trace_dispatcher.rb', line 52

def poll()
  trace = TraceQueue.instance.getTrace()
  if trace != nil
    send(trace)
    true
  else
    false
  end
end

#send(trace) ⇒ Object



66
67
68
# File 'lib/agent/trace_dispatcher.rb', line 66

def send(trace)
  dispatch(@host, @port, @user, @pass, TraceDispatcher.convert_to_json(trace))
end

#startObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/agent/trace_dispatcher.rb', line 33

def start()
  @running = true
  @thread = Thread.new {
    while @running do
      while poll() do
        ## noop
      end
      sleep(0.1)
    end
  }
end

#stopObject



45
46
47
48
49
50
# File 'lib/agent/trace_dispatcher.rb', line 45

def stop()
  if @thread != nil
    @running = false
    @thread.join
  end
end