|
| 1 | +--- |
| 2 | +title: Ruby on Rails |
| 3 | +weight: 36 |
| 4 | +head: |
| 5 | + title: "Instrumenting Ruby on Rails with OpenTelemetry" |
| 6 | +metatags: |
| 7 | + title: "Instrumenting Ruby on Rails with OpenTelemetry" |
| 8 | + description: "Instrument your Ruby on Rails application with OpenTelemetry and send traces to Checkly." |
| 9 | +menu: |
| 10 | + integrations: |
| 11 | + parent: "Instrumenting your code with OpenTelemetry" |
| 12 | +beta: true |
| 13 | +--- |
| 14 | + |
| 15 | +This guide will help you instrument your Ruby on Rails application(s) with OpenTelemetry and send traces to Checkly. |
| 16 | +<!--more--> |
| 17 | +Although this guide is for [Ruby on Rails](https://rubyonrails.org/), the steps are largely the same as instrumenting |
| 18 | +any Ruby application with OpenTelemetry. This guide assumes you have a basic understanding of Ruby on Rails and already |
| 19 | +have a working Rails application. |
| 20 | + |
| 21 | +## Step 1: Install the OpenTelemetry package |
| 22 | + |
| 23 | +Go to the root of your Rails app and add the basic OpenTelemetry SDK, OTLP exporter and instrumentation gems to your Gemfile: |
| 24 | + |
| 25 | +```bash |
| 26 | +bundle add opentelemetry-sdk \ |
| 27 | +opentelemetry-exporter-otlp \ |
| 28 | +opentelemetry-instrumentation-all |
| 29 | +``` |
| 30 | + |
| 31 | +This should add the following lines: |
| 32 | +```ruby |
| 33 | +# Gemfile |
| 34 | +gem "opentelemetry-sdk", "~> 1.4" |
| 35 | +gem "opentelemetry-exporter-otlp", "~> 0.26.3" |
| 36 | +gem "opentelemetry-instrumentation-all", "~> 0.60.0" |
| 37 | +``` |
| 38 | + |
| 39 | +## Step 2: Initialize the instrumentation |
| 40 | + |
| 41 | +As per the Ruby on Rails convention, we add an `instrumentation.rb` file to the `config/initializers` directory. |
| 42 | + |
| 43 | +```ruby |
| 44 | +# config/initializers/instrumentation.rb |
| 45 | + |
| 46 | +require 'opentelemetry/sdk' |
| 47 | +require 'opentelemetry/instrumentation/all' |
| 48 | + |
| 49 | +class ChecklySampler |
| 50 | + |
| 51 | + def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:) |
| 52 | + tracestate = OpenTelemetry::Trace.current_span(parent_context).context.tracestate |
| 53 | + decision = tracestate.value('checkly') ? OpenTelemetry::SDK::Trace::Samplers::Decision::RECORD_AND_SAMPLE : |
| 54 | + OpenTelemetry::SDK::Trace::Samplers::Decision::DROP |
| 55 | + puts(decision) |
| 56 | + OpenTelemetry::SDK::Trace::Samplers::Result.new(decision: decision, attributes: {}, tracestate: tracestate) |
| 57 | + end |
| 58 | + |
| 59 | + def description |
| 60 | + 'ChecklySampler' |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | +OpenTelemetry::SDK.configure do |c| |
| 65 | + c.use_all() |
| 66 | +end |
| 67 | + |
| 68 | +OpenTelemetry.tracer_provider.sampler = ChecklySampler.new |
| 69 | +``` |
| 70 | +Notice the `ChecklySampler` configuration. This is a custom, **head-based sampler** that will only sample spans that |
| 71 | +are generated by Checkly by inspecting the trace state. This way you only pay for the egress traffic generated by Checkly |
| 72 | +and not for any other traffic. Also note that the `use_all()` method will automatically install all available |
| 73 | +instrumentation libraries. |
| 74 | + |
| 75 | +## Step 3: Start your app with the instrumentation |
| 76 | + |
| 77 | +{{< markdownpartial "/_shared/otel-api-and-endpoint.md" >}} |
| 78 | + |
| 79 | +You can now restart your Rails app with the instrumentation enabled. |
| 80 | + |
| 81 | +```bash |
| 82 | +rails server |
| 83 | +``` |
| 84 | + |
| 85 | +## Debugging and troubleshooting |
| 86 | + |
| 87 | +If you run into any issues, you can also output any traces to the console as follows: |
| 88 | + |
| 89 | +```bash |
| 90 | +env OTEL_TRACES_EXPORTER=console rails server |
| 91 | +``` |
| 92 | + |
| 93 | +Similarly, you can set the `OTEL_LOG_LEVEL` environment variable to `DEBUG` to get more detailed logs. |
| 94 | + |
| 95 | +```bash |
| 96 | +env OTEL_LOG_LEVEL=DEBUG rails server |
| 97 | +``` |
| 98 | + |
| 99 | +## Further reading |
| 100 | + |
| 101 | +- [https://opentelemetry.io/docs/languages/ruby/getting-started/](https://opentelemetry.io/docs/languages/ruby/getting-started/) |
0 commit comments