OpenTelemetry Ruby On Rails

Vladimir Mihailenco
October 20, 2024
2 min read

OpenTelemetry can be integrated with Ruby on Rails to add observability and monitoring capabilities to your Rails applications.

By integrating OpenTelemetry, you can collect telemetry data, such as distributed traces and metrics, and send it to various backends for analysis and visualization.

What is OpenTelemetry?

OpenTelemetry is an open source observability framework that helps developers instrument, generate, collect, and export telemetry data from software applications.

OpenTelemetry provides a unified set of APIs, libraries, and instrumentation tools for capturing and transmitting traces, metrics, and logs to effectively monitor and troubleshoot distributed systems.

The primary goal of OpenTelemetry is to provide a standardized and vendor-agnostic approach to instrumenting applications for observability. It aims to simplify the process of collecting telemetry data by providing a single, consistent API and providing integration with various OpenTelemetry backends.

What is Ruby on Rails?

Ruby on Rails is a popular open source web application framework written in the Ruby programming language.

Rails follows the Model-View-Controller (MVC) architectural pattern and provides a set of conventions and libraries to simplify and accelerate web development.

Ruby on Rails has gained popularity due to its focus on developer happiness, productivity, and code simplicity. It has been used to build a wide range of applications, from small websites to large, high-traffic platforms.

Rails instrumentation

OpenTelemetry provides instrumentation for various Ruby libraries and frameworks, including Rails. To install OpenTelemetry instrumentation for Ruby on Rails:

shell
gem install opentelemetry-instrumentation-rails

If you use bundler, add opentelemetry-instrumentation-rails to your Gemfile.

Usage

By integrating OpenTelemetry with Ruby on Rails, you can gain valuable insight into the performance, behavior, and dependencies of your application. You can monitor and troubleshoot problems, optimize performance, and ensure the reliability of your Rails applications.

To instrument your Rails app, call use with the name of the instrumentation:

ruby
require 'uptrace'
require 'opentelemetry-instrumentation-rails'

Uptrace.configure_opentelemetry(dsn: '<FIXME>') do |c|
  c.use 'OpenTelemetry::Instrumentation::Rails'
end

Alternatively, you can call use_all to install all available instrumentations:

ruby
require 'uptrace'
require 'opentelemetry-instrumentation-rails'

Uptrace.configure_opentelemetry(dsn: '<FIXME>') do |c|
  c.use_all
end

See example for details.

Instrumenting ActiveRecord

Just like with Rails, you need to install ActiveRecord instrumentation:

shell
gem install opentelemetry-instrumentation-active_record

And call use with the name of the instrumentation:

ruby
require 'uptrace'
require 'opentelemetry-instrumentation-active_record'

Uptrace.configure_opentelemetry(dsn: '<FIXME>') do |c|
  c.use 'OpenTelemetry::Instrumentation::ActiveRecord'
end

What is Uptrace?

Uptrace is a distributed tracing tool that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues.

Uptrace comes with an intuitive query builder, rich dashboards, alerting rules with notifications, and integrations for most languages and frameworks.

Uptrace can process billions of spans and metrics on a single server and allows you to monitor your applications at 10x lower cost.

In just a few minutes, you can try Uptrace by visiting the cloud demo (no login required) or running it locally with Docker. The source code is available on GitHub.

What's next?

OpenTelemetry allows you to instrument specific parts of your code for custom telemetry collection. You can use OpenTelemetry Ruby APIs to manually create spans and add custom attributes, events, or metrics to capture additional information.