Giter Site home page Giter Site logo

marginalia's Introduction

marginalia Build Status

Attach comments to your ActiveRecord queries. By default, it adds the application, controller, and action names as a comment at the end of each query.

This helps when searching log files for queries, and seeing where slow queries came from.

For example, once enabled, your logs will look like:

Account Load (0.3ms)  SELECT `accounts`.* FROM `accounts` 
WHERE `accounts`.`queenbee_id` = 1234567890 
LIMIT 1 
/*application:BCX,controller:project_imports,action:show*/

You can also use these query comments along with a tool like pt-query-digest to automate identification of controllers and actions that are hotspots for slow queries.

This gem was created at 37signals. You can read more about how we use it on our blog.

This has been tested and used in production with the mysql2 and pg gems, and is tested on Rails 5.2 through 6.1, and Ruby 2.6 through 3.0. It is also tested for sqlite3. As of Rails 7, Marginalia is a part of Rails itself and does not need to be separately included.

Rails version support will follow supported versions in the Ruby on Rails maintenance policy and Ruby support will follow maintained versions in the Ruby maintenance policy.

Patches are welcome for other database adapters.

Installation

# Gemfile
gem 'marginalia'

Customization

Optionally, you can set the application name shown in the log like so in an initializer (e.g. config/initializers/marginalia.rb):

Marginalia.application_name = "BCX"

The name will default to your Rails application name.

Components

You can also configure the components of the comment that will be appended, by setting Marginalia::Comment.components. By default, this is set to:

Marginalia::Comment.components = [:application, :controller, :action]

Which results in a comment of application:#{application_name},controller:#{controller.name},action:#{action_name}.

You can re-order or remove these components. You can also add additional comment components of your desire by defining new module methods for Marginalia::Comment which return a string. For example:

module Marginalia
  module Comment
    def self.mycommentcomponent
      "TEST"
    end
  end
end

Marginalia::Comment.components = [:application, :mycommentcomponent]

Which will result in a comment like application:#{application_name},mycommentcomponent:TEST The calling controller is available to these methods via @controller.

Marginalia ships with :application, :controller, and :action enabled by default. In addition, implementation is provided for:

  • :line (for file and line number calling query). :line supports a configuration by setting a regexp in Marginalia::Comment.lines_to_ignore to exclude parts of the stacktrace from inclusion in the line comment.
  • :controller_with_namespace to include the full classname (including namespace) of the controller.
  • :job to include the classname of the ActiveJob being performed.
  • :hostname to include Socket.gethostname.
  • :pid to include current process id.
  • :db_host to include the configured database hostname.
  • :socket to include the configured database socket.
  • :database to include the configured database name.

Pull requests for other included comment components are welcome.

Prepend comments

By default marginalia appends the comments at the end of the query. Certain databases, such as MySQL will truncate the query text. This is the case for slow query logs and the results of querying some InnoDB internal tables where the length of the query is more than 1024 bytes.

In order to not lose the marginalia comments from your logs, you can prepend the comments using this option:

Marginalia::Comment.prepend_comment = true

Inline query annotations

In addition to the request or job-level component-based annotations, Marginalia may be used to add inline annotations to specific queries using a block-based API.

For example, the following code:

Marginalia.with_annotation("foo") do
  Account.where(queenbee_id: 1234567890).first
end

will issue this query:

Account Load (0.3ms)  SELECT `accounts`.* FROM `accounts`
WHERE `accounts`.`queenbee_id` = 1234567890
LIMIT 1
/*application:BCX,controller:project_imports,action:show*/ /*foo*/

Nesting with_annotation blocks will concatenate the comment strings.

Caveats

Prepared statements

Be careful when using Marginalia with prepared statements. If you use a component like request_id then every query will be unique and so ActiveRecord will create a new prepared statement for each potentially exhausting system resources. Disable prepared statements if you wish to use components with high cardinality values.

Contributing

Start by bundling and creating the test database:

bundle
rake db:mysql:create
rake db:postgresql:create

Then, running rake will run the tests on all the database adapters (mysql, mysql2, postgresql and sqlite):

rake

marginalia's People

Contributors

ankane avatar arthurnn avatar byroot avatar codekitchen avatar douglasr avatar dylanahsmith avatar eileencodes avatar fatkodima avatar igorwwwwwwwwwwwwwwwwwwww avatar jasonhl avatar jeremy avatar kuahyeow avatar mattyoho avatar nate00 avatar noahhl avatar orien avatar qrush avatar rilian avatar roodion avatar rud avatar shaneog avatar sharshenov avatar sinjo avatar sj26 avatar sribalakumar avatar stanhu avatar steveklabnik avatar technoweenie avatar tnm avatar viraptor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

marginalia's Issues

not able to install 1.6.0 in mac os catalina

gem install --debug marginalia -v '1.6.0'
Exception `LoadError' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/resolv.rb:168 - LoadError
Exception `Errno::EHOSTUNREACH' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/resolv.rb:791 - No route to host - sendto(2) for "fe80::1" port 53
Exception `Resolv::ResolvTimeout' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/resolv.rb:675 - Resolv::ResolvTimeout
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `Errno::EHOSTUNREACH' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/resolv.rb:791 - No route to host - sendto(2) for "fe80::1" port 53
Exception `Resolv::ResolvTimeout' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/resolv.rb:675 - Resolv::ResolvTimeout
Exception `Resolv::DNS::Config::NXDomain' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/resolv.rb:546 - _rubygems._tcp.api.rubygems.org
Exception `Errno::EHOSTUNREACH' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/resolv.rb:791 - No route to host - sendto(2) for "fe80::1" port 53
Exception `Resolv::ResolvTimeout' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/resolv.rb:675 - Resolv::ResolvTimeout
Exception `Resolv::DNS::Config::NXDomain' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/resolv.rb:546 - _rubygems._tcp.api.rubygems.org.home
Exception `Resolv::ResolvError' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/resolv.rb:491 - DNS result has no information for _rubygems._tcp.api.rubygems.org
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `OpenSSL::SSL::SSLErrorWaitReadable' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/openssl/buffering.rb:182 - read would block
Exception `Errno::EEXIST' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/fileutils.rb:252 - File exists @ dir_s_mkdir - /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0
Exception `Errno::ENOENT' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/fileutils.rb:1450 - No such file or directory @ unlink_internal - /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/specifications/marginalia-1.6.0.gemspec
Exception `Errno::EEXIST' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/fileutils.rb:252 - File exists @ dir_s_mkdir - /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/marginalia-1.6.0
Exception `Errno::ENOENT' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/fileutils.rb:1330 - No such file or directory @ rb_file_s_lstat - /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/marginalia-1.6.0/.gitignore
Exception `Errno::ENOENT' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/fileutils.rb:1330 - No such file or directory @ rb_file_s_lstat - /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/marginalia-1.6.0/.gitignore
Exception `Errno::ENOENT' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/fileutils.rb:1450 - No such file or directory @ unlink_internal - /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/marginalia-1.6.0/.gitignore
Exception `Errno::EEXIST' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/fileutils.rb:252 - File exists @ dir_s_mkdir - /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/marginalia-1.6.0
Exception `NoMethodError' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/rubygems/package/tar_reader.rb:71 - undefined method `seek' for #<Zlib::GzipReader:0x007fd32bc666b8
Exception `Gem::InstallError' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/rubygems/ext/builder.rb:89 - rake failed, exit code 1
Exception `Errno::EEXIST' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/fileutils.rb:252 - File exists @ dir_s_mkdir - /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-19/2.2.0-static/marginalia-1.6.0
Exception `Gem::Ext::BuildError' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/rubygems/ext/builder.rb:143 - ERROR: Failed to build gem native extension.

    /Users/aavudaiappan/.rbenv/versions/2.2.5/bin/ruby mkrf_conf.rb
Building native extensions.  This could take a while...

rake failed, exit code 1

Gem files will remain installed in /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/marginalia-1.6.0 for inspection.
Results logged to /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-19/2.2.0-static/marginalia-1.6.0/gem_make.out

Exception `Gem::Ext::BuildError' at /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/2.2.0/rubygems/request_set.rb:171 - ERROR: Failed to build gem native extension.

    /Users/aavudaiappan/.rbenv/versions/2.2.5/bin/ruby mkrf_conf.rb
Building native extensions.  This could take a while...

rake failed, exit code 1

Gem files will remain installed in /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/marginalia-1.6.0 for inspection.
Results logged to /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-19/2.2.0-static/marginalia-1.6.0/gem_make.out

ERROR:  Error installing marginalia:
	ERROR: Failed to build gem native extension.

    /Users/aavudaiappan/.rbenv/versions/2.2.5/bin/ruby mkrf_conf.rb
Building native extensions.  This could take a while...

rake failed, exit code 1

Gem files will remain installed in /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/marginalia-1.6.0 for inspection.
Results logged to /Users/aavudaiappan/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-19/2.2.0-static/marginalia-1.6.0/gem_make.out

Ruby warning on marginalia 1.11.0

Hi, I'm see a warning after upgrading to marginalia 1.11.0. We're on Ruby 2.7.4.

vendor/bundle/ruby/2.7.0/gems/marginalia-1.11.0/lib/marginalia.rb:79: warning: Skipping set of ruby2_keywords flag for exec_query_with_marginalia (method accepts keywords or method does not accept argument splat)

cc @byroot

Initializes db connection during `rake assets:precompile` in rails 4

It looks like this gem will cause an initialization of the database connection during it's require phase, which makes rake assets:precompile fail when run on a host without a database configuration. Rails 4 removed the intialize_on_precompile configuration, so it's important that this gem not trigger connection during initialization. For now, we can work around with environment variables but it would be good to get this fixed!

Strategy for using outside Rails controllers like grape?

I love using this gem with pghero, but i'm wondering the best way to make use of customizing my my own component to use outside of a rails controller, in my case it's grape.

I know i can add my own component, and in my case, grape offers env["api.endpoint"] https://github.com/ruby-grape/grape#current-route-and-endpoint but i'm not sure what the best way to pass this information in from what i believe requires a grape middlewear from Grape::Middleware::Base

You can go ahead and close this, just seeking advice, improving SEO for others. Or maybe there is a simpler solution as this has most likely aged.

please update rack dependency

Hi,

We are still stuck on version 0.2.4.

Today I tried to update the gem and can't update because of the rack (~> 1.5) dependency that is new (compared to v 0.2 ๐Ÿ˜‰ )

We use other gems that have a rack (~> 2.0) dependency.
Is there any option you can upgrade the dependency here?

Kind regards,
Dennis

How do I ignore certain queries?

I have a lot of logs where I see comments for statements like BEGIN and COMMIT which I want to disable for performance reasons. e.g

BEGIN /*hostname:MY_APPLICATION_NAME,context:CUSTOM CONTEXT*/

Is there a way to disable marginalia comments for certain queries?

Rails 5 / Ruby 2.5 bug

I receive the calling line of usr/local/lib/ruby/2.5.0/monitor.rb:226:in 'mon_synchronize' using Rails 5 and Ruby 2.5, which is not useful.

I've added a PRto add Ruby 2.5 support to the test suite, to see if there are breaking tests.

Versions:
Rails: 5.1.6
Ruby: 2.5.1
Marginalia: 1.5.0

OT: is this project maintained any more?

Sidekiq documents are now out-of-date

The gem now includes the Sidekiq middleware, so the example in the wiki might lead someone to duplicate the code in their app, potentially overriding any future upstream improvements or fixes.

I tried forking the wiki, but it seems like it's not editable by non-committers.

Project status

Hi,

Just opening an issue to request the status of this project - is it still active and maintained?

The last RubyGems version, 1.8.0, was released in March 2019 and there are a fair number of PRs open, some for nearly 18 months. Also the CI builds are currently broken and have been for some time, although #105 seems to fix that.

Many thanks.

Rails 7?

The Readme only cites rails 6.1, and the updates all predate 7. Is there an intent to make this work with Rails 7? Given this is Basecamp, I assume you are still using this internally, right?

Trouble fetching current_admin

I'm able to get controller.current admin from the view.
however when i did this in comment.rb
def self.current_admin
@controller.current_admin.to_s if @controller.respond_to? :current_admin
end

SystemStackError in ApplicationController#sitemap

stack level too deep

Application Trace | Framework Trace | Full Trace

actionpack (3.2.15) lib/action_dispatch/middleware/reloader.rb:70

Request

Parameters:

None

Show session dump

Show env dump
Response

Headers:

None

prepend_comment is breaking ActiveRecord's explain

When Marginalia::Comment.prepend_comment is set to true in a rails app, ActiveRecord explain stops showing the explain results in the rails console

Screenshot 2020-01-01 at 15 17 38

Environment

Database: PostgreSQL 10.10
Rails: 5.2.3
Ruby: 2.6.5
Marginalia: 1.8.0

mysql gem built even when not necessary

If I include marginalia in a Gemfile, even if I don't use/need mysql, it looks like the gem will always be built on ruby < 2.4.
It seems like there's no differentiation between installing marginalia as a dependency and for development in case of that extension.

This is causing an issue, because I can't successfully build the mysql gem on the current system.

`with_annotation` doesn't work on relations

While starting to use with_annotation in some of our queries, I am finding that it doesn't work on where methods (and I suspect other AR relation methods).

Rails version: 5.2
Gem version: 1.8.0

# works
Marginalia.with_annotation("foo") { User.first }
#=> SELECT * FROM `users` LIMIT 1 /*foo*/

# works
Marginalia.with_annotation("foo") { User.find_by_id(1) }
#=> SELECT * FROM `users` WHERE `id` = 1 LIMIT 1 /*foo*/

# doesn't
Marginalia.with_annotation("foo") { User.where(id: 1) }
#=> SELECT * FROM `users` WHERE `id` = 1 LIMIT 11

Is this a known thing or any hints as to how to address it?

Marginalia comments prevents Rails 6.0 from getting index names with SQLite3

Migrating from Rails 5.2 to Rails 6.0.4.1, and I got this error when loading schema in sqlite:

#<NoMethodError: undefined method `size' for nil:NilClass>

I understood where the issue comes from and I need help about how it can be fixed. Here are the reproducing steps and the explanation for this error.

Reproducing steps

Create a rails 6.0 app with marginalia and two migrations: one adding an expression index, the second adding another index on the same table.

rails new index_names_bug
cd index_names_bug
bundle add marginalia
rails generate model Product name:string description:text
rails generate migration AddExpressionIndexToProducts

set the content of this migration to

class AddExpressionIndexToProducts < ActiveRecord::Migration[6.0]
  def change
    add_index :products, 'lower(name)'
  end
end

then add another migration

rails generate migration AddClassicIndexToProducts

set the content to

class AddClassicIndexToProducts < ActiveRecord::Migration[6.0]
  def change
    add_index :products, :description
  end
end

run

rails db:migrate

to see the following error

== 20210824125823 AddExpressionIndexToProducts: migrating =====================
-- add_index(:products, "lower(name)")

   -> 16.6489s
== 20210824125823 AddExpressionIndexToProducts: migrated (16.6491s) ===========

== 20210824125906 AddClassicIndexToProducts: migrating ========================
-- add_index(:products, :description)

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

undefined method `size' for nil:NilClass
/app/db/migrate/20210824125906_add_classic_index_to_products.rb:3:in `change'

Caused by:
NoMethodError: undefined method `size' for nil:NilClass
/app/db/migrate/20210824125906_add_classic_index_to_products.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

It fails at the last migration

Why does it happen?

When adding an index, activerecord checks if the index name exists and get the list of indexes defined on the table. Fetching the list of indexes is done in lib/active_record/connection_adapters/sqlite3/schema_statements.rb.

The query PRAGMA index_list(table_name) return one row per index, and each row is used to get the column name with a PRAGMA index_info(index_name). As the index is an expression index, the column name is nil.

This is handled here:

https://github.com/rails/rails/blob/0d304eae601f085274b2e2c04316e025b443da62/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb#L24

https://github.com/rails/rails/blob/0d304eae601f085274b2e2c04316e025b443da62/activerecord/lib/active_record/connection_adapters/sqlite3/schema_statements.rb#L32-L34

A regular expression is used to get the expression used to create the index from the SQL. The regexp is /\bON\b\s*"?(\w+?)"?\s*\((?<expressions>.+?)\)(?:\s*WHERE\b\s*(?<where>.+))?\z/i. This regexp does not match the trailing comment appended by marginalia, so expressions is set to nil, so columns is set to nil too, and some code asks for columns.size and produces the error NoMethodError: undefined method 'size' for nil:NilClass.

How to fix?

That's where I need help. Options I've seen so far:

  • use a regexp which ignores trailing comments.
    using # /\bON\b\s*"?(\w+?)"?\s*\((?<expressions>.+?)\)(?:\s*WHERE\b\s*(?<where>.+))?(?:\s\/\*.*\*\/)?\z/i =~ index_sql will work. I can issue a pull request against rails but it would not be fixed in the 6.0 branch.
  • monkey patch ActiveRecord::ConnectionAdapters::SQLite3::SchemaStatements#indexes with the good regexp.
    I would prefer avoiding that.
  • disable marginalia for some SQL commands.
    Is it possible?

What do you recommend here?

Allow to report the database reference

From the guides https://guides.rubyonrails.org/active_record_multiple_databases.html on multiple databases, we have the following database.yml file:

production:
  primary:
    database: my_primary_database
    user: root
    adapter: mysql
  primary_replica:
    database: my_primary_database
    user: root_readonly
    adapter: mysql
    replica: true
  animals:
    database: my_animals_database
    user: animals_root
    adapter: mysql
    migrations_paths: db/animals_migrate
  animals_replica:
    database: my_animals_database
    user: animals_readonly
    adapter: mysql
    replica: true

I would like to signal which database the query is running. Given the replicas have the same database name, the database comment on marginalia won't work.
Is it possible to add the animals and animals_replica names to the comments?

line number missing

Hi,

So I have Marginalia::Comment.components = [:line, :application, :controller, :action] in my initializer but the line number is always blank... /*line:,application:Redline,controller:claims,action:index*/ any ideas on what I'm missing?

Many thanks

Mongoid Support ?

I wonder if there's any gem with something like this or support for Mongoid in the future, thanks.

Error installing marginalia.ERROR: Failed to build gem native extension

I am facing issues while installing marginalia-1.6.0.
I am using ruby version 2.2.5 and rails 3.2.22.5.
when I run bundle install, the process gets failed at marginalia installation step.

$ gem install marginalia -v '1.6.0'
Building native extensions.  This could take a while...
ERROR:  Error installing marginalia:
	ERROR: Failed to build gem native extension.

    /Users/veniladri/.rbenv/versions/2.2.5/bin/ruby mkrf_conf.rb

rake failed, exit code 1

Gem files will remain installed in /Users/veniladri/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/gems/marginalia-1.6.0 for inspection.
Results logged to /Users/veniladri/.rbenv/versions/2.2.5/lib/ruby/gems/2.2.0/extensions/x86_64-darwin-19/2.2.0-static/marginalia-1.6.0/gem_make.out

Please suggest what can be going wrong?

Thanks,
Venky

Marginalia with request_id and job_id bloats PostgreSQLAdapter::StatementPool

Hey! I just discovered that if you include the http request_id or the ActiveJob job id inside the marginalia annotation, it will bloat the the PostgreSQLAdapter::StatementPool cache.

Each unique SQL query will create a new prepared_statement entry, and because of the unique annotation, it won't get re-used, so it will just sit around in memory doing nothing. Here is the line inside Rails that generates the prepared statement:

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L644

Here is a screenshot of me listing all the entries in the statement pool, and you'll be able to see a query that could have been re-used, but wasn't because of the unique annotation.

image

So each unique combination of the annotation will result in a new prepared statement entry. I'm not sure how to fix this, or even if there is a possible fix? Maybe use of marginalia means that you can't use the prepared statement cache?

Namespace of controller is not included

Hi,

we're using your gem since a few months in all our apps and are quite happy with it. It really makes the debugging for slow queries much more comfortable.

There is one topic that we'd like to improve and I'd like to hear your opinion about it. Currently the controller name included in the comment is missing the namespace which is sometimes very confusing.

E.g. since we're having an AccountsController and an API::V1::AccountsController in our application for both controllers the comment would look like the following one:

SELECT * FROM accounts ... /* application:appname, controller: accounts, action: show */

The reason for this is the usage of the ActionController::Base#controller_name method.
What about changing it to using the class name? Or should we better add a second method like self.controller_with_namespace to the Marginalia::Comment module? Should I provide a pull request and would you probably merge it?

Cheers, Klaus

Rails 6.0 Connection Adapter Problem

 undefined method `spec' for #<ActiveRecord::ConnectionAdapters::NullPool:0x00007ffbee3e7eb0>
 from  marginalia/lib/marginalia/comment.rb:166:in `connection_config'

I'm unable to start the application on Rails 6 with Marginalia.

I'm using:

Anyone else having this problem?

Prefix query with comment instead of suffix?

Hey there, this is pretty awesome - would you be open to flipping the order of the underlying SQL query and the Marginalia comment?

i.e. SELECT * FROM foo /*line:foo.rb*/ becomes /*line:foo.rb*/ SELECT * FROM foo? We have some queries that are pretty long and it's a little easier to see the metadata upfront

Can either switch it to the default or make it some kind of configuration option

Rails 4 support

is this somehow possible with Rails 4 now (maybe merged in) or why is there still a dependency to activerecord < 3.3?

Thanx!

only ever get "block in execute"

The only thing I am getting in my slow query log is this:

line:(eval):31:in `block in execute'

and often I get no commenting at all.

My marginalia.rb looks like this:

Marginalia.application_name = "OurApp"

Marginalia::Comment.components = [:application, :controller, :line, :job]

The comment is the same, regardless if it is coming from a controller action, a delayed job, or the console! Even the line number is always the same.

Maybe I am misunderstanding something?

[edit: using Rails 4.2.9 with ruby 2.1.10]

Warnings with 2.7

/Users/russ/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/bundler/gems/marginalia-48a04a14b413/lib/marginalia.rb:94: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call

Odd behaviour if code contains syntax errors or produces a stack level too low error

Marginalia is obfuscating the root cause of syntax errors and recursive code etc. An example:

class Test
  incude SomeModule
end

When executing the above code, instead of getting a undefined method incude I will get a recursive death spiral outputting:

./vendor/bundle/ruby/2.4.0/gems/marginalia-1.5.0/lib/marginalia.rb:58:in 'execute_with_marginalia'

This is really nasty, as e.g. your test suite will will produce an unusable output if a developer have made a syntax error or similar, instead of failing the specs with a useful one.

NoMethodError: undefined method `helper' for ActionController::API:Class

When I try to install the gem as is, I get the error in the issue title. Is anyone else getting the same error?

I've looked into this error and it sounds like instead of using ActiveSupport.on_load :action_controller do on lib/marginalia/railtie.rb:13 it should be using ActiveSupport.on_load :action_controller_base do. Can someone confirm this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.