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 =
{
  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’



129
130
131
# File 'lib/ting_yun/instrumentation/support/split_controller.rb', line 129

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

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



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

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)


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

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

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



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

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_params(@rule["split"]["bodyParams"], params)
  name << split_params(@rule["split"]["cookieParams"], cookie)
  name << split_header(@rule["split"]["headerParams"], header)
  name = name[0..-2] << split_method
  name.strip
end

#namespaceObject



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

def namespace
  @rule["name"]
end

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

Returns:

  • (Boolean)


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

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



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

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
  unless _t == 0
    raise 'this param  is unmatched  with the rule' unless _v.send(_r, _v2)
  end
end

#rulesObject



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

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



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

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



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

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

#split_params(_r, params) ⇒ Object



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

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



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

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)


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

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