Permalink
Browse files

Simplify packet generation implementation.

Warning: this means that calling gauge on statsite will now actually be a gauge instead of a key value. If you want to use a key_value, use StatsD.key_value instead.
  • Loading branch information...
1 parent b357bfa commit 49e30cec1a235e7205cdc8b4e34f4637eb3e1579 @wvanbergen wvanbergen committed Feb 7, 2014
Showing with 20 additions and 21 deletions.
  1. +5 −16 lib/statsd/instrument.rb
  2. +15 −5 test/statsd_test.rb
View
@@ -171,10 +171,12 @@ def gauge(key, value, *metric_options)
# histogram:123.45|h
def histogram(key, value, *metric_options)
+ raise NotImplementedError, "StatsD.histogram only supported on :datadog implementation." unless self.implementation == :datadog
collect(:h, key, value, hash_argument(metric_options))
end
def key_value(key, value, *metric_options)
+ raise NotImplementedError, "StatsD.key_value only supported on :statsite implementation." unless self.implementation == :statsite
collect(:kv, key, value, hash_argument(metric_options))
end
@@ -235,24 +237,11 @@ def clean_tags(tags)
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 :c
- 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 = self.prefix ? self.prefix + '.' : ''
+ command << "#{k}:#{v}|#{type}"
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
+ raise ArgumentError, "Tags are only supported on :datadog implementation" unless self.implementation == :datadog
command << "|##{clean_tags(tags).join(',')}"
end
View
@@ -66,7 +66,12 @@ def test_statsd_histogram_on_datadog
StatsD.stubs(:implementation).returns(:datadog)
StatsD.expects(:collect).with(:h, 'values.hg', 12.33, :sample_rate => 0.2, :tags => ['tag_123', 'key-name:value123'])
StatsD.histogram('values.hg', 12.33, :sample_rate => 0.2, :tags => ['tag_123', 'key-name:value123'])
- end
+ end
+
+ def test_raise_when_using_histograms_and_not_on_datadog
+ StatsD.stubs(:implementation).returns(:other)
+ assert_raises(NotImplementedError) { StatsD.histogram('foohg', 3.33) }
+ end
def test_collect_respects_enabled
StatsD.stubs(:enabled).returns(false)
@@ -146,18 +151,23 @@ def test_rewrite_shitty_tags
StatsD.increment('fooc', 3, 1.0, ['topic:foo : foo', 'bar '])
end
- def test_supports_gauge_syntax_on_statsite
+ def test_supports_key_value_syntax_on_statsite
StatsD.stubs(:implementation).returns(:statsite)
StatsD.expects(:write_packet).with("fooy:42|kv\n")
- StatsD.gauge('fooy', 42)
+ StatsD.key_value('fooy', 42)
end
- def test_supports_gauge_timestamp_on_statsite
+ def test_supports_key_value_with_timestamp_on_statsite
StatsD.stubs(:implementation).returns(:statsite)
StatsD.expects(:write_packet).with("fooy:42|kv|@123456\n")
- StatsD.gauge('fooy', 42, 123456)
+ StatsD.key_value('fooy', 42, 123456)
+ end
+
+ def test_raise_when_using_key_value_and_not_on_statsite
+ StatsD.stubs(:implementation).returns(:other)
+ assert_raises(NotImplementedError) { StatsD.key_value('fookv', 3.33) }
end
def test_support_key_prefix

0 comments on commit 49e30ce

Please sign in to comment.