Build Status

AWS X-Ray SDK for Ruby (beta)

Screenshot of the AWS X-Ray console


The AWS X-Ray SDK for Ruby is compatible with Ruby 2.3.6 and newer Ruby versions. It has experimental support for JRuby (still forthcoming).

To install the Ruby gem for your project, add it to your project Gemfile. You must also add either the Oj or JrJackson gems, for MRI and JRuby respectively, for JSON parsing. The default JSON parser will not work properly, currently.

# Gemfile
gem 'aws-xray-sdk'
gem 'oj', platform: :mri
gem 'jrjackson', platform: :jruby

Then run bundle install.

Getting Help

Use the following community resources for getting help with the SDK. We use the GitHub issues for tracking bugs and feature requests.

Opening Issues

If you encounter a bug with the AWS X-Ray SDK for Ruby, we want to hear about it. Before opening a new issue, search the existing issues to see if others are also experiencing the issue. Include the version of the AWS X-Ray SDK for Ruby, Ruby language, and other gems if applicable. In addition, include the repro case when appropriate.

The GitHub issues are intended for bug reports and feature requests. For help and questions about using the AWS SDK for Ruby, use the resources listed in the Getting Help section. Keeping the list of open issues lean helps us respond in a timely manner.


The developer guide provides in-depth guidance about using the AWS X-Ray service. The API Reference provides documentation for public APIs of all classes in the SDK.

Quick Start


require 'aws-xray-sdk'

# configure path based sampling rules in case of web app
my_sampling_rules = {
  version: 1,
  rules: [
      description: 'healthcheck',
      service_name: '*',
      http_method: 'GET',
      url_path: '/ping',
      fixed_target: 0,
      rate: 0
  default: {
    fixed_target: 1,
    rate: 0.2

user_config = {
  sampling: true,
  sampling_rules: my_sampling_rules,
  name: 'default_segment_name',
  daemon_address: '',
  context_missing: 'LOG_ERROR',
  patch: %I[net_http aws_sdk]


Working with Rails

# Edit Gemfile to add XRay middleware
gem 'aws-xray-sdk', require: ['aws-xray-sdk/facets/rails/railtie']

# Configure the recorder in app_root/config/initializers/aws_xray.rb
Rails.application.config.xray = {
  # default segment name generated by XRay middleware
  name: 'myrails',
  patch: %I[net_http aws_sdk],
  # record db transactions as subsegments
  active_record: true

Adding metadata/annotations using recorder

require 'aws-xray-sdk'

# Add annotations to the current active entity
XRay.recorder.annotations[:k1] = 'v1'
XRay.recorder.annotations.update k2: 'v2', k3: 3

# Add metadata to the current active entity
XRay.recorder.[:k1] = 'v1' # add to default namespace
XRay.recorder.(namespace: :my_ns).update k2: 'v2'

XRay.recorder.sampled? do
  XRay.recorder..update my_meta # expensive metadata generation here


require 'aws-xray-sdk'

XRay.recorder.capture('name_for_subsegment') do |subsegment|
  resp = myfunc()
  subsegment.annotations.update k1: 'v1'

# Manually apply the parent segment for the captured subsegment
XRay.recorder.capture('name_for_subsegment', segment: my_segment) do |subsegment|

Thread Injection

require 'aws-xray-sdk'

XRay.recorder.configure({patch: %I[net_http]})

uri = URI.parse('')
# Get the active entity from current call context
entity = XRay.recorder.current_entity

workers = (0...3).map do do
      # set X-Ray context for this thread
      XRay.recorder.inject_context entity do
        http =, uri.port)
    rescue ThreadError

Start a custom segment/subsegment

require 'aws-xray-sdk'

# Start a segment
segment = XRay.recorder.begin_segment 'my_service'
# Start a subsegment
subsegment = XRay.recorder.begin_subsegment 'outbound_call', namespace: 'remote'

# Add metadata or annotation here if necessary
my_annotations = {
  k1: 'v1',
  k2: 1024
segment.annotations.update my_annotations

# Add metadata to default namespace
subsegment.[:k1] = 'v1'

# Set user for the segment (subsegment is not supported)
segment.user = 'my_name'

# End segment/subsegment


The AWS X-Ray SDK for Ruby is licensed under the Apache 2.0 License. See LICENSE and NOTICE for more information.