adhearsion-stats 0.0.1

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: d587ad87763835b7a5f856f76682467a653924f7
4
+ data.tar.gz: 1d4afde478e4c00eb75b01b36bf263d0fc57dacc
5
+ SHA512:
6
+ metadata.gz: db4891c188316c18910dd11ed2b0afc93394a2fcfc1d4b42833d30e3ba55f996a96ad567e361e77909082dcf6075455f8a91ec5a293fd906602842ef1d108e46
7
+ data.tar.gz: b71ad1fe5885410f537439a03a7c1786124d471b6d0ae026c25890086e3530aa69f6831859a87d8de3f622a1bc26d61fbaea2a77918f4d9de13c364b312e2c74
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ # Gem related stuff
2
+ *.gem
3
+ /pkg
4
+ /.bundle
5
+ /Gemfile.lock
6
+ /vendor
7
+
8
+ # RBX stuff
9
+ /.rbx/
10
+
11
+ # Editor temp/backup files
12
+ *~
13
+ .*.sw?
14
+
15
+ # General
16
+ /nbproject
17
+ .DS_Store
18
+ /.rvmrc
19
+ /.yardoc
20
+ /doc
21
+ /tmp
22
+ /coverage
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - jruby-19mode
7
+ - rbx-2.1.1
8
+ - ruby-head
9
+ matrix:
10
+ allow_failures:
11
+ - rvm: rbx-2.1.1
12
+ - rvm: ruby-head
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec', cli: '--format documentation', all_on_start: false do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec/" }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2013 Adhearsion Foundation Inc.
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,121 @@
1
+ # Adhearsion Stats [![Build Status](https://secure.travis-ci.org/JustinAiken/adhearsion-stats.png?branch=master)](http://travis-ci.org/JustinAiken/adhearsion-stats)[![Code Climate](https://codeclimate.com/github/JustinAiken/adhearsion-stats.png)](https://codeclimate.com/github/JustinAiken/adhearsion-stats)[![Coverage Status](https://coveralls.io/repos/JustinAiken/adhearsion-stats/badge.png)](https://coveralls.io/r/JustinAiken/adhearsion-stats)
2
+
3
+ This is an Adhearsion plugin for sending metrics to a [statsd server](https://github.com/etsy/statsd/).
4
+
5
+ It uses the [statsd gem](https://github.com/reinh/statsd).
6
+
7
+ ## Usage Example
8
+
9
+ ```ruby
10
+ class WellTrackedCallController < Adhearsion::CallController
11
+
12
+ before_call :start_stats
13
+ after_call :end_stats
14
+
15
+ def run
16
+ answer
17
+ ...
18
+ hangup
19
+ end
20
+
21
+ private
22
+
23
+ def start_stats
24
+ @call_started = Time.now.to_f
25
+ AdhearsionStats.increment "calls.started"
26
+ AdhearsionStats.gauge "calls.in_progress", "+1"
27
+ end
28
+
29
+ def end_stats
30
+ AdhearsionStats.gauge "calls.in_progress", "-1"
31
+
32
+ #Track call duration in ms
33
+ call_duration = Time.now.to_f - @call_started
34
+ AdhearsionStats.timing "calls.length", (call_duration * 1000).to_i
35
+ end
36
+ end
37
+ ```
38
+
39
+ ## Optional Logging
40
+
41
+ If you set the `log_metrics` option to true, it will generate a log file called `adhearsion-stats.log` next to the `adhearsion.log` showing every call send to statsd:
42
+
43
+ ```
44
+ ...
45
+ 2014-01-14 01:08:53 PM: increment(foo)
46
+ 2014-01-14 01:08:53 PM: timing(bar,100)
47
+ ...
48
+ ```
49
+
50
+ ## Stat Types
51
+
52
+ #### Counters (`#increment` and `#decrement`)
53
+
54
+ Either increments or decrements a simple counter. Mostly used for checking rate of things happening.
55
+ By default will go up or down one, but you can also pass a value to increment a set amount:
56
+
57
+ ```ruby
58
+ AdhearsionStats.increment "foo", 10
59
+ ```
60
+
61
+ #### Timers (`#timing` and `#time`)
62
+
63
+ Besides timing, can also be used for percents:
64
+
65
+ ```ruby
66
+ 6.times do
67
+ AdhearsionStats.timing "foo", 100
68
+ end
69
+
70
+ 4.times do
71
+ AdhearsionStats.timing "foo", 0
72
+ end
73
+
74
+ # Sixty percent of the time, it works every time!
75
+
76
+ ```
77
+
78
+ A block method is also available if you want to time an action:
79
+
80
+ ```ruby
81
+
82
+ AdhearsionStats.time "time_spent_in_menu" do
83
+ menu menu_message do
84
+ ...
85
+ end
86
+ end
87
+
88
+ ```
89
+
90
+ #### Gauges (`#gauge`)
91
+
92
+ Used to track running counts of something - these aren't reset each flush period.
93
+
94
+ ```ruby
95
+ AdhearsionStats.gauge "foo", +4 #This now goes up 4 and stays up 4 until the next time the gauge is sent something
96
+ ```
97
+
98
+ ### Links
99
+
100
+ * [Adhearsion](http://adhearsion.com)
101
+ * [Source](https://github.com/JustinAiken/adhearsion-stats)
102
+ * [Bug Tracker](https://github.com/JustinAiken/adhearsion-stats/issues)
103
+
104
+ ### Note on Patches/Pull Requests
105
+
106
+ * Fork the project.
107
+ * Make your feature addition or bug fix.
108
+ * Add tests for it. This is important so I don't break it in a future version unintentionally.
109
+ * Commit, do not mess with Rakefile, version, or history.
110
+ * If you want to have your own version, that is fine but bump version in a commit by itself so I can ignore when I pull
111
+ * Send me a pull request. Bonus points for topic branches.
112
+
113
+ ### Credits
114
+
115
+ Original author: [JustinAiken](https://github.com/JustinAiken)
116
+
117
+ Developed for [Mojo Lingo](http://mojolingo.com).
118
+
119
+ ### Copyright
120
+
121
+ Copyright (c) 2013 Adhearsion Foundation Inc. MIT license (see LICENSE for details).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # -*- ruby -*-
2
+ require 'bundler/gem_tasks'
3
+ require 'bundler/setup'
4
+
5
+ task :default => :spec
6
+
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new :spec
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "adhearsion-stats/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "adhearsion-stats"
7
+ s.version = AdhearsionStats::VERSION
8
+ s.authors = ["JustinAiken"]
9
+ s.email = ["jaiken@mojolingo.com"]
10
+ s.homepage = "https://github.com/polysics/adhearsion-stats"
11
+ s.summary = %q{Adhearsion plugin for stats}
12
+ s.description = %q{Adhearsion plugin for stats. Reports via statsd.}
13
+ s.license = 'MIT'
14
+
15
+ s.rubyforge_project = "adhearsion-stats"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency %q<adhearsion>, ["~> 2.1"]
22
+ s.add_runtime_dependency %q<statsd-ruby>
23
+
24
+ s.add_development_dependency %q<coveralls>, ['>= 0']
25
+ s.add_development_dependency %q<bundler>, ["~> 1.0"]
26
+ s.add_development_dependency %q<rspec>, ["~> 2.5"]
27
+ s.add_development_dependency %q<rake>, [">= 0"]
28
+ s.add_development_dependency %q<guard-rspec>
29
+ end
@@ -0,0 +1,5 @@
1
+ module AdhearsionStats; end
2
+
3
+ require "adhearsion-stats/version"
4
+ require "adhearsion-stats/metrics_logger"
5
+ require "adhearsion-stats/plugin"
@@ -0,0 +1,17 @@
1
+ require 'logger'
2
+
3
+ module AdhearsionStats
4
+
5
+ class MetricsLogger < Logger
6
+ def format_message(severity, timestamp, progname, msg)
7
+ nice = timestamp.strftime("%Y-%m-%d %I:%M:%S %p")
8
+ "#{nice}: #{msg}\n"
9
+ end
10
+ end
11
+
12
+ def self.setup_logger
13
+ logfile = File.open(File.dirname(__FILE__) + "#{Adhearsion.root}/log/adhearsion-stats.log", 'a')
14
+ logfile.sync = true
15
+ @metrics_logger = MetricsLogger.new(logfile)
16
+ end
17
+ end
@@ -0,0 +1,35 @@
1
+ require 'statsd'
2
+
3
+ module AdhearsionStats
4
+
5
+ class << self
6
+ attr_accessor :statsd, :loaded, :metrics_logger
7
+ end
8
+
9
+ class Plugin < Adhearsion::Plugin
10
+
11
+ config :statsd do
12
+ host "127.0.0.1", desc: "The host that statsd is found at"
13
+ port 8125, desc: "The port that statsd is found at"
14
+ log_metrics false, desc: "Whether or not to log stats sent to statsd"
15
+ end
16
+
17
+ init :statsd do
18
+ AdhearsionStats.setup_logger if Adhearsion.config.statsd.log_metrics
19
+
20
+ AdhearsionStats.statsd = Statsd.new Adhearsion.config.statsd.host, Adhearsion.config.statsd.port
21
+ AdhearsionStats.loaded = true
22
+
23
+ logger.info "Adhearsion-Stats has been loaded"
24
+ end
25
+ end
26
+
27
+ class << self
28
+ def method_missing(meth, *args, &blk)
29
+ if Adhearsion.config[:statsd].log_metrics
30
+ metrics_logger.send :info, "#{meth}(#{args.join(",")})"
31
+ end
32
+ AdhearsionStats.statsd.send meth, *args
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module AdhearsionStats
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe AdhearsionStats do
4
+
5
+ describe "plugin configuration" do
6
+ it "sets the default values" do
7
+ Adhearsion.config[:statsd].host.should == '127.0.0.1'
8
+ Adhearsion.config[:statsd].port.should == 8125
9
+ Adhearsion.config[:statsd].log_metrics.should be_false
10
+ end
11
+ end
12
+
13
+ describe "initialization" do
14
+ let(:logfile) { double "File", 'sync=' => true }
15
+ let!(:dummy_logger) { AdhearsionStats::MetricsLogger.new(STDOUT) }
16
+
17
+ before do
18
+ File.stub(:open).and_return logfile
19
+ AdhearsionStats::MetricsLogger.stub(:new).with(logfile).and_return dummy_logger
20
+ Adhearsion.config[:statsd].log_metrics = true
21
+ Adhearsion::Plugin.initializers.each { |plugin_initializer| plugin_initializer.run }
22
+ end
23
+
24
+ it "sets init variables" do
25
+ subject.statsd.should be_an_instance_of Statsd
26
+ subject.statsd.host.should == "127.0.0.1"
27
+ subject.statsd.port.should == 8125
28
+
29
+ subject.metrics_logger.should be_an_instance_of AdhearsionStats::MetricsLogger
30
+ subject.loaded.should be_true
31
+ end
32
+
33
+ describe "the plugin" do
34
+ let(:statsd) { FakeStatsd.new }
35
+ let(:stats_sent) { statsd.socket.buffer.flatten }
36
+
37
+ before { subject.statsd = statsd }
38
+
39
+ it "has methods that calls statsd" do
40
+ subject.increment "foo"
41
+ subject.timing "bar", 100
42
+ subject.gauge "biz", "+1"
43
+
44
+ stats_sent.should include "foo:1|c"
45
+ stats_sent.should include "bar:100|ms"
46
+ stats_sent.should include "biz:+1|g"
47
+ end
48
+
49
+ describe "logging" do
50
+ it "adds logging to all reporting methods" do
51
+ subject.metrics_logger.should_receive(:info).with 'increment(foo)'
52
+ subject.increment "foo"
53
+
54
+ subject.metrics_logger.should_receive(:info).with 'timing(bar,100)'
55
+ subject.timing "bar", 100
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,16 @@
1
+ require 'adhearsion'
2
+
3
+ require 'coveralls'
4
+ Coveralls.wear!
5
+
6
+ require 'adhearsion-stats'
7
+ require 'support/statsd_test_helper'
8
+
9
+ RSpec.configure do |config|
10
+ config.color_enabled = true
11
+ config.tty = true
12
+
13
+ config.filter_run focus: true
14
+ config.run_all_when_everything_filtered = true
15
+ end
16
+
@@ -0,0 +1,17 @@
1
+ class FakeUDPSocket
2
+ def buffer
3
+ @buffer ||= []
4
+ end
5
+
6
+ def send(message, *rest)
7
+ buffer.push [message]
8
+ end
9
+
10
+ def to_s; buffer; end
11
+ end
12
+
13
+ class FakeStatsd < Statsd
14
+ def socket
15
+ @socket ||= FakeUDPSocket.new
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adhearsion-stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - JustinAiken
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: adhearsion
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: statsd-ruby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '2.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '2.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Adhearsion plugin for stats. Reports via statsd.
112
+ email:
113
+ - jaiken@mojolingo.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .rspec
120
+ - .travis.yml
121
+ - Gemfile
122
+ - Guardfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - adhearsion-stats.gemspec
127
+ - lib/adhearsion-stats.rb
128
+ - lib/adhearsion-stats/metrics_logger.rb
129
+ - lib/adhearsion-stats/plugin.rb
130
+ - lib/adhearsion-stats/version.rb
131
+ - spec/adhearsion-stats/plugin_spec.rb
132
+ - spec/spec_helper.rb
133
+ - spec/support/statsd_test_helper.rb
134
+ homepage: https://github.com/polysics/adhearsion-stats
135
+ licenses:
136
+ - MIT
137
+ metadata: {}
138
+ post_install_message:
139
+ rdoc_options: []
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - '>='
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project: adhearsion-stats
154
+ rubygems_version: 2.0.3
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Adhearsion plugin for stats
158
+ test_files: []