Ruby
Traces | Metrics | App Logs | Custom Logs | Profiling |
---|---|---|---|---|
✅ | ✖ | ✖ | ✖ | ✅ |
This guide walks you through setting up Application Performance Monitoring (APM) on a Ruby application. These instructions can also be found on the Installation page in your Middleware Account. View example code here.
Prerequisites
- Middleware Agent (MW Agent): To install the MW Agent, see our Installation Guide.
- Ruby Version 3.0.0+: Check your Ruby version with
ruby --version
Install
Step 1: Add Gems to Your Gemfile
gem 'opentelemetry-sdk' gem 'opentelemetry-exporter-otlp' gem 'opentelemetry-instrumentation-all' gem 'pyroscope' gem 'middleware_apm_linux', '~> 2.1.0'
gem 'opentelemetry-sdk' gem 'opentelemetry-exporter-otlp' gem 'opentelemetry-instrumentation-all' gem 'pyroscope' gem 'middleware_apm_windows', '~> 2.1.0'
gem 'opentelemetry-sdk' gem 'opentelemetry-exporter-otlp' gem 'opentelemetry-instrumentation-all' gem 'pyroscope' gem 'middleware_apm_linux', '~> 2.1.0'
Install the gems with bundle install
bundle install
Step 2: Import Tracker
Add the following initialization code at the beginning of your Ruby application:
require 'middleware/ruby_gem_linux' Middleware::RubyGem.init
Add the following initialization code at the beginning of your Ruby application:
require 'middleware/ruby_gem_windows' Middleware::RubyGem.init
Add the following initialization code at the beginning of your Ruby application:
require 'middleware/ruby_gem_linux' Middleware::RubyGem.init
For example, if you are using Ruby on Rails, you can add the initialization code in config/environment.rb
. Your config/environment.rb
should look like below:
# Load the Rails application. require_relative "application" require 'middleware/ruby_gem_linux' # Initialize Middleware.io application monitoring Middleware::RubyGem.init # Initialize the Rails application. Rails.application.initialize!
Step 3: Capture Application Data
Traces
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:9320 \ OTEL_SERVICE_NAME="<Your Service Name>" \ OTEL_RESOURCE_ATTRIBUTES=project.name="<Your Project Name>" \ MW_API_KEY="<MW_API_KEY>" \ <YOUR COMMAND>
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:9320 \ OTEL_SERVICE_NAME="<Your Service Name>" \ OTEL_RESOURCE_ATTRIBUTES=project.name="<Your Project Name>" \ MW_API_KEY="<MW_API_KEY>" \ <YOUR COMMAND>
Heroku buildpack for Middleware auto-configures required environment variables and there is no additional configuration required.
However, if you wish to override default values for the auto-configured environment variables in Ruby execution environment.
OTEL_SERVICE_NAME="<Your Service Name>" \ OTEL_RESOURCE_ATTRIBUTES=project.name="<Your Project Name>" \
For Ruby on Rails, you can override the default values by setting environment variables in config/environment.rb
file. This configuration should be before Middleware gem is initialized.
# Load the Rails application. require_relative "application" require 'middleware/ruby_gem_linux' # Override default values for Middleware.io application monitoring ENV['OTEL_SERVICE_NAME'] = '<Your Service Name>' ENV['OTEL_RESOURCE_ATTRIBUTES'] = 'project.name=<Your Project Name>' # Initialize Middleware.io application monitoring Middleware::RubyGem.init
Profiling
Application Profiling is auto-configured upon completion of Step 2.
Continuous Profiling
Continuous profiling captures real-time performance insights from your application to enable rapid identification of resource allocation, bottlenecks, and more. Navigate to the Continuous Profiling section to learn more about using Continuous Profiling with the Ruby APM.
Need assistance or want to learn more about Middleware? Contact our support team in Slack.