Instrument Ruby applications with OpenTelemetry
This guide shows how to instrument your Ruby application with OpenTelemetry and export the traces to Dynatrace.
To learn more about how Dynatrace works with OpenTelemetry, see Send data to Dynatrace with OpenTelemetry.
Prerequisites
- Dynatrace version 1.222+
- W3C Trace Context is enabled
- From the Dynatrace menu, go to Settings > Server-side service monitoring > Deep monitoring > Distributed tracing.
- Turn on Send W3C Trace Context HTTP headers.
Overview
To monitor your Ruby application with OpenTelemetry
Instrument your application
Send the data to Dynatrace
Configure context propagation
Restart your application and verify the data in Dynatrace
Configure data capture to meet privacy requirements
Instrument your application
You can use automatic instrumentation (provided by OpenTelemetry) or instrument manually.
Add the gems of the instrumentations for the frameworks you use to your gemfile and run bundle install
(see OpenTelemetry rubygems page)
gem 'opentelemetry-instrumentation-<USED_FRAMEWORK>' #TODO add your framework
To instrument manually, add the gem to the gemfile, run bundle install
and add the code snippet below to any Ruby method you want to monitor.
Set names for the tracer, the span, and add attributes as you see fit.
gem 'opentelemetry-api'
tracer = OpenTelemetry.tracer_provider.tracer('manual')
span = tracer.start_span("my-span", kind: :internal) #TODO Replace with the name of your span
OpenTelemetry::Trace.with_span(span) do |span, context|
span.set_attribute("my-key-1", "my-value-1") #TODO Add attributes
# your original 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
Send data to Dynatrace
To send data to Dynatrace, you need to add the gems to the gemfile, run bundle install
, and add and configure the code snippet below in your Ruby application code:
gem 'opentelemetry-sdk'
gem 'opentelemetry-exporter-otlp'
With OneAgent, you can simply point to a local endpoint without an authentication token to enable trace ingestion.
Additionally, make sure to set the following environment variable (without this, no spans will be exported):
OTEL_EXPORTER_OTLP_COMPRESSION=none
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
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
c.use_all
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: 'http://localhost:14499/otlp/v1/traces'
)
)
)
end
When using OneAgent, make sure to enable the public Extension Execution Controller in your Dynatrace Settings, otherwise no data will be sent.
In the Dynatrace menu, go to Settings > Preferences > Extension Execution Controller. The toggles Enable Extension Execution Controller and Enable local PIPE/HTTP metric and Log Ingest API should be active.
Without OneAgent, you need to set the endpoint to a specific URL containing your environment ID and configure an authentication token.
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
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
c.use_all
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: '<URL>', #TODO Replace <URL> to your SaaS/Managed-URL as mentioned in the next step
headers: {
"Authorization": "Api-Token <TOKEN>" #TODO Replace <TOKEN> with your API Token as mentioned in the next step
}
)
)
)
end
Lastly, you need to define the correct endpoint and token, to make sure your data arrives where it should be.
- To set the endpoint:
- Use your Environment ID to set the endpoint to which your app will send traces as follows:
- Dynatrace SaaS
https://{your-environment-id}.live.dynatrace.com/api/v2/otlp/v1/traces
- Dynatrace Managed
https://{your-domain}/e/{your-environment-id}/api/v2/otlp/v1/traces
- Dynatrace ActiveGate
https://{your-activegate-endpoint}/e/{your-environment-id}/api/v2/otlp/v1/traces
- You may need to include the port to your ActiveGate endpoint. For example:
https://{your-activegate-endpoint}:9999/e/{your-environment-id}/api/v2/otlp/v1/traces
- If you are running a containerized ActiveGate, you need to use the FQDN of it. For example:
https://{your-activegate-service-name}.dynatrace.svc.cluster.local/e/{your-environment-id}/api/v2/otlp/v1/traces
- You may need to include the port to your ActiveGate endpoint. For example:
- Dynatrace SaaS
- Replace
<URL>
in the code snippet above with your endpoint.
- Use your Environment ID to set the endpoint to which your app will send traces as follows:
- To create an authentication token
- In the Dynatrace menu, go to Access tokens and select Generate new token.
- Provide a Token name.
- In the Search scopes box, search for
Ingest OpenTelemetry traces
and select the checkbox. - Select Generate token.
- Select Copy to copy the token to your clipboard.
- Save the token in a safe place; you can't display it again.
- Replace
<TOKEN>
in the code snippet above with your token.
Configure context propagation optional
If you use manual instrumentation or a framework that is not supported by OpenTelemetry, you need to configure context propagation.
If your application receives a request or calls other applications, you need to configure context propagation to make sure the spans are linked together.
- Whenever receiving an incoming request, you need to extract the parent context and create the new span as a child of it.
- In our example, we have used a Sinatra framework. Other frameworks might need slight adaptations. Here is how:
parent_context = OpenTelemetry.propagation.extract(
request.env, #for example in WEBrick framework, `request` without `env` was sufficient.
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
- If your application calls another service, you need to ensure that you propagate the context, adding it to your outgoing request.
- In our example, we use a simple Net HTTP request:
request = Net::HTTP::Get.new(uri.request_uri)
OpenTelemetry.propagation.inject(request)
response = http.request(request)
Verify that the traces are ingested into Dynatrace
A few minutes after restarting your app, look for your spans:
- In the Dynatrace menu, go to Distributed traces and select the Ingested traces tab.
- If you use OneAgent, go to Distributed traces and select the PurePaths tab.
- Your spans will be part of an existing PurePath, if the root of your call is already monitored by OneAgent.
If your application does not receive any traffic, there will be no traces.
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 that's 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.