OpenTelemetry guide for Ruby on Rails

In this article, you will learn how to use OpenTelemetry with Uptrace to monitor Ruby on Rails performance.

Uptrace

What is tracing?

Distributed tracingRuby on Rails - 图2open in new window allows to precisely pinpoint the problem in complex systems, especially those built using a microservices architecture.

Tracing allow to follow requests as they travel through distributed systems. You get a full context of what is different, what is broken, and which logs & errors are relevant.

Ruby on Rails - 图3

Using tracing, you can break down requests into spansRuby on Rails - 图4open in new window. Span is an operation (unit of work) your app performs handling a request, for example, a database query or a network call.

Trace is a tree of spans that shows the path that a request makes through an app. Root span is the first span in a trace.

Ruby on Rails - 图5

To learn more about tracing, see Distributed tracing using OpenTelemetryRuby on Rails - 图6open in new window.

What is OpenTelemetry?

OpenTelemetryRuby on Rails - 图7open in new window is an open source and vendor-neutral API for distributed tracingRuby on Rails - 图8open in new window (including logs and errors) and metricsRuby on Rails - 图9open in new window.

Otel specifies how to collect and export telemetry data in a vendor agnostic way. With OpenTelemetry, you can instrumentRuby on Rails - 图10open in new window your application once and then add or change vendors without changing the instrumentation, for example, many open source tracing toolsRuby on Rails - 图11open in new window already support OpenTelemetry.

OpenTelemetry is available for most programming languages and provides interoperability across different languages and environments.

Creating spans

You can create a span using OpenTelemetry Ruby APIRuby on Rails - 图12open in new window like this:

  1. require 'opentelemetry'
  2. tracer = OpenTelemetry.tracer_provider.tracer('my_app_or_gem', '1.0.0')
  3. def some_func()
  4. tracer.in_span('some-func') do |span|
  5. # the code you are measuring
  6. end
  7. end

What is Uptrace?

UptraceRuby on Rails - 图13open in new window is an open source and blazingly fast distributed tracing toolRuby on Rails - 图14open in new window powered by OpenTelemetry and ClickHouse. It allows you to identify and fix bugs in production faster knowing what conditions lead to which errors

You can install UptraceRuby on Rails - 图15open in new window by downloading a DEB/RPM package or a pre-compiled binary.

Example application

In this tutorial, you will be instrumenting a toy appRuby on Rails - 图16open in new window that uses Django and PostgreSQL database client. You can retrieve the source code with the following command:

  1. git clone git@github.com:uptrace/uptrace.git
  2. cd example/rails

The app comes with some dependencies that you can install with:

  1. bundle install

Configuring OpenTelemetry

Uptrace provides OpenTelemetry RubyRuby on Rails - 图17open in new window distro that configures OpenTelemetry SDK for you. To install the distro:

  1. gem install uptrace

Then you need to initialize OpenTelemetry whenever the app is started:

  1. require 'uptrace'
  2. # copy your project DSN here or use UPTRACE_DSN env var
  3. Uptrace.configure_opentelemetry(dsn: '') do |c|
  4. c.use_all
  5. c.service_name = 'myservice'
  6. c.service_version = '1.0.0'
  7. end

See documentationRuby on Rails - 图18open in new window for details.

Instrumenting Rails

To instrument Ruby on Rails app, you need a corresponding OpenTelemetry Rails instrumentationRuby on Rails - 图19open in new window:

  1. gem install opentelemetry-instrumentation-rails

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

  1. require 'uptrace'
  2. require 'opentelemetry-instrumentation-rails'
  3. Uptrace.configure_opentelemetry(dsn: '') do |c|
  4. c.use 'OpenTelemetry::Instrumentation::Rails'
  5. end

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

  1. require 'uptrace'
  2. require 'opentelemetry-instrumentation-rails'
  3. Uptrace.configure_opentelemetry(dsn: '') do |c|
  4. c.use_all
  5. end

Instrumenting ActiveRecord

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

  1. gem install opentelemetry-instrumentation-active_record

And call use with the name of the instrumentation:

  1. require 'uptrace'
  2. require 'opentelemetry-instrumentation-active_record'
  3. Uptrace.configure_opentelemetry(dsn: '') do |c|
  4. c.use 'OpenTelemetry::Instrumentation::ActiveRecord'
  5. end

What’s next?

Next, you can learn about OpenTelemetry Ruby APIRuby on Rails - 图20open in new window to create your own instrumentations or browse existing instrumentationsRuby on Rails - 图21open in new window provided by the community.