getsentry / sentry-ruby

Sentry SDK for Ruby
https://sentry.io/for/ruby
MIT License
930 stars 493 forks source link

NoMethodError after update sentry to version 5.11 for db configuration #2109

Closed Piioo closed 1 year ago

Piioo commented 1 year ago

Issue Description

Hello,

after update sentry-ruby to version 5.11 we got this errors every time when an error happen on our platform.

NoMethodError: undefined method `[]' for nil:NilClass (NoMethodError)

              span.set_data(Span::DataConventions::DB_SYSTEM, db_config[:adapter]) if db_config[:adapter]
                                                                                               ^^^^^^^^^^
  from sentry-rails (5.11.0) lib/sentry/rails/tracing/active_record_subscriber.rb:36:in `block (2 levels) in subscribe!'
  from sentry-rails (5.11.0) lib/sentry/rails/tracing/abstract_subscriber.rb:47:in `block in record_on_current_span'
  from sentry-ruby (5.11.0) lib/sentry/hub.rb:109:in `block in with_child_span'
  from sentry-ruby (5.11.0) lib/sentry/span.rb:206:in `with_child_span'
  from sentry-ruby (5.11.0) lib/sentry/hub.rb:107:in `with_child_span'
  from sentry-ruby (5.11.0) lib/sentry-ruby.rb:456:in `with_child_span'
  from sentry-rails (5.11.0) lib/sentry/rails/tracing/abstract_subscriber.rb:43:in `record_on_current_span'
  from sentry-rails (5.11.0) lib/sentry/rails/tracing/active_record_subscriber.rb:15:in `block in subscribe!'
  from sentry-rails (5.11.0) lib/sentry/rails/tracing/abstract_subscriber.rb:34:in `block (2 levels) in subscribe_to_event'
  from activesupport (7.0.8) lib/active_support/notifications/fanout.rb:270:in `finish'
  from activesupport (7.0.8) lib/active_support/notifications/fanout.rb:76:in `block in finish'
  from activesupport (7.0.8) lib/active_support/notifications/fanout.rb:91:in `block in iterate_guarding_exceptions'
  from activesupport (7.0.8) lib/active_support/notifications/fanout.rb:90:in `each'
  from activesupport (7.0.8) lib/active_support/notifications/fanout.rb:90:in `iterate_guarding_exceptions'
  from activesupport (7.0.8) lib/active_support/notifications/fanout.rb:76:in `finish'
  from activesupport (7.0.8) lib/active_support/notifications/instrumenter.rb:49:in `finish_with_state'
  from activesupport (7.0.8) lib/active_support/notifications/instrumenter.rb:30:in `instrument'
  from sentry-rails (5.11.0) lib/sentry/rails/tracing.rb:54:in `instrument'
  from activerecord (7.0.8) lib/active_record/connection_adapters/abstract_adapter.rb:743:in `log'
  from activerecord (7.0.8) lib/active_record/connection_adapters/postgresql/database_statements.rb:17:in `query'
  from activerecord (7.0.8) lib/active_record/connection_adapters/abstract/database_statements.rb:95:in `query_value'
  from activerecord-cockroachdb-adapter (7.0.3) lib/active_record/connection_adapters/cockroachdb_adapter.rb:248:in `initialize'
  from activerecord-cockroachdb-adapter (7.0.3) lib/active_record/connection_adapters/cockroachdb_adapter.rb:46:in `new'
  from activerecord-cockroachdb-adapter (7.0.3) lib/active_record/connection_adapters/cockroachdb_adapter.rb:46:in `cockroachdb_connection'
  from activerecord (7.0.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:656:in `public_send'
  from activerecord (7.0.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:656:in `new_connection'
  from activerecord (7.0.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:700:in `checkout_new_connection'
  from activerecord (7.0.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:679:in `try_to_checkout_new_connection'
  from activerecord (7.0.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:640:in `acquire_connection'
  from activerecord (7.0.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:341:in `checkout'
  from activerecord (7.0.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:181:in `connection'
  from activerecord (7.0.8) lib/active_record/connection_adapters/abstract/connection_handler.rb:211:in `retrieve_connection'
  from activerecord (7.0.8) lib/active_record/connection_handling.rb:313:in `retrieve_connection'
  from activerecord (7.0.8) lib/active_record/connection_handling.rb:280:in `connection'
  from activerecord (7.0.8) lib/active_record/model_schema.rb:580:in `load_schema!'
  from activerecord (7.0.8) lib/active_record/attributes.rb:264:in `load_schema!'
  from activerecord (7.0.8) lib/active_record/encryption/encryptable_record.rb:122:in `load_schema!'
  from activerecord (7.0.8) lib/active_record/model_schema.rb:566:in `block in load_schema'
  from activerecord (7.0.8) lib/active_record/model_schema.rb:563:in `synchronize'
  from activerecord (7.0.8) lib/active_record/model_schema.rb:563:in `load_schema'
  from activerecord (7.0.8) lib/active_record/model_schema.rb:419:in `columns_hash'
  from activerecord (7.0.8) lib/active_record/inheritance.rb:87:in `descends_from_active_record?'
  from activerecord (7.0.8) lib/active_record/inheritance.rb:93:in `finder_needs_type_condition?'
  from activerecord (7.0.8) lib/active_record/core.rb:441:in `relation'
  from activerecord (7.0.8) lib/active_record/scoping/named.rb:45:in `default_scoped'
  from activerecord (7.0.8) lib/active_record/scoping/named.rb:32:in `all'
  from activerecord (7.0.8) lib/active_record/scoping/named.rb:175:in `block in scope'

How do we may avoid this?

Reproduction Steps

I do not know how to reproduce.

Expected Behavior

The original error is shown in sentry ui

Actual Behavior

Only a sentry error is shown in the sentry ui

Ruby Version

3.2.2

SDK Version

5.11

Integration and Its Version

Rails 7.0.8

Sentry Config

  Sentry.init do |config|
    config.dsn = Rails.configuration.x.foo.sentry.dsn
    config.environment = Rails.configuration.x.foo.sentry.environment

    # testing, I do not know what Sentry is shown with this
    config.breadcrumbs_logger = [:active_support_logger]

    config.traces_sample_rate = 1.0
    config.traces_sampler = lambda do |_sampling_context|
      # sampling_context[:transaction_context] contains the information about the transaction
      # sampling_context[:parent_sampled] contains the transaction's parent's sample decision
      # true # return value can be a boolean or a float between 0.0 and 1.0
      !!Rails.configuration.x.foo.test_api_enabled
    end

    # Im not sure if this is needed with config.send_default_pii = false as default
    filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters)
    config.before_send = lambda do |event, _hint|
      filter.filter(event.to_hash)
    end

    config.before_send_transaction = lambda do |event, _hint|
      filter.filter(event.to_hash)
    end
  end
sl0thentr0py commented 1 year ago

@Piioo thanks for the report, I will add a guard.

But also can you give me your config/database.yml without the sensitive information? Curious why this happens and if I can enable your use case.

Piioo commented 1 year ago

We use the cockroachdb

Gem: https://github.com/cockroachdb/activerecord-cockroachdb-adapter

database.yml (this is changed prod config)

development:
  adapter: cockroachdb
  host: foo.bar.local
  database: dev_db
  user: root
  port: 26257
  pool: 25
  disable_cockroachdb_telemetry: true

docker-compose.yml

version: "2"
services:
  db:
    image: cockroachdb/cockroach:v22.2.10
    command: start-single-node --insecure
    ports:
      - "26257:26257"
      - "8081:8080"
    volumes:
      - database:/cockroach/cockroach-data

volumes:
  database: