tweek_metrics 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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDEzZmFhN2JjMGI5N2UyMzJhZDkxNzJmODFmMDljY2ZlNzM5ZDk3OA==
5
+ data.tar.gz: !binary |-
6
+ ZDQ5NTlkZTlhZjkxNjkwM2QwZTRlMzU0MmQ4YzBjNWI2NTRhODQxMw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YTYxZjk4MzA1ODA4YWM5OWMzY2QyMDMzZGM4MTU5MjRmY2ZlYTkzNjVlYWM0
10
+ MzNjM2Y4ZTcxNmNlODZhZTg4YTQ5NzhkN2EyMDdjOWM0MWI1NTI1NjI4YWUy
11
+ NzJmYWU2ZGJkNWI5NDhjNmI2YTZjYjMxMjg3NjFiZjc3NzM3Zjc=
12
+ data.tar.gz: !binary |-
13
+ NGVjMTBmNmM2ZWQ3NzlmMjRhY2E4NmM2MTdlOTAyZWQxMDJmYTBkOGY3NTcz
14
+ NGJhZmRjMWZjYTExY2MyM2ZlMmI5MGVmNDQ1OTRkOTQ1NjM0MzUzNjNlYTUy
15
+ ZWU4NWQyMTFhNjk1ZmQ1ZTFmODQ3ZGM2NGEyMzRiOWJiMWQ4ZWQ=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tweek_metrics.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 elvio
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
+ # TweekMetrics
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tweek_metrics'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tweek_metrics
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
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 new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ require "bundler"
2
+ Bundler.setup
3
+
4
+ require "bundler/gem_tasks"
5
+ require "rspec/core/rake_task"
6
+
7
+ task :build do
8
+ system "gem build tweek_metrics.gemspec"
9
+ end
10
+
11
+ task :install => :build do
12
+ system "gem install pkg/tweek_metrics-#{TweekApiAuth::VERSION}.gem"
13
+ end
14
+
15
+ task :release => :build do
16
+ system "git tag -a v#{TweekMetrics::VERSION} -m 'Tagging #{TweekMetrics::VERSION}'"
17
+ system "git push --tags"
18
+ system "rm *.gem"
19
+ end
20
+
21
+ RSpec::Core::RakeTask.new("spec") do |spec|
22
+ spec.pattern = "spec/**/*_spec.rb"
23
+ end
24
+
25
+ task :default => :spec
@@ -0,0 +1,46 @@
1
+ require "benchmark"
2
+
3
+ module TweekMetrics
4
+ class Handler
5
+ attr_reader :instruments, :key
6
+
7
+ def initialize(instruments, key, &block)
8
+ @instruments = instruments
9
+ @key = key
10
+ @block = block
11
+
12
+ handle!
13
+ end
14
+
15
+ def handle!
16
+ if @block
17
+ begin
18
+ @real_time = Benchmark.measure { @block.call }.real
19
+ rescue => e
20
+ @exception = e
21
+ end
22
+ end
23
+
24
+ if @exception
25
+ TweekMetrics.statsd.increment(create_key(TweekMetrics.namespace, :global, :exception, @exception.class.name))
26
+ TweekMetrics.statsd.increment(create_key(TweekMetrics.namespace, TweekMetrics.client, :exception, @exception.class.name))
27
+ else
28
+ if instruments.include?(:counter)
29
+ TweekMetrics.statsd.increment(create_key(TweekMetrics.namespace, :global, key))
30
+ TweekMetrics.statsd.increment(create_key(TweekMetrics.namespace, TweekMetrics.client, key))
31
+ end
32
+
33
+ if instruments.include?(:time)
34
+ TweekMetrics.statsd.timing(create_key(TweekMetrics.namespace, :global, key), @real_time)
35
+ TweekMetrics.statsd.timing(create_key(TweekMetrics.namespace, TweekMetrics.client, key), @real_time)
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def create_key(*items)
43
+ items.flatten.compact.join(".")
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module TweekMetrics
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,42 @@
1
+ require "statsd"
2
+ require "tweek_metrics/version"
3
+ require "tweek_metrics/handler"
4
+
5
+ module TweekMetrics
6
+ @@config = {}
7
+ @@statsd = nil
8
+
9
+ def self.set_statsd_host(host)
10
+ @@config[:statsd_host] = host
11
+ end
12
+
13
+ def self.set_statsd_port(port)
14
+ @@config[:statsd_port] = port
15
+ end
16
+
17
+ def self.set_client(client)
18
+ @@config[:client] = client
19
+ end
20
+
21
+ def self.set_namespace(namespace)
22
+ @@config[:namespace] = namespace
23
+ end
24
+
25
+ def self.namespace
26
+ @@config[:namespace]
27
+ end
28
+
29
+ def self.client
30
+ @@config[:client]
31
+ end
32
+
33
+ def self.statsd
34
+ @@statsd ||= Statsd.new(@@config[:statsd_host], @@config[:statsd_port])
35
+ end
36
+
37
+ def self.measure(instruments, key, &block)
38
+ raise ArgumentError.new("instruments must be an Array") unless instruments.kind_of?(Array)
39
+ raise ArgumentError.new("key must be an Array") unless key.kind_of?(Array)
40
+ Handler.new(instruments, key, &block)
41
+ end
42
+ end
@@ -0,0 +1,56 @@
1
+ require "spec_helper"
2
+
3
+ describe TweekMetrics::Handler do
4
+ let(:statsd) { double("Statsd") }
5
+
6
+ class Fake
7
+ def self.execute
8
+ end
9
+ end
10
+
11
+ before do
12
+ TweekMetrics.stub(:statsd).and_return(statsd)
13
+ statsd.stub(:increment)
14
+ statsd.stub(:timing)
15
+ end
16
+
17
+ context "measuring counter" do
18
+ it "increments the global counter" do
19
+ TweekMetrics.measure([:counter, :time], [:request]) { Fake.execute }
20
+ expect(statsd).to have_received(:increment).with("#{$API_NAME}.global.request")
21
+ end
22
+
23
+ it "increments the client specific counter" do
24
+ TweekMetrics.measure([:counter, :time], [:request]) { Fake.execute }
25
+ expect(statsd).to have_received(:increment).with("#{$API_NAME}.#{$CLIENT_NAME}.request")
26
+ end
27
+ end
28
+
29
+ context "measuring timing" do
30
+ it "adds a new value to the global timing" do
31
+ TweekMetrics.measure([:counter, :time], [:request]) { Fake.execute }
32
+ expect(statsd).to have_received(:timing).with("#{$API_NAME}.global.request", kind_of(Numeric))
33
+ end
34
+
35
+ it "adds a new value to the client specific timing" do
36
+ TweekMetrics.measure([:counter, :time], [:request]) { Fake.execute }
37
+ expect(statsd).to have_received(:timing).with("#{$API_NAME}.#{$CLIENT_NAME}.request", kind_of(Numeric))
38
+ end
39
+ end
40
+
41
+ context "exception handler" do
42
+ before do
43
+ expect(Fake).to receive(:execute).and_raise(ArgumentError.new)
44
+ end
45
+
46
+ it "increments the global exception counter" do
47
+ TweekMetrics.measure([:counter, :time], [:request]) { Fake.execute }
48
+ expect(statsd).to have_received(:increment).with("#{$API_NAME}.global.exception.ArgumentError")
49
+ end
50
+
51
+ it "increments the client exception counter" do
52
+ TweekMetrics.measure([:counter, :time], [:request]) { Fake.execute }
53
+ expect(statsd).to have_received(:increment).with("#{$API_NAME}.#{$CLIENT_NAME}.exception.ArgumentError")
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,14 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "tweek_metrics"
4
+ require "rspec/expectations"
5
+
6
+ $CLIENT_NAME = "tester"
7
+ $API_NAME = "test_api"
8
+
9
+ RSpec.configure do |config|
10
+ config.before(:all) do
11
+ TweekMetrics.set_client($CLIENT_NAME)
12
+ TweekMetrics.set_namespace($API_NAME)
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ describe TweekMetrics do
4
+ let(:instruments) { [:timing, :counter] }
5
+ let(:key) { [:account, :test] }
6
+
7
+ it "delegates to TweekMetrics::Handler" do
8
+ expect(TweekMetrics::Handler).to receive(:new).with(instruments, key)
9
+ TweekMetrics.measure(instruments, key)
10
+ end
11
+
12
+ context "when instruments are not known" do
13
+ it "raises an error" do
14
+ expect { TweekMetrics.measure(1, key) }.to raise_error(ArgumentError)
15
+ end
16
+ end
17
+
18
+ context "when provided instruments are not an Array like" do
19
+ it "raises an error" do
20
+ expect { TweekMetrics.measure(instruments, 1) }.to raise_error(ArgumentError)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tweek_metrics/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tweek_metrics"
8
+ spec.version = TweekMetrics::VERSION
9
+ spec.authors = ["elvio"]
10
+ spec.email = ["elvio@elviovicosa.com"]
11
+ spec.description = %q{Measure activity using StatsD}
12
+ spec.summary = %q{Measure activity using StatsD: Simple wrapper for Tweek specific needs}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "statsd-ruby"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tweek_metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - elvio
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: statsd-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Measure activity using StatsD
70
+ email:
71
+ - elvio@elviovicosa.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/tweek_metrics.rb
82
+ - lib/tweek_metrics/handler.rb
83
+ - lib/tweek_metrics/version.rb
84
+ - spec/handler_spec.rb
85
+ - spec/spec_helper.rb
86
+ - spec/tweek_metrics_spec.rb
87
+ - tweek_metrics.gemspec
88
+ homepage: ''
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.0.0
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: ! 'Measure activity using StatsD: Simple wrapper for Tweek specific needs'
112
+ test_files:
113
+ - spec/handler_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/tweek_metrics_spec.rb