sniffer 0.3.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,42 +6,43 @@ module Sniffer
6
6
  module EthonAdapter
7
7
  # overrides http_request method
8
8
  module Http
9
- def self.included(base)
10
- base.class_eval do
11
- alias_method :http_request_without_sniffer, :http_request
12
- alias_method :http_request, :http_request_with_sniffer
13
- end
9
+ def http_request_with_sniffer(url, action_name, options = {})
10
+ make_sniffer_request(url, action_name, options)
11
+
12
+ http_request_without_sniffer(url, action_name, options)
14
13
  end
15
14
 
16
- # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
17
- def http_request_with_sniffer(url, action_name, options = {})
18
- if Sniffer.enabled?
19
- @data_item = Sniffer::DataItem.new
20
- uri = URI("http://" + url)
15
+ private
21
16
 
22
- @data_item.request = Sniffer::DataItem::Request.new(host: uri.host,
23
- method: action_name.upcase,
24
- port: options[:port] || uri.port,
25
- headers: options[:headers].to_h,
26
- body: options[:body].to_s)
17
+ def make_sniffer_request(url, action_name, options)
18
+ return unless Sniffer.enabled?
27
19
 
28
- Sniffer.store(@data_item)
29
- end
20
+ @data_item = Sniffer::DataItem.new
21
+ uri = URI("http://#{url}")
30
22
 
31
- http_request_without_sniffer(url, action_name, options)
23
+ @data_item.request = Sniffer::DataItem::Request.new(host: uri.host,
24
+ method: action_name.upcase,
25
+ port: options[:port] || uri.port,
26
+ headers: options[:headers].to_h,
27
+ body: options[:body].to_s)
28
+
29
+ Sniffer.store(@data_item)
32
30
  end
33
- end
34
- # rubocop:enable Metrics/AbcSize,Metrics/MethodLength
35
31
 
36
- # overrides perform method
37
- module Operations
38
- def self.included(base)
39
- base.class_eval do
40
- alias_method :perform_without_sniffer, :perform
41
- alias_method :perform, :perform_with_sniffer
32
+ # Only used when prepending, see all_prepend.rb
33
+ module Prepend
34
+ include Http
35
+
36
+ def http_request(url, action_name, options = {})
37
+ make_sniffer_request(url, action_name, options)
38
+
39
+ super(url, action_name, options)
42
40
  end
43
41
  end
42
+ end
44
43
 
44
+ # overrides perform method
45
+ module Operations
45
46
  # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
46
47
  def perform_with_sniffer
47
48
  bm = Benchmark.realtime do
@@ -49,12 +50,12 @@ module Sniffer
49
50
  end
50
51
 
51
52
  if Sniffer.enabled?
52
- uri = URI("http://" + @url)
53
+ uri = URI("http://#{@url}")
53
54
  query = uri.path
54
55
  query += "?#{uri.query}" if uri.query
55
56
  @data_item.request.query = query
56
57
 
57
- status = @response_headers.scan(%r{HTTP\/... (\d{3})}).flatten[0].to_i
58
+ status = @response_headers.scan(%r{HTTP/... (\d{3})}).flatten[0].to_i
58
59
  hash_headers = @response_headers
59
60
  .split(/\r?\n/)[1..-1]
60
61
  .each_with_object({}) do |item, res|
@@ -66,7 +67,7 @@ module Sniffer
66
67
  headers: hash_headers,
67
68
  body: @response_body,
68
69
  timing: bm)
69
- @data_item.log
70
+ Sniffer.notify_response(@data_item)
70
71
 
71
72
  end
72
73
 
@@ -76,12 +77,33 @@ module Sniffer
76
77
  @return_code
77
78
  end
78
79
  # rubocop:enable Metrics/AbcSize,Metrics/MethodLength
80
+
81
+ # Only used when prepending, see all_prepend.rb
82
+ module Prepend
83
+ include Operations
84
+
85
+ def perform
86
+ perform_with_sniffer
87
+ end
88
+ end
79
89
  end
80
90
  end
81
91
  end
82
92
  end
83
93
 
84
94
  if defined?(::Ethon::Easy)
85
- Ethon::Easy::Http.send(:include, Sniffer::Adapters::EthonAdapter::Http)
86
- Ethon::Easy::Operations.send(:include, Sniffer::Adapters::EthonAdapter::Operations)
95
+ if defined?(Sniffer::Adapters::EthonAdapter::PREPEND)
96
+ Ethon::Easy.prepend Sniffer::Adapters::EthonAdapter::Http::Prepend
97
+ Ethon::Easy.prepend Sniffer::Adapters::EthonAdapter::Operations::Prepend
98
+ else
99
+ Ethon::Easy.class_eval do
100
+ include Sniffer::Adapters::EthonAdapter::Http
101
+ alias_method :http_request_without_sniffer, :http_request
102
+ alias_method :http_request, :http_request_with_sniffer
103
+
104
+ include Sniffer::Adapters::EthonAdapter::Operations
105
+ alias_method :perform_without_sniffer, :perform
106
+ alias_method :perform, :perform_with_sniffer
107
+ end
108
+ end
87
109
  end
@@ -6,58 +6,94 @@ module Sniffer
6
6
  module EventMachineAdapter
7
7
  # Overrides #send_request, #parse_response, #on_body_data
8
8
  module Client
9
- def self.included(base)
10
- base.class_eval do
11
- alias_method :send_request_without_sniffer, :send_request
12
- alias_method :send_request, :send_request_with_sniffer
13
- alias_method :parse_response_header_without_sniffer, :parse_response_header
14
- alias_method :parse_response_header, :parse_response_header_with_sniffer
15
- alias_method :on_body_data_without_sniffer, :on_body_data
16
- alias_method :on_body_data, :on_body_data_with_sniffer
17
- end
18
- end
19
-
20
- # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
21
9
  def send_request_with_sniffer(head, body)
22
- if Sniffer.enabled?
23
- @data_item = Sniffer::DataItem.new
24
- @data_item.response = Sniffer::DataItem::Response.new
25
- @data_item.request = Sniffer::DataItem::Request.new(host: @req.host,
26
- method: @req.method.to_sym,
27
- query: encode_query(@req.uri, @req.query),
28
- headers: @req.headers,
29
- body: @req.body.to_s,
30
- port: @req.port)
31
- Sniffer.store(@data_item)
32
-
33
- @start_time = Time.now
34
- end
35
-
10
+ send_request_sniffer(head, body)
36
11
  send_request_without_sniffer(head, body)
37
12
  end
38
- # rubocop:enable Metrics/AbcSize,Metrics/MethodLength
39
13
 
40
14
  def parse_response_header_with_sniffer(header, version, status)
41
- if Sniffer.enabled?
42
- @data_item.response.timing = Time.now - @start_time
43
- @data_item.response.status = status
44
- @data_item.response.headers = header
45
- end
46
-
15
+ parse_response_header_sniffer(header, version, status)
47
16
  parse_response_header_without_sniffer(header, version, status)
48
17
  end
49
18
 
50
19
  def on_body_data_with_sniffer(data)
51
- if Sniffer.enabled?
52
- @data_item.response.body = data
53
- @data_item.log
20
+ on_body_data_sniffer(data)
21
+ on_body_data_without_sniffer(data)
22
+ end
23
+
24
+ private
25
+
26
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
27
+ def send_request_sniffer(_head, _body)
28
+ return unless Sniffer.enabled?
29
+
30
+ @data_item = Sniffer::DataItem.new
31
+ @data_item.response = Sniffer::DataItem::Response.new
32
+ @data_item.request = Sniffer::DataItem::Request.new(host: @req.host,
33
+ method: @req.method.to_sym,
34
+ query: encode_query(@req.uri, @req.query),
35
+ headers: @req.headers,
36
+ body: @req.body.to_s,
37
+ port: @req.port)
38
+ Sniffer.store(@data_item)
39
+
40
+ @start_time = Time.now
41
+ end
42
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
43
+
44
+ def parse_response_header_sniffer(header, _version, status)
45
+ return unless Sniffer.enabled?
46
+
47
+ @data_item.response.timing = Time.now - @start_time
48
+ @data_item.response.status = status
49
+ @data_item.response.headers = header
50
+ end
51
+
52
+ def on_body_data_sniffer(data)
53
+ return unless Sniffer.enabled?
54
+
55
+ @data_item.response.body = data
56
+ Sniffer.notify_response(@data_item)
57
+ end
58
+
59
+ # Only used when prepending, see all_prepend.rb
60
+ module Prepend
61
+ include Client
62
+
63
+ def send_request(head, body)
64
+ send_request_sniffer(head, body)
65
+ super(head, body)
54
66
  end
55
67
 
56
- on_body_data_without_sniffer(data)
68
+ def parse_response_header(header, version, status)
69
+ parse_response_header_sniffer(header, version, status)
70
+ super(header, version, status)
71
+ end
72
+
73
+ def on_body_data(data)
74
+ on_body_data_sniffer(data)
75
+ super(data)
76
+ end
57
77
  end
58
78
  end
59
79
  end
60
80
  end
61
81
  end
62
82
 
63
- EventMachine::HttpClient.send(:include, Sniffer::Adapters::EventMachineAdapter::Client) if defined?(::EventMachine)
83
+ EventMachine::HttpClient.include Sniffer::Adapters::EventMachineAdapter::Client if defined?(::EventMachine::HttpClient)
84
+
85
+ if defined?(::EventMachine::HttpClient)
86
+ if defined?(Sniffer::Adapters::EventMachineAdapter::PREPEND)
87
+ EventMachine::HttpClient.prepend Sniffer::Adapters::EventMachineAdapter::Client::Prepend
88
+ else
89
+ EventMachine::HttpClient.class_eval do
90
+ include Sniffer::Adapters::EventMachineAdapter::Client
91
+ alias_method :send_request_without_sniffer, :send_request
92
+ alias_method :send_request, :send_request_with_sniffer
93
+ alias_method :parse_response_header_without_sniffer, :parse_response_header
94
+ alias_method :parse_response_header, :parse_response_header_with_sniffer
95
+ alias_method :on_body_data_without_sniffer, :on_body_data
96
+ alias_method :on_body_data, :on_body_data_with_sniffer
97
+ end
98
+ end
99
+ end
@@ -4,46 +4,76 @@ module Sniffer
4
4
  module Adapters
5
5
  # Excon adapter
6
6
  module ExconAdapter
7
- def self.included(base)
8
- base.class_eval do
9
- alias_method :request_without_sniffer, :request
10
- alias_method :request, :request_with_sniffer
11
- end
12
- end
13
-
14
- # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
15
7
  def request_with_sniffer(params = {}, &block)
16
- if Sniffer.enabled?
17
- datum = data.merge(params)
18
- data_item = Sniffer::DataItem.new
19
- data_item.request = Sniffer::DataItem::Request.new(host: datum[:host],
20
- method: datum[:method],
21
- query: datum[:path] + ::Excon::Utils.query_string(datum),
22
- headers: datum[:headers] || {},
23
- body: datum[:body].to_s,
24
- port: datum[:port])
25
-
26
- Sniffer.store(data_item)
27
- end
8
+ data_item = request_sniffer_before(params)
28
9
 
29
10
  bm = Benchmark.realtime do
30
11
  @response = request_without_sniffer(params, &block)
31
12
  end
32
13
 
33
- if Sniffer.enabled?
34
- data_item.response = Sniffer::DataItem::Response.new(status: @response.status,
35
- headers: @response.headers,
36
- body: @response.body,
37
- timing: bm)
14
+ request_sniffer_after(params, bm, data_item)
15
+ @response
16
+ end
17
+
18
+ private
38
19
 
39
- data_item.log
40
- end
20
+ # rubocop:disable Metrics/MethodLength
21
+ def request_sniffer_before(params)
22
+ return unless Sniffer.enabled?
41
23
 
42
- @response
24
+ datum = data.merge(params)
25
+ data_item = Sniffer::DataItem.new
26
+ data_item.request = Sniffer::DataItem::Request.new(host: datum[:host],
27
+ method: datum[:method],
28
+ query: datum[:path] + ::Excon::Utils.query_string(datum),
29
+ headers: datum[:headers] || {},
30
+ body: datum[:body].to_s,
31
+ port: datum[:port])
32
+
33
+ Sniffer.store(data_item)
34
+
35
+ data_item
36
+ end
37
+ # rubocop:enable Metrics/MethodLength
38
+
39
+ def request_sniffer_after(_params, benchmark, data_item)
40
+ return unless Sniffer.enabled?
41
+
42
+ data_item.response = Sniffer::DataItem::Response.new(status: @response.status,
43
+ headers: @response.headers,
44
+ body: @response.body,
45
+ timing: benchmark)
46
+
47
+ Sniffer.notify_response(data_item)
48
+ end
49
+
50
+ # Only used when prepending, see all_prepend.rb
51
+ module Prepend
52
+ include ExconAdapter
53
+
54
+ def request(params = {}, &block)
55
+ data_item = request_sniffer_before(params)
56
+
57
+ bm = Benchmark.realtime do
58
+ @response = super(params, &block)
59
+ end
60
+
61
+ request_sniffer_after(params, bm, data_item)
62
+ @response
63
+ end
43
64
  end
44
- # rubocop:enable Metrics/AbcSize,Metrics/MethodLength
45
65
  end
46
66
  end
47
67
  end
48
68
 
49
- ::Excon::Connection.send(:include, Sniffer::Adapters::ExconAdapter) if defined?(::Excon::Connection)
69
+ if defined?(::Excon::Connection)
70
+ if defined?(Sniffer::Adapters::ExconAdapter::PREPEND)
71
+ ::Excon::Connection.prepend Sniffer::Adapters::ExconAdapter::Prepend
72
+ else
73
+ ::Excon::Connection.class_eval do
74
+ include Sniffer::Adapters::ExconAdapter
75
+ alias_method :request_without_sniffer, :request
76
+ alias_method :request, :request_with_sniffer
77
+ end
78
+ end
79
+ end
@@ -4,13 +4,6 @@ module Sniffer
4
4
  module Adapters
5
5
  # HTTP adapter
6
6
  module HTTPAdapter
7
- def self.included(base)
8
- base.class_eval do
9
- alias_method :request_without_sniffer, :request
10
- alias_method :request, :request_with_sniffer
11
- end
12
- end
13
-
14
7
  # private
15
8
 
16
9
  # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
@@ -55,7 +48,7 @@ module Sniffer
55
48
  body: @res.body,
56
49
  timing: bm)
57
50
 
58
- data_item.log
51
+ Sniffer.notify_response(data_item)
59
52
  end
60
53
 
61
54
  return @res unless opts.follow
@@ -64,9 +57,28 @@ module Sniffer
64
57
  perform(request, opts)
65
58
  end
66
59
  end
67
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
60
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
61
+
62
+ # Only used when prepending, see all_prepend.rb
63
+ module Prepend
64
+ include HTTPAdapter
65
+
66
+ def request(*args)
67
+ request_with_sniffer(*args)
68
+ end
69
+ end
68
70
  end
69
71
  end
70
72
  end
71
73
 
72
- HTTP::Client.send(:include, Sniffer::Adapters::HTTPAdapter) if defined?(::HTTP::Client)
74
+ if defined?(::HTTP::Client)
75
+ if defined?(Sniffer::Adapters::HTTPAdapter::PREPEND)
76
+ HTTP::Client.prepend Sniffer::Adapters::HTTPAdapter::Prepend
77
+ else
78
+ HTTP::Client.class_eval do
79
+ include Sniffer::Adapters::HTTPAdapter
80
+ alias_method :request_without_sniffer, :request
81
+ alias_method :request, :request_with_sniffer
82
+ end
83
+ end
84
+ end
@@ -4,56 +4,88 @@ module Sniffer
4
4
  module Adapters
5
5
  # HttpClient adapter
6
6
  module HTTPClientAdapter
7
- def self.included(base)
8
- base.class_eval do
9
- alias_method :do_get_block_without_sniffer, :do_get_block
10
- alias_method :do_get_block, :do_get_block_with_sniffer
7
+ def do_get_block_with_sniffer(req, proxy, conn, &block)
8
+ data_item = do_get_block_sniffer_before(req)
9
+
10
+ retryable_response = nil
11
+
12
+ bm = Benchmark.realtime do
13
+ do_get_block_without_sniffer(req, proxy, conn, &block)
14
+ rescue HTTPClient::RetryableResponse => e
15
+ retryable_response = e
11
16
  end
17
+
18
+ do_get_block_sniffer_after(data_item, conn, bm)
19
+
20
+ raise retryable_response unless retryable_response.nil?
12
21
  end
13
22
 
14
- # private
23
+ private
15
24
 
16
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
17
- def do_get_block_with_sniffer(req, proxy, conn, &block)
18
- if Sniffer.enabled?
19
- data_item = Sniffer::DataItem.new
20
- data_item.request = Sniffer::DataItem::Request.new(host: req.header.request_uri.host,
21
- query: req.header.create_query_uri,
22
- method: req.header.request_method,
23
- headers: req.headers,
24
- body: req.body,
25
- port: req.header.request_uri.port)
26
-
27
- Sniffer.store(data_item)
28
- end
25
+ # rubocop:disable Metrics/AbcSize
26
+ def do_get_block_sniffer_before(req)
27
+ return unless Sniffer.enabled?
29
28
 
30
- retryable_response = nil
29
+ data_item = Sniffer::DataItem.new
30
+ data_item.request = Sniffer::DataItem::Request.new(host: req.header.request_uri.host,
31
+ query: req.header.create_query_uri,
32
+ method: req.header.request_method,
33
+ headers: req.headers,
34
+ body: req.body,
35
+ port: req.header.request_uri.port)
31
36
 
32
- bm = Benchmark.realtime do
33
- begin
34
- do_get_block_without_sniffer(req, proxy, conn, &block)
37
+ Sniffer.store(data_item)
38
+
39
+ data_item
40
+ end
41
+ # rubocop:enable Metrics/AbcSize
42
+
43
+ def do_get_block_sniffer_after(data_item, conn, benchmark)
44
+ return unless Sniffer.enabled?
45
+
46
+ res = conn.pop
47
+ data_item.response = Sniffer::DataItem::Response.new(status: res.status_code.to_i,
48
+ headers: res.headers,
49
+ body: res.body,
50
+ timing: benchmark)
51
+
52
+ conn.push(res)
53
+
54
+ Sniffer.notify_response(data_item)
55
+ end
56
+
57
+ # Only used when prepending, see all_prepend.rb
58
+ module Prepend
59
+ include HTTPClientAdapter
60
+
61
+ def do_get_block(req, proxy, conn, &block)
62
+ data_item = do_get_block_sniffer_before(req)
63
+
64
+ retryable_response = nil
65
+
66
+ bm = Benchmark.realtime do
67
+ super(req, proxy, conn, &block)
35
68
  rescue HTTPClient::RetryableResponse => e
36
69
  retryable_response = e
37
70
  end
38
- end
39
-
40
- if Sniffer.enabled?
41
- res = conn.pop
42
- data_item.response = Sniffer::DataItem::Response.new(status: res.status_code.to_i,
43
- headers: res.headers,
44
- body: res.body,
45
- timing: bm)
46
71
 
47
- conn.push(res)
72
+ do_get_block_sniffer_after(data_item, conn, bm)
48
73
 
49
- data_item.log
74
+ raise retryable_response unless retryable_response.nil?
50
75
  end
51
-
52
- raise retryable_response unless retryable_response.nil?
53
76
  end
54
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
55
77
  end
56
78
  end
57
79
  end
58
80
 
59
- HTTPClient.send(:include, Sniffer::Adapters::HTTPClientAdapter) if defined?(::HTTPClient)
81
+ if defined?(::HTTPClient)
82
+ if defined?(Sniffer::Adapters::HTTPClientAdapter::PREPEND)
83
+ HTTPClient.prepend Sniffer::Adapters::HTTPClientAdapter::Prepend
84
+ else
85
+ HTTPClient.class_eval do
86
+ include Sniffer::Adapters::HTTPClientAdapter
87
+ alias_method :do_get_block_without_sniffer, :do_get_block
88
+ alias_method :do_get_block, :do_get_block_with_sniffer
89
+ end
90
+ end
91
+ end
@@ -7,44 +7,75 @@ module Sniffer
7
7
  module Adapters
8
8
  # Net::HTTP adapter
9
9
  module NetHttpAdapter
10
- def self.included(base)
11
- base.class_eval do
12
- alias_method :request_without_sniffer, :request
13
- alias_method :request, :request_with_sniffer
14
- end
15
- end
16
-
17
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
18
10
  def request_with_sniffer(req, body = nil, &block)
19
- if started? && Sniffer.enabled?
20
- data_item = Sniffer::DataItem.new
21
- data_item.request = Sniffer::DataItem::Request.new(host: @address,
22
- method: req.method,
23
- query: req.path,
24
- port: @port,
25
- headers: req.each_header.collect.to_h,
26
- body: req.body.to_s)
27
-
28
- Sniffer.store(data_item)
29
- end
11
+ data_item = request_sniffer_before(req)
30
12
 
31
13
  bm = Benchmark.realtime do
32
14
  @response = request_without_sniffer(req, body, &block)
33
15
  end
34
16
 
35
- if started? && Sniffer.enabled?
36
- data_item.response = Sniffer::DataItem::Response.new(status: @response.code.to_i,
37
- headers: @response.each_header.collect.to_h,
38
- body: @response.body.to_s,
39
- timing: bm)
40
-
41
- data_item.log
42
- end
17
+ request_sniffer_after(data_item, bm)
43
18
 
44
19
  @response
45
20
  end
46
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
21
+
22
+ private
23
+
24
+ def request_sniffer_before(req)
25
+ return unless started? && Sniffer.enabled?
26
+
27
+ data_item = Sniffer::DataItem.new
28
+ data_item.request = Sniffer::DataItem::Request.new(host: @address,
29
+ method: req.method,
30
+ query: req.path,
31
+ port: @port,
32
+ headers: req.each_header.collect.to_h,
33
+ body: req.body.to_s)
34
+
35
+ Sniffer.store(data_item)
36
+
37
+ data_item
38
+ end
39
+
40
+ def request_sniffer_after(data_item, benchmark)
41
+ return unless started? && Sniffer.enabled?
42
+
43
+ data_item.response = Sniffer::DataItem::Response.new(status: @response.code.to_i,
44
+ headers: @response.each_header.collect.to_h,
45
+ body: @response.body.to_s,
46
+ timing: benchmark)
47
+
48
+ Sniffer.notify_response(data_item)
49
+ end
50
+
51
+ # Only used when prepending, see all_prepend.rb
52
+ module Prepend
53
+ include NetHttpAdapter
54
+
55
+ def request(req, body = nil, &block)
56
+ data_item = request_sniffer_before(req)
57
+
58
+ bm = Benchmark.realtime do
59
+ @response = super(req, body, &block)
60
+ end
61
+
62
+ request_sniffer_after(data_item, bm)
63
+
64
+ @response
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ if defined?(::Net::HTTP)
72
+ if defined?(Sniffer::Adapters::NetHttpAdapter::PREPEND)
73
+ Net::HTTP.prepend Sniffer::Adapters::NetHttpAdapter::Prepend
74
+ else
75
+ Net::HTTP.class_eval do
76
+ include Sniffer::Adapters::NetHttpAdapter
77
+ alias_method :request_without_sniffer, :request
78
+ alias_method :request, :request_with_sniffer
47
79
  end
48
80
  end
49
81
  end
50
- Net::HTTP.send(:include, Sniffer::Adapters::NetHttpAdapter)