Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: adds RoR docs + tweaks [sc-00] #1024

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions site/content/docs/open-telemetry/instrumenting-code/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ Below you will find instructions on how to instrument your popular languages and
img="/docs/images/integrations/otel/otel-languages/go_icon.svg"
link="/docs/open-telemetry/instrumenting-code/go/"
>}}
{{< doc-card
class="three-column-card"
headerTag="h3"
title="Ruby"
img="/docs/images/integrations/otel/otel-languages/ruby_icon.svg"
link="/docs/open-telemetry/instrumenting-code/ruby/"
>}}
</div>
<br>

Expand All @@ -86,9 +79,9 @@ Below you will find instructions on how to instrument your popular languages and
{{< doc-card
class="three-column-card"
headerTag="h3"
title=".NET"
img="/docs/images/integrations/otel/otel-languages/dot-net_icon.svg"
link="/docs/open-telemetry/instrumenting-code/dot-net/"
title="Ruby on Rails"
img="/docs/images/integrations/otel/otel-languages/ruby_on_rails_icon.svg"
link="/docs/open-telemetry/instrumenting-code/ruby-on-rails/"
>}}
</div>
<br>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
---
title: Express
weight: 38
head:
title: "Instrumenting Express.js with OpenTelemetry"
metatags:
title: "Instrumenting Express.js with OpenTelemetry"
description: "Instrument your Express.js application with OpenTelemetry and send traces to Checkly."
menu:
integrations:
parent: "Instrumenting your code with OpenTelemetry"
Expand Down
101 changes: 101 additions & 0 deletions site/content/docs/open-telemetry/instrumenting-code/ruby-on-rails.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
title: Ruby on Rails
weight: 36
head:
title: "Instrumenting Ruby on Rails with OpenTelemetry"
metatags:
title: "Instrumenting Ruby on Rails with OpenTelemetry"
description: "Instrument your Ruby on Rails application with OpenTelemetry and send traces to Checkly."
menu:
integrations:
parent: "Instrumenting your code with OpenTelemetry"
beta: true
---

This guide will help you instrument your Ruby on Rails application(s) with OpenTelemetry and send traces to Checkly.
<!--more-->
Although this guide is for [Ruby on Rails](https://rubyonrails.org/), the steps are largely the same as instrumenting
any Ruby application with OpenTelemetry. This guide assumes you have a basic understanding of Ruby on Rails and already
have a working Rails application.

## Step 1: Install the OpenTelemetry package

Go to the root of your Rails app and add the basic OpenTelemetry SDK, OTLP exporter and instrumentation gems to your Gemfile:

```bash
bundle add opentelemetry-sdk \
opentelemetry-exporter-otlp \
opentelemetry-instrumentation-all
```

This should add the following lines:
```ruby
# Gemfile
gem "opentelemetry-sdk", "~> 1.4"
gem "opentelemetry-exporter-otlp", "~> 0.26.3"
gem "opentelemetry-instrumentation-all", "~> 0.60.0"
```

## Step 2: Initialize the instrumentation

As per the Ruby on Rails convention, we add an `instrumentation.rb` file to the `config/initializers` directory.

```ruby
# config/initializers/instrumentation.rb

require 'opentelemetry/sdk'
require 'opentelemetry/instrumentation/all'

class ChecklySampler

def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:)
tracestate = OpenTelemetry::Trace.current_span(parent_context).context.tracestate
decision = tracestate.value('checkly') ? OpenTelemetry::SDK::Trace::Samplers::Decision::RECORD_AND_SAMPLE :
OpenTelemetry::SDK::Trace::Samplers::Decision::DROP
puts(decision)
OpenTelemetry::SDK::Trace::Samplers::Result.new(decision: decision, attributes: {}, tracestate: tracestate)
end

def description
'ChecklySampler'
end
end

OpenTelemetry::SDK.configure do |c|
c.use_all()
end

OpenTelemetry.tracer_provider.sampler = ChecklySampler.new
```
Notice the `ChecklySampler` configuration. This is a custom, **head-based sampler** that will only sample spans that
are generated by Checkly by inspecting the trace state. This way you only pay for the egress traffic generated by Checkly
and not for any other traffic. Also note that the `use_all()` method will automatically install all available
instrumentation libraries.

## Step 3: Start your app with the instrumentation

{{< markdownpartial "/_shared/otel-api-and-endpoint.md" >}}

You can now restart your Rails app with the instrumentation enabled.

```bash
rails server
```

## Debugging and troubleshooting

If you run into any issues, you can also output any traces to the console as follows:

```bash
env OTEL_TRACES_EXPORTER=console rails server
```

Similarly, you can set the `OTEL_LOG_LEVEL` environment variable to `DEBUG` to get more detailed logs.

```bash
env OTEL_LOG_LEVEL=DEBUG rails server
```

## Further reading

- [https://opentelemetry.io/docs/languages/ruby/getting-started/](https://opentelemetry.io/docs/languages/ruby/getting-started/)
14 changes: 0 additions & 14 deletions site/content/docs/open-telemetry/instrumenting-code/ruby.md

This file was deleted.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading