Sha256: ae5585c44f59d8f4744092038618f343c09529133fd3ba5382ec360bb9cd94c7

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

# frozen-string-literal: true

module RuboCop
  module Cop
    module StatsD
      # This Rubocop will check for using the return value of StatsD metric calls, which is deprecated.
      # To check your codebase, use the following Rubocop invocation:
      #
      #     rubocop --require `bundle show statsd-instrument`/lib/statsd/instrument/rubocop/metric_return_value.rb \
      #       --only StatsD/MetricReturnValue
      #
      # This cop cannot autocorrect offenses. In production code, StatsD should be used in a fire-and-forget
      # fashion. This means that you shouldn't rely on the return value. If you really need to access the
      # emitted metrics, you can look into `capture_statsd_calls`
      class MetricReturnValue < Cop
        MSG = 'Do not use the return value of StatsD metric methods'

        STATSD_METRIC_METHODS = %i{increment gauge measure set histogram distribution key_value}
        INVALID_PARENTS = %i{lvasgn array pair send return yield}

        def on_send(node)
          if node.receiver&.type == :const && node.receiver&.const_name == "StatsD"
            if STATSD_METRIC_METHODS.include?(node.method_name) && node.arguments.last&.type != :block_pass
              add_offense(node.parent) if INVALID_PARENTS.include?(node.parent&.type)
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
statsd-instrument-2.5.1 lib/statsd/instrument/rubocop/metric_return_value.rb
statsd-instrument-2.5.0 lib/statsd/instrument/rubocop/metric_return_value.rb