/
definition.rb
83 lines (70 loc) · 2.35 KB
/
definition.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
76
77
78
79
80
81
82
83
module Mri
module Instrumentation
class Definition
# Represents a YAML probe definition
#
attr_reader :path,
:group,
:probes
def initialize( path )
@path = path
@probes = []
@group = setup_group()
setup_probes()
end
# Read and parse the definition
#
def read
@read ||= YAML.load( IO.read( @path ) )
end
private
# Infer the group
#
def setup_group
@path.match( /([^\/|.]*).yml$/ )[1].to_sym
end
# Setup all probes
#
def setup_probes
read.each do |probe|
probe.each_pair do |probe, definition|
@probes << setup_probe( probe,
safe_definition( definition, "desc" ),
safe_definition( definition, "arguments" ),
safe_definition( definition, "return" ),
safe_definition( definition, "storage" ) )
end
end
end
# Don't assume all argument and return definitions is given
#
def safe_definition( definition, key )
definition ? definition[key] : nil
end
# Setup a single probe
#
def setup_probe( probe, description, arguments, returns, storage )
Mri::Instrumentation::Probe.new( group, probe, description, setup_arguments( arguments ), returns, storage )
end
# Setup arguments for a single probe
#
def setup_arguments( arguments )
arguments_with_probe( arguments ).map do |argument|
setup_argument( arguments, argument )
end
end
# Setup a single argument
#
def setup_argument( arguments, argument )
if argument.is_a?( Mri::Instrumentation::Argument )
argument
else
Mri::Instrumentation::Argument.new( argument.keys.first, argument.values.first, arguments.index(argument) )
end
end
def arguments_with_probe( arguments )
( arguments || [] ) << Mri::Instrumentation::Argument.probe
end
end
end
end