Skip to main content
Use the OpenTelemetry Ruby SDK to instrument Ruby applications in a standard, vendor-agnostic, and future-proof way and send telemetry data to Honeycomb. In this guide, we will walk you through instrumenting with OpenTelemetry for Ruby, which will include adding both automatic and custom, or manual, instrumentation to your applications.

Before You Begin

Before you can set up automatic and custom instrumentation for your Ruby service, you will need to do a few things.

Prepare Your Development Environment

To complete the required steps, you will need:
  • A working Ruby environment with Ruby 2.5 or higher
  • A service written in Ruby

Get Your Honeycomb API Key

To send data to Honeycomb, you’ll need to sign up for a free Honeycomb account and create a Honeycomb Ingest API Key. To get started, you can create a key that you expect to swap out when you deploy to production. Name it something helpful, perhaps noting that it’s a getting started key. Make note of your API key; for security reasons, you will not be able to see the key again, and you will need it later!
For setup, make sure you check the “Can create datasets” checkbox so that your data will show up in Honeycomb. Later, when you replace this key with a permanent one, you can uncheck that box.
If you want to use an API key you previously stored in a secure location, you can also look up details for Honeycomb API Keys any time in your Environment Settings, and use them to retrieve keys from your storage location.

Add Automatic Instrumentation

To enable automatic instrumentation, you will add instrumentation packages to your application.

Acquire Dependencies

Add these gems to your Gemfile:
Install the gems using your terminal:

Initialize

Initialize OpenTelemetry early in your application lifecycle. For Rails applications, we recommend that you use a Rails initializer. For other Ruby services, initialize as early as possible in the startup process.

Configure the OpenTelemetry SDK

Use environment variables to configure OpenTelemetry to send events to Honeycomb:
If you are a Honeycomb Classic user, the Dataset also must be specified using the x-honeycomb-dataset header. A Dataset is a bucket where data gets stored in Honeycomb.

Run

Run your Ruby application. In Honeycomb’s UI, you should now see your application’s incoming requests and outgoing HTTP calls generate traces.

Add Custom Instrumentation

Automatic instrumentation is the easiest way to get started with instrumenting your code, but to get the most insight into your system, you should add custom, or manual, instrumentation where appropriate. To do this, use the OpenTelemetry SDK to access the currently executing span and add attributes to it, and/or to create new spans. To add custom instrumentation, you need to add the OpenTelemetry SDK gem to your Gemfile:

Acquire a Tracer

To create spans, you need to get a Tracer.
When you create a Tracer, OpenTelemetry requires you to give it a name as a string. This string is the only required parameter. When traces are sent to Honeycomb, the name of the Tracer is turned into the library.name field, which can be used to show all spans created from a particular tracer. In general, pick a name that matches the appropriate scope for your traces. If you have one tracer for each service, then use the service name. If you have multiple tracers that live in different “layers” of your application, then use the name that corresponds to that “layer”. The library.name field is also used with traces created from instrumentation libraries.

Add Attributes to Spans

It is often helpful to add information to a currently executing span in a trace. For example, you may have an application or service that handles users, and you want to associate the user with the span when querying your service in Honeycomb. To do this, get the current span from the context and set an attribute with the user ID:
This will add a user.id field to the current span so that you can use the field in WHERE, GROUP BY, or ORDER clauses in the Honeycomb query builder.

Create New Spans

Automatic instrumentation can show the shape of requests to your system, but only you know the really important parts. To get the full picture of what is happening, you will have to add custom instrumentation and create some custom spans. To do this, grab the tracer from the OpenTelemetry API, and use it to create spans and add attributes.

Add Multi-Span Attributes

Sometimes you want to add the same attribute to many spans within the same trace. This attribute may include variables calculated during your program, or other useful values for correlation or debugging purposes. To add this attribute to multiple spans, leverage the OpenTelemetry concept of baggage. Baggage allows you to add a key with a value as an attribute to every subsequent child span of the current application context, as long as you configured a BaggageSpanProcessor when you initialized OpenTelemetry.
  1. Install the baggage span processor gem using your terminal:
  2. When configuring the OpenTelemetry SDK tracer provider, add the baggage span processor: