statsd-instrument 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -136,6 +136,17 @@ AWS::S3::Base.singleton_class.extend StatsD::Instrument
136
136
  AWS::S3::Base.singleton_class.statsd_measure :request, 'S3.request'
137
137
  ```
138
138
 
139
+ ### Dynamic Metric Names
140
+
141
+ You can use a lambda function instead of a string dynamically set
142
+ the name of the metric. The lambda function must accept two arguments:
143
+ the object the function is being called on and the array of arguments
144
+ passed.
145
+
146
+ ```ruby
147
+ GoogleBase.statsd_count :insert, lamdba{|object, args| object.class.to_s.downcase + "." + args.first.to_s + ".insert" }
148
+ ```
149
+
139
150
  ## Reliance on DNS
140
151
  Out of the box StatsD is set up to be unidirectional fire-and-forget over UDP. Configuring the StatsD host to be a non-ip will trigger a DNS lookup (ie synchronous round trip network call) for each metric sent. This can be particularly problematic in clouds that have a shared DNS infrastructure such as AWS.
141
152
 
@@ -30,7 +30,7 @@ module StatsD
30
30
  def statsd_measure(method, name, sample_rate = StatsD.default_sample_rate)
31
31
  add_to_method(method, name, :measure) do |old_method, new_method, metric_name, *args|
32
32
  define_method(new_method) do |*args, &block|
33
- StatsD.measure(metric_name, nil, sample_rate) { send(old_method, *args, &block) }
33
+ StatsD.measure(metric_name.respond_to?(:call) ? metric_name.call(self, args) : metric_name, nil, sample_rate) { send(old_method, *args, &block) }
34
34
  end
35
35
  end
36
36
  end
@@ -47,7 +47,10 @@ module StatsD
47
47
  truthiness = (yield(result) rescue false) if block_given?
48
48
  result
49
49
  ensure
50
- StatsD.increment("#{metric_name}." + (truthiness == false ? 'failure' : 'success'), sample_rate)
50
+ result = truthiness == false ? 'failure' : 'success'
51
+ key = metric_name.respond_to?(:call) ? metric_name.call(self, args) : metric_name
52
+
53
+ StatsD.increment("#{key}.#{result}", sample_rate)
51
54
  end
52
55
  end
53
56
  end
@@ -65,7 +68,7 @@ module StatsD
65
68
  truthiness = (yield(result) rescue false) if block_given?
66
69
  result
67
70
  ensure
68
- StatsD.increment(metric_name, sample_rate) if truthiness
71
+ StatsD.increment(metric_name.respond_to?(:call) ? metric_name.call(self, args) : metric_name, sample_rate) if truthiness
69
72
  end
70
73
  end
71
74
  end
@@ -74,7 +77,7 @@ module StatsD
74
77
  def statsd_count(method, name, sample_rate = StatsD.default_sample_rate)
75
78
  add_to_method(method, name, :count) do |old_method, new_method, metric_name|
76
79
  define_method(new_method) do |*args, &block|
77
- StatsD.increment(metric_name, sample_rate)
80
+ StatsD.increment(metric_name.respond_to?(:call) ? metric_name.call(self, args) : metric_name, sample_rate)
78
81
  send(old_method, *args, &block)
79
82
  end
80
83
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "statsd-instrument"
3
- s.version = '1.4.0'
3
+ s.version = '1.5.0'
4
4
  s.authors = ["Jesse Storimer", "Tobias Lutke"]
5
5
  s.email = ["jesse@shopify.com"]
6
6
  s.homepage = "http://github.com/shopify/statsd-instrument"
@@ -53,6 +53,9 @@ class ActiveMerchant::UniqueGateway < ActiveMerchant::Base
53
53
  end
54
54
  end
55
55
 
56
+ class GatewaySubClass < ActiveMerchant::Gateway
57
+ end
58
+
56
59
  ActiveMerchant::Base.extend StatsD::Instrument
57
60
 
58
61
  class StatsDTest < Test::Unit::TestCase
@@ -128,6 +131,13 @@ class StatsDTest < Test::Unit::TestCase
128
131
  ActiveMerchant::Gateway.new.purchase(true)
129
132
  end
130
133
 
134
+ def test_statsd_count_with_name_as_lambda
135
+ ActiveMerchant::Gateway.statsd_count(:ssl_post, lambda {|object, args| object.class.to_s.downcase + ".insert." + args.first.to_s})
136
+
137
+ StatsD.expects(:increment).with('gatewaysubclass.insert.true', 1)
138
+ GatewaySubClass.new.purchase(true)
139
+ end
140
+
131
141
  def test_statsd_count_with_method_receiving_block
132
142
  ActiveMerchant::Base.statsd_count :post_with_block, 'ActiveMerchant.Base.post_with_block'
133
143
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statsd-instrument
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-10-12 00:00:00.000000000 Z
13
+ date: 2013-01-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mocha