Module: Stratagem::Instrumentation::Models::Tracing

Included in:
Annotations
Defined in:
lib/stratagem/instrumentation/models/tracing.rb

Constant Summary collapse

@@invocations_audit =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.invocations_auditObject



6
7
8
# File 'lib/stratagem/instrumentation/models/tracing.rb', line 6

def self.invocations_audit
  @@invocations_audit
end

Instance Method Details

#add_invocation(enumeration, invocation) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/stratagem/instrumentation/models/tracing.rb', line 88

def add_invocation(enumeration, invocation)
  # puts "adding invocation: #{invocation.inspect}"
  invocations = (enumeration ||= [])
  existing = invocations.find {|i| 
    i.controller_path == invocation.controller_path && 
    i.controller_action == invocation.controller_action && 
    i.line_number == invocation.line_number
  }
  invocations << invocation unless existing
  invocations_audit << invocation
end

#clear_invocationsObject



29
30
31
32
33
# File 'lib/stratagem/instrumentation/models/tracing.rb', line 29

def clear_invocations
  read_invocations.clear
  write_invocations.clear
  invocations_audit.clear
end

#controller_trace(regex = /_controller\.rb/) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/stratagem/instrumentation/models/tracing.rb', line 57

def controller_trace(regex = /_controller\.rb/)
  trace_index = nil
  trace_line = nil
  caller.reverse.each_with_index do |line,i|
    if (line =~ regex)
      trace_index = i
      trace_line = line
      break
    end
  end

  if trace_line
    path,action,line = parse_trace_line(trace_line)
    [path,action,line,caller,trace_index]
  else
    []
  end
end

#invocation(method, args, enumeration, type, object = nil, alternate_model = nil) ⇒ Object



51
52
53
54
55
# File 'lib/stratagem/instrumentation/models/tracing.rb', line 51

def invocation(method, args, enumeration, type, object=nil, alternate_model=nil)
  path,action,line,trace,index = controller_trace
  args = args.first if args && (args.size == 1) && (args.first.kind_of?(Array))
  add_invocation enumeration, Stratagem::Instrumentation::MethodInvocation.new(method, path, action, line, object, alternate_model || model, caller, args, type) if (path)
end

#invocations_auditObject



10
11
12
# File 'lib/stratagem/instrumentation/models/tracing.rb', line 10

def invocations_audit
  @@invocations_audit
end

#parse_trace_line(trace_line) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/stratagem/instrumentation/models/tracing.rb', line 76

def parse_trace_line(trace_line)
  path,line,action = trace_line.split(':')
  path = path.gsub(/.*?\/app/, 'app')
  if (action)
    action.gsub!(/[`']/, '').gsub!('in ', '')
    line = line.to_i
    [path,action,line]
  else
    []
  end
end

#read_invocation(method, *args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/stratagem/instrumentation/models/tracing.rb', line 39

def read_invocation(method, *args)
  # ensure that the read did not stem from a write operation

  # the first line in the stack trace will be StrataGem instrumentation
  # if the second line is active record then the invocation was called from another invocation
  # and should be ignored
  unless (caller()[1].include?('active_record/base.rb'))
    path,action,line,trace,index = controller_trace(/active_record\/base\.rb/)
    invocation(method, args, read_invocations, :read) unless (action =~ /create/) || (action =~ /update/) || (action =~ /save/)
  end
end

#read_invocationsObject



14
15
16
# File 'lib/stratagem/instrumentation/models/tracing.rb', line 14

def read_invocations
  @read_invocations ||= []
end

#validator_called(validation, args) ⇒ Object



22
23
24
25
26
27
# File 'lib/stratagem/instrumentation/models/tracing.rb', line 22

def validator_called(validation, args)
  params = args.find {|a| a.kind_of?(Hash) } || {}
  (args-[params]).each do |field|
    self.validators << ValidatorDefinition.new(validation, field, params, model)
  end
end

#write_invocation(object, model, method, args) ⇒ Object



35
36
37
# File 'lib/stratagem/instrumentation/models/tracing.rb', line 35

def write_invocation(object, model, method, args)
  invocation(method, args, write_invocations, :write, object, model)
end

#write_invocationsObject



18
19
20
# File 'lib/stratagem/instrumentation/models/tracing.rb', line 18

def write_invocations
  @write_invocations ||= []
end