lens 0.0.8.2 → 0.0.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ffe1d6b48785a27208d3177a2f133181419c0033
4
- data.tar.gz: 0f0126706efa0927f5db3acc5965c7cf8d8d1f90
3
+ metadata.gz: c971e912fe10e548adfe94f7af50b6156feeb2fa
4
+ data.tar.gz: dce92e686de9b3ee2044019a05af28019a0a02e5
5
5
  SHA512:
6
- metadata.gz: d314b6a59368d6d861ce8af3b2d4077a278d785c4226ec147e7855057c2996a2a438d00501da0fcd796b011135ca7fa4abd07e02a2a21d844adb49685ae4c9d7
7
- data.tar.gz: 4feebcf5fa63e742c8b3db2996baf7f47b8ad8c5cb8b3cd82b5ebef074440ed8f52b82fbc23134cd1a783cf6e7eac6bd52369c637cf6f6105bd07ca4694dac29
6
+ metadata.gz: f5f3e93bb5d2313d6eafa07e7a688fb7753590bdb62132c612f3931c253425ef85323dba31d653f147aa5383072a1e0bcf7a056a2c8ffebc167700a22b3bc028
7
+ data.tar.gz: d8bfcf390e538fb0d29563a77ddfa7d2ef166bc5d6fdfb22faacaf573f6641eb333d09ad0192aedfeda80cb36e155f4c6fe92198c7911ff4cd5babfc2f0929bb
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ *.swp
3
+ *.swo
4
+ coverage
5
+ /Gemfile.lock
data/.hound.yml ADDED
@@ -0,0 +1,2 @@
1
+ ruby:
2
+ config_file: .rubocop.yml
data/.rubocop.yml ADDED
@@ -0,0 +1,237 @@
1
+ AllCops:
2
+ Exclude:
3
+ - "vendor/**/*"
4
+ - "db/schema.rb"
5
+ - "spec/*_helper.rb"
6
+ - "bin/*"
7
+ UseCache: false
8
+ Style/CollectionMethods:
9
+ Description: Preferred collection methods.
10
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#map-find-select-reduce-size
11
+ Enabled: true
12
+ PreferredMethods:
13
+ collect: map
14
+ collect!: map!
15
+ find: detect
16
+ find_all: select
17
+ reduce: inject
18
+ Style/DotPosition:
19
+ Description: Checks the position of the dot in multi-line method calls.
20
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains
21
+ Enabled: true
22
+ EnforcedStyle: leading
23
+ SupportedStyles:
24
+ - leading
25
+ - trailing
26
+ Style/FileName:
27
+ Description: Use snake_case for source file names.
28
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#snake-case-files
29
+ Enabled: false
30
+ Exclude: []
31
+ Style/GuardClause:
32
+ Description: Check for conditionals that can be replaced with guard clauses
33
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals
34
+ Enabled: true
35
+ MinBodyLength: 5
36
+ Style/IfUnlessModifier:
37
+ Description: Favor modifier if/unless usage when you have a single-line body.
38
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#if-as-a-modifier
39
+ Enabled: true
40
+ MaxLineLength: 100
41
+ Style/OptionHash:
42
+ Description: Don't use option hashes when you can use keyword arguments.
43
+ Enabled: false
44
+ Style/PercentLiteralDelimiters:
45
+ Description: Use `%`-literal delimiters consistently
46
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#percent-literal-braces
47
+ Enabled: false
48
+ PreferredDelimiters:
49
+ "%": "()"
50
+ "%i": "()"
51
+ "%q": "()"
52
+ "%Q": "()"
53
+ "%r": "{}"
54
+ "%s": "()"
55
+ "%w": "()"
56
+ "%W": "()"
57
+ "%x": "()"
58
+ Style/PredicateName:
59
+ Description: Check the names of predicate methods.
60
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#bool-methods-qmark
61
+ Enabled: true
62
+ NamePrefix:
63
+ - is_
64
+ - has_
65
+ - have_
66
+ NamePrefixBlacklist:
67
+ - is_
68
+ Exclude:
69
+ - spec/**/*
70
+ Style/RaiseArgs:
71
+ Description: Checks the arguments passed to raise/fail.
72
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#exception-class-messages
73
+ Enabled: false
74
+ EnforcedStyle: exploded
75
+ SupportedStyles:
76
+ - compact
77
+ - exploded
78
+ Style/SignalException:
79
+ Description: Checks for proper usage of fail and raise.
80
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#fail-method
81
+ Enabled: false
82
+ EnforcedStyle: semantic
83
+ SupportedStyles:
84
+ - only_raise
85
+ - only_fail
86
+ - semantic
87
+ Style/SingleLineBlockParams:
88
+ Description: Enforces the names of some block params.
89
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#reduce-blocks
90
+ Enabled: false
91
+ Methods:
92
+ - reduce:
93
+ - a
94
+ - e
95
+ - inject:
96
+ - a
97
+ - e
98
+ Style/SingleLineMethods:
99
+ Description: Avoid single-line methods.
100
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-single-line-methods
101
+ Enabled: true
102
+ AllowIfMethodIsEmpty: true
103
+ Style/StringLiterals:
104
+ Description: Checks if uses of quotes match the configured preference.
105
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-string-literals
106
+ Enabled: true
107
+ EnforcedStyle: single_quotes
108
+ SupportedStyles:
109
+ - single_quotes
110
+ - double_quotes
111
+ Style/StringLiteralsInInterpolation:
112
+ Description: Checks if uses of quotes inside expressions in interpolated strings
113
+ match the configured preference.
114
+ Enabled: true
115
+ EnforcedStyle: single_quotes
116
+ SupportedStyles:
117
+ - single_quotes
118
+ - double_quotes
119
+ Style/TrailingComma:
120
+ Description: Checks for trailing comma in parameter lists and literals.
121
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas
122
+ Enabled: true
123
+ EnforcedStyleForMultiline: no_comma
124
+ SupportedStyles:
125
+ - comma
126
+ - no_comma
127
+ Metrics/LineLength:
128
+ Max: 100
129
+ Metrics/AbcSize:
130
+ Description: A calculated magnitude based on number of assignments, branches, and
131
+ conditions.
132
+ Enabled: false
133
+ Max: 15
134
+ Metrics/ClassLength:
135
+ Description: Avoid classes longer than 100 lines of code.
136
+ Enabled: false
137
+ CountComments: false
138
+ Max: 100
139
+ Metrics/ModuleLength:
140
+ CountComments: false
141
+ Max: 100
142
+ Description: Avoid modules longer than 100 lines of code.
143
+ Enabled: false
144
+ Metrics/CyclomaticComplexity:
145
+ Description: A complexity metric that is strongly correlated to the number of test
146
+ cases needed to validate a method.
147
+ Enabled: false
148
+ Max: 6
149
+ Metrics/MethodLength:
150
+ Description: Avoid methods longer than 10 lines of code.
151
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#short-methods
152
+ Enabled: true
153
+ CountComments: false
154
+ Max: 20
155
+ Metrics/ParameterLists:
156
+ Description: Avoid parameter lists longer than three or four parameters.
157
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#too-many-params
158
+ Enabled: true
159
+ Max: 5
160
+ CountKeywordArgs: true
161
+ Metrics/PerceivedComplexity:
162
+ Description: A complexity metric geared towards measuring complexity for a human
163
+ reader.
164
+ Enabled: false
165
+ Max: 7
166
+ Lint/AssignmentInCondition:
167
+ Description: Don't use assignment in conditions.
168
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#safe-assignment-in-condition
169
+ Enabled: false
170
+ AllowSafeAssignment: true
171
+ Style/InlineComment:
172
+ Description: Avoid inline comments.
173
+ Enabled: false
174
+ Style/AccessorMethodName:
175
+ Description: Check the naming of accessor methods for get_/set_.
176
+ Enabled: true
177
+ Style/Alias:
178
+ Description: Use alias_method instead of alias.
179
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#alias-method
180
+ Enabled: false
181
+ Style/Documentation:
182
+ Description: Document classes and non-namespace modules.
183
+ Enabled: false
184
+ Style/DoubleNegation:
185
+ Description: Checks for uses of double negation (!!).
186
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-bang-bang
187
+ Enabled: false
188
+ Style/EachWithObject:
189
+ Description: Prefer `each_with_object` over `inject` or `reduce`.
190
+ Enabled: false
191
+ Style/EmptyLiteral:
192
+ Description: Prefer literals to Array.new/Hash.new/String.new.
193
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#literal-array-hash
194
+ Enabled: false
195
+ Style/ModuleFunction:
196
+ Description: Checks for usage of `extend self` in modules.
197
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#module-function
198
+ Enabled: false
199
+ Style/OneLineConditional:
200
+ Description: Favor the ternary operator(?:) over if/then/else/end constructs.
201
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#ternary-operator
202
+ Enabled: false
203
+ Style/PerlBackrefs:
204
+ Description: Avoid Perl-style regex back references.
205
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-perl-regexp-last-matchers
206
+ Enabled: false
207
+ Style/Send:
208
+ Description: Prefer `Object#__send__` or `Object#public_send` to `send`, as `send`
209
+ may overlap with existing methods.
210
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#prefer-public-send
211
+ Enabled: false
212
+ Style/SpecialGlobalVars:
213
+ Description: Avoid Perl-style global variables.
214
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-cryptic-perlisms
215
+ Enabled: false
216
+ Style/VariableInterpolation:
217
+ Description: Don't interpolate global, instance and class variables directly in
218
+ strings.
219
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#curlies-interpolate
220
+ Enabled: false
221
+ Style/WhenThen:
222
+ Description: Use when x then ... for one-line cases.
223
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#one-line-cases
224
+ Enabled: false
225
+ Lint/EachWithObjectArgument:
226
+ Description: Check for immutable argument given to each_with_object.
227
+ Enabled: true
228
+ Lint/HandleExceptions:
229
+ Description: Don't suppress exception.
230
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#dont-hide-exceptions
231
+ Enabled: true
232
+ Lint/LiteralInCondition:
233
+ Description: Checks of literals used in conditions.
234
+ Enabled: false
235
+ Lint/LiteralInInterpolation:
236
+ Description: Checks for literals used in interpolation.
237
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,22 @@
1
+ language: ruby
2
+ cache: bundler
3
+ sudo: false
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1.0
8
+ - 2.2.2
9
+ - 2.2.3
10
+ - ruby-head
11
+ - jruby-19mode
12
+ - jruby-head
13
+ - rbx-2
14
+ env:
15
+ global:
16
+ - JRUBY_OPTS="-J-Xmx1024M --debug"
17
+ matrix:
18
+ allow_failures:
19
+ - rvm: jruby-19mode
20
+ - rvm: jruby-head
21
+ - rvm: rbx-2
22
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'simplecov', require: false
7
+ gem 'codeclimate-test-reporter', require: false
8
+ end
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 CHANGED
@@ -42,3 +42,13 @@ Lens.configure do |config|
42
42
  config.port = 3001
43
43
  end
44
44
  ```
45
+
46
+ If you want to see information about memory allocations - you should enable this functionality:
47
+ ```ruby
48
+ # config/initializers/lens.rb
49
+ Lens.configure do |config|
50
+ # ...
51
+ config.show_memory_usage = true
52
+ # ...
53
+ end
54
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lens.gemspec ADDED
@@ -0,0 +1,28 @@
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 = %w(zzet kgorin artygus)
10
+ spec.email = ['me@zzet.org', 'me@kgor.in', 'artygus@engineeram.net']
11
+ spec.summary = 'Gem to send Rails request stats'
12
+ spec.homepage = 'https://github.com/lenshq/lens_client'
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_runtime_dependency 'lz4-ruby'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.6'
23
+ spec.add_development_dependency 'rails', '>= 3.0'
24
+ spec.add_development_dependency 'rake', '~> 0'
25
+ spec.add_development_dependency 'rspec', '~> 3.2'
26
+ spec.add_development_dependency 'pry', '~> 0.10'
27
+ spec.add_development_dependency 'pry-nav', '~> 0'
28
+ end
@@ -1,6 +1,6 @@
1
1
  module Lens
2
2
  class Configuration
3
- attr_accessor :app_key, :secret, :protocol, :host, :port
3
+ attr_accessor :app_key, :secret, :protocol, :host, :port, :show_memory_usage
4
4
 
5
5
  def protocol
6
6
  @protocol || default_protocol
@@ -18,6 +18,10 @@ module Lens
18
18
  default_compressor
19
19
  end
20
20
 
21
+ def with_memory_usage?
22
+ @show_memory_usage || default_memory_usage
23
+ end
24
+
21
25
  private
22
26
 
23
27
  def default_port
@@ -35,5 +39,9 @@ module Lens
35
39
  def default_compressor
36
40
  Compression::LZ4
37
41
  end
42
+
43
+ def default_memory_usage
44
+ false
45
+ end
38
46
  end
39
47
  end
data/lib/lens/trace.rb CHANGED
@@ -8,7 +8,7 @@ module Lens
8
8
  @gc_statistics.enable
9
9
 
10
10
  @allocations_data = Lens::AllocationsData.new
11
- @allocations_data.enable
11
+ @allocations_data.enable if Lens.configuration.with_memory_usage?
12
12
  end
13
13
 
14
14
  def add(event)
data/lib/lens/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lens
2
- VERSION = "0.0.8.2"
2
+ VERSION = "0.0.9"
3
3
  end
data/lib/lens.rb CHANGED
@@ -4,7 +4,6 @@ raise 'WTF!?!?! Where is Rails man? ❨╯°□°❩╯︵┻━┻' unless defi
4
4
 
5
5
  require 'lens/core'
6
6
  require 'lens/railtie'
7
- require 'lens_memprof'
8
7
 
9
8
  module Lens
10
9
  class << self
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Lens::EventFormatter do
4
+ let(:event) { double(
5
+ payload: {},
6
+ duration: 2.33,
7
+ time: Time.now - 2.seconds,
8
+ end: Time.now,
9
+ name: 'dummy.name')
10
+ }
11
+ let(:records) { [] }
12
+ let(:formatter) { Lens::EventFormatter.new(event, records) }
13
+
14
+ describe "#formatted" do
15
+ subject { formatter.formatted }
16
+
17
+ it "returns hash" do
18
+ expect(subject).to be_a(Hash)
19
+ end
20
+
21
+ it "has data key" do
22
+ expect(subject).to have_key(:data)
23
+ end
24
+
25
+ it "data key is not empty" do
26
+ expect(subject[:data]).to_not be_empty
27
+ end
28
+
29
+ it "required fields are present" do
30
+ required_keys = [:action, :controller, :params, :method, :url, :records, :time, :duration]
31
+ expect(required_keys - subject[:data].keys).to be_empty
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+ require 'support/helpers'
3
+
4
+ describe Lens do
5
+ describe '.configure' do
6
+ context 'when user params' do
7
+ let(:params) do
8
+ {
9
+ app_key: 'secret-key',
10
+ protocol: 'https',
11
+ host: 'example.com',
12
+ port: 8080,
13
+ show_memory_usage: true
14
+ }
15
+ end
16
+ before { configure(params) }
17
+ subject { described_class.configuration }
18
+
19
+ it { expect(subject.app_key).to eq params[:app_key] }
20
+ it { expect(subject.protocol).to eq params[:protocol] }
21
+ it { expect(subject.host).to eq params[:host] }
22
+ it { expect(subject.port).to eq params[:port] }
23
+ it { expect(subject.with_memory_usage?).to eq params[:show_memory_usage] }
24
+ end
25
+
26
+ context 'when default params' do
27
+ describe 'compressor' do
28
+ subject { described_class.configuration.compressor }
29
+
30
+ it { is_expected.to respond_to :compress }
31
+ it { is_expected.to be Lens::Compression::LZ4 }
32
+ end
33
+
34
+ describe 'configuration' do
35
+ subject { described_class.configuration }
36
+
37
+ it { expect(subject.app_key).to eq nil }
38
+ it { expect(subject.protocol).to eq 'http' }
39
+ it { expect(subject.host).to eq 'lenshq.io' }
40
+ it { expect(subject.port).to eq 80 }
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '.start' do
46
+ context 'without configuration' do
47
+ subject { described_class.start }
48
+
49
+ it { expect { subject }.to raise_error Lens::ConfigurationError }
50
+ end
51
+
52
+ context 'with configuration' do
53
+ let(:params) { { app_key: 'some_key' } }
54
+
55
+ around(:example) do |example|
56
+ configure(params)
57
+ described_class.start
58
+
59
+ example.run
60
+
61
+ described_class.stop
62
+ end
63
+
64
+ it 'client has been started properly' do
65
+ expect(Lens::Worker.running?).to be_truthy
66
+ end
67
+ end
68
+ end
69
+
70
+ describe '.stop' do
71
+ context 'not running' do
72
+ it 'client has been stopped properly' do
73
+ expect(Lens::Worker.running?).to be_falsey
74
+ described_class.stop
75
+ expect(Lens::Worker.running?).to be_falsey
76
+ end
77
+ end
78
+
79
+ context 'running' do
80
+ let(:params) { { app_key: 'some_key' } }
81
+
82
+ around(:example) do |example|
83
+ configure(params)
84
+ described_class.start
85
+
86
+ example.run
87
+
88
+ described_class.stop
89
+ end
90
+
91
+ it 'client has been stopped properly' do
92
+ expect(Lens::Worker.running?).to be_truthy
93
+ described_class.stop
94
+ expect(Lens::Worker.running?).to be_falsey
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ def configure(params)
101
+ described_class.configure do |config|
102
+ config.app_key = params[:app_key] if params[:app_key].present?
103
+ config.protocol = params[:protocol] if params[:protocol].present?
104
+ config.host = params[:host] if params[:host].present?
105
+ config.port = params[:port] if params[:port].present?
106
+ config.show_memory_usage = params[:show_memory_usage] if params[:show_memory_usage].present?
107
+ end
108
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+ require 'support/helpers'
3
+
4
+ describe Lens::Sender do
5
+ before { Lens.configure { |config| config.app_key = 'app_key_123' } }
6
+ let(:http) { stub_http }
7
+
8
+ it 'makes a single request when sending notices' do
9
+ expect(http).to receive(:post).once
10
+ Lens.sender.send_to_lens('abc123')
11
+ end
12
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Lens::Trace do
4
+ describe '.process' do
5
+ before { Lens.configure { |config| config.show_memory_usage = true } }
6
+
7
+ let(:first_event) { generate_event('start_processing.action_controller') }
8
+ let(:last_event) { generate_event('process_action.action_controller') }
9
+
10
+ context 'when first message' do
11
+ before { described_class.process(first_event) }
12
+
13
+ it 'creates new thread' do
14
+ expect(described_class.present?).to be true
15
+ end
16
+ end
17
+
18
+ context 'when last message' do
19
+ before do
20
+ Lens::Worker.start({})
21
+ allow_any_instance_of(Lens::Worker).to receive :push
22
+ end
23
+
24
+ it 'kills thread' do
25
+ described_class.process(first_event)
26
+ expect(described_class.present?).to be true
27
+
28
+ described_class.process(last_event)
29
+ expect(described_class.present?).to be false
30
+ end
31
+
32
+ it 'pushes data to lens server' do
33
+ expect_any_instance_of(Lens::Worker).to receive :push
34
+ described_class.process(first_event)
35
+ described_class.process(last_event)
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ def generate_event(name = 'event_name')
42
+ Lens::Event.new(
43
+ name: name,
44
+ started: Time.current,
45
+ finished: Time.current,
46
+ transaction_id: 1,
47
+ payload: {
48
+ controller: 'controller',
49
+ action: 'action'
50
+ }
51
+ )
52
+ end
@@ -0,0 +1,119 @@
1
+ require 'spec_helper'
2
+
3
+ describe Lens::Worker do
4
+ let(:worker) { Lens::Worker.new({}) }
5
+ before { Lens::Worker.start({}) }
6
+ after { Lens::Worker.stop }
7
+
8
+ describe '.start' do
9
+ it 'creates thread' do
10
+ Lens::Worker.stop
11
+ expect(Lens::Worker.instance).to be nil
12
+
13
+ Lens::Worker.start({})
14
+ expect(Lens::Worker.instance).to be_truthy
15
+ end
16
+
17
+ context 'when started twice' do
18
+ it 'creates thread' do
19
+ Lens::Worker.stop
20
+ expect(Lens::Worker.instance).to be nil
21
+
22
+ Lens::Worker.start({})
23
+ first_instance = Lens::Worker.instance
24
+ Lens::Worker.start({})
25
+ expect(Lens::Worker.instance).to eq first_instance
26
+ end
27
+ end
28
+ end
29
+
30
+ describe '.stop' do
31
+ before { Lens::Worker.start({}) }
32
+
33
+ it 'sets running? to false' do
34
+ Lens::Worker.stop(force: true)
35
+ expect(Lens::Worker.running?).to be false
36
+ end
37
+
38
+ context 'when forced' do
39
+ it 'calls forced stop' do
40
+ expect_any_instance_of(Lens::Worker).to receive :shutdown!
41
+ Lens::Worker.stop(force: true)
42
+ end
43
+ end
44
+
45
+ context 'when graceful' do
46
+ it 'calls graceful stop' do
47
+ expect_any_instance_of(Lens::Worker).to receive :shutdown
48
+ Lens::Worker.stop
49
+ end
50
+ end
51
+
52
+ context 'when stopped twice' do
53
+ it 'doesnt raise errors' do
54
+ 2.times { Lens::Worker.stop }
55
+ expect { Lens::Worker }.not_to raise_error
56
+ end
57
+ end
58
+ end
59
+
60
+ describe '#process' do
61
+ let(:sender) { spy }
62
+
63
+ before do
64
+ Lens.sender = sender
65
+ worker.start
66
+ end
67
+
68
+ it 'sends data to lens' do
69
+ worker.process({})
70
+ expect(sender).to have_received :send_to_lens
71
+ end
72
+ end
73
+
74
+ describe '#shutdown!' do
75
+ it 'kills thread thread immediately' do
76
+ worker.start
77
+ expect(worker.thread).to be_truthy
78
+
79
+ worker.shutdown!
80
+ expect(worker.thread.stop?).to be true
81
+ end
82
+ end
83
+
84
+ describe '#shutdown' do
85
+ it 'kills thread thread' do
86
+ worker.start
87
+ expect(worker.thread).to be_truthy
88
+
89
+ worker.shutdown
90
+ expect(worker.thread.stop?).to be true
91
+ end
92
+ end
93
+
94
+ describe '.push' do
95
+ let(:sender) { spy }
96
+ subject { described_class.instance.queue.length }
97
+
98
+ before do
99
+ Lens.sender = sender
100
+ worker.start
101
+ 2.times { described_class.instance.push({}) }
102
+ end
103
+
104
+ it { is_expected.to eq 2 }
105
+ end
106
+ end
107
+
108
+ describe Lens::Worker::Queue do
109
+ let(:queue) { described_class.new(max_size: 1) }
110
+
111
+ describe '#push' do
112
+ context 'when queue is full' do
113
+ before { 2.times { queue.push({}) } }
114
+ subject { queue.length }
115
+
116
+ it { is_expected.to eq 1 }
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,32 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+ end
5
+
6
+ if ENV['CI']
7
+ require 'codeclimate-test-reporter'
8
+ CodeClimate::TestReporter.start
9
+ end
10
+
11
+ require 'rspec'
12
+ require 'rails'
13
+ require 'lens'
14
+
15
+ Dir[File.expand_path('../../spec/support/**/*.rb', __FILE__)].each { |f| require f }
16
+
17
+ RSpec.configure do |config|
18
+ config.after :each do
19
+ Lens.stop
20
+ Lens.configuration = nil
21
+ end
22
+
23
+ config.mock_with :rspec
24
+ config.color = true
25
+ config.run_all_when_everything_filtered = true
26
+
27
+ config.include Helpers
28
+
29
+ config.profile_examples = 3
30
+ config.order = :random
31
+ Kernel.srand config.seed
32
+ end
@@ -0,0 +1,16 @@
1
+ module Helpers
2
+ def stub_http(options = {})
3
+ response = options[:response] || Net::HTTPSuccess.new('1.2', '200', 'OK')
4
+ allow(response).to receive(:body).with(options[:body] || '{"id":"1234"}')
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
+ )
13
+ allow(Net::HTTP).to receive(:new).and_return(http)
14
+ http
15
+ end
16
+ 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.8.2
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - zzet
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2015-12-06 00:00:00.000000000 Z
13
+ date: 2015-12-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: lz4-ruby
@@ -110,33 +110,24 @@ dependencies:
110
110
  - - ~>
111
111
  - !ruby/object:Gem::Version
112
112
  version: '0'
113
- - !ruby/object:Gem::Dependency
114
- name: rake-compiler
115
- requirement: !ruby/object:Gem::Requirement
116
- requirements:
117
- - - '>='
118
- - !ruby/object:Gem::Version
119
- version: '0'
120
- type: :development
121
- prerelease: false
122
- version_requirements: !ruby/object:Gem::Requirement
123
- requirements:
124
- - - '>='
125
- - !ruby/object:Gem::Version
126
- version: '0'
127
113
  description:
128
114
  email:
129
115
  - me@zzet.org
130
116
  - me@kgor.in
131
117
  - artygus@engineeram.net
132
118
  executables: []
133
- extensions:
134
- - ext/lens_memprof/extconf.rb
119
+ extensions: []
135
120
  extra_rdoc_files: []
136
121
  files:
122
+ - .gitignore
123
+ - .hound.yml
124
+ - .rubocop.yml
125
+ - .travis.yml
126
+ - Gemfile
127
+ - LICENSE.txt
137
128
  - README.md
138
- - ext/lens_memprof/extconf.rb
139
- - ext/lens_memprof/lens_memprof.c
129
+ - Rakefile
130
+ - lens.gemspec
140
131
  - lib/lens.rb
141
132
  - lib/lens/allocations_data.rb
142
133
  - lib/lens/compression.rb
@@ -152,6 +143,13 @@ files:
152
143
  - lib/lens/trace.rb
153
144
  - lib/lens/version.rb
154
145
  - lib/lens/worker.rb
146
+ - spec/lens/event_formatter_spec.rb
147
+ - spec/lens/lens_spec.rb
148
+ - spec/lens/sender_spec.rb
149
+ - spec/lens/trace_spec.rb
150
+ - spec/lens/worker_spec.rb
151
+ - spec/spec_helper.rb
152
+ - spec/support/helpers.rb
155
153
  homepage: https://github.com/lenshq/lens_client
156
154
  licenses:
157
155
  - MIT
@@ -176,5 +174,12 @@ rubygems_version: 2.0.14
176
174
  signing_key:
177
175
  specification_version: 4
178
176
  summary: Gem to send Rails request stats
179
- test_files: []
177
+ test_files:
178
+ - spec/lens/event_formatter_spec.rb
179
+ - spec/lens/lens_spec.rb
180
+ - spec/lens/sender_spec.rb
181
+ - spec/lens/trace_spec.rb
182
+ - spec/lens/worker_spec.rb
183
+ - spec/spec_helper.rb
184
+ - spec/support/helpers.rb
180
185
  has_rdoc:
@@ -1,4 +0,0 @@
1
- require 'mkmf'
2
-
3
- dir_config('lens_memprof')
4
- create_makefile('lens_memprof')
@@ -1,75 +0,0 @@
1
- #include "ruby.h"
2
- #include "ruby/intern.h"
3
-
4
- /*
5
- * Debugger for VALUE
6
- *
7
- static void d(VALUE v) {
8
- ID sym_puts = rb_intern("puts");
9
- ID sym_inspect = rb_intern("inspect");
10
- rb_funcall(rb_mKernel, sym_puts, 1, rb_funcall(v, sym_inspect, 0));
11
- }
12
- */
13
-
14
- /*
15
- * Count of RUBY_INTERNAL_EVENT_NEWOBJ events
16
- * */
17
- typedef struct lens_allocations_struct {
18
- uint64_t count;
19
- } lens_allocations_t;
20
-
21
- /*
22
- * Use TLS for storing allocations info
23
- * More info: https://gcc.gnu.org/onlinedocs/gcc-3.4.1/gcc/Thread-Local.html
24
- * */
25
- static __thread lens_allocations_t lens_allocations;
26
-
27
- static inline lens_allocations_t* allocations() { return &lens_allocations; }
28
-
29
- void lens_memory_increment(rb_event_flag_t flag, VALUE node, VALUE self, ID mid, VALUE klass) {
30
- (void)(flag); (void)(node); (void)(self); (void)(mid); (void)(klass);
31
- allocations()->count++;
32
- }
33
-
34
- void lens_activate_memprof() {
35
- rb_add_event_hook(lens_memory_increment, RUBY_INTERNAL_EVENT_NEWOBJ, Qnil);
36
- }
37
-
38
- void lens_deactivate_memprof() {
39
- rb_remove_event_hook(lens_memory_increment);
40
- }
41
-
42
- static VALUE start() {
43
- lens_activate_memprof();
44
- return Qnil;
45
- }
46
-
47
- static VALUE stop() {
48
- lens_deactivate_memprof();
49
- return Qnil;
50
- }
51
-
52
- static VALUE reset() {
53
- allocations()->count = 0;
54
- return Qnil;
55
- }
56
-
57
- /*
58
- * Initialize on require
59
- * */
60
- void Init_lens_memprof();
61
- VALUE get_allocations_count(VALUE self);
62
-
63
- void Init_lens_memprof()
64
- {
65
- VALUE CMemProf = rb_define_module("LensCMemprof");
66
- rb_define_method(CMemProf, "allocations", get_allocations_count, 0);
67
- rb_define_method(CMemProf, "start", start, 0);
68
- rb_define_method(CMemProf, "reset", reset, 0);
69
- rb_define_method(CMemProf, "stop", stop, 0);
70
- }
71
-
72
- VALUE get_allocations_count(VALUE self)
73
- {
74
- return UINT2NUM(allocations()->count);
75
- }