# OpenTelemetry Ruby distro for Uptrace

# Prerequisites

This tutorial assumes you have installed and configured OpenTelemetry SDK by following the Getting started guide.

# Installation

Add to Gemfile:

gem 'uptrace'
1

Or install the gem manually:

gem install uptrace
1

# Configuration

You configure Uptrace distribution using a DSN (Data Source Name, e.g. https://<token>@api.uptrace.dev/<project_id>) from the project settings page.

require 'uptrace'

# Copy your project DSN here or use UPTRACE_DSN env var.
Uptrace.configure_opentelemetry(dsn: '') do |c|
  # c is OpenTelemetry::SDK::Configurator
  c.service_name = 'myservice'
  c.service_version = '1.0.0'
end
1
2
3
4
5
6
7
8

You can also use environment variables to configure the client:

Env varDescription
UPTRACE_DSNA data source that is used to connect to uptrace.dev. For example, https://<key>@api.uptrace.dev/<project_id>.
OTEL_RESOURCE_ATTRIBUTESKey-value pairs to be used as resource attributes. For example, service.name=myservice,service.version=1.0.0.
OTEL_PROPAGATORSPropagators to be used as a comma separated list. The default is tracecontext,baggage.

# OpenTelemetry API

All the code below is also available as a runnable otel-apiopen in new window example.

# Creating a tracer

To start creating spans, you need a tracer. You create a tracer by specifying a tracer name (AKA instrumentation library name):

require 'opentelemetry'

tracer = OpenTelemetry.tracer_provider.tracer('my_app_or_gem', '0.1.0')
1
2
3

You can have as many tracers as you want. Use the tracer name to identify the library that produces the spans.

# Creating a span

Once you have a tracer, creating spans is easy:

# Create a span with name "operation-name" and kind="server".
tracer.in_span('operation-name', kind: OpenTelemetry::Trace::SpanKind::SERVER) do |span|
  do_some_work
end
1
2
3
4

Internally that does the following:

# Create a span.
span = tracer.start_span('operation-name', kind: OpenTelemetry::Trace::SpanKind::SERVER)

# Activate the span in the current context.
OpenTelemetry::Trace.with_span(span) do |span|
  do_some_work
end

# Finish the span when the operation is completed.
span.finish
1
2
3
4
5
6
7
8
9
10

# Adding span attributes

To record contextual information you annotate spans with attributes that carry information specific to the operation. For example, an HTTP endpoint may have such attributes as http.method = GET and http.route = /projects/:id.

# To avoid expensive computations, check that span is recording
# before setting any attributes.
if span.recording?
  span.set_attribute('http.method", 'GET')
  span.set_attribute('http.route", '/projects/:id')
end
1
2
3
4
5
6

You can name attributes as you want, but for common operations you should use semantic attributesopen in new window convention. It defines a list of common attribute keys with their meaning and possible values.

# Adding span events

You can annotate spans with events that have start time and arbitrary number of attributes. The main difference between events and spans is that events don't have end time (and therefore no duration).

Events usually represent exceptions, errors, logs, and messages (such as in RPC). But you can record custom events as well.

span.add_event(
  name: 'log',
  attributes: {
    'log.severity' => 'error',
    'log.message' => 'User not found',
    'enduser.id' => '123'
  }
)
1
2
3
4
5
6
7
8

# Recording exceptions

OpenTelemetry provides a shortcut to record an exception.

rescue Exception => e
  # Record the exception and update the span status.
  span.record_exception(e)
  span.status = OpenTelemetry::Trace::Status.new(
    OpenTelemetry::Trace::Status::ERROR,
    description: e.to_s
  )
end
1
2
3
4
5
6
7
8

# Trace Context and the active span

OpenTelemetry stores the current active span in a context and saves the context in a thread-local storage. You can nest contexts inside each other and OpenTelemetry will automatically activate the parent span context when you end the span.

tracer.in_span sets the active span for you, but you can also activate the span manually:

OpenTelemetry::Trace.with_span(main) do |span|
  do_some_work
end
1
2
3

To get the current span:

span = OpenTelemetry::Trace.current_span
1

# What is next?

By now, you should have a configured OpenTelemetry distribution and a solid grasp of OpenTelemetry API. Next, use that knowledge to instrument your code and check available instrumentationsopen in new window.