Ruby OpenTracing (deprecated)

OpenTracing is deprecated. Instana support for OpenTracing is scheduled to be removed in June 2025.

OpenTracing provides vendor-neutral APIs and instrumentation for distributed tracing. The Instana Ruby gem implements these APIs and allows you to use the Instana gem with your OpenTracing calls. Existing applications that use the OpenTracing API or those applications that want to add support can easily integrate with this Ruby gem.

For more information about OpenTracing, see OpenTracing.

For best results when using OpenTracing, take note of the best practices that are outlined in this OpenTracing page.

To start, set the Instana tracer as the global tracer for OpenTracing:

require 'opentracing'
OpenTracing.global_tracer = ::Instana.tracer

Then, OpenTracing code can be run normally:

begin
  span = OpenTracing.start_span('job')
  # The code to be instrumented
  @id = User.find_by_name('john.smith')
  span.set_tag(:job_id, @id)
rescue => e
  span.set_tag(:error, e.message)
ensure
  span.finish
end

Example

The following is an over-simplified example of some code that calls the OpenTracing APIs.

# Note:  The instana gem automatically sets the Instana tracer
# to `OpenTracing.global_tracer`.  Once the gem is loaded, you can
# immediately start making OpenTracing calls.
#
require "opentracing"

entry_span = OpenTracing.start_span("HandMadeRackServer")

entry_span.set_tag(:'http.method', :get)
entry_span.set_tag(:'http.url', "/users")
entry_span.set_tag(:'span.kind', "entry")

intermediate_span = OpenTracing.start_span("myintermediate", :child_of => entry_span)
intermediate_span.finish()

db_span = OpenTracing.start_span('mydbspan', :child_of => entry_span)
db_span.set_tag(:'db.instance', "users")
db_span.set_tag(:'db.statement', "SELECT * FROM user_table")
db_span.set_tag(:'db.type', "mysql")
db_span.set_tag(:'db.user', "mysql_login")
db_span.set_tag(:'span.kind', "exit")
db_span.finish()

intermediate_span = OpenTracing.start_span("myintermediate", :child_of => entry_span)
intermediate_span.log("ALLOK", :message => "All seems ok")
intermediate_span.finish()

entry_span.set_tag(:'http.status_code', 200)
entry_span.finish()