Skip to content

Commit 5e43f4b

Browse files
committed
add example for x-ray sampling on rails
1 parent 410ce42 commit 5e43f4b

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# frozen_string_literal: true
2+
3+
# Copyright The OpenTelemetry Authors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
require 'bundler/inline'
8+
9+
gemfile(true) do
10+
source 'https://rubygems.org'
11+
12+
gem 'concurrent-ruby', '1.3.4'
13+
gem 'rails', '~> 7.0.4'
14+
gem 'puma'
15+
16+
gem 'opentelemetry-sdk'
17+
gem 'opentelemetry-instrumentation-rails'
18+
gem 'opentelemetry-sampler-xray', path: './../' # Use local version of the X-Ray Sampler
19+
# gem 'opentelemetry-sampler-xray' # Use RubyGems version of the X-Ray Sampler
20+
end
21+
22+
require "action_controller/railtie"
23+
require "action_mailer/railtie"
24+
require "rails/test_unit/railtie"
25+
26+
class App < Rails::Application
27+
config.root = __dir__
28+
config.consider_all_requests_local = true
29+
30+
routes.append do
31+
root to: 'welcome#index'
32+
get "/test" => 'welcome#test'
33+
end
34+
end
35+
36+
class WelcomeController < ActionController::Base
37+
def index
38+
render inline: 'Successfully called "/" endpoint'
39+
end
40+
41+
def test
42+
render inline: 'Successfully called "/test" endpoint'
43+
end
44+
end
45+
46+
ENV['OTEL_TRACES_EXPORTER'] = 'console'
47+
ENV['OTEL_SERVICE_NAME'] = 'xray-sampler-on-rails-service'
48+
OpenTelemetry::SDK.configure do |c|
49+
c.use_all({ 'OpenTelemetry::Instrumentation::ActiveRecord' => { enabled: false } })
50+
end
51+
52+
OpenTelemetry.tracer_provider.sampler = OpenTelemetry::Sampler::XRay::AWSXRayRemoteSampler.new(resource:OpenTelemetry::SDK::Resources::Resource.create({
53+
"service.name"=>"xray-sampler-on-rails-service"
54+
}))
55+
56+
App.initialize!
57+
58+
run App
59+
60+
#### Running and using the Sample App
61+
# To run this example run the `rackup` command with this file
62+
# Example: rackup trace_request_demonstration.ru
63+
# Navigate to http://localhost:9292/
64+
# Spans for any requests sampled by the X-Ray Sampler will appear in the console
65+
66+
#### Required configuration in the OpenTelemetry Collector
67+
# In order for sampling rules to be obtained from AWS X-Ray, the awsproxy extension
68+
# must be configured in the OpenTelemetry Collector, which will use your AWS credentials.
69+
# - https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/awsproxy#aws-proxy
70+
# Without the awsproxy extension, the X-Ray Sampler will use a fallback sampler
71+
# with a sampling strategy of "1 request/second, plus 5% of any additional requests"
72+
73+
#### Testing out configurable X-Ray Sampling Rules against the "service.name" resource attribute.
74+
# Create a new Sampling Rule with the following matching criteria in AWS CloudWatch Settings for X-Ray Traces.
75+
# - https://console.aws.amazon.com/cloudwatch/home#xray:settings/sampling-rules
76+
# Matching Criteria
77+
# ServiceName = xray-sampler-on-rails-service
78+
# ServiceType = *
79+
# Host = *
80+
# ResourceARN = *
81+
# HTTPMethod = *
82+
# URLPath = *
83+
# For the above matching criteria, try out the following settings to sample or not sample requests
84+
# - Limit to 0r/sec then 0 fixed rate
85+
# - Limit to 1r/sec then 0 fixed rate (May take 30 seconds for this setting to apply)
86+
# - Limit to 0r/sec then 100% fixed rate
87+
88+
#### Testing out configurable X-Ray Sampling Rules against the "/test" endpoint in this sample app.
89+
# Create a new Sampling Rule with the following matching criteria in AWS CloudWatch Settings for X-Ray Traces.
90+
# - https://console.aws.amazon.com/cloudwatch/home#xray:settings/sampling-rules
91+
# Matching Criteria
92+
# ServiceName = *
93+
# ServiceType = *
94+
# Host = *
95+
# ResourceARN = *
96+
# HTTPMethod = *
97+
# URLPath = /test
98+
# For the above matching criteria, try out the following settings to sample or not sample requests
99+
# - Limit to 0r/sec then 0 fixed rate
100+
# - Limit to 1r/sec then 0 fixed rate (May take 30 seconds for this setting to apply)
101+
# - Limit to 0r/sec then 100% fixed rate

0 commit comments

Comments
 (0)