lib/statsd/instrument.rb in statsd-instrument-2.3.5 vs lib/statsd/instrument.rb in statsd-instrument-2.4.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'socket' require 'logger' # The StatsD module contains low-level metrics for collecting metrics and sending them to the backend. # @@ -26,10 +28,14 @@ # # @!attribute logger # The logger to use in case of any errors. The logger is also used as default logger # for the LoggerBackend (although this can be overwritten). # +# @!attribute default_tags +# The tags to apply to all metrics. +# @return [Array<String>, Hash<String, String>, nil] The default tags, or <tt>nil</tt> when no default tags is used +# # @see StatsD::Instrument::Backends::LoggerBackend # @return [Logger] # # @see StatsD::Instrument <tt>StatsD::Instrument</tt> contains module to instrument # existing methods with StatsD metrics. @@ -86,11 +92,12 @@ # @param metric_options (see StatsD#measure) # @return [void] def statsd_measure(method, name, *metric_options) add_to_method(method, name, :measure) do define_method(method) do |*args, &block| - StatsD.measure(StatsD::Instrument.generate_metric_name(name, self, *args), *metric_options) { super(*args, &block) } + metric_name = StatsD::Instrument.generate_metric_name(name, self, *args) + StatsD.measure(metric_name, *metric_options) { super(*args, &block) } end end end # Adds execution duration instrumentation to a method as a distribution. @@ -102,11 +109,12 @@ # @return [void] # @note Supported by the datadog implementation only (in beta) def statsd_distribution(method, name, *metric_options) add_to_method(method, name, :distribution) do define_method(method) do |*args, &block| - StatsD.distribution(StatsD::Instrument.generate_metric_name(name, self, *args), *metric_options) { super(*args, &block) } + metric_name = StatsD::Instrument.generate_metric_name(name, self, *args) + StatsD.distribution(metric_name, *metric_options) { super(*args, &block) } end end end # Adds success and failure counter instrumentation to a method. @@ -131,24 +139,31 @@ truthiness = result = super(*args, &block) rescue truthiness = false raise else - truthiness = (yield(result) rescue false) if block_given? + if block_given? + begin + truthiness = yield(result) + rescue + truthiness = false + end + end result ensure suffix = truthiness == false ? 'failure' : 'success' - StatsD.increment("#{StatsD::Instrument.generate_metric_name(name, self, *args)}.#{suffix}", 1, *metric_options) + metric_name = "#{StatsD::Instrument.generate_metric_name(name, self, *args)}.#{suffix}" + StatsD.increment(metric_name, 1, *metric_options) end end end end - # Adds success and failure counter instrumentation to a method. + # Adds success counter instrumentation to a method. # # A method call will be considered successful if it does not raise an exception, and the result is true-y. - # Only for successful calls, the metric will be icnremented + # Only for successful calls, the metric will be incremented. # # @param method (see #statsd_measure) # @param name (see #statsd_measure) # @param metric_options (see #statsd_measure) # @yield (see #statsd_count_success) @@ -163,14 +178,23 @@ truthiness = result = super(*args, &block) rescue truthiness = false raise else - truthiness = (yield(result) rescue false) if block_given? + if block_given? + begin + truthiness = yield(result) + rescue + truthiness = false + end + end result ensure - StatsD.increment(StatsD::Instrument.generate_metric_name(name, self, *args), *metric_options) if truthiness + if truthiness + metric_name = StatsD::Instrument.generate_metric_name(name, self, *args) + StatsD.increment(metric_name, *metric_options) + end end end end end @@ -184,11 +208,12 @@ # @param metric_options (see #statsd_measure) # @return [void] def statsd_count(method, name, *metric_options) add_to_method(method, name, :count) do define_method(method) do |*args, &block| - StatsD.increment(StatsD::Instrument.generate_metric_name(name, self, *args), 1, *metric_options) + metric_name = StatsD::Instrument.generate_metric_name(name, self, *args) + StatsD.increment(metric_name, 1, *metric_options) super(*args, &block) end end end @@ -252,13 +277,18 @@ end def add_to_method(method, name, action, &block) instrumentation_module = statsd_instrumentation_for(method, name, action) - raise ArgumentError, "already instrumented #{method} for #{self.name}" if instrumentation_module.method_defined?(method) - raise ArgumentError, "could not find method #{method} for #{self.name}" unless method_defined?(method) || private_method_defined?(method) + if instrumentation_module.method_defined?(method) + raise ArgumentError, "Already instrumented #{method} for #{self.name}" + end + unless method_defined?(method) || private_method_defined?(method) + raise ArgumentError, "could not find method #{method} for #{self.name}" + end + method_scope = method_visibility(method) instrumentation_module.module_eval(&block) instrumentation_module.send(method_scope, method) prepend(instrumentation_module) unless self < instrumentation_module @@ -267,24 +297,28 @@ def remove_from_method(method, name, action) statsd_instrumentation_for(method, name, action).send(:remove_method, method) end def method_visibility(method) - case - when private_method_defined?(method) + if private_method_defined?(method) :private - when protected_method_defined?(method) + elsif protected_method_defined?(method) :protected else :public end end end attr_accessor :logger, :default_sample_rate, :prefix attr_writer :backend + attr_reader :default_tags + def default_tags=(tags) + @default_tags = StatsD::Instrument::Metric.normalize_tags(tags) + end + def backend @backend ||= StatsD::Instrument::Environment.default_backend end # Emits a duration metric. @@ -380,11 +414,11 @@ # # @example # http_response = StatsD.distribution('HTTP.call.duration') do # HTTP.get(url) # end - def distribution(key, value=nil, *metric_options, &block) + def distribution(key, value = nil, *metric_options, &block) value, metric_options = parse_options(value, metric_options) return collect_metric(:d, key, value, metric_options) unless block_given? start = StatsD::Instrument.current_timestamp @@ -440,19 +474,18 @@ # Converts old-style ordered arguments in an argument hash for backwards compatibility. # @param args [Array] The list of non-required arguments. # @return [Hash] The hash of optional arguments. def hash_argument(args) - return {} if args.length == 0 + return {} if args.empty? 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 + hash end def parse_options(value, metric_options) if value.is_a?(Hash) && metric_options.empty? metric_options = [value]