Class: Caliper::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/caliper/tracer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Tracer

Returns a new instance of Tracer.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/caliper/tracer.rb', line 9

def initialize(env)
  @uuid = UUID.generate

  @http_method = env['REQUEST_METHOD']
  @uri = env['REQUEST_URI'] # full URI
  @path = env['PATH_INFO'] # user path_info because it's in the rack spec rather than REQUEST_PATH

  @remote_ip = env["action_dispatch.remote_ip"] #requesting ip
  @remote_ip = @remote_ip.first if @remote_ip.is_a?(Array)

  @host = Socket.gethostname

  @samples = []
end

Instance Attribute Details

#extra_infoObject

Returns the value of attribute extra_info.



7
8
9
# File 'lib/caliper/tracer.rb', line 7

def extra_info
  @extra_info
end

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/caliper/tracer.rb', line 7

def host
  @host
end

#http_methodObject

Returns the value of attribute http_method.



7
8
9
# File 'lib/caliper/tracer.rb', line 7

def http_method
  @http_method
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/caliper/tracer.rb', line 7

def path
  @path
end

#remote_ipObject

Returns the value of attribute remote_ip.



7
8
9
# File 'lib/caliper/tracer.rb', line 7

def remote_ip
  @remote_ip
end

#reqsObject

Returns the value of attribute reqs.



7
8
9
# File 'lib/caliper/tracer.rb', line 7

def reqs
  @reqs
end

#samplesObject

Returns the value of attribute samples.



7
8
9
# File 'lib/caliper/tracer.rb', line 7

def samples
  @samples
end

#uriObject

Returns the value of attribute uri.



7
8
9
# File 'lib/caliper/tracer.rb', line 7

def uri
  @uri
end

#uuidObject

Returns the value of attribute uuid.



7
8
9
# File 'lib/caliper/tracer.rb', line 7

def uuid
  @uuid
end

Instance Method Details

#add_to_trace(data = {}) ⇒ Object



39
40
41
# File 'lib/caliper/tracer.rb', line 39

def add_to_trace(data = {})
  @extra_info = data
end

#finishObject



43
44
45
46
47
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
# File 'lib/caliper/tracer.rb', line 43

def finish
  return true if samples.size == 0

  trace_data = {
    "uuid" => uuid,
    "http_method" => http_method,
    "uri" => uri,
    "path" => path,
    "remote_ip" => remote_ip.to_s,
    "host" => host,
    "reqs" => reqs,
    "extra_info" => extra_info,
    "samples" => []
  }

  samples.each do |sample|
    trace_data["samples"] << {
      "name" => sample.name,
      "duration" => sample.duration,
      #"start" => sample.time,
      #"end" => sample.end,
      "payload" => sample.payload
    }
    if sample.name[/process_action.action_controller/]
      trace_data["view_runtime"] = sample.payload[:view_runtime]
      trace_data["db_runtime"] = sample.payload[:db_runtime]
      trace_data["duration"] = sample.duration
    end
  end
  CaliperApi.create_trace(
    Yajl::Encoder.encode({ "trace" => trace_data })
  )
end

#record(event) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/caliper/tracer.rb', line 24

def record(event)
  event.payload.delete(:caliper_tracer) if event.payload[:caliper_tracer]
  return if event.name[/active_record/] && (event.payload[:name] == nil || event.payload[:name][/SCHEMA/])

  # strip un-required payload data for activerecord notifications
  if event.name[/active_record/]
    ["binds", "connection_id"].each do |key|
      event.payload.delete(key)
    end
  end

  #Caliper.logger.debug [event.name, event.time, event.end, event.transaction_id, event.payload].join("||")
  @samples << event
end