http_statsd 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -62,6 +62,10 @@ c.decrement("metric.counter", sample_rate)
62
62
  c.count("metric.counter", 121, sample_rate)
63
63
  c.gauge("metric.gauge", 123)
64
64
  c.timing("metric.timing", 15.52, sample_rate)
65
+ c.batch do |b| # this will send the metrics as one http request
66
+ b.increment("metric.counter")
67
+ b.gauge("metric.gauge", 120)
68
+ end
65
69
  ```
66
70
 
67
71
  ## Contributing
@@ -12,36 +12,75 @@ module HttpStatsd
12
12
  def increment(name, sample_rate = nil)
13
13
  query = {:name => name}
14
14
  query.merge!({:sample_rate => sample_rate}) if sample_rate
15
- request(:increment, query)
15
+ get(:increment, query)
16
16
  end
17
17
 
18
18
  def decrement(name, sample_rate = nil)
19
19
  query = {:name => name}
20
20
  query.merge!({:sample_rate => sample_rate}) if sample_rate
21
- request(:decrement, query)
21
+ get(:decrement, query)
22
22
  end
23
23
 
24
24
  def count(name, value, sample_rate = nil)
25
25
  query = {:name => name, :value => value}
26
26
  query.merge!({:sample_rate => sample_rate}) if sample_rate
27
- request(:count, query)
27
+ get(:count, query)
28
28
  end
29
29
 
30
30
  def gauge(name, value)
31
31
  query = {:name => name, :value => value}
32
- request(:gauge, query)
32
+ get(:gauge, query)
33
33
  end
34
34
 
35
35
  def timing(name, value, sample_rate = nil)
36
36
  query = {:name => name, :value => value}
37
37
  query.merge!({:sample_rate => sample_rate}) if sample_rate
38
- request(:timing, query)
38
+ get(:timing, query)
39
+ end
40
+
41
+ def batch
42
+ batch_operation = BatchOperation.new
43
+ yield batch_operation
44
+ post(:batch, {:metrics => batch_operation.metrics.join("\n")})
39
45
  end
40
46
 
41
47
  private
42
- def request(action, query = {})
48
+ def get(action, query = {})
43
49
  options = {:basic_auth => @auth, :query => query}
44
50
  self.class.get("#{@base_uri}/#{action}", options)
45
51
  end
52
+
53
+ def post(action, body = {})
54
+ options = {:basic_auth => @auth, :body => body}
55
+ self.class.post("#{@base_uri}/#{action}", options)
56
+ end
57
+
58
+ class BatchOperation
59
+ attr_accessor :metrics
60
+
61
+ def initialize
62
+ @metrics = []
63
+ end
64
+
65
+ def increment(name, sample_rate = nil)
66
+ @metrics << "count #{name} 1 #{sample_rate}".strip
67
+ end
68
+
69
+ def decrement(name, sample_rate = nil)
70
+ @metrics << "count #{name} -1 #{sample_rate}".strip
71
+ end
72
+
73
+ def count(name, value, sample_rate = nil)
74
+ @metrics << "count #{name} #{value} #{sample_rate}".strip
75
+ end
76
+
77
+ def gauge(name, value)
78
+ @metrics << "gauge #{name} #{value}".strip
79
+ end
80
+
81
+ def timing(name, value, sample_rate = nil)
82
+ @metrics << "timing #{name} #{value} #{sample_rate}".strip
83
+ end
84
+ end
46
85
  end
47
86
  end
@@ -49,6 +49,17 @@ module HttpStatsd
49
49
  204
50
50
  end
51
51
 
52
+ post '/batch' do
53
+ metrics = params["metrics"]
54
+ metrics.split("\n").each do |metric|
55
+ metric = metric.split
56
+ args = [metric[0], metric[1], metric[2]]
57
+ args << metric[3].to_f if metric[3]
58
+ statsd.send(*args)
59
+ end
60
+ 201
61
+ end
62
+
52
63
  get '/' do
53
64
  200
54
65
  end
@@ -1,3 +1,3 @@
1
1
  module HttpStatsd
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -48,4 +48,61 @@ describe HttpStatsd::Client do
48
48
  client.timing("metric.one", 11.1, 0.1)
49
49
  end
50
50
  end
51
+
52
+ describe "batch" do
53
+ it "should make a single request with multiple metrics" do
54
+ stub_request(:post, "api1:pass1@localhost/batch").
55
+ with(:body => {:metrics => "count metric.one 1 0.2\ngauge metric.two 2"})
56
+ client.batch do |b|
57
+ b.count("metric.one", 1, 0.2)
58
+ b.gauge("metric.two", 2)
59
+ end
60
+ end
61
+ end
62
+
63
+ describe HttpStatsd::Client::BatchOperation do
64
+ let(:batch) { HttpStatsd::Client::BatchOperation.new }
65
+
66
+ describe "#metrics" do
67
+ it "is initially an empty array" do
68
+ batch.metrics.should eql([])
69
+ end
70
+ end
71
+
72
+ describe "#increment" do
73
+ it "should add an increment metric to the batch" do
74
+ batch.increment("metric.one", 0.2)
75
+ batch.metrics.should eql(["count metric.one 1 0.2"])
76
+ end
77
+ end
78
+
79
+ describe "#decrement" do
80
+ it "should add an decrement metric to the batch" do
81
+ batch.decrement("metric.one", 0.2)
82
+ batch.metrics.should eql(["count metric.one -1 0.2"])
83
+ end
84
+ end
85
+
86
+ describe "#count" do
87
+ it "should add a count metric to the batch" do
88
+ batch.count("metric.one", 12, 0.2)
89
+ batch.metrics.should eql(["count metric.one 12 0.2"])
90
+ end
91
+ end
92
+
93
+ describe "#gauge" do
94
+ it "should add a gauge metric to the batch" do
95
+ batch.gauge("metric.one", 12)
96
+ batch.metrics.should eql(["gauge metric.one 12"])
97
+ end
98
+ end
99
+
100
+ describe "#timing" do
101
+ it "should add a timing metric to the batch" do
102
+ batch.timing("metric.one", 12, 0.2)
103
+ batch.metrics.should eql(["timing metric.one 12 0.2"])
104
+ end
105
+ end
106
+
107
+ end
51
108
  end
@@ -67,4 +67,14 @@ describe HttpStatsd::Server do
67
67
  :sample_rate => "0.1"
68
68
  end
69
69
  end
70
+
71
+ describe "/batch" do
72
+ it "should process batch metrics" do
73
+ statsd.should_receive(:timing).with("metric.one", "11.1", 0.1)
74
+ statsd.should_receive(:gauge).with("metric.two", "12.2", 0.2)
75
+ statsd.should_receive(:count).with("metric.three", "13.3", 0.3)
76
+ post "/batch", :metrics =>
77
+ "timing metric.one 11.1 0.1\ngauge metric.two 12.2 0.2\n count metric.three 13.3 0.3"
78
+ end
79
+ end
70
80
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_statsd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-26 00:00:00.000000000 Z
12
+ date: 2012-10-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sinatra
@@ -160,7 +160,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
160
160
  version: '0'
161
161
  segments:
162
162
  - 0
163
- hash: 1477210855689509257
163
+ hash: 3862746067465240188
164
164
  required_rubygems_version: !ruby/object:Gem::Requirement
165
165
  none: false
166
166
  requirements:
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
169
  version: '0'
170
170
  segments:
171
171
  - 0
172
- hash: 1477210855689509257
172
+ hash: 3862746067465240188
173
173
  requirements: []
174
174
  rubyforge_project:
175
175
  rubygems_version: 1.8.24