Skip to content

Instantly share code, notes, and snippets.

@LimeBlast
Last active April 26, 2021 14:35
Show Gist options
  • Select an option

  • Save LimeBlast/ae36fb67c0892a1bb120e5ee1bbdd83e to your computer and use it in GitHub Desktop.

Select an option

Save LimeBlast/ae36fb67c0892a1bb120e5ee1bbdd83e to your computer and use it in GitHub Desktop.

Revisions

  1. LimeBlast revised this gist Apr 26, 2021. 1 changed file with 45 additions and 0 deletions.
    45 changes: 45 additions & 0 deletions marginalia.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    # frozen_string_literal: true

    # Based on https://gist.github.com/lfittl/4d2d53b9a4d8b095acd9a175da1106c6
    # with references to sidekiq and sentry removed (as we don't use them).

    module Marginalia
    module Comment
    # Having unique values like the request ID in BEGIN/COMMIT/ROLLBACK will bloat
    # pg_stat_statements with unnecessary data, per the pg_stat_statements.c source:
    # "For utility statements, we just hash the query string to get an ID."
    # (which includes the comment string)
    #
    # Unfortunately we don't have easy access to the SQL statement text in this
    # part of Marginalia, so we improvise with known call stacks that produce
    # utility statements.
    UTILITY_STATEMENT_CALLSTACK_FILTERS = [
    %r{/active_record/connection_adapters/postgresql/database_statements.rb:\d+:in `begin_db_transaction}, # BEGIN
    %r{/active_record/connection_adapters/postgresql/database_statements.rb:\d+:in `commit_db_transaction}, # COMMIT
    %r{/active_record/connection_adapters/postgresql/database_statements.rb:\d+:in `exec_rollback_db_transaction} # ROLLBACK
    ].freeze
    def self.utility_statement?
    callstack = caller
    UTILITY_STATEMENT_CALLSTACK_FILTERS.any? do |filter|
    callstack.any? { |s| s[filter] }
    end
    end

    def self.request_id
    return unless marginalia_controller.respond_to?(:request) && marginalia_controller.request.respond_to?(:uuid)
    return if utility_statement?

    marginalia_controller.request.uuid
    end

    def self.release
    return unless ENV.key?('HEROKU_SLUG_COMMIT')
    return if utility_statement?

    ENV.fetch('HEROKU_SLUG_COMMIT')
    end
    end
    end

    Marginalia::Comment.lines_to_ignore = %r{\.rvm|gem|vendor/|marginalia|sentry|rbenv|/usr/local/lib/ruby}
    Marginalia::Comment.components = %i[application controller action line job request_id release trace_id]
  2. LimeBlast created this gist Apr 26, 2021.
    46 changes: 46 additions & 0 deletions error
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    NoMethodError: undefined method `trace_id' for Marginalia::Comment:Module
    /app/vendor/bundle/ruby/2.5.0/gems/marginalia-1.10.1/lib/marginalia/comment.rb:25:in `block in construct_comment'
    /app/vendor/bundle/ruby/2.5.0/gems/marginalia-1.10.1/lib/marginalia/comment.rb:24:in `each'
    /app/vendor/bundle/ruby/2.5.0/gems/marginalia-1.10.1/lib/marginalia/comment.rb:24:in `construct_comment'
    /app/vendor/bundle/ruby/2.5.0/gems/marginalia-1.10.1/lib/marginalia.rb:50:in `annotate_sql'
    /app/vendor/bundle/ruby/2.5.0/gems/marginalia-1.10.1/lib/marginalia.rb:71:in `execute_with_marginalia'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/postgresql/schema_statements.rb:286:in `client_min_messages='
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/postgresql_adapter.rb:673:in `configure_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/postgresql_adapter.rb:658:in `connect'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/connection_handling.rb:87:in `connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/migration.rb:648:in `connection'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/migration.rb:664:in `block in method_missing'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/migration.rb:634:in `block in say_with_time'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/migration.rb:634:in `say_with_time'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/migration.rb:654:in `method_missing'
    /app/db/schema.rb:17:in `block in <top (required)>'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/schema.rb:41:in `instance_eval'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/schema.rb:41:in `define'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/schema.rb:61:in `define'
    /app/db/schema.rb:14:in `<top (required)>'
    /app/vendor/bundle/ruby/2.5.0/gems/activesupport-4.2.11.19/lib/active_support/dependencies.rb:268:in `load'
    /app/vendor/bundle/ruby/2.5.0/gems/activesupport-4.2.11.19/lib/active_support/dependencies.rb:268:in `block in load'
    /app/vendor/bundle/ruby/2.5.0/gems/activesupport-4.2.11.19/lib/active_support/dependencies.rb:240:in `load_dependency'
    /app/vendor/bundle/ruby/2.5.0/gems/activesupport-4.2.11.19/lib/active_support/dependencies.rb:268:in `load'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/tasks/database_tasks.rb:221:in `load_schema_for'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/tasks/database_tasks.rb:238:in `block in load_schema_current'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/tasks/database_tasks.rb:278:in `block in each_current_configuration'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/tasks/database_tasks.rb:277:in `each'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/tasks/database_tasks.rb:277:in `each_current_configuration'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/tasks/database_tasks.rb:237:in `load_schema_current'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/railties/databases.rake:237:in `block (3 levels) in <top (required)>'
    /app/vendor/bundle/ruby/2.5.0/gems/activerecord-4.2.11.19/lib/active_record/railties/databases.rake:241:in `block (3 levels) in <top (required)>'
    Tasks: TOP => db:schema:load
    (See full trace by running task with --trace)