DataDog / dd-trace-rb

Datadog Tracing Ruby Client
https://docs.datadoghq.com/tracing/
Other
304 stars 373 forks source link

How to disable active_record traces ? #2256

Open adrys-lab opened 2 years ago

adrys-lab commented 2 years ago

Current behaviour we would like to disable active_record traces cause with the ones from mysql2 is enough for us. Once we try to disable them, they are still appearing with:

  # disable instrumenting
  c.tracing.instrument(:active_record, service_name: "#{service}-active-record", enabled: false)
  c.tracing.instrument(:active_support, service_name: "#{service}-active-support", enabled: false)

Meanwhile, if we don't add the instrumentation, they appear with the default service_name as mysql2, as documented in the doc: https://docs.datadoghq.com/tracing/trace_collection/dd_libraries/ruby/#active-record

Entire Config file:

require './components/platform/base/graphql/schema.rb'
require File.expand_path('../../lib/datadog_tracer', __dir__)

require 'datadog/statsd'
require 'ddtrace'

Datadog.configure do |c|
  service = 'web'
  hostname = DatadogTracer.resolve_datadog_hostname
  c.env = Rails.env
  c.agent.host = hostname
  c.service = service
  c.tags = DatadogTracer.tags

  # runtime
  c.runtime_metrics.enabled = true
  c.runtime_metrics.statsd = Datadog::Statsd.new(hostname, DatadogTracer.statsd_port)

  # tracing
  c.tracing.enabled = ENV.fetch('DATADOG_TRACER_ENABLED', false)
  c.tracing.priority_sampling = true
  c.tracing.report_hostname = true
  c.tracing.log_injection = true
  c.tracing.partial_flush.enabled = true

  # disable instrumenting
  c.tracing.instrument(:active_record, service_name: "#{service}-active-record", enabled: false)
  c.tracing.instrument(:active_support, service_name: "#{service}-active-support", enabled: false)

  # instrumenting
  c.tracing.instrument(:action_mailer, service_name: "#{service}-action-mailer")
  c.tracing.instrument(:active_job, service_name: "#{service}-active-job")
  c.tracing.instrument(:aws, service_name: "#{service}-aws")
  c.tracing.instrument(:elasticsearch, service_name: "#{service}-elasticsearch")
  c.tracing.instrument(:mysql2, service_name: "#{service}-mysql2")
  c.tracing.instrument(:rack, request_queuing: true)
  c.tracing.instrument(:rails, service_name: service, distributed_tracing: true)
  c.tracing.instrument(:rake, service_name: "#{service}-rake", quantize: { args: { show: :all } })
  c.tracing.instrument(:redis, service_name: "#{service}-redis-cache")
  c.tracing.instrument(:redis, describes: { host: ENV['REDIS_HOST'] }, service_name: "#{service}-redis-sidekiq")
  c.tracing.instrument(:shoryuken, service_name: "#{service}-shoryuken")
  c.tracing.instrument(:sidekiq, service_name: "#{service}-sidekiq", client_service_name: "#{service}-sidekiq")

  # web backend-graphql-api
  c.tracing.instrument(:graphql, schemas: [::Platform::Base::GraphQL::Schema], service_name: ENV['BACKEND_GRAPHQL_API'].present? ? 'backend-graphql-api' : 'web')
end

Expected behaviour Traces are not appearing anymore.

Environment

adrys-lab commented 2 years ago

We have seen this configuration in the source code of ddtrace

        # ActiveRecord integration constants
        # @public_api Changing resource names, tag names, or environment variables creates breaking changes.
        module Ext
          ENV_ENABLED = 'DD_TRACE_ACTIVE_RECORD_ENABLED'.freeze
          ENV_ANALYTICS_ENABLED = 'DD_TRACE_ACTIVE_RECORD_ANALYTICS_ENABLED'.freeze
          ENV_ANALYTICS_SAMPLE_RATE = 'DD_TRACE_ACTIVE_RECORD_ANALYTICS_SAMPLE_RATE'.freeze
          SERVICE_NAME = 'active_record'.freeze
          SPAN_INSTANTIATION = 'active_record.instantiation'.freeze
          SPAN_SQL = 'active_record.sql'.freeze
          SPAN_TYPE_INSTANTIATION = 'custom'.freeze
          TAG_COMPONENT = 'active_record'.freeze
          TAG_OPERATION_INSTANTIATION = 'instantiation'.freeze
          TAG_OPERATION_SQL = 'sql'.freeze
          TAG_DB_CACHED = 'active_record.db.cached'.freeze
          TAG_DB_NAME = 'active_record.db.name'.freeze
          TAG_DB_VENDOR = 'active_record.db.vendor'.freeze
          TAG_INSTANTIATION_CLASS_NAME = 'active_record.instantiation.class_name'.freeze
          TAG_INSTANTIATION_RECORD_COUNT = 'active_record.instantiation.record_count'.freeze
        end

and usage

module Datadog
  module Tracing
    module Contrib
      module ActiveRecord
        module Configuration
          # Custom settings for the ActiveRecord integration
          # @public_api
          class Settings < Contrib::Configuration::Settings
            option :enabled do |o|
              o.default { env_to_bool(Ext::ENV_ENABLED, true) }
              o.lazy
            end

Should we mandatorily add an ENV VAR DD_TRACE_ACTIVE_RECORD_ENABLED = false ? 🤔

Where is this documented ?

marcotc commented 1 year ago

👋 sorry for the delay, if you'd like for active_record and active_support spans to not be generated, you can remove these lies altogether from your configuration.

-  c.tracing.instrument(:active_record, service_name: "#{service}-active-record", enabled: false)
-  c.tracing.instrument(:active_support, service_name: "#{service}-active-support", enabled: false)

One caveat is that c.tracing.instrument(:rails) will activate active_record and active_support behind the scenes so you'll have to remove c.tracing.instrument(:rails) as well. You should then activate only the Rails-related instrumentations that you need.

Here's the exhaustive list of all instrumentations that Rails activates when you do c.tracing.instrument(:rails):

rack
action_cable
action_mailer
active_support
action_pack
action_view
active_job
active_record
lograge
semantic_logger

Feel free to pick only the ones you can about, and enable them with c.tracing.instrument(INSTRUMENTATION_NAME_HERE).

adrys-lab commented 1 year ago

hey @marcotc thanks for your help as always.

i have found some ENV VARS that allow to disable specific tracing instrumentation without disabling rails.

DD_TRACE_ACTIVE_RECORD_ENABLED disables this specific one,

so this worked for me.

  DD_TRACE_ACTIVE_RECORD_ENABLED  = false
  DD_TRACE_ACTIVE_SUPPORT_ENABLED = false

i have found that inspecting directly gem source code, but i think it deserves to be documented in your public rails instrucmentation for the gem 🙏