Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: support sniffer block usage #35

Closed
wants to merge 15 commits into from

Conversation

dsalahutdinov
Copy link
Contributor

@dsalahutdinov dsalahutdinov commented Dec 13, 2017

[WIP] for #27

Hi!
May be i did not get the idea properly, so i have some questions:

  1. Do we need to support the default Sniffer without any capture block?
  2. Do we need to support nested capture (as it is done now)
  3. What way we will configure local capture sniffer. This doesn't look good
config = Confgi.new |config|
  config.enabled!
  ...
end
captured = Sniffer.capture(config) do
  # make some http requests
end

@aderyabin
Copy link
Owner

Sorry, for the misconception.
For example, we have a default config:

Sniffer.config do |c|
  c.logger = Logger.new($stdout)
end

And I want to capture requests in two different blocks of code:

sniffer1 = Sniffer.new(logger: Logger.new('foo.log'), enabled: true) # it merges with default config
# some code
sniffer1.disable!

# another code

sniffer2 = Sniffer.capture(logger: Logger.new('bar.log') do #sniffer2 enabled
# some requests
end #sniffer2 disabled

Yes, I think we have to support nesting (it's a challenge). It writes the request to both sniffer instances:

sniffer1 = Sniffer.new(logger: Logger.new('foo.log'), enabled: true)
# requests log into sniffer1
sniffer2 = Sniffer.capture(logger: Logger.new('bar.log') do #sniffer2 enabled
# requests log into both sniffers
end
# requests log into sniffer1
sniffer1.disable!

@dsalahutdinov
Copy link
Contributor Author

Hi, please take a look, PR is still in WIP (ugly code somwhere, lack of the tests), but want to discuss the idea

  1. Have the default sniffer, which is disabled by default (works as it worked before)
  2. Calling capture - enables another instance, which is enabled by default and capturing all the requests inside the block

So i made:

  • the Instance class for containing the data (making Sniffer class is not easy, because it is module everywhere)
  • Sniffer module singleton methods are the one entrypoint to work with all the sniffer instances (the api is mostly the same, as it was), but methods like enabled?, store and log - works with all the nested instances depending of their's enable status

In usual case works like was before

@dsalahutdinov
Copy link
Contributor Author

One thing I missed, it that we don't want to have default sniffer (without creating at least one instance - the sniffer should not work)

@dsalahutdinov
Copy link
Contributor Author

dsalahutdinov commented Dec 16, 2017

Made some changes:

  1. extracted sniffer instance to capture class, where all the stuff is saved:
capture = Sniffer.new
puts capture.data
  1. no default instance is enables, so to start sniffer you need to instantiate is
capture = Sniffer.new(enabled: true)
  1. Capture method with block allows to add more sniffer:
capture = Sniffer.capture(enabled: true) do
  # make http requests

  # supports for nesting
  nested = Sniffer.capture(enabled: true, logger: HipsterLogger.new) do
    # capture to this, outer, and to default (according to enable settings)
  end
end

Wait for your review @aderyabin

@dsalahutdinov
Copy link
Contributor Author

@aderyabin , could you please review

Gemfile Outdated
@@ -2,5 +2,6 @@ source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'anyway_config', github: 'palkan/anyway_config'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already defined in gemspec file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants