项目作者: aderyabin

项目描述 :
Log and Analyze Outgoing HTTP Requests
高级语言: Ruby
项目地址: git://github.com/aderyabin/sniffer.git
创建时间: 2017-10-17T17:22:51Z
项目社区:https://github.com/aderyabin/sniffer

开源协议:MIT License

下载


Sniffer

Build Gem Version Join the chat at https://gitter.im/aderyabin/sniffer

Sniffer aims to help:

  • Log outgoing HTTP requests. Sniffer logs as JSON format for export to ELK, Logentries and etc.
  • Debug requests. Sniffer allows to save all requests/responses in storage for future debugging

Sniffer supports most common HTTP accessing libraries:

Demo

demo

Installation

Add this line to your application’s Gemfile:

  1. gem 'sniffer'

If you wish Sniffer to use Module#prepend instead of alias_method, you can cause individual adapters to use prepend instead with:

  1. gem 'sniffer', require: ['http_prepend', 'httpclient_prepend', 'sniffer']

It’s important that 'sniffer' is the last item in the list. See the lib directory for a list of prependable adapters.

If you want all adapters to use prepend:

  1. gem 'sniffer', require: ['all_prepend', 'sniffer']

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install sniffer

Usage

Here’s some simple examples to get you started:

  1. require 'http'
  2. require 'sniffer'
  3. Sniffer.enable!
  4. HTTP.get('http://example.com/?lang=ruby&author=matz')
  5. Sniffer.data[0].to_h
  6. # => {:request=>
  7. # {:host=>"example.com",
  8. # :query=>"/?lang=ruby&author=matz",
  9. # :port=>80,
  10. # :headers=>{"Accept-Encoding"=>"gzip;q=1.0,deflate;q=0.6,identity;q=0.3", "Connection"=>"close"},
  11. # :body=>"",
  12. # :method=>:get},
  13. # :response=>
  14. # {:status=>200,
  15. # :headers=>
  16. # {"Content-Encoding"=>"gzip",
  17. # "Cache-Control"=>"max-age=604800",
  18. # "Content-Type"=>"text/html",
  19. # "Date"=>"Thu, 26 Oct 2017 13:47:00 GMT",
  20. # "Etag"=>"\"359670651+gzip\"",
  21. # "Expires"=>"Thu, 02 Nov 2017 13:47:00 GMT",
  22. # "Last-Modified"=>"Fri, 09 Aug 2013 23:54:35 GMT",
  23. # "Server"=>"ECS (lga/1372)",
  24. # "Vary"=>"Accept-Encoding",
  25. # "X-Cache"=>"HIT",
  26. # "Content-Length"=>"606",
  27. # "Connection"=>"close"},
  28. # :body=> "OK",
  29. # :timing=>0.23753299983218312}}

You can clear saved data

  1. Sniffer.clear!

You can configure capacity of storage to prevent the huge memory usage and set up log rotation.
By default log rotation is active (when capacity is set) and log works like a queue.
If rotation is disabled - requests will be logged until result log size reaches the capacity.

  1. # will fill the storage and stop logging
  2. Sniffer.config.store = {capacity: 1000, rotate: false}
  3. # will rotate logs to fit 1000 results (rotate is true by default)
  4. Sniffer.config.store = {capacity: 1000}

You can reset config to default

  1. Sniffer.reset!

You can enable and disable Sniffer

  1. Sniffer.enable!
  2. Sniffer.disable!

By default output log looks like that:

  1. D, [2017-10-26T16:47:14.007152 #59511] DEBUG -- : {"port":80,"host":"example.com","query":"/?lang=ruby&author=matz","rq_connection":"close","method":"get","request_body":"","status":200,"rs_accept_ranges":"bytes","rs_cache_control":"max-age=604800","rs_content_type":"text/html","rs_date":"Thu, 26 Oct 2017 13:47:13 GMT","rs_etag":"\"359670651+gzip\"","rs_expires":"Thu, 02 Nov 2017 13:47:13 GMT","rs_last_modified":"Fri, 09 Aug 2013 23:54:35 GMT","rs_server":"ECS (lga/1385)","rs_vary":"Accept-Encoding","rs_x_cache":"HIT","rs_content_length":"1270","rs_connection":"close","timing":0.513012999901548,"response_body":"OK"}

where rq_xxx is request header and rs_xxx - response header

Configuration

Sniffer default options:

  1. Sniffer.config do |c|
  2. c.logger = Logger.new($stdout)
  3. c.severity = Logger::Severity::DEBUG
  4. # HTTP options to log
  5. c.log = {
  6. request_url: true,
  7. request_headers: true,
  8. request_body: true,
  9. request_method: true,
  10. response_status: true,
  11. response_headers: true,
  12. response_body: true,
  13. timing: true
  14. }
  15. c.store = true # save requests/responses to Sniffer.data
  16. c.enabled = false # Sniffer disabled by default
  17. c.url_whitelist = nil
  18. c.url_blacklist = nil
  19. end

Whitelist

You can add specific host url to whitelist as regexp or string. Sniffer will store only requests that matched.

  1. Sniffer.config.url_whitelist = /whitelisted.com/
  2. HTTP.get('http://example.com')
  3. Sniffer.data[0].to_h
  4. # => {}
  5. HTTP.get('http://whitelisted.com/')
  6. Sniffer.data[0].to_h
  7. # => {{:request=>{:host=>"whitelisted.com", ....}}

Blacklist

You can add specific host url to blacklist as regexp or string. Sniffer will ignore all matched requests.

  1. Sniffer.config.url_blacklist = /blacklisted.com/
  2. HTTP.get('http://blacklisted.com')
  3. Sniffer.data[0].to_h
  4. # => {}
  5. HTTP.get('http://example.com')
  6. Sniffer.data[0].to_h
  7. # => {{:request=>{:host=>"example.com", ...}}

Middleware

You can add the middleware to run custom code before/after the sniffed data was logged.

  1. Sniffer.middleware do |chain|
  2. chain.add MyHook
  3. end
  4. class MyHook
  5. def request(data_item)
  6. puts "Before work"
  7. yield
  8. puts "After work"
  9. end
  10. def response(data_item)
  11. puts "Before work"
  12. yield
  13. puts "After work"
  14. end
  15. end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Development (with Docker)

Get local development environment working and tests running is very easy with docker-compose:

  1. docker-compose run app bundle
  2. docker-compose run app bundle exec rspec

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/aderyabin/sniffer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Acknowledge

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Sniffer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.