http_statsd 0.0.1

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.
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --backtrace
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - jruby-18mode
6
+ - jruby-19mode
7
+ - rbx-18mode
8
+ - rbx-19mode
9
+ - 1.8.7
10
+ - ree
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in http_statsd.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Grzesiek Kolodziejczyk
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,73 @@
1
+ # HttpStatsd
2
+
3
+ [![Build Status](https://secure.travis-ci.org/grk/http_statsd.png)](http://travis-ci.org/grk/http_statsd)
4
+
5
+ HttpStatsd provides a HTTP proxy for the statsd protocol. The use case is when
6
+ you need to collect metrics from an external source, but for obvious reasons
7
+ don't want to expose the udp interface of statsd.
8
+
9
+ It also provides a client for that proxy for easy integration with Ruby code.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'http_statsd'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install http_statsd
24
+
25
+ ## Usage
26
+
27
+ ### Server
28
+
29
+ There is an example `config.ru` file included in the repo. To use the proxy,
30
+ you need to provide at least one api key, with:
31
+
32
+ ```ruby
33
+ HttpStatsd::Server.set :api_keys, {
34
+ "username1" => "pass1",
35
+ "username2" => "pass2"
36
+ }
37
+ ```
38
+
39
+ You can also set the statsd server host and port (default is "localhost:8125"):
40
+
41
+ ```ruby
42
+ HttpStatsd::Server.set :statsd_host, "statsd.example.com"
43
+ HttpStatsd::Server.set :statsd_port, 8125
44
+ ```
45
+
46
+ ### Client
47
+
48
+ To access the server, you can use the built in client:
49
+
50
+ ```ruby
51
+ c = HttpStatsd::Client.new(base_uri: "http://statsd.example.com",
52
+ username: "username1", password: "pass1")
53
+ ```
54
+
55
+ Then, you can use the same interface as with the
56
+ [statsd-ruby](https://github.com/github/statsd-ruby) gem:
57
+
58
+ ```ruby
59
+ sample_rate = 0.1
60
+ c.increment("metric.counter", sample_rate)
61
+ c.decrement("metric.counter", sample_rate)
62
+ c.count("metric.counter", 121, sample_rate)
63
+ c.gauge("metric.gauge", 123)
64
+ c.timing("metric.timing", 15.52, sample_rate)
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "run specs"
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => [:spec]
@@ -0,0 +1,14 @@
1
+ # example config.ru
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ Bundler.require
7
+
8
+ require 'http_statsd/server'
9
+
10
+ HttpStatsd::Server.set :api_keys, {
11
+ "api1" => "pass1",
12
+ "api2" => "pass2"
13
+ }
14
+ run HttpStatsd::Server
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'http_statsd/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "http_statsd"
8
+ gem.version = HttpStatsd::VERSION
9
+ gem.authors = ["Grzesiek Kolodziejczyk"]
10
+ gem.email = ["gk@code-fu.pl"]
11
+ gem.summary = %q{HTTP proxy for statsd with basic auth}
12
+ gem.homepage = "https://github.com/grk/http_statsd"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_runtime_dependency "sinatra"
20
+ gem.add_runtime_dependency "statsd-ruby"
21
+ gem.add_runtime_dependency "httparty"
22
+
23
+ gem.add_development_dependency "rspec"
24
+ gem.add_development_dependency "rack-test"
25
+ gem.add_development_dependency "webmock"
26
+ gem.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,47 @@
1
+ require "httparty"
2
+
3
+ module HttpStatsd
4
+ class Client
5
+ include HTTParty
6
+
7
+ def initialize(opts = {})
8
+ @base_uri = opts[:base_uri]
9
+ @auth = {:username => opts[:username], :password => opts[:password]}
10
+ end
11
+
12
+ def increment(name, sample_rate = nil)
13
+ query = {:name => name}
14
+ query.merge!({:sample_rate => sample_rate}) if sample_rate
15
+ request(:increment, query)
16
+ end
17
+
18
+ def decrement(name, sample_rate = nil)
19
+ query = {:name => name}
20
+ query.merge!({:sample_rate => sample_rate}) if sample_rate
21
+ request(:decrement, query)
22
+ end
23
+
24
+ def count(name, value, sample_rate = nil)
25
+ query = {:name => name, :value => value}
26
+ query.merge!({:sample_rate => sample_rate}) if sample_rate
27
+ request(:count, query)
28
+ end
29
+
30
+ def gauge(name, value)
31
+ query = {:name => name, :value => value}
32
+ request(:gauge, query)
33
+ end
34
+
35
+ def timing(name, value, sample_rate = nil)
36
+ query = {:name => name, :value => value}
37
+ query.merge!({:sample_rate => sample_rate}) if sample_rate
38
+ request(:timing, query)
39
+ end
40
+
41
+ private
42
+ def request(action, query = {})
43
+ options = {:basic_auth => @auth, :query => query}
44
+ self.class.get("#{@base_uri}/#{action}", options)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,56 @@
1
+ require 'statsd'
2
+ require 'sinatra'
3
+
4
+ module HttpStatsd
5
+ class Server < Sinatra::Base
6
+ set :api_keys, {}
7
+ set :statsd_host, "localhost"
8
+ set :statsd_port, 8125
9
+
10
+ def statsd
11
+ @statsd ||= Statsd.new(settings.statsd_host, settings.statsd_port)
12
+ end
13
+
14
+ use Rack::Auth::Basic do |username, password|
15
+ settings.api_keys[username] == password
16
+ end
17
+
18
+ get '/increment' do
19
+ args = [params["name"]]
20
+ args << params["sample_rate"].to_f if params["sample_rate"]
21
+ statsd.increment(*args)
22
+ 204
23
+ end
24
+
25
+ get '/decrement' do
26
+ args = [params["name"]]
27
+ args << params["sample_rate"].to_f if params["sample_rate"]
28
+ statsd.decrement(*args)
29
+ 204
30
+ end
31
+
32
+ get '/count' do
33
+ args = [params["name"], params["value"].to_f]
34
+ args << params["sample_rate"].to_f if params["sample_rate"]
35
+ statsd.count(*args)
36
+ 204
37
+ end
38
+
39
+ get '/gauge' do
40
+ args = [params["name"], params["value"].to_f]
41
+ statsd.gauge(*args)
42
+ 204
43
+ end
44
+
45
+ get '/timing' do
46
+ args = [params["name"], params["value"].to_f]
47
+ args << params["sample_rate"].to_f if params["sample_rate"]
48
+ statsd.timing(*args)
49
+ 204
50
+ end
51
+
52
+ get '/' do
53
+ 200
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module HttpStatsd
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "http_statsd/version"
2
+ require "http_statsd/server"
3
+ require "http_statsd/client"
4
+
5
+ module HttpStatsd
6
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe HttpStatsd::Client do
4
+ let(:base_uri) { "http://localhost" }
5
+ let(:username) { "api1" }
6
+ let(:password) { "pass1" }
7
+ let(:client) { HttpStatsd::Client.new(:base_uri => base_uri,
8
+ :username => username, :password => password) }
9
+
10
+ describe "increment" do
11
+ it "should make a request with metric name and sample rate" do
12
+ stub_request(:get, "api1:pass1@localhost/increment").
13
+ with(:query => {:name => "metric.one", :sample_rate => "0.1"})
14
+ client.increment("metric.one", "0.1")
15
+ end
16
+ end
17
+
18
+ describe "decrement" do
19
+ it "should make a request with metric name and sample rate" do
20
+ stub_request(:get, "api1:pass1@localhost/decrement").
21
+ with(:query => {:name => "metric.one", :sample_rate => "0.1"})
22
+ client.decrement("metric.one", "0.1")
23
+ end
24
+ end
25
+
26
+ describe "count" do
27
+ it "should make a request with metric name, value and sample rate" do
28
+ stub_request(:get, "api1:pass1@localhost/count").
29
+ with(:query => {:name => "metric.one", :value => "11.1",
30
+ :sample_rate => "0.1"})
31
+ client.count("metric.one", 11.1, 0.1)
32
+ end
33
+ end
34
+
35
+ describe "gauge" do
36
+ it "should make a request with metric name and value" do
37
+ stub_request(:get, "api1:pass1@localhost/gauge").
38
+ with(:query => {:name => "metric.one", :value => "11.1"})
39
+ client.gauge("metric.one", 11.1)
40
+ end
41
+ end
42
+
43
+ describe "timing" do
44
+ it "should make a request with metric name, value and sample rate" do
45
+ stub_request(:get, "api1:pass1@localhost/timing").
46
+ with(:query => {:name => "metric.one", :value => "11.1",
47
+ :sample_rate => "0.1"})
48
+ client.timing("metric.one", 11.1, 0.1)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe HttpStatsd::Server do
4
+ let(:statsd) { mock(Statsd).as_null_object }
5
+
6
+ before(:all) do
7
+ app.set :api_keys, {"app1" => "key1"}
8
+ end
9
+
10
+ before(:each) do
11
+ authorize "app1", "key1"
12
+ Statsd.stub(:new => statsd)
13
+ end
14
+
15
+ describe "api keys" do
16
+ it "should allow valid api keys" do
17
+ authorize "app1", "key1"
18
+ get "/"
19
+ last_response.should be_ok
20
+ end
21
+
22
+ it "should reject invalid api keys" do
23
+ authorize "app2", "key2"
24
+ get "/"
25
+ last_response.status.should eql(401)
26
+ end
27
+ end
28
+
29
+ describe "/increment" do
30
+ it "should create statsd client with given host and port" do
31
+ Statsd.should_receive(:new).with("localhost", 8125).and_return(statsd)
32
+ get "/increment", :name => "metric.one"
33
+ end
34
+
35
+ it "should increment in statsd" do
36
+ statsd.should_receive(:increment).with("metric.one", 0.1)
37
+ get "/increment", :name => "metric.one", :sample_rate => "0.1"
38
+ end
39
+ end
40
+
41
+ describe "/decrement" do
42
+ it "should decrement in statsd" do
43
+ statsd.should_receive(:decrement).with("metric.one", 0.1)
44
+ get "/decrement", :name => "metric.one", :sample_rate => "0.1"
45
+ end
46
+ end
47
+
48
+ describe "/count" do
49
+ it "should count in statsd" do
50
+ statsd.should_receive(:count).with("metric.one", 11.1, 0.1)
51
+ get "/count", :name => "metric.one", :value => "11.1",
52
+ :sample_rate => "0.1"
53
+ end
54
+ end
55
+
56
+ describe "/gauge" do
57
+ it "should store gauge in statsd" do
58
+ statsd.should_receive(:gauge).with("metric.one", 11.1)
59
+ get "/gauge", :name => "metric.one", :value => "11.1"
60
+ end
61
+ end
62
+
63
+ describe "/timing" do
64
+ it "should measure timing in statsd" do
65
+ statsd.should_receive(:timing).with("metric.one", 11.1, 0.1)
66
+ get "/timing", :name => "metric.one", :value => "11.1",
67
+ :sample_rate => "0.1"
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'rack/test'
5
+ require 'webmock/rspec'
6
+
7
+ require 'http_statsd'
8
+
9
+ def app
10
+ HttpStatsd::Server
11
+ end
12
+
13
+ app.set :environment, :test
14
+ app.set :raise_errors, true
15
+ app.set :logging, false
16
+
17
+ RSpec.configure do |config|
18
+ config.include Rack::Test::Methods
19
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: http_statsd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Grzesiek Kolodziejczyk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: statsd-ruby
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: httparty
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rack-test
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: webmock
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rake
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description:
127
+ email:
128
+ - gk@code-fu.pl
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - .rspec
135
+ - .travis.yml
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - examples/config.ru
141
+ - http_statsd.gemspec
142
+ - lib/http_statsd.rb
143
+ - lib/http_statsd/client.rb
144
+ - lib/http_statsd/server.rb
145
+ - lib/http_statsd/version.rb
146
+ - spec/client_spec.rb
147
+ - spec/server_spec.rb
148
+ - spec/spec_helper.rb
149
+ homepage: https://github.com/grk/http_statsd
150
+ licenses: []
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ none: false
157
+ requirements:
158
+ - - ! '>='
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ segments:
162
+ - 0
163
+ hash: 1477210855689509257
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ none: false
166
+ requirements:
167
+ - - ! '>='
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ segments:
171
+ - 0
172
+ hash: 1477210855689509257
173
+ requirements: []
174
+ rubyforge_project:
175
+ rubygems_version: 1.8.24
176
+ signing_key:
177
+ specification_version: 3
178
+ summary: HTTP proxy for statsd with basic auth
179
+ test_files:
180
+ - spec/client_spec.rb
181
+ - spec/server_spec.rb
182
+ - spec/spec_helper.rb