lens 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 10f546261d6a085dbc5a3f8697d5353f90cb99ea
4
+ data.tar.gz: e2aa9893e2c0cf249d3924f16f376cdb8afa0b12
5
+ SHA512:
6
+ metadata.gz: 79a3b8440a837c148818c90f34b2cf1e5899c39a15c16a7cbb728166f37c703c2a2d40f46ea76a2799612ab6cd3f5724ca850f8101534e11f49fdad9c7b2a01f
7
+ data.tar.gz: d5fa938e9c2b2d82c37265ac5a14e9b4479627d4043d2bf4035ecc9bf61ed02b223c4cef64b8c8eca0dce61daac699dff6a5199e4cb9871c283567327576861f
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.swp
2
+ *.swo
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gem 'rails', '>= 3.0'
3
+
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,112 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lens (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionmailer (4.0.3)
10
+ actionpack (= 4.0.3)
11
+ mail (~> 2.5.4)
12
+ actionpack (4.0.3)
13
+ activesupport (= 4.0.3)
14
+ builder (~> 3.1.0)
15
+ erubis (~> 2.7.0)
16
+ rack (~> 1.5.2)
17
+ rack-test (~> 0.6.2)
18
+ activemodel (4.0.3)
19
+ activesupport (= 4.0.3)
20
+ builder (~> 3.1.0)
21
+ activerecord (4.0.3)
22
+ activemodel (= 4.0.3)
23
+ activerecord-deprecated_finders (~> 1.0.2)
24
+ activesupport (= 4.0.3)
25
+ arel (~> 4.0.0)
26
+ activerecord-deprecated_finders (1.0.3)
27
+ activesupport (4.0.3)
28
+ i18n (~> 0.6, >= 0.6.4)
29
+ minitest (~> 4.2)
30
+ multi_json (~> 1.3)
31
+ thread_safe (~> 0.1)
32
+ tzinfo (~> 0.3.37)
33
+ arel (4.0.2)
34
+ builder (3.1.4)
35
+ coderay (1.1.0)
36
+ diff-lcs (1.2.5)
37
+ erubis (2.7.0)
38
+ hike (1.2.3)
39
+ i18n (0.6.11)
40
+ mail (2.5.4)
41
+ mime-types (~> 1.16)
42
+ treetop (~> 1.4.8)
43
+ method_source (0.8.2)
44
+ mime-types (1.25.1)
45
+ minitest (4.7.5)
46
+ multi_json (1.11.0)
47
+ polyglot (0.3.5)
48
+ pry (0.10.1)
49
+ coderay (~> 1.1.0)
50
+ method_source (~> 0.8.1)
51
+ slop (~> 3.4)
52
+ pry-nav (0.2.4)
53
+ pry (>= 0.9.10, < 0.11.0)
54
+ rack (1.5.2)
55
+ rack-test (0.6.2)
56
+ rack (>= 1.0)
57
+ rails (4.0.3)
58
+ actionmailer (= 4.0.3)
59
+ actionpack (= 4.0.3)
60
+ activerecord (= 4.0.3)
61
+ activesupport (= 4.0.3)
62
+ bundler (>= 1.3.0, < 2.0)
63
+ railties (= 4.0.3)
64
+ sprockets-rails (~> 2.0.0)
65
+ railties (4.0.3)
66
+ actionpack (= 4.0.3)
67
+ activesupport (= 4.0.3)
68
+ rake (>= 0.8.7)
69
+ thor (>= 0.18.1, < 2.0)
70
+ rake (10.3.2)
71
+ rspec (3.2.0)
72
+ rspec-core (~> 3.2.0)
73
+ rspec-expectations (~> 3.2.0)
74
+ rspec-mocks (~> 3.2.0)
75
+ rspec-core (3.2.3)
76
+ rspec-support (~> 3.2.0)
77
+ rspec-expectations (3.2.1)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.2.0)
80
+ rspec-mocks (3.2.1)
81
+ diff-lcs (>= 1.2.0, < 2.0)
82
+ rspec-support (~> 3.2.0)
83
+ rspec-support (3.2.2)
84
+ slop (3.6.0)
85
+ sprockets (2.11.0)
86
+ hike (~> 1.2)
87
+ multi_json (~> 1.0)
88
+ rack (~> 1.0)
89
+ tilt (~> 1.1, != 1.3.0)
90
+ sprockets-rails (2.0.1)
91
+ actionpack (>= 3.0)
92
+ activesupport (>= 3.0)
93
+ sprockets (~> 2.8)
94
+ thor (0.19.1)
95
+ thread_safe (0.3.4)
96
+ tilt (1.4.1)
97
+ treetop (1.4.15)
98
+ polyglot
99
+ polyglot (>= 0.3.1)
100
+ tzinfo (0.3.42)
101
+
102
+ PLATFORMS
103
+ ruby
104
+
105
+ DEPENDENCIES
106
+ bundler (~> 1.6)
107
+ lens!
108
+ pry (~> 0.10)
109
+ pry-nav
110
+ rails (>= 3.0)
111
+ rake
112
+ rspec (~> 3.2.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 kgorin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Lens
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'lens'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install lens
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/lens/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lens.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lens/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lens"
8
+ spec.version = Lens::VERSION
9
+ spec.authors = ["kgorin", "artygus"]
10
+ spec.email = ["me@kgor.in", "artygus@engineeram.net"]
11
+ spec.summary = %q{Gem to send Rails request stats}
12
+ spec.homepage = "http://coub.com"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake" , '~> 0'
22
+ spec.add_development_dependency 'rspec' , '~> 3.2'
23
+ spec.add_development_dependency "pry" , '~> 0.10'
24
+ spec.add_development_dependency "pry-nav", '~> 0'
25
+ end
@@ -0,0 +1,25 @@
1
+ module Lens
2
+ class Configuration
3
+ attr_accessor :app_id, :secret, :protocol, :host, :port
4
+
5
+ def protocol
6
+ 'https'
7
+ end
8
+
9
+ def host
10
+ @host || default_host
11
+ end
12
+
13
+ def port
14
+ @port || default_port
15
+ end
16
+
17
+ def default_port
18
+ 80
19
+ end
20
+
21
+ def default_host
22
+ 'lens.coub.com'
23
+ end
24
+ end
25
+ end
data/lib/lens/core.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "lens/version"
2
+ require "lens/configuration"
3
+ require "lens/sender"
4
+ require "lens/trace"
5
+
6
+ module Lens
7
+ if defined?(Rails)
8
+ require "lens/railtie"
9
+ end
10
+ end
@@ -0,0 +1,35 @@
1
+ require 'rails'
2
+
3
+ module Lens
4
+ class Railtie < Rails::Railtie
5
+ ActiveSupport::Notifications.subscribe('start_processing.action_controller') do |name, started, finished, id, data|
6
+ Trace.create(id)
7
+ end
8
+
9
+ ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
10
+ next unless Trace.current
11
+ event = ActiveSupport::Notifications::Event.new(*args)
12
+ Trace.current.add(event) if event.name != 'SCHEMA'
13
+ end
14
+
15
+ ActiveSupport::Notifications.subscribe(/^render_(template|action|collection)\.action_view/) do |*args|
16
+ next unless Trace.current
17
+ event = ActiveSupport::Notifications::Event.new(*args)
18
+ Trace.current.add(event)
19
+ end
20
+
21
+ ActiveSupport::Notifications.subscribe('net_http.request') do |*args|
22
+ next unless Trace.current
23
+ event = ActiveSupport::Notifications::Event.new(*args)
24
+ Trace.current.add(event)
25
+ end
26
+
27
+ ActiveSupport::Notifications.subscribe('process_action.action_controller') do |*args|
28
+ next unless Trace.current
29
+ event = ActiveSupport::Notifications::Event.new(*args)
30
+ if event.payload[:controller] && event.payload[:action]
31
+ Trace.current.complete(event)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,68 @@
1
+ require 'pry'
2
+
3
+ module Lens
4
+ class Sender
5
+ NOTICES_URI = 'api/v1/record'
6
+ HTTP_ERRORS = [Timeout::Error,
7
+ Errno::EINVAL,
8
+ Errno::ECONNRESET,
9
+ EOFError,
10
+ Net::HTTPBadResponse,
11
+ Net::HTTPHeaderSyntaxError,
12
+ Net::ProtocolError,
13
+ Errno::ECONNREFUSED].freeze
14
+
15
+ def initialize(options = {})
16
+ [ :app_id,
17
+ :secret,
18
+ :protocol,
19
+ :host,
20
+ :port
21
+ ].each do |option|
22
+ instance_variable_set("@#{option}", options.send(option))
23
+ end
24
+ end
25
+
26
+ def send_to_lens(data)
27
+ response = send_request(url.path, data, {'X-API-Key' => secret})
28
+ end
29
+
30
+ attr_reader :app_id,
31
+ :secret,
32
+ :protocol,
33
+ :host,
34
+ :port
35
+
36
+ private
37
+
38
+ def http_connection
39
+ setup_http_connection
40
+ end
41
+
42
+ def setup_http_connection
43
+ Net::HTTP.new(url.host, url.port)
44
+ rescue => e
45
+ log(:error, "[Honeybadger::Sender#setup_http_connection] Failure initializing the HTTP connection.\nError: #{e.class} - #{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}")
46
+ raise e
47
+ end
48
+
49
+ def send_request(path, data, headers = {})
50
+ http_connection.post(path, data, http_headers(headers))
51
+ rescue *HTTP_ERRORS => e
52
+ raise e
53
+ nil
54
+ end
55
+
56
+ def http_headers(headers=nil)
57
+ {}.tap do |hash|
58
+ hash.merge!(HEADERS)
59
+ hash.merge!({'X-API-Key' => secret})
60
+ hash.merge!(headers) if headers
61
+ end
62
+ end
63
+
64
+ def url
65
+ URI.parse("#{protocol}://#{host}:#{port}").merge(NOTICES_URI)
66
+ end
67
+ end
68
+ end
data/lib/lens/trace.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'rails'
2
+
3
+ module Lens
4
+ class Trace
5
+ attr_reader :id, :duration
6
+
7
+ def self.current
8
+ Thread.current[:__lens_trace]
9
+ end
10
+
11
+ def self.create(id)
12
+ Thread.current[:__lens_trace] = new(id)
13
+ end
14
+
15
+ def initialize(id)
16
+ @id = id
17
+ @events = []
18
+ @duration = 0
19
+ end
20
+
21
+ def add(event)
22
+ @events.push event
23
+ end
24
+
25
+ def complete(event)
26
+ @duration = event.duration
27
+ Rails.logger.info "all [LENS] >>> #{@events}"
28
+ Rails.logger.info "last [LENS] >>> #{event}"
29
+ Thread.current[:__lens_trace] = nil
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Lens
2
+ VERSION = "0.0.2"
3
+ end
data/lib/lens.rb ADDED
@@ -0,0 +1,27 @@
1
+ require "lens/core"
2
+ require 'pry'
3
+
4
+ module Lens
5
+ HEADERS = {
6
+ 'Content-type' => 'application/json',
7
+ 'Content-Encoding' => 'deflate',
8
+ 'Accept' => 'text/json, application/json',
9
+ 'User-Agent' => "LENS-Ruby #{VERSION}; #{RUBY_VERSION}; #{RUBY_PLATFORM}"
10
+ }.freeze
11
+
12
+ class << self
13
+ attr_accessor :sender
14
+ attr_writer :configuration
15
+
16
+ def configure
17
+ yield(configuration)
18
+
19
+ self.sender = Sender.new(configuration)
20
+ self.sender
21
+ end
22
+
23
+ def configuration
24
+ @configuration ||= Configuration.new
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+ require 'support/helpers'
3
+
4
+ describe Lens::Sender do
5
+ before { reset_config }
6
+ let(:http) { stub_http }
7
+
8
+ it "makes a single request when sending notices" do
9
+ http.should_receive(:post).once
10
+ Lens.sender.send_to_lens('abc123')
11
+ end
12
+
13
+ end
@@ -0,0 +1,12 @@
1
+ require 'rspec'
2
+ require 'lens'
3
+
4
+ Dir[File.expand_path('../../spec/support/**/*.rb', __FILE__)].each {|f| require f}
5
+
6
+ RSpec.configure do |c|
7
+ c.mock_with :rspec
8
+ c.color = true
9
+ c.run_all_when_everything_filtered = true
10
+
11
+ c.include Helpers
12
+ end
@@ -0,0 +1,22 @@
1
+ module Helpers
2
+ def stub_http(options = {})
3
+ response = options[:response] || Net::HTTPSuccess.new('1.2', '200', 'OK')
4
+ response.stub(:body => options[:body] || '{"id":"1234"}')
5
+ http = double(:post => response,
6
+ :read_timeout= => nil,
7
+ :open_timeout= => nil,
8
+ :ca_file= => nil,
9
+ :verify_mode= => nil,
10
+ :use_ssl= => nil)
11
+ Net::HTTP.stub(:new).and_return(http)
12
+ http
13
+ end
14
+
15
+ def reset_config
16
+ Lens.configuration = nil
17
+ Lens.configure do |config|
18
+ config.app_id = 'app_key_123'
19
+ config.secret = 'abc123'
20
+ end
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lens
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - kgorin
8
+ - artygus
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-05-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.6'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.6'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.2'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.2'
56
+ - !ruby/object:Gem::Dependency
57
+ name: pry
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0.10'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.10'
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry-nav
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description:
85
+ email:
86
+ - me@kgor.in
87
+ - artygus@engineeram.net
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - lens.gemspec
99
+ - lib/lens.rb
100
+ - lib/lens/configuration.rb
101
+ - lib/lens/core.rb
102
+ - lib/lens/railtie.rb
103
+ - lib/lens/sender.rb
104
+ - lib/lens/trace.rb
105
+ - lib/lens/version.rb
106
+ - spec/lens/sender_spec.rb
107
+ - spec/spec_helper.rb
108
+ - spec/support/helpers.rb
109
+ homepage: http://coub.com
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.4.6
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Gem to send Rails request stats
133
+ test_files:
134
+ - spec/lens/sender_spec.rb
135
+ - spec/spec_helper.rb
136
+ - spec/support/helpers.rb
137
+ has_rdoc: