Skip to content

Commit

Permalink
feature: work with default sniffer and possibility to instantiace cus…
Browse files Browse the repository at this point in the history
…tom with sniffing into every
  • Loading branch information
dsalahutdinov committed Dec 14, 2017
1 parent e6ec9d9 commit fb5b5c6
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 49 deletions.
55 changes: 38 additions & 17 deletions lib/sniffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,57 @@
module Sniffer
class << self
extend Forwardable
def current
stack.last
end
def_delegators :current, :config, :enable!, :disable!, :enabled?,
:clear!, :reset!, :data, :store, :logger, :configure

def capture(config = nil)
controller = Controller.new(config)
stack.push(controller)
def capture(options = {})
# TODO: make Anyway config to support creation with hash
config = Sniffer::Config.new
{ enabled: true }.merge!(options).each do |k, v|
config.send("#{k}=", v)
end
sniffer = Instance.new(config)
sniffers.push(sniffer)
yield if block_given?
controller
sniffer
ensure
stack.pop
sniffers.pop
end

def enabled?
sniffers.any?(&:enabled?)
end

def store(data_item)
sniffers.each do |sniffer|
sniffer.store(data_item) if sniffer.enabled?
end
end

def log(data_item)
sniffers.each do |sniffer|
next if !sniffer.enabled? || sniffer.logger.nil?
sniffer.logger.log(sniffer.config.severity, data_item.to_json)
end
end

private

def stack
Thread.current[:stack] ||= [Controller.new]
def default
sniffers.first
end
def_delegators :default, :config, :enable!, :disable!, :clear!, :reset!, :data

def sniffers
Thread.current[:sniffers] ||= [Instance.new]
end
end

# Holds all the sniffer logic
class Controller
def initialize(config = nil)
@config = config || Config.new
class Instance
def initialize(config = Config.new)
@config = config
end

def config
@config ||= Config.new
yield @config if block_given?
@config
end
Expand Down Expand Up @@ -83,7 +104,7 @@ def logger
end
end

private_constant :Controller
private_constant :Instance
end

require_relative "sniffer/adapters/net_http_adapter"
Expand Down
2 changes: 1 addition & 1 deletion lib/sniffer/adapters/curb_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def sniffer_response(timing)
body: body_str,
timing: timing)

data_item.log
Sniffer.log(data_item)
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/sniffer/adapters/ethon_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ def perform_with_sniffer
headers: hash_headers,
body: @response_body,
timing: bm)
@data_item.log

Sniffer.log(@data_item)
end

if Ethon.logger.debug?
Expand Down
2 changes: 1 addition & 1 deletion lib/sniffer/adapters/eventmachine_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def parse_response_header_with_sniffer(header, version, status)
def on_body_data_with_sniffer(data)
if Sniffer.enabled?
@data_item.response.body = data
@data_item.log
Sniffer.log(@data_item)
end

on_body_data_without_sniffer(data)
Expand Down
2 changes: 1 addition & 1 deletion lib/sniffer/adapters/http_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def request_with_sniffer(verb, uri, opts = {})
body: @res.body.to_s,
timing: bm)

data_item.log
Sniffer.log(data_item)
end

return @res unless opts.follow
Expand Down
2 changes: 1 addition & 1 deletion lib/sniffer/adapters/httpclient_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def do_get_block_with_sniffer(req, proxy, conn, &block)

conn.push(res)

data_item.log
Sniffer.log(data_item)
end

raise retryable_response unless retryable_response.nil?
Expand Down
2 changes: 1 addition & 1 deletion lib/sniffer/adapters/net_http_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def request_with_sniffer(req, body = nil, &block)
body: @response.body.to_s,
timing: bm)

data_item.log
Sniffer.log(data_item)
end

@response
Expand Down
2 changes: 1 addition & 1 deletion lib/sniffer/adapters/patron_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def request_with_sniffer(action_name, url, headers, options = {})
body: @res.body.to_s,
timing: bm)

data_item.log
Sniffer.log(data_item)
end

@res
Expand Down
11 changes: 3 additions & 8 deletions lib/sniffer/data_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ def to_h
}
end

def log
Sniffer.logger.log(Sniffer.config.severity, to_json)
end

def to_log
return {} unless Sniffer.config.logger
request.to_log.merge(response.to_log)
(request.nil? ? {} : request.to_log).merge(response.nil? ? {} : response.to_log)
end

def to_json
Expand Down Expand Up @@ -66,7 +61,7 @@ def to_log
hash[:query] = query
end

if log_settings["request_headers"]
if log_settings["request_headers"] && headers
headers.each do |(k, v)|
hash[:"rq_#{k.to_s.tr("-", '_').downcase}"] = v
end
Expand Down Expand Up @@ -97,7 +92,7 @@ def to_log
{}.tap do |hash|
hash[:status] = status if log_settings["response_status"]

if log_settings["response_headers"]
if log_settings["response_headers"] && headers
headers.each do |(k, v)|
hash[:"rs_#{k.to_s.tr("-", '_').downcase}"] = v
end
Expand Down
28 changes: 20 additions & 8 deletions spec/sniffer/data_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@
expect(described_class.new.to_h).to eq(request: nil, response: nil)
end

context "#to_log"
it 'returns {} if logger disabled' do
Sniffer.config.logger = false
expect(subject.to_log).to eq({})
end
context "#to_log" do
it 'returns {} if logger disabled' do
subject.request = Sniffer::DataItem::Request.new
subject.response = Sniffer::DataItem::Response.new
expect(subject.to_log).to(
eq(
port: nil,
host: nil,
query: nil,
method: nil,
request_body: nil,
status: nil,
timing: nil,
response_body: nil
)
)
end

it 'returns {} if logger is nil' do
Sniffer.config.logger = nil
expect(subject.to_log).to eq({})
it 'returns {} if request and response are nil' do
expect(subject.to_log).to eq({})
end
end
end
22 changes: 14 additions & 8 deletions spec/sniffer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
end

describe ".store" do
it 'stores data items' do
it 'stores data items', enabled: true do
data_item = Sniffer::DataItem.new
expect {
Sniffer.store(data_item)
}.to change { Sniffer.data.include?(data_item) }.to(true)
end

it 'stores no more than capacity if set (and rotate by default)' do
it 'stores no more than capacity if set (and rotate by default)', enabled: true do
Sniffer.config.store = { capacity: 1 }

first = Sniffer::DataItem.new
Expand All @@ -51,7 +51,7 @@
expect(Sniffer.data.include?(second)).to be_truthy
end

it 'do not stores data without rotation' do
it 'do not stores data without rotation', enabled: true do
Sniffer.config.store = { capacity: 1, rotate: false }

first = Sniffer::DataItem.new
Expand All @@ -65,7 +65,7 @@
end

context ".clear!" do
it 'clears data' do
it 'clears data', enabled: true do
Sniffer.store(Sniffer::DataItem.new)

expect {
Expand Down Expand Up @@ -99,18 +99,24 @@
end.not_to change { Sniffer.enabled? }
end

context 'capture' do
context 'capture', enabled: true do
it do
item = Sniffer::DataItem.new
Sniffer.data.push(item)

captured_item = Sniffer::DataItem.new
nested_captured_item = Sniffer::DataItem.new
nested = nil
captured = Sniffer.capture do
Sniffer.data.push(captured_item)
Sniffer.store(captured_item)
nested = Sniffer.capture do
Sniffer.store(nested_captured_item)
end
end

expect(Sniffer.data).to eq [item]
expect(captured.data).to eq [captured_item]
expect(Sniffer.data).to eq [item, captured_item, nested_captured_item]
expect(captured.data).to eq [captured_item, nested_captured_item]
expect(nested.data).to eq [nested_captured_item]
end
end
end

0 comments on commit fb5b5c6

Please sign in to comment.