• Home
  • Extend
  • OpenTelemetry
  • Integration walk-throughs
  • Instrument your Ruby application with OpenTelemetry

Instrument your Ruby application with OpenTelemetry

This walkthrough shows how to add observability to your Ruby application using the OpenTelemetry Ruby libraries and tools.

FeatureSupported
Automatic InstrumentationYes
Automatic OneAgent IngestionNo

Prerequisites

  • Dynatrace version 1.222+
  • For tracing, W3C Trace Context is enabled
    1. From the Dynatrace menu, go to Settings > Preferences > OneAgent features.
    2. Turn on Send W3C Trace Context HTTP headers.

Get the Dynatrace access details

Determine the API base URL

For details on how to assemble the base OTLP endpoint URL, see Export with OTLP. The URL should end in /api/v2/otlp.

Get API access token

The access token for ingesting traces, logs, and metrics can be generated in your Dynatrace menu under Access tokens.

Export with OTLP has more details on the format and the necessary access scopes.

Choose how you want to instrument your application

OpenTelemetry supports on Ruby automatic and manual instrumentation, or a combination of both.

Which instrumentation should I choose?

It's a good idea to start with automatic instrumentation and add manual instrumentation if the automatic approach doesn't work or doesn't provide enough information.

Initialize OpenTelementry

  1. Add the following dependencies to your Gemfile.

    shell
    gem 'opentelemetry-sdk' gem 'opentelemetry-exporter-otlp'
  2. Add the following require declaration.

    ruby
    require 'opentelemetry/sdk' require 'opentelemetry/exporter/otlp'
  3. Add the init_opentelemetry function to startup code and provide the variables DT_API_URL and DT_API_TOKEN with the values for the Dynatrace URL and access token.

    ruby
    DT_API_URL = '' DT_API_TOKEN = '' def init_opentelemetry OpenTelemetry::SDK.configure do |c| c.service_name = 'ruby-quickstart' #TODO Replace with the name of your application c.service_version = '1.0.1' #TODO Replace with the version of your application for name in ["dt_metadata_e617c525669e072eebe3d0f08212e8f2.properties", "/var/lib/dynatrace/enrichment/dt_metadata.properties"] do begin c.resource = OpenTelemetry::SDK::Resources::Resource.create(Hash[*File.read(name.start_with?("/var") ? name : File.read(name)).split(/[=\n]+/)]) rescue end end c.add_span_processor( OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new( OpenTelemetry::Exporter::OTLP::Exporter.new( endpoint: DT_API_URL + "/v1/traces", headers: { "Authorization": "Api-Token " + DT_API_TOKEN } ) ) ) end end
  4. Call init_opentelemetry as early as possible during the startup of your application to ensure OpenTelemetry is initialized right from the start.

Automatically instrument your application optional

  1. Add the following dependency to your Gemfile.

    shell
    gem 'opentelemetry-instrumentation-all'
  2. Add the following require declaration.

    ruby
    require 'opentelemetry/instrumentation/all'
  3. Add the following line after c.service_version in the init_opentelemetry function.

    ruby
    c.use_all

Manually instrument your application optional

Add tracing

  1. To create new spans, we first need a tracer object.

    ruby
    tracer = OpenTelemetry.tracer_provider.tracer('my-tracer')
  2. With tracer, we can now use start_span() to create and start new spans.

    ruby
    span = tracer.start_span("Call to /myendpoint", kind: :internal) OpenTelemetry::Trace.with_span(span) do |span, context| span.set_attribute("http.method", "GET") span.set_attribute("net.protocol.version", "1.1") # TODO your code goes here end rescue Exception => e span&.record_exception(e) span&.status = OpenTelemetry::Trace::Status.error("Unhandled exception of type: #{e.class}") raise e ensure span&.finish

    In the above code, we:

    • Create a new span and name it "Call to /myendpoint"
    • Add two attributes, following the semantic naming convention, specific to the action of this span: information on the HTTP method and version
    • Add a TODO in place of the eventual business logic
    • Call the span's finish() method to complete the span (in an ensure block to ensure the method is called)

Collect metrics

The OpenTelemetry SDK for Ruby does not support metrics yet.

Connect logs

The OpenTelemetry SDK for Ruby does not support logs yet.

Ensure context propagation optional

Context propagation is particularly important when network calls (for example, REST) are involved.

If you are using automatic instrumentation and your networking libraries are covered by automatic instrumentation, this will be automatically taken care of by the instrumentation libraries. Otherwise, your code needs to take this into account.

Extracting the context when receiving a request

The following example uses the default propagator's extract() method to extract and recreate the context from the request, in parent_context. We can then pass that context to a start_span call to continue the previous trace with our spans.

ruby
parent_context = OpenTelemetry.propagation.extract( env, getter: OpenTelemetry::Common::Propagation.rack_env_getter ) span = tracer.start_span("hello world", with_parent: parent_context) OpenTelemetry::Trace.with_span(span) do |span, context| span.set_attribute("my-key-1", "my-value-1") # ... expansive query 'Hello World! :)' end ensure span&.finish end

Injecting the context when sending requests

The following example uses Ruby's standard Net:HTTP library to call an instrumented third-party service. To add the necessary trace headers, we use the default propagator's inject() method.

ruby
request = Net::HTTP::Get.new(uri.request_uri) OpenTelemetry.propagation.inject(request) response = http.request(request)

Configure data capture to meet privacy requirements optional

While Dynatrace automatically captures all OpenTelemetry resource and span attributes, only attribute values specified in the allowlist are stored and displayed in the Dynatrace web UI. This prevents accidental storage of personal data, so you can meet your privacy requirements and control the amount of monitoring data stored.

To view your custom span attributes, you need to allow them in the Dynatrace web UI first.

  • Span attributes: In the Dynatrace menu, go to Settings and select Server-side service monitoring > Span attributes.
  • Resource attributes: In the Dynatrace menu, go to Settings and select Server-side service monitoring > Resource attributes.

Verify data ingestion into Dynatrace

Once you have finished the instrumentation of your application, perform a couple of test actions to create and send demo traces, metrics, and logs and verify that they were correctly ingested into Dynatrace.

To do that for traces, in the Dynatrace menu, go to Distributed traces and select the Ingested traces tab. If you use OneAgent, select PurePaths instead.

Metrics and logs can be found under their respective entries at Observe and explore.

Related topics
  • Enrich ingested data with Dynatrace-specific dimensions

    Learn how to automatically enrich your telemetry data with Dynatrace-specific dimensions.