Skip to content

Rails reload fails when instrumenting class methods in config/initializers #301

@dmitri-minkin

Description

@dmitri-minkin

Environment

Ruby interpreter version: ruby 2.7.5
statsd-instrument version: 3.1.2
StatsD backend: Datadog
Rails: 7.0.2.3
Machine: Mac M1
OS: Mac OS Monterey 12.3.1

Description

Anything that triggers reloading will throw error from statsd-instrument when instrumenting class methods from config/initializer.

How to reproduce

Given this code in Rails:

# app/services/pin/manager.rb

module Pin
  module Manager
        class << self
           def consume_and_grant
           end
        end
  end
end
# config/initializers/statsd.rb

Rails.application.reloader.to_prepare do
  Pin::Manager.singleton_class.extend(StatsD::Instrument)
  Pin::Manager.singleton_class.statsd_count_success(:consume_and_grant, "Pin.Manager.consume_and_grant")
end

Reload fails in console:

  1. Run in terminal: bin/rails console
  2. Reload in rails console: reload!

Observe this error:

Reloading...
ArgumentError: Already instrumented consume_and_grant for
from /Users/<user>/.gem/ruby/2.7.5/gems/statsd-instrument-3.1.2/lib/statsd/instrument.rb:263:in `add_to_method'

Possible cause

StatsD::Instrument having a cache that is likely not being reset on reload

Workaround

You can instrument inside the file that has the module being instrumented:

# app/services/pin/manager.rb

module Pin
  module Manager
        class << self
            def consume_and_grant
            end
        end
  end
end

Pin::Manager.singleton_class.extend(StatsD::Instrument)
Pin::Manager.singleton_class.statsd_count_success(:consume_and_grant, "Pin.Manager.consume_and_grant")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions