Skip to content
Browse files

Make private methods actually private.

  • Loading branch information...
1 parent ff47432 commit ec6dd2e45b3a7da3348a72da4efefcc71c1addf2 @wvanbergen wvanbergen committed
Showing with 109 additions and 100 deletions.
  1. +106 −97 lib/statsd/instrument.rb
  2. +3 −3 test/statsd_test.rb
View
203 lib/statsd/instrument.rb
@@ -25,6 +25,10 @@ def self.port=(port)
invalidate_socket
end
+ def self.invalidate_socket
+ @socket = nil
+ end
+
module Instrument
def self.generate_metric_name(metric_name, callee, *args)
@@ -129,127 +133,132 @@ def remove_from_method(method, name, action)
end
end
- # glork:320|ms
- def self.measure(key, value = nil, *metric_options)
- if value.is_a?(Hash) && metric_options.empty?
- metric_options = [value]
- value = nil
- end
+ class << self
- result = nil
- ms = value || 1000 * Benchmark.realtime do
- result = yield
- end
+ # glork:320|ms
+ def measure(key, value = nil, *metric_options)
+ if value.is_a?(Hash) && metric_options.empty?
+ metric_options = [value]
+ value = nil
+ end
- collect(:ms, key, ms, hash_argument(metric_options))
- result
- end
+ result = nil
+ ms = value || 1000 * Benchmark.realtime do
+ result = yield
+ end
- # gorets:1|c
- def self.increment(key, value = 1, *metric_options)
- if value.is_a?(Hash) && metric_options.empty?
- metric_options = [value]
- value = 1
+ collect(:ms, key, ms, hash_argument(metric_options))
+ result
end
- collect(:incr, key, value, hash_argument(metric_options))
- end
+ # gorets:1|c
+ def counter(key, value = 1, *metric_options)
+ if value.is_a?(Hash) && metric_options.empty?
+ metric_options = [value]
+ value = 1
+ end
- # gaugor:333|g
- # guagor:1234|kv|@1339864935 (statsite)
- def self.gauge(key, value, *metric_options)
- collect(:g, key, value, hash_argument(metric_options))
- end
+ collect(:incr, key, value, hash_argument(metric_options))
+ end
- # histogram:123.45|h
- def self.histogram(key, value, *metric_options)
- collect(:h, key, value, hash_argument(metric_options))
- end
+ alias_method :increment, :counter
- # uniques:765|s
- def self.set(key, value, *metric_options)
- collect(:s, key, value, hash_argument(metric_options))
- end
+ # gaugor:333|g
+ # guagor:1234|kv|@1339864935 (statsite)
+ def gauge(key, value, *metric_options)
+ collect(:g, key, value, hash_argument(metric_options))
+ end
- private
+ # histogram:123.45|h
+ def histogram(key, value, *metric_options)
+ collect(:h, key, value, hash_argument(metric_options))
+ end
- def self.hash_argument(args)
- return {} if args.length == 0
- return args.first if args.length == 1 && args.first.is_a?(Hash)
+ def key_value(key, value, *metric_options)
+ collect(:kv, key, value, hash_argument(metric_options))
+ end
- order = [:sample_rate, :tags]
- hash = {}
- args.each_with_index do |value, index|
- hash[order[index]] = value
- end
-
- return hash
- end
+ # uniques:765|s
+ def set(key, value, *metric_options)
+ collect(:s, key, value, hash_argument(metric_options))
+ end
- def self.invalidate_socket
- @socket = nil
- end
+ private
- def self.socket
- if @socket.nil?
- @socket = UDPSocket.new
- @socket.connect(host, port)
+ def hash_argument(args)
+ return {} if args.length == 0
+ return args.first if args.length == 1 && args.first.is_a?(Hash)
+
+ order = [:sample_rate, :tags]
+ hash = {}
+ args.each_with_index do |value, index|
+ hash[order[index]] = value
+ end
+
+ return hash
end
- @socket
- end
- def self.collect(type, k, v, options = {})
- return unless enabled
- sample_rate = options[:sample_rate] || StatsD.default_sample_rate
- return if sample_rate < 1 && rand > sample_rate
+ def socket
+ if @socket.nil?
+ @socket = UDPSocket.new
+ @socket.connect(host, port)
+ end
+ @socket
+ end
- packet = generate_packet(type, k, v, sample_rate, options[:tags])
- write_packet(packet)
- end
+ def collect(type, k, v, options = {})
+ return unless enabled
+ sample_rate = options[:sample_rate] || StatsD.default_sample_rate
+ return if sample_rate < 1 && rand > sample_rate
- def self.write_packet(command)
- if mode.to_s == 'production'
- begin
- socket.send(command, 0)
- rescue SocketError, IOError, SystemCallError => e
- logger.error e
- end
- else
- logger.info "[StatsD] #{command}"
+ packet = generate_packet(type, k, v, sample_rate, options[:tags])
+ write_packet(packet)
end
- end
- def self.clean_tags(tags)
- tags.map do |tag|
- components = tag.split(':', 2)
- components.map { |c| c.gsub(/[^\w\.-]+/, '_') }.join(':')
+ def write_packet(command)
+ if mode.to_s == 'production'
+ begin
+ socket.send(command, 0)
+ rescue SocketError, IOError, SystemCallError => e
+ logger.error e
+ end
+ else
+ logger.info "[StatsD] #{command}"
+ end
end
- end
- def self.generate_packet(type, k, v, sample_rate = default_sample_rate, tags = nil)
- command = "#{self.prefix + '.' if self.prefix}#{k}:#{v}"
- case type
- when :incr
- command << '|c'
- when :ms
- command << '|ms'
- when :g
- command << (self.implementation == :statsite ? '|kv' : '|g')
- when :h
- raise NotImplementedError, "Histograms only supported on DataDog implementation." unless self.implementation == :datadog
- command << '|h'
- when :s
- command << '|s'
+ def clean_tags(tags)
+ tags.map do |tag|
+ components = tag.split(':', 2)
+ components.map { |c| c.gsub(/[^\w\.-]+/, '_') }.join(':')
+ end
end
- command << "|@#{sample_rate}" if sample_rate < 1 || (self.implementation == :statsite && sample_rate > 1)
- if tags
- raise ArgumentError, "Tags are only supported on Datadog" unless self.implementation == :datadog
- command << "|##{clean_tags(tags).join(',')}"
- end
+ def generate_packet(type, k, v, sample_rate = default_sample_rate, tags = nil)
+ command = "#{self.prefix + '.' if self.prefix}#{k}:#{v}"
+ case type
+ when :incr
+ command << '|c'
+ when :ms
+ command << '|ms'
+ when :g
+ command << (self.implementation == :statsite ? '|kv' : '|g')
+ when :h
+ raise NotImplementedError, "Histograms only supported on DataDog implementation." unless self.implementation == :datadog
+ command << '|h'
+ when :s
+ command << '|s'
+ end
+
+ command << "|@#{sample_rate}" if sample_rate < 1 || (self.implementation == :statsite && sample_rate > 1)
+ if tags
+ raise ArgumentError, "Tags are only supported on Datadog" unless self.implementation == :datadog
+ command << "|##{clean_tags(tags).join(',')}"
+ end
- command << "\n" if self.implementation == :statsite
- return command
+ command << "\n" if self.implementation == :statsite
+ return command
+ end
end
end
View
6 test/statsd_test.rb
@@ -138,9 +138,9 @@ def test_raise_when_using_tags_and_not_on_datadog
def test_rewrite_shitty_tags
StatsD.stubs(:implementation).returns(:datadog)
- assert_equal ['igno_red'], StatsD.clean_tags(['igno,red'])
- assert_equal ['igno_red'], StatsD.clean_tags(['igno red'])
- assert_equal ['test:test_test'], StatsD.clean_tags(['test:test:test'])
+ assert_equal ['igno_red'], StatsD.send(:clean_tags, ['igno,red'])
+ assert_equal ['igno_red'], StatsD.send(:clean_tags, ['igno red'])
+ assert_equal ['test:test_test'], StatsD.send(:clean_tags, ['test:test:test'])
StatsD.expects(:write_packet).with("fooc:3|c|#topic:foo_foo,bar_")
StatsD.increment('fooc', 3, 1.0, ['topic:foo : foo', 'bar '])

0 comments on commit ec6dd2e

Please sign in to comment.
Something went wrong with that request. Please try again.