Getting Started
This page will show you how to get started with OpenTelemetry in Ruby.
You will learn how you can instrument a simple application automatically, in such a way that traces, metrics and logs are emitted to the console.
Prerequisites¶
Ensure that you have the following installed locally:
- MRI Ruby >=
3.0
, jruby >=9.3.2.0
, or truffleruby >= 22.1 - Bundler
{{% alert title="Warning" color="warning" %}} jruby
only targets
compatibility with MRI Ruby 2.6.8, which is EOL. This project does not
officially support MRI Ruby 2.6.8, and provides jruby
support on a best-effort
basis until the jruby
project supports compatibility with more modern Ruby
runtimes.
While tested, support for truffleruby
is on a best-effort basis at this time.
{{% /alert %}}
Example Application¶
The following example uses a basic Rails application. If you are not using Rails, that's ok — you can use OpenTelemetry Ruby with other web frameworks as well, such as Sinatra and Rack. For a complete list of libraries for supported frameworks, see the registry.
For more elaborate examples, see examples.
Dependencies¶
To begin, install rails:
Create the application¶
Create a new api-only application called dice-ruby
and change into the newly
created folder dice-ruby
Create a controller for rolling a dice:
This will create a file called app/controllers/dice_controller.rb
. Open that
file in your preferred editor and update it with the following code:
Next, open the config/routes.rb
file and add the following code:
Run the application with the following command and open http://localhost:8080/rolldice in your web browser to ensure it is working.
If everything works fine you should see a number between 1 and 6 returned to you. You can now stop the application and instrument it using OpenTelemetry.
Instrumentation¶
Install the opentelemetry-sdk
and opentelemetry-instrumentation-all
packages:
The inclusion of opentelemetry-instrumentation-all
provides
instrumentations for Rails, Sinatra, several HTTP libraries, and more.
For Rails applications, the usual way to initialize OpenTelemetry is in a Rails initializer. For other Ruby services, perform this initialization as early as possible in the start-up process.
Create a file named config/initializers/opentelemetry.rb
with the following
code:
The call c.use_all()
enables all instrumentations in the instrumentation/all
package. If you have more advanced configuration needs, see configuring
specific instrumentation libraries.
Run the instrumented app¶
You can now run your instrumented app and have it print to the console for now:
Open http://localhost:8080/rolldice in your web browser and reload the page a few times. You should see the spans printed in the console, such as the following:
What next?¶
Adding tracing to a single service is a great first step. OpenTelemetry provides a few more features that will allow you gain even deeper insights!
- Exporters allow you to export your data to a preferred backend.
- Context propagation is perhaps one of the most powerful concepts in OpenTelemetry because it will upgrade your single service trace into a distributed trace, which makes it possible for OpenTelemetry vendors to visualize a request from end-to-end across process and network boundaries.
- Span events allow you to add a human-readable message on a span that represents "something happening" during its lifetime.
- Manual instrumentation will give provide you the ability to enrich your traces with domain specific data.