OpenTelemetry Sinatra monitoring

Vladimir Mihailenco
October 25, 2024
2 min read

Sinatra is a lightweight web application framework for Ruby. It is designed to provide a simple and flexible way to build web applications. While Sinatra itself does not have built-in support for OpenTelemetry, you can integrate OpenTelemetry instrumentation into your Sinatra application to collect observability data.

What is OpenTelemetry?

OpenTelemetry is an observability framework – an API, SDK, and tools designed to aid in the generation and collection of application telemetry data such as metrics, logs, and distributed traces. It provides a vendor-neutral way to instrument applications and export telemetry data to various observability backends.

OpenTelemetry supports multiple programming languages and platforms, making it suitable for a wide range of applications and environments.

OpenTelemetry enables developers to instrument their code and collect telemetry data, which can then be exported to various OpenTelemetry backends or observability platforms for analysis and visualization. The OpenTelemetry Collector serves as an intermediary layer that can receive, process, and export telemetry data with advanced features like tail sampling and metric aggregation.

Sinatra instrumentation

To instrument Sinatra app, you need a corresponding Sinatra OpenTelemetry instrumentation:

shell
gem install opentelemetry-instrumentation-sinatra

If you use bundler, add opentelemetry-instrumentation-sinatra to your Gemfile.

Getting started

To get started, call use with the name of the instrumentation:

ruby
require 'uptrace'
require 'opentelemetry-instrumentation-sinatra'

Uptrace.configure_opentelemetry(dsn: '<FIXME>') do |c|
  c.use 'OpenTelemetry::Instrumentation::Sinatra
end

Alternatively, you can call use_all to install all available instrumentations:

ruby
require 'uptrace'
require 'opentelemetry-instrumentation-sinatra'

Uptrace.configure_opentelemetry(dsn: '<FIXME>') do |c|
  c.use_all
end

What is Uptrace?

Uptrace is a OpenTelemetry APM that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues. For Ruby instrumentation, see the OpenTelemetry Ruby guide and compare with top APM tools.

Uptrace Overview

Uptrace comes with an intuitive query builder, rich dashboards, alerting rules with notifications, and integrations for most languages and frameworks.

Uptrace can process billions of spans and metrics on a single server and allows you to monitor your applications at 10x lower cost.

In just a few minutes, you can try Uptrace by visiting the cloud demo (no login required) or running it locally with Docker. The source code is available on GitHub.

What's next?

Next, you can instrument your Sinatra application code to create spans and record telemetry data. You can use the OpenTelemetry Ruby Tracing API to create spans around specific operations or code blocks you want to track. For example, you can create a span around a specific route or a database query.

For full-featured Ruby applications with built-in observability, explore Rails instrumentation with comprehensive auto-instrumentation support.