lens 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +5 -5
- data/lib/lens/configuration.rb +6 -2
- data/lib/lens/core.rb +1 -0
- data/lib/lens/event_formatter.rb +38 -0
- data/lib/lens/railtie.rb +0 -6
- data/lib/lens/sender.rb +9 -9
- data/lib/lens/trace.rb +30 -16
- data/lib/lens/version.rb +1 -1
- data/spec/lens/event_formatter_spec.rb +23 -0
- data/spec/support/helpers.rb +9 -7
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a012ff1b501e256ecf5d23cd85ddbab93691ec19
|
4
|
+
data.tar.gz: bca5e5234778aa9affe85d7f93a40897a4b8c752
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb648a4b48e3111a157ed48553fba8f88ca266f614d16814798c62ca3df61e3f70c1b338bc077244e0e325d029e13331ab99f5e6d08d674d5e9b492048af89ca
|
7
|
+
data.tar.gz: c020252259fa32e042c5654918e5d7cb3712a9d0138be712151913dfe2a7100fe0d9ee36397c28ec6afaf38755288bcf2d1b6f19aec2e04c7cc998fed780c448
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
lens (0.0.
|
4
|
+
lens (0.0.2)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -67,7 +67,7 @@ GEM
|
|
67
67
|
activesupport (= 4.0.3)
|
68
68
|
rake (>= 0.8.7)
|
69
69
|
thor (>= 0.18.1, < 2.0)
|
70
|
-
rake (
|
70
|
+
rake (0.9.6)
|
71
71
|
rspec (3.2.0)
|
72
72
|
rspec-core (~> 3.2.0)
|
73
73
|
rspec-expectations (~> 3.2.0)
|
@@ -106,7 +106,7 @@ DEPENDENCIES
|
|
106
106
|
bundler (~> 1.6)
|
107
107
|
lens!
|
108
108
|
pry (~> 0.10)
|
109
|
-
pry-nav
|
109
|
+
pry-nav (~> 0)
|
110
110
|
rails (>= 3.0)
|
111
|
-
rake
|
112
|
-
rspec (~> 3.2
|
111
|
+
rake (~> 0)
|
112
|
+
rspec (~> 3.2)
|
data/lib/lens/configuration.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
module Lens
|
2
2
|
class Configuration
|
3
|
-
attr_accessor :
|
3
|
+
attr_accessor :app_key, :secret, :protocol, :host, :port
|
4
4
|
|
5
5
|
def protocol
|
6
|
-
|
6
|
+
@protocol || default_protocol
|
7
7
|
end
|
8
8
|
|
9
9
|
def host
|
@@ -21,5 +21,9 @@ module Lens
|
|
21
21
|
def default_host
|
22
22
|
'lens.coub.com'
|
23
23
|
end
|
24
|
+
|
25
|
+
def default_protocol
|
26
|
+
'https'
|
27
|
+
end
|
24
28
|
end
|
25
29
|
end
|
data/lib/lens/core.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Lens
|
2
|
+
class EventFormatter
|
3
|
+
def initialize(event, records)
|
4
|
+
@event = event
|
5
|
+
@records = records
|
6
|
+
end
|
7
|
+
|
8
|
+
def json_formatted
|
9
|
+
formatted.to_json
|
10
|
+
end
|
11
|
+
|
12
|
+
def formatted
|
13
|
+
@formatted ||= { data: event_data }
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def event_payload
|
19
|
+
@event_payload ||= @event.payload
|
20
|
+
end
|
21
|
+
|
22
|
+
def event_data
|
23
|
+
@event_data ||=
|
24
|
+
{
|
25
|
+
action: event_payload[:action],
|
26
|
+
controller: event_payload[:controller],
|
27
|
+
params: event_payload[:params],
|
28
|
+
method: event_payload[:method],
|
29
|
+
url: event_payload[:path],
|
30
|
+
records: @records,
|
31
|
+
duration: @event.duration,
|
32
|
+
meta: {
|
33
|
+
client_version: VERSION
|
34
|
+
}
|
35
|
+
}
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/lens/railtie.rb
CHANGED
@@ -18,12 +18,6 @@ module Lens
|
|
18
18
|
Trace.current.add(event)
|
19
19
|
end
|
20
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
21
|
ActiveSupport::Notifications.subscribe('process_action.action_controller') do |*args|
|
28
22
|
next unless Trace.current
|
29
23
|
event = ActiveSupport::Notifications::Event.new(*args)
|
data/lib/lens/sender.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'pry'
|
2
|
+
require 'json'
|
2
3
|
|
3
4
|
module Lens
|
4
5
|
class Sender
|
5
|
-
NOTICES_URI = 'api/v1/
|
6
|
+
NOTICES_URI = 'api/v1/data/rec'
|
6
7
|
HTTP_ERRORS = [Timeout::Error,
|
7
8
|
Errno::EINVAL,
|
8
9
|
Errno::ECONNRESET,
|
@@ -13,8 +14,7 @@ module Lens
|
|
13
14
|
Errno::ECONNREFUSED].freeze
|
14
15
|
|
15
16
|
def initialize(options = {})
|
16
|
-
[ :
|
17
|
-
:secret,
|
17
|
+
[ :app_key,
|
18
18
|
:protocol,
|
19
19
|
:host,
|
20
20
|
:port
|
@@ -24,11 +24,10 @@ module Lens
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def send_to_lens(data)
|
27
|
-
response = send_request(url.path, data
|
27
|
+
response = send_request(url.path, data)
|
28
28
|
end
|
29
29
|
|
30
|
-
attr_reader :
|
31
|
-
:secret,
|
30
|
+
attr_reader :app_key,
|
32
31
|
:protocol,
|
33
32
|
:host,
|
34
33
|
:port
|
@@ -42,12 +41,12 @@ module Lens
|
|
42
41
|
def setup_http_connection
|
43
42
|
Net::HTTP.new(url.host, url.port)
|
44
43
|
rescue => e
|
45
|
-
log(:error, "[
|
44
|
+
log(:error, "[Lens::Sender#setup_http_connection] Failure initializing the HTTP connection.\nError: #{e.class} - #{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}")
|
46
45
|
raise e
|
47
46
|
end
|
48
47
|
|
49
48
|
def send_request(path, data, headers = {})
|
50
|
-
http_connection.post(path, data, http_headers(headers))
|
49
|
+
http_connection.post(path, data.to_json, http_headers(headers))
|
51
50
|
rescue *HTTP_ERRORS => e
|
52
51
|
raise e
|
53
52
|
nil
|
@@ -56,7 +55,8 @@ module Lens
|
|
56
55
|
def http_headers(headers=nil)
|
57
56
|
{}.tap do |hash|
|
58
57
|
hash.merge!(HEADERS)
|
59
|
-
hash.merge!({'X-
|
58
|
+
hash.merge!({'X-Auth-Token' => app_key})
|
59
|
+
hash.merge!({'Content-Type' =>'application/json'})
|
60
60
|
hash.merge!(headers) if headers
|
61
61
|
end
|
62
62
|
end
|
data/lib/lens/trace.rb
CHANGED
@@ -2,31 +2,45 @@ require 'rails'
|
|
2
2
|
|
3
3
|
module Lens
|
4
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
5
|
def initialize(id)
|
16
6
|
@id = id
|
17
|
-
@
|
18
|
-
@duration = 0
|
7
|
+
@data = []
|
19
8
|
end
|
20
9
|
|
21
10
|
def add(event)
|
22
|
-
@
|
11
|
+
@data.push event.payload
|
23
12
|
end
|
24
13
|
|
25
14
|
def complete(event)
|
26
|
-
|
27
|
-
|
28
|
-
|
15
|
+
formatted_data = Lens::EventFormatter.new(event, @data).json_formatted
|
16
|
+
log(data)
|
17
|
+
send(data)
|
29
18
|
Thread.current[:__lens_trace] = nil
|
30
19
|
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def send(data)
|
24
|
+
log(data)
|
25
|
+
Lens.sender.send_to_lens(data)
|
26
|
+
end
|
27
|
+
|
28
|
+
def log(data)
|
29
|
+
Rails.logger.info "all [LENS] >>> #{data}" if verbose?
|
30
|
+
end
|
31
|
+
|
32
|
+
def verbose?
|
33
|
+
true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class << Trace
|
38
|
+
def current
|
39
|
+
Thread.current[:__lens_trace]
|
40
|
+
end
|
41
|
+
|
42
|
+
def create(id)
|
43
|
+
Thread.current[:__lens_trace] = new(id)
|
44
|
+
end
|
31
45
|
end
|
32
46
|
end
|
data/lib/lens/version.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Lens::EventFormatter do
|
4
|
+
let(:event) { double(payload: {}, duration: 2.33) }
|
5
|
+
let(:records) { [] }
|
6
|
+
let(:formatter) { Lens::EventFormatter.new(event, records) }
|
7
|
+
|
8
|
+
describe "#formatted" do
|
9
|
+
subject { formatter.formatted }
|
10
|
+
|
11
|
+
it "returns hash" do
|
12
|
+
expect(subject).to be_a(Hash)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "has data key" do
|
16
|
+
expect(subject).to have_key(:data)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "data key is not empty" do
|
20
|
+
expect(subject[:data]).to_not be_empty
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/support/helpers.rb
CHANGED
@@ -2,12 +2,14 @@ module Helpers
|
|
2
2
|
def stub_http(options = {})
|
3
3
|
response = options[:response] || Net::HTTPSuccess.new('1.2', '200', 'OK')
|
4
4
|
response.stub(:body => options[:body] || '{"id":"1234"}')
|
5
|
-
http = double(
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
http = double(
|
6
|
+
:post => response,
|
7
|
+
:read_timeout= => nil,
|
8
|
+
:open_timeout= => nil,
|
9
|
+
:ca_file= => nil,
|
10
|
+
:verify_mode= => nil,
|
11
|
+
:use_ssl= => nil
|
12
|
+
)
|
11
13
|
Net::HTTP.stub(:new).and_return(http)
|
12
14
|
http
|
13
15
|
end
|
@@ -15,7 +17,7 @@ module Helpers
|
|
15
17
|
def reset_config
|
16
18
|
Lens.configuration = nil
|
17
19
|
Lens.configure do |config|
|
18
|
-
config.
|
20
|
+
config.app_key = 'app_key_123'
|
19
21
|
config.secret = 'abc123'
|
20
22
|
end
|
21
23
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lens
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kgorin
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-05
|
12
|
+
date: 2015-06-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -99,10 +99,12 @@ files:
|
|
99
99
|
- lib/lens.rb
|
100
100
|
- lib/lens/configuration.rb
|
101
101
|
- lib/lens/core.rb
|
102
|
+
- lib/lens/event_formatter.rb
|
102
103
|
- lib/lens/railtie.rb
|
103
104
|
- lib/lens/sender.rb
|
104
105
|
- lib/lens/trace.rb
|
105
106
|
- lib/lens/version.rb
|
107
|
+
- spec/lens/event_formatter_spec.rb
|
106
108
|
- spec/lens/sender_spec.rb
|
107
109
|
- spec/spec_helper.rb
|
108
110
|
- spec/support/helpers.rb
|
@@ -131,6 +133,7 @@ signing_key:
|
|
131
133
|
specification_version: 4
|
132
134
|
summary: Gem to send Rails request stats
|
133
135
|
test_files:
|
136
|
+
- spec/lens/event_formatter_spec.rb
|
134
137
|
- spec/lens/sender_spec.rb
|
135
138
|
- spec/spec_helper.rb
|
136
139
|
- spec/support/helpers.rb
|