Ruby

Installation

Add this line to your application’s Gemfile:

  1. gem 'opentracing'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install opentracing

opentracing supports Ruby 2.0+.

Usage

Everyday consumers of this opentracing gem really only need to worry about a couple of key abstractions: the start_active_span and start_span methods, the Span and ScopeManager interfaces, and binding a Tracer at runtime. Here are code snippets demonstrating some important use cases.

Singleton initialization

As early as possible, call

  1. require 'opentracing'
  2. OpenTracing.global_tracer = MyTracerImplementation.new(...)

Where MyTracerImplementation is your tracer. For testing, you can use the provided OpenTracing::Tracer

Non-Singleton initialization

If you prefer direct control to singletons, manage ownership of the Tracer implementation explicitly.

Scopes and within-process propagation

For any thread, at most one Span may be “active”. Of course there may be many other Spans involved with the thread which are (a) started, (b) not finished, and yet © not “active”: perhaps they are waiting for I/O, blocked on a child Span, or otherwise off of the critical path.

It’s inconvenient to pass an active Span from function to function manually, so OpenTracing requires that every Tracer contains a ScopeManager that grants access to the active Span through a Scope. Any Span may be transferred to another callback or thread, but not Scope.

Accessing the active Span through Scope

  1. # Access to the active span is straightforward.
  2. span = OpenTracing.active_span
  3. if span
  4. span.set_tag('...', '...')
  5. end
  6. # or
  7. scope = OpenTracing.scope_manager.active
  8. if scope
  9. scope.span.set_tag('...', '...')
  10. end

Starting a new Span

The common case starts a Scope that’s automatically registered for intra-process propagation via ScopeManager.

Note that start_active_span('...') automatically finishes the span on Scope#close (start_active_span('...', finish_on_close: false) does not finish it, in contrast).

  1. # Automatic activation of the Span.
  2. # By default the active span will be finished when the returned scope is closed.
  3. # This can be controlled by passing finish_on_close parameter to
  4. # start_active_span
  5. scope = OpenTracing.start_active_span('operation_name')
  6. # Do things.
  7. # Block form of start_active_span
  8. # start_active_span optionally accepts a block. If a block is passed to
  9. # start_active_span it will yield the newly created scope. The scope will
  10. # be closed and its associated span will be finished unless
  11. # finish_on_close: false is passed to start_active_span.
  12. OpenTracing.start_active_span('operation_name') do |scope|
  13. # Do things.
  14. end
  15. # Manual activation of the Span.
  16. # Spans can be managed manually. This is equivalent to the more concise examples
  17. # above.
  18. span = OpenTracing.start_span('operation_name')
  19. OpenTracing.scope_manager.activate(span)
  20. scope = OpenTracing.scope_manager.active
  21. # Do things.
  22. # If there is an active Scope, it will act as the parent to any newly started
  23. # Span unless ignore_active_scope: true is passed to start_span or
  24. # start_active_span.
  25. # create a root span, ignoring the currently active scope (if it's set)
  26. scope = OpenTracing.start_active_span('operation_name', ignore_active_scope: true)
  27. # or
  28. span = OpenTracing.start_span('operation_name', ignore_active_scope: true)
  29. # It's possible to create a child Span given an existing parent Span by
  30. # using the child_of option.
  31. parent_scope = OpenTracing.start_active_span('parent_operation, ignore_active_scope: true)
  32. child_scope = OpenTracing.start_active_span('child_operation', child_of: parent_scope.span)
  33. # or
  34. parent_span = OpenTracing.start_span('parent_operation', ignore_active_scope: true)
  35. child_span = OpenTracing.start_span('child_operation', child_of: parent_span)

Serializing to the wire

Using Net::HTTP:

  1. client = Net::HTTP.new("myservice.com")
  2. req = Net::HTTP::Post.new("/")
  3. span = OpenTracing.start_span("my_span")
  4. OpenTracing.inject(span.context, OpenTracing::FORMAT_RACK, req)
  5. res = client.request(req)
  6. #...

Using Faraday middleware:

  1. class TraceMiddleware < Faraday::Middleware
  2. def call(env)
  3. span = OpenTracing.start_span("my_span")
  4. OpenTracing.inject(span.context, OpenTracing::FORMAT_RACK, env)
  5. @app.call(env).on_complete do
  6. span.finish
  7. end
  8. end
  9. end

Deserializing from the wire

The OpenTracing Ruby gem provides a specific Rack header extraction format, since most Ruby web servers get their HTTP Headers from Rack. Keep in mind that Rack automatically uppercases all headers and replaces dashes with underscores. This means that if you use dashes and underscores and case-sensitive baggage, it will not be possible to discern once Rack has processed it.

  1. class MyRackApp
  2. def call(env)
  3. extracted_ctx = @tracer.extract(OpenTracing::FORMAT_RACK, env)
  4. span = @tracer.start_span("my_app", child_of: extracted_ctx)
  5. span.finish
  6. [200, {}, ["hello"]]
  7. end
  8. end