Ruby OpenTelemetry auto-instrumentation
Obtaining telemetry data from applications written in Ruby has never been easier. Thanks to Opentelemetry-Ruby the instrumentation process is very simple. Enabling auto-instrumentation requires you to perform three steps, explained in detail below. First, installation of the required gems, which hold the OpenTelemetry SDK and library-specific instrumentation. Second, code changes required for instrumentation enable and lastly, the exporter configuration.
How to instrument your Ruby application?
There are a few simple steps to instrument the application and export the telemetry data by OpenTelemetry Protocol exporter - version 0.12.0.
-
Gems Installation
Installation of the packages listed below is required to apply the instrumentation and export telemetry data. All listed gems are located on RubyGems.org and they can be installed in two different ways:- Gem command
$ gem install opentelemetry-sdk -v 0.12.0
$ gem install opentelemetry-exporter-otlp -v 0.12.0 - Bundler, the packages have to be inserted into your gemfile and bundle install command has to be run
gem 'opentelemetry-sdk', '0.12.0'
gem 'opentelemetry-exporter-otlp', '0.12.0'
Installation of the gems above is mandatory. The next step is to install instrumentation packages corresponding to the libraries used in the application. A complete list of available plugins can be found here. There are two solutions:
- Installation of the specific packages - for example, if the application is a Sinatra HTTP server which is also performing some HTTP requests using Net HTTP package. To instrument all the incoming and outgoing requests corresponding instrumented packages have to be installed:
$ gem install opentelemetry-instrumentation-sinatra -v 0.12.0
$ gem install opentelemetry-instrumentation-net_http -v 0.12.0 - Installation of the “all in one” package - installing this package will install all available instrumentation packages
$ gem install opentelemetry-instrumentation-all -v 0.12.0
- Gem command
-
Required code changes
To enable instrumentation in the application and export the telemetry data it is enough to add the code below to the project. Two examples below present a specific package and an “all in one” instrumentation.- Specific package instrumentation - in this example only Sinatra and Net HTTP libraries will be instrumented.
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
Bundler.require
OpenTelemetry::SDK.configure do |c|
c.use 'OpenTelemetry::Instrumentation::Sinatra'
c.use 'OpenTelemetry::Instrumentation::Net::HTTP'
c.add_span_processor(
OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(
OpenTelemetry::Exporter::OTLP::Exporter.new()
)
)
end - "All in one" instrumentation - this configuration will instrument all available package:
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
Bundler.require
OpenTelemetry::SDK.configure do |c|
c.use_all
c.add_span_processor(
OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(
OpenTelemetry::Exporter::OTLP::Exporter.new()
)
)
end
- Specific package instrumentation - in this example only Sinatra and Net HTTP libraries will be instrumented.
-
Telemetry data exporter configuration
The final step is to configure the exporter host and service name. This can be done directly in the code or by environment variables. In this example, the exporter will be configured by environment variables.OTEL_EXPORTER_OTLP_SPAN_ENDPOINT=collection-sumologic-otelcol.sumologic:55681/v1/trace
- environment variable configures the endpoint where telemetry data will be sent.
In this example, the value of the variable points to the default Sumologic Kubernetes Collector. For Kubernetes environments see the available endpoints for a direct connection. For other environments see endpoints and protocols.
OTEL_EXPORTER_OTLP_INSECURE=true
- has to be set in case of lack of security certificate-
OTEL_RESOURCE_ATTRIBUTES=service.name=my-service
- configure the service name
-
Kubernetes metadata configuration
In scenarios when your application runs on Kuberenetes, it is important to insert an additional attribute to the span generated by your microservice that allows the OpenTelemetry collector to properly assign Kubernetes infrastructure metadata.
Thek8s.pod.ip
attribute contains the IP address of the pod and the simplest way to configure it in the Kubernetes deployment yaml file is to add the below code into thespec.template.spec.containers
section:
env:
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: OTEL_RESOURCE_ATTRIBUTES
value: "service.name=my-service,k8s.pod.ip=$(MY_POD_IP)"
- name: OTEL_EXPORTER_OTLP_SPAN_ENDPOINT
value: "collection-sumologic-otelcol.sumologic:55681/v1/trace"
- name: OTEL_EXPORTER_OTLP_INSECURE
value: "true"