Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Track ActiveRecord connection pool stats #716

Open
rainerborene opened this issue Apr 6, 2021 · 3 comments
Open

Suggestion: Track ActiveRecord connection pool stats #716

rainerborene opened this issue Apr 6, 2021 · 3 comments
Labels

Comments

@rainerborene
Copy link

rainerborene commented Apr 6, 2021

A suggestion for a new probe on this library by using this piece of code: ActiveRecord::Base.connection_pool.stat

@krystof-k

This comment was marked as outdated.

@tombruijn
Copy link
Member

The data not being in the previous comment was because of preload_app! loading AppSignal in the Puma primary process. There the probes didn't have access to the connection pool data. We added docs for how to configure AppSignal when using this Puma config: https://docs.appsignal.com/ruby/integrations/puma.html#usage-with-preload_app

@krystof-k
Copy link

krystof-k commented Aug 12, 2024

One more update from me. Based on @tombruijn findings, here is my final setup for the ActiveRecord connection pool probe, working both in Puma workers and everywhere else:

intiializers/appsignal.rb rests the same:

Appsignal::Probes.register(:active_record_connection_pool, lambda do
  ActiveRecord::Base.connection_pool.stat.each do |key, value|
    hostname = Appsignal.config[:hostname] || Socket.gethostname
    Appsignal.set_gauge("active_record_connection_pool_#{key}", value, {hostname:})
  end
end)

config/puma.rb is a little cleaner, however it is still required to re-register the probe to include the Puma worker number:

preload_app!

before_fork do
  # Stop AppSignal probes in the Puma main process
  Appsignal::Probes.stop
  # Deregister AppSignal probe from the Puma main process
  Appsignal::Probes.unregister(:active_record_connection_pool)
end

on_worker_boot do |worker_index|
  # Register the AppSignal probe in the Puma worker process
  Appsignal::Probes.register(:active_record_connection_pool, lambda do
    ActiveRecord::Base.connection_pool.stat.each do |key, value|
      hostname = Appsignal.config[:hostname] || Socket.gethostname
      Appsignal.set_gauge("active_record_connection_pool_#{key}", value, {hostname:, process: worker_index})
    end
  end)
  # Start AppSignal probes in the Puma worker process
  Appsignal::Probes.start
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants