lib/statsd/instrument/strict.rb in statsd-instrument-2.8.0 vs lib/statsd/instrument/strict.rb in statsd-instrument-2.9.0

- old
+ new

@@ -13,11 +13,11 @@ # - The metric methods are not retuning a Metric instance. # - Only accept keyword arguments for tags and sample_rate, rather than position arguments. # - Only accept a position argument for value, rather than a keyword argument. # - The provided arguments have the right type. # - # You can enable thois monkeypatch by changing your Gemfile as follows: + # You can enable this monkeypatch by changing your Gemfile as follows: # # gem 'statsd-instrument', require: 'statsd/instrument/strict' # # By doing this as part of your QA/CI, you can find where you are still using deprecated patterns, # and fix them before the deprecated behavior is removed in the next major version. @@ -79,10 +79,15 @@ check_tags_and_sample_rate(sample_rate, tags) super end + def key_value(*) + raise NotImplementedError, "The key_value metric type will be removed " \ + "from the next major version of statsd-instrument" + end + private def check_block_or_numeric_value(value) if block_given? raise ArgumentError, "The value argument should not be set when providing a block" unless value == UNSPECIFIED @@ -97,53 +102,63 @@ end unless tags.nil? || tags.is_a?(Hash) || tags.is_a?(Array) raise ArgumentError, "The tags argument should be a hash or an array, got #{tags.inspect}" end end + end + module VoidCollectMetric + protected + def collect_metric(type, name, value, sample_rate:, tags: nil, prefix:, metadata: nil) super - nil # We explicitly discard the return value, so people cannot depend on it. + StatsD::Instrument::VOID end end module StrictMetaprogramming - def statsd_measure(method, name, sample_rate: nil, tags: nil, no_prefix: false) + def statsd_measure(method, name, sample_rate: nil, tags: nil, + no_prefix: false, client: StatsD.singleton_client) + check_method_and_metric_name(method, name) - # Unfortunately, we have to inline the new method implementation ebcause we have to fix the + # Unfortunately, we have to inline the new method implementation because we have to fix the # Stats.measure call to not use the `as_dist` and `prefix` arguments. add_to_method(method, name, :measure) do define_method(method) do |*args, &block| key = StatsD::Instrument.generate_metric_name(nil, name, self, *args) - StatsD.measure(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do + client.measure(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do super(*args, &block) end end end end - def statsd_distribution(method, name, sample_rate: nil, tags: nil, no_prefix: false) + def statsd_distribution(method, name, sample_rate: nil, tags: nil, + no_prefix: false, client: StatsD.singleton_client) + check_method_and_metric_name(method, name) - # Unfortunately, we have to inline the new method implementation ebcause we have to fix the + # Unfortunately, we have to inline the new method implementation because we have to fix the # Stats.distribution call to not use the `prefix` argument. add_to_method(method, name, :distribution) do define_method(method) do |*args, &block| key = StatsD::Instrument.generate_metric_name(nil, name, self, *args) - StatsD.distribution(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do + client.distribution(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do super(*args, &block) end end end end - def statsd_count_success(method, name, sample_rate: nil, tags: nil, no_prefix: false) + def statsd_count_success(method, name, sample_rate: nil, tags: nil, + no_prefix: false, client: StatsD.singleton_client) + check_method_and_metric_name(method, name) - # Unfortunately, we have to inline the new method implementation ebcause we have to fix the + # Unfortunately, we have to inline the new method implementation because we have to fix the # Stats.increment call to not use the `prefix` argument. add_to_method(method, name, :count_success) do define_method(method) do |*args, &block| begin @@ -161,20 +176,22 @@ end result ensure suffix = truthiness == false ? 'failure' : 'success' key = "#{StatsD::Instrument.generate_metric_name(nil, name, self, *args)}.#{suffix}" - StatsD.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) + client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) end end end end - def statsd_count_if(method, name, sample_rate: nil, tags: nil, no_prefix: false) + def statsd_count_if(method, name, sample_rate: nil, tags: nil, + no_prefix: false, client: StatsD.singleton_client) + check_method_and_metric_name(method, name) - # Unfortunately, we have to inline the new method implementation ebcause we have to fix the + # Unfortunately, we have to inline the new method implementation because we have to fix the # Stats.increment call to not use the `prefix` argument. add_to_method(method, name, :count_if) do define_method(method) do |*args, &block| begin @@ -192,27 +209,29 @@ end result ensure if truthiness key = StatsD::Instrument.generate_metric_name(nil, name, self, *args) - StatsD.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) + client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) end end end end end - def statsd_count(method, name, sample_rate: nil, tags: nil, no_prefix: false) + def statsd_count(method, name, sample_rate: nil, tags: nil, + no_prefix: false, client: StatsD.singleton_client) + check_method_and_metric_name(method, name) - # Unfortunately, we have to inline the new method implementation ebcause we have to fix the + # Unfortunately, we have to inline the new method implementation because we have to fix the # Stats.increment call to not use the `prefix` argument. add_to_method(method, name, :count) do define_method(method) do |*args, &block| key = StatsD::Instrument.generate_metric_name(nil, name, self, *args) - StatsD.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) + client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) super(*args, &block) end end end @@ -230,6 +249,7 @@ end end end StatsD.singleton_class.prepend(StatsD::Instrument::Strict) +StatsD::Instrument::LegacyClient.prepend(StatsD::Instrument::VoidCollectMetric) StatsD::Instrument.prepend(StatsD::Instrument::StrictMetaprogramming)