https://github.com/basecamp/marginalia
Attach comments to ActiveRecord's SQL queries
https://github.com/basecamp/marginalia
Keywords from Contributors
mvc activerecord activejob rubygem background-jobs crash-reporting rack sidekiq sinatra rspec
Last synced: about 3 hours ago
JSON representation
Repository metadata
Attach comments to ActiveRecord's SQL queries
- Host: GitHub
- URL: https://github.com/basecamp/marginalia
- Owner: basecamp
- License: mit
- Created: 2012-03-11T21:35:11.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2024-08-05T02:12:32.000Z (over 1 year ago)
- Last Synced: 2025-12-13T14:46:54.882Z (11 days ago)
- Language: Ruby
- Homepage:
- Size: 201 KB
- Stars: 1,762
- Watchers: 19
- Forks: 146
- Open Issues: 11
- Releases: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
README.md
marginalia 
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 inMarginalia::Comment.lines_to_ignore
to exclude parts of the stacktrace from inclusion in the line comment.:controller_with_namespaceto include the full classname (including namespace)
of the controller.:jobto include the classname of the ActiveJob being performed.:hostnameto includeSocket.gethostname.:pidto include current process id.:db_hostto include the configured database hostname.:socketto include the configured database socket.:databaseto 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
Owner metadata
- Name: Basecamp
- Login: basecamp
- Email: support@basecamp.com
- Kind: organization
- Description: HEY! It's in Basecamp!
- Website: https://basecamp.com/
- Location: Chicago, IL
- Twitter:
- Company:
- Icon url: https://avatars.githubusercontent.com/u/13131?v=4
- Repositories: 218
- Last ynced at: 2025-10-20T08:47:33.556Z
- Profile URL: https://github.com/basecamp
GitHub Events
Total
- Issues event: 3
- Watch event: 29
- Issue comment event: 3
- Pull request event: 2
- Fork event: 1
Last Year
- Issues event: 1
- Watch event: 17
- Issue comment event: 3
- Pull request event: 2
- Fork event: 1
Committers metadata
Last synced: about 14 hours ago
Total Commits: 206
Total Committers: 50
Avg Commits per committer: 4.12
Development Distribution Score (DDS): 0.748
Commits in past year: 0
Committers in past year: 0
Avg Commits per committer in past year: 0.0
Development Distribution Score (DDS) in past year: 0.0
| Name | Commits | |
|---|---|---|
| Arthur Neves | a****n@g****m | 52 |
| Noah Lorang | n****h@d****m | 38 |
| Nick Quaranto | n****k@q****o | 17 |
| Samuel Cochran | s****6@s****m | 15 |
| Chris Sinjakli | c****s@s****k | 9 |
| Orien Madgwick | _@o****o | 5 |
| Matt Yoho | m****y@m****m | 4 |
| Stanisław Pitucha | s****a@e****m | 4 |
| Dylan Thacker-Smith | D****h@s****m | 3 |
| Jean Boussier | j****r@g****m | 3 |
| Nate Sullivan | n****e@a****u | 3 |
| Stan Hu | s****u@g****m | 3 |
| eileencodes | e****s@g****m | 3 |
| Jason Hiltz-Laforge | j****l@s****m | 2 |
| Laust Rud Jacobsen | l****t@o****o | 2 |
| Rustam Sharshenov | r****m@s****m | 2 |
| Shane O'Grady | s****e@o****e | 2 |
| Ted Nyman | t****d@t****o | 2 |
| Igor | i****w | 2 |
| Douglas Robertson | dr@e****m | 2 |
| Brian Palmer | b****p@i****m | 2 |
| Andrew Kane | a****1@g****m | 2 |
| Jason Katz-Brown | j****n@a****m | 2 |
| risk danger olson | t****e@g****m | 1 |
| Arthur Nogueira Neves | g****b@a****m | 1 |
| Chris Sinjakli | c****s@g****m | 1 |
| Igor Wiedler | i****r@t****g | 1 |
| Keeran Raj Hawoldar | k****n@g****m | 1 |
| rilian | d****s@g****m | 1 |
| fatkodima | f****3@g****m | 1 |
| and 20 more... | ||
Committer domains:
- shopify.com: 2
- datarobots.com: 1
- quaran.to: 1
- sj26.com: 1
- sinjakli.co.uk: 1
- orien.io: 1
- mattyoho.com: 1
- envato.com: 1
- academia.edu: 1
- object.io: 1
- sharshenov.com: 1
- ogrady.ie: 1
- ted.io: 1
- edgeoftheearth.com: 1
- instructure.com: 1
- airbnb.com: 1
- arthurnn.com: 1
- gocardless.com: 1
- travis-ci.org: 1
- github.com: 1
- freshdesk.com: 1
- steveklabnik.com: 1
- unsupervised.com: 1
- mgrachev.com: 1
- klaus-meyer.net: 1
- jroes.net: 1
- joehorsnell.com: 1
- piji.ca: 1
- dio.jp: 1
Issue and Pull Request metadata
Last synced: about 1 month ago
Total issues: 37
Total pull requests: 77
Average time to close issues: over 1 year
Average time to close pull requests: 6 months
Total issue authors: 36
Total pull request authors: 50
Average comments per issue: 1.89
Average comments per pull request: 2.3
Merged pull request: 43
Bot issues: 0
Bot pull requests: 0
Past year issues: 2
Past year pull requests: 2
Past year average time to close issues: N/A
Past year average time to close pull requests: N/A
Past year issue authors: 2
Past year pull request authors: 1
Past year average comments per issue: 0.0
Past year average comments per pull request: 0.0
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0
Top Issue Authors
- keithpitt (2)
- nhorton (1)
- venkateshan-niladri (1)
- ukd1 (1)
- klausmeyer (1)
- tobstarr (1)
- joehorsnell (1)
- dennisvandehoef (1)
- bcherry (1)
- shaneog (1)
- kwent (1)
- anmolarora (1)
- guillaumebriday (1)
- PikachuEXE (1)
- FlintOFF (1)
Top Pull Request Authors
- Sinjo (4)
- sj26 (4)
- geoffharcourt (3)
- gogainda (3)
- ankane (3)
- eileencodes (3)
- orien (2)
- casperisfine (2)
- nate00 (2)
- joshuay03 (2)
- glebm (2)
- ghiculescu (2)
- viraptor (2)
- keeran (2)
- rud (2)
Top Issue Labels
Top Pull Request Labels
Package metadata
- Total packages: 2
-
Total downloads:
- rubygems: 136,193,803 total
- Total docker downloads: 868,463,130
- Total dependent packages: 4 (may contain duplicates)
- Total dependent repositories: 845 (may contain duplicates)
- Total versions: 46
- Total maintainers: 6
- Total advisories: 1
gem.coop: marginalia
Attach comments to your ActiveRecord queries.
- Homepage: https://github.com/basecamp/marginalia
- Documentation: http://www.rubydoc.info/gems/marginalia/
- Licenses: MIT
- Latest release: 1.11.1 (published over 4 years ago)
- Last Synced: 2025-12-23T13:33:02.059Z (1 day ago)
- Versions: 23
- Dependent Packages: 0
- Dependent Repositories: 0
- Downloads: 68,089,682 Total
- Docker Downloads: 434,231,565
-
Rankings:
- Dependent repos count: 0.0%
- Dependent packages count: 0.0%
- Average: 0.182%
- Docker downloads count: 0.318%
- Downloads: 0.41%
- Maintainers (6)
rubygems.org: marginalia
Attach comments to your ActiveRecord queries.
- Homepage: https://github.com/basecamp/marginalia
- Documentation: http://www.rubydoc.info/gems/marginalia/
- Licenses: MIT
- Latest release: 1.11.1 (published over 4 years ago)
- Last Synced: 2025-12-23T23:31:42.912Z (about 18 hours ago)
- Versions: 23
- Dependent Packages: 4
- Dependent Repositories: 845
- Downloads: 68,104,121 Total
- Docker Downloads: 434,231,565
-
Rankings:
- Docker downloads count: 0.379%
- Downloads: 0.434%
- Dependent repos count: 1.138%
- Stargazers count: 1.234%
- Average: 1.428%
- Forks count: 2.102%
- Dependent packages count: 3.283%
- Maintainers (6)
- Advisories:
Dependencies
- rails >= 0
- minitest >= 0 development
- mocha >= 0 development
- mysql2 >= 0 development
- pg >= 0 development
- rake >= 0 development
- sidekiq >= 0 development
- sqlite3 >= 0 development
- actionpack >= 5.2
- activerecord >= 5.2
- actions/checkout v2 composite
- ruby/setup-ruby v1 composite
- mysql * docker
- postgres * docker
Score: 32.1203644390462