Distributed tracing instrumentation for Ruby on Rails applications based on Opentracing standard.
Add this line to your application's Gemfile:
gem 'opentracing_rails'
This gem is based on Opentracing standard but is not bound to any implementation of it. Therefore, you need to choose which implementation want to use.
Once you have chosen the backend, add the following lines into an initializer specifying it:
For instance, the CNCF project Jaeger and its ruby implementation jaeger-client-ruby.
# config/initializers/opentracing.rb
require 'jaeger/client'
OpenTracing.global_tracer = Jaeger::Client.build(
service_name: Rails.application.class.parent_name,
host: 'localhost',
port: 6831)
An alternative to Jaeger, there is Zipkin which has its ruby implementation zipking-ruby-opentracing.
# config/initializers/opentracing.rb
require 'zipkin/tracer'
OpenTracing.global_tracer = Zipkin::Tracer.build(
url: 'http://localhost:9411',
service_name: Rails.application.class.parent_name)
For running a Jaeger instance, you can use the official All-in-one docker image
they provide for testing purposes. Within this image, also, you can find running the Jaeger UI running on http://localhost:16686
.
The simplest way to start the all in one docker image is to use the pre-built image published to DockerHub (a single command line).
docker run -d -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 -p5775:5775/udp -p6831:6831/udp -p6832:6832/udp \
-p5778:5778 -p16686:16686 -p14268:14268 -p9411:9411 jaegertracing/all-in-one:latest
OpenTracing::Rails defines a current_span
method which tells the whole trace information collected until the moment.
TODO: more info, if needed. maybe classname?
TODO: section aim description
conn = Faraday.new(url: 'http://localhost:3001/') do |faraday|
faraday.use Faraday::Tracer, span: current_span
faraday.request :url_encoded
faraday.adapter Faraday.default_adapter
end
p conn.get('/').body
TODO: explain how to create a new span inside the current trace. smth like that:
span = @tracer.start_span('interesting_transaction,
child_of: parent_span,
tags: {
'component' => 'faraday',
'span.kind' => 'client',
'http.method' => 'GET'
'http.url' => 'internal'
}
)
# do all the interesting things here
span.finish
OpenTracing::Rails traces all ActiveRecord transactions by default, without any configuration. There are some options that can be set, though:
skip_schema_queries
: Avoid adding to the trace internal AR queries for schema, to not pollute the trace. Default: true.
- Avoid changing config.ru for adding
use Rack::Tracer
- Add ActiveRecord tracer within a middleware
- Add Sidekiq support
- Set railsy names to traces (for better understanding on Opentracing backend)