Setting Up Datadog APM for Ruby - Rails and the Gemfile

Adding APM to a Ruby application takes its shape from adding the datadog gem to the Gemfile and deciding where to load the auto-instrumentation, datadog/auto_instrument. Rails and Hanami require it inside the Gemfile, while other applications require it after loading their libraries.

Installation Splits Two Ways: Rails / Hanami and Other Ruby Applications

The tracing library for Ruby is a single gem called datadog, and how you install it splits two ways depending on the framework you use. With Rails or Hanami, a single line in the Gemfile loads everything up to auto-instrumentation, and configuration goes in config/initializers/datadog.rb. For other Ruby applications, you write only the gem in the Gemfile and place the auto-instrumentation require in the application code (as of October 2026; source: Tracing Ruby Applications).

The two installation paths for Ruby applications (as of October 2026)
ItemRails / HanamiOthers (such as Sinatra)
Gemfilegem 'datadog', require: 'datadog/auto_instrument'gem 'datadog'
Where auto-instrumentation is loadedThe require: option in the GemfileIn the application code, after the require of the libraries you want to instrument
Where configuration goesDatadog.configure in config/initializers/datadog.rbDatadog.configure in the application code
Order to watchAuto-instrumentation takes effect before the initializerThe docs specify placing auto-instrumentation after the libraries' require
Source: Based on Tracing Ruby Applications

The last row of the table is the crux on either path. In Rails, auto-instrumentation runs first, so some settings arrive too late if you write them in an initializer. In other applications, conversely, the official documentation specifies placing auto-instrumentation after loading the libraries. Both are a question of "when is it loaded," and the code itself takes only a few lines. If you have not installed the Agent yet, completing installing the Agent on Linux first lets you try the steps that follow as they are.

What the Single auto_instrument Line Brings Into Instrumentation

For many commonly used gems, Datadog provides instrumentation on its side. However, being provided does not mean it is enabled automatically (as of October 2026; source: Tracing Ruby Applications).

Two ways to enable instrumentation (as of October 2026)
MethodWhat gets enabledWhen it fits
Load datadog/auto_instrumentAll supported libraries that are already loaded, at onceThe start of adoption, when you first want to see everything
Write them one by one, as in c.tracing.instrument :railsOnly the libraries you writeWhen you want to decide the scope and settings of instrumentation one at a time
Source: Tracing Ruby Applications

When using auto-instrumentation in applications other than Rails or Hanami, load the libraries you want to instrument first, and load auto-instrumentation last.

require 'sinatra'
require 'faraday'
require 'redis'

require 'datadog/auto_instrument'

With this order, requests to Sinatra, outbound HTTP calls made through Faraday, and commands to Redis each become spans and line up in a single trace. The Rails integration works by receiving Active Support Instrumentation notifications. It follows request processing, database calls, template rendering, and cache operations (source: Tracing Ruby Applications). The relationship between traces and spans is summarized in the Trace glossary entry.

Service Names Are Assigned per Library, and Some Settings Cannot Be Changed in an Initializer

Right after installation, the service list may show, besides the application's name, names such as the database adapter as separate services. This is because each library's instrumentation has its own default service name. You can override the name by passing service_name to c.tracing.instrument. For Active Record, you can also use describes, which splits names per connection (as of October 2026; source: Tracing Ruby Applications).

Main settings that affect service names (as of October 2026)
SettingDefault behaviorWhen you want to change it
Rails service_nameA name inferred from the Rails application's namespacec.tracing.instrument :rails, service_name: '…'
Active Record service_nameThe name of the database adapter (for example, mysql2)To split per connection, specify the connection with describes
split_by_domain for Faraday and othersfalseWith true, the called domain becomes the service name
DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLEDfalseWith true, the defaults of all instrumentation are aligned to the application's service name
Source: Tracing Ruby Applications

The last setting in the table comes with a Rails-specific caveat. When you load auto-instrumentation in the Gemfile, instrumentation is applied before config/initializers/*.rb. The official documentation covers this order in its Configuration timing with auto-instrumentation section. Settings that are read at the moment instrumentation is applied must be passed as environment variables rather than in an initializer (source: Tracing Ruby Applications). If you want to avoid environment variables, there are two alternatives. Either keep auto-instrumentation and override service_name per library, or remove the require: option from the Gemfile and enable every library you use with c.tracing.instrument. How to think about naming services is covered in the APM Service glossary entry.

When a Service Does Not Appear, Read the Gem's State From the Startup Logs

On the receiving Agent, enable trace intake with DD_APM_ENABLED=true as an environment variable or apm_enabled: true in the configuration file. By default, the Agent listens on HTTP port 8126 (as of October 2026; source: Tracing Ruby Applications). The Ruby gem looks for a destination in this order: explicit configuration, the Unix domain socket at /var/run/datadog/apm.socket, and then 127.0.0.1:8126. How to send to an Agent on another host or container, and how to pass DD_ENV / DD_SERVICE / DD_VERSION, are the same regardless of language, so I leave them to the Python setup article. If the application still does not appear in the service list, what you read are the two diagnostic outputs the gem itself produces.

Two outputs for reading the gem's state (as of October 2026)
OutputSetting to enable itWhat it tells youEnvironment to use it in
Startup logsDD_TRACE_STARTUP_LOGS or c.diagnostics.startup_logs.enabledThe configuration at startup and a report on whether instrumentation was enabled correctlyOutput by default when the gem judges the environment to be other than development
Debug modeDD_TRACE_DEBUG or c.diagnostics.debugDetailed logs including suppressed errors, and the sending of traces to the AgentTest environments where you can control load (not recommended for production and similar environments)
Source: Debugging and diagnostics in Tracing Ruby Applications

Read the startup logs first. They are not output by default on a local development machine, so start with DD_TRACE_STARTUP_LOGS=true and check whether the configuration and instrumentation were enabled in the form you expected. Only when the startup logs do not reveal the cause, turn on debug mode in a test environment. Because the output becomes extremely large under load, the official documentation does not recommend using it in production and similar environments. Note that Ruby runtime metrics are disabled by default. To enable them, in addition to DD_RUNTIME_METRICS_ENABLED=true, add the dogstatsd-ruby gem to the Gemfile (source: Runtime Metrics).

Keep Tracing Off While RSpec and Tests Run

Once you add the gem to the Gemfile, it is also loaded on local development machines and in tests run in CI. According to the official documentation, tracing is enabled by default and runs if the gem is loaded in any of the following three ways (as of October 2026; source: Tracing Ruby Applications).

  • When loaded through datadog/auto_instrument
  • When tracing.instrument is called inside Datadog.configure
  • When code calls Datadog::Tracing.trace
Source: Tracing Ruby Applications

Traces created by test runs do not help you see what is happening in production, so the official documentation provides ways to stop tracing during tests and specs. There are two: set DD_TRACE_ENABLED to false in the environment that runs the tests, or stop it from code in the test configuration.

Datadog.configure do |c|
  c.tracing.enabled = false
end

In my view, stopping it with the environment variable lets you keep the Gemfile and initializer identical to production. It takes only one line in the CI configuration and keeps test-only branching out of the application code. If you want to track the slowness or flakiness of the tests themselves, the official documentation points to Datadog Test Optimization, used with the datadog-ci gem.

In the Compatibility Table, Look First Only at the MRI Minimum and the OS Column

Ruby version support is in the table on the compatibility page, and as of October 2026, the minimum MRI version with a Support type of latest is 2.5. Versions 2.4 and earlier and JRuby are treated as end of life (EOL) and can use only older versions of the gem (source: Ruby Compatibility Requirements). What you want to check before the version numbers is the OS column.

Support type by OS (as of October 2026)
OSSupport type
Linux x86_64Supported
Linux aarch64Supported
macOSDevelopment environment only
WindowsNot implemented
Source: Ruby Compatibility Requirements

Because macOS is treated as development environment only, even if traces appear on your local Mac, confirm production decisions on Linux. If you run Ruby applications on Windows, tracing with this gem is outside the table's scope. The supported versions per library are in the Integrations section of the same page, and the Rails integration is tabulated by combinations of MRI version and Rails version. When upgrading, check production's ruby -v and the Rails version in Gemfile.lock against this table before proceeding.

Check That Logs Carry the Trace ID and That You Can Move From a Trace to the Same Request's Logs

Once configuration is done, start the application in a development environment and call a page or API just once. According to the official documentation, once configured, the service appears in the APM service list within a few minutes (as of October 2026; source: Tracing Ruby Applications). Continuous Profiler, which shows load per method, can be added later, so verify with only traces and logs at first. The profiler works with the same gem: add DD_PROFILING_ENABLED=true and wrap the startup command with ddprofrb exec (source: Enabling the Profiler).

What to check right after running it
Where to lookExpected stateWhere to go back if it differs
Service listOne service with the application's name appearsAgent intake and destination settings, startup logs
Services other than the applicationDatabase and outbound HTTP calls line up under the names you choseThe service_name overrides and, for Rails, the environment variable settings
Contents of a single traceSpans for database and outbound calls line up under the requestOutside Rails, the position of the auto-instrumentation require
Log linesThey carry the trace ID, and the same request's logs open from the traceThe type of logger in use and the trace remapper
Source: Based on Tracing Ruby Applications / Correlating Ruby Logs and Traces

The preparation for connecting to logs depends on the logger you use. With Rails' ActiveSupport::TaggedLogging (the default), semantic_logger, and lograge, the setting that injects the trace ID into logs is added automatically. However, lograge requires disabling TaggedLogging, and to tie logs to traces you also add the trace remapper (source: Correlating Ruby Logs and Traces). For loggers outside Rails, call Datadog::Tracing.log_correlation in the format and put dd.trace_id and the like into the message. Finally, open one log line from the request you just called. If it carries the trace ID and you can move from the trace view to the same request's logs, the installation is complete.

References

Share