Module: TingYun::Instrumentation::Support::SplitController

Included in:
Rails3::ActionController, Rails::ControllerEvent, TingYun::Instrumentation::Sinatra::Action
Defined in:
lib/ting_yun/instrumentation/support/split_controller.rb

Constant Summary collapse

HTTP =
{
  'GET' => 1,
  'POST' => 2,
  'PUT' => 3,
  'DELETE' => 4,
  'HEAD' => 5,
  'PATCH' => 3
}
RULE =
{
  0=> :any,
  1=> :eql?,
  2=> :start_with?,
  3=> :end_with?,
  4=> :include?,
  5=> :match
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ruleObject

Returns the value of attribute rule.



7
8
9
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 7

def rule
  @rule
end

#tingyun_http_verbObject

Returns the value of attribute tingyun_http_verb.



7
8
9
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 7

def tingyun_http_verb
  @tingyun_http_verb
end

Instance Method Details

#dot_flattened(nested_hash, result = {}) ⇒ Object

turns => {‘b’ => ‘c’} into => ‘c’



131
132
133
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 131

def dot_flattened(nested_hash, result={})
  TingYun::Instrumentation::Support::ParameterFiltering.dot_flattened(nested_hash, result={})
end

#find_rule(method, path, header, params) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 26

def find_rule(method, path, header, params)
  @tingyun_http_verb = method
  @rule = rules.detect do |_r|
    method_match?(method, _r["match"]["method"]) and
        url_match?(path, _r["match"]["match"], _r["match"]["value"]) and
        params_match?(header, dot_flattened(params), _r["match"]["params"])
  end
end

#method_match?(method, _r) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 44

def method_match?(method, _r)
  _r == 0 || _r == HTTP[method]
end

#name(path, header, params, cookie) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 78

def name(path, header, params, cookie)
  return nil if @rule.nil?

  name = ""
  name << split_url(path.split('/'))
  name << "?"
  name << split_params(@rule["split"]["urlParams"], params)
  name << split_header(@rule["split"]["headerParams"], header)
  name << split_params(@rule["split"]["bodyParams"], params)
  name << split_params(@rule["split"]["cookieParams"], cookie)
  name = name[0..-2] << split_method
  name.strip
end

#namespaceObject



35
36
37
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 35

def namespace
  @rule["name"]
end

#params_match?(header, params, _rs) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 52

def params_match?(header, params, _rs)
  return true if _rs.empty?
  begin
    _rs.each do |_r|
      next if _r["name"].nil? || _r["name"].strip.empty?
      if _r["type"] == 2
        raise_error(header["HTTP_#{_r["name"].upcase}"], RULE[_r["match"]], _r["value"], _r["type"])
      else
        raise_error(params[_r["name"].downcase], RULE[_r["match"]], _r["value"],  _r["type"])
      end
    end
  rescue
    return false
  end
  return true
end

#raise_error(_v, _r, _v2, _t) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 69

def raise_error(_v, _r, _v2, _t)
  raise 'this param unexist so the rule is unmatched' if _v.nil? or _v2.nil? or _v2.strip.empty? rescue false
  return if _v && _r==:any
  unless _t == 0
    raise 'this param  is unmatched  with the rule' unless _v.send(_r, _v2)
  end
end

#rulesObject



39
40
41
42
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 39

def rules
  require 'ting_yun/support/serialize/json_wrapper'
  TingYun::Support::Serialize::JSONWrapper.load(TingYun::Agent.config[:'nbs.naming.rules'])
end

#split_header(_r, header) ⇒ Object



123
124
125
126
127
128
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 123

def split_header(_r, header)
  return '' if _r.nil? or _r.strip.empty?
  query_string =''
  _r.split(',').each {|_i|query_string +="#{_i}=#{header["HTTP_#{_i.upcase}"]}&"}
  query_string
end

#split_methodObject



108
109
110
111
112
113
114
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 108

def split_method
   if @rule["split"]["method"]
     "(#{tingyun_http_verb})"
   else
     ''
   end
end

#split_params(_r, params) ⇒ Object



116
117
118
119
120
121
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 116

def split_params(_r, params)
  return '' if _r.nil? or _r.strip.empty?
  query_string =''
  _r.split(',').each {|_i|query_string +="#{_i}=#{params[_i]}&"}
  query_string
end

#split_url(url) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 92

def split_url(url)
  uri = @rule["split"]["uri"]
  return '' if uri.nil? or uri.strip.empty?
  if uri.include? ','
    _i = uri.split(',').map{|n|n.to_i}
    url.values_at(*_i).join('/')
  else
    _i = uri.to_i
    if _i > 0
      url.values_at(1.._i).join('/')
    else
      url.values_at(_i..-1).join('/')
    end
  end
end

#url_match?(url, _r, value) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 48

def url_match?(url, _r, value)
  (!value.nil?) and (!value.strip.empty? rescue true) and url.send(RULE[_r], value.downcase)
end