This gist demonstrates how to augment a Ruby application to use an experimental implementation of OpenTelemetry Logs for Ruby.
PRs with source code for the experimental gems can be found at:
The branches used in the example Gemfile are experimental and subject to change.
Though there's a working solution in these branches, we want to get the code reviewed, approved, and merged to the
opentelemetry-ruby and opentelemetry-ruby-contrib repositories. Once we're at that stage, opentelemetry-ruby can
release experimental versions of opentelemetry-logs-api
, opentelemetry-logs-sdk
, and opentelemetry-exporter-otlp-logs
.
I try to keep the branches up to date with the latest code on main
, so you shouldn't miss any new features or fixes.
In addition, I'll do my best to keep them functioning. If something's broken or you have some feedback, please comment
on the PRs linked above.
You can find a demo using a Rails 7.0 application here: https://github.com/kaylareopelle/otel_ruby_logs_demo
To start using experimental OpenTelemetry logs for Ruby in your application, you'll need to update two files in your application:
Replace your current installation of opentelemetry-sdk
, opentelemetry-instrumentation-all
, and opentelemetry-exporter-otlp
with the gems in the example Gemfile.
Even if you're not explicitly installing opentelemetry-api
and some of the other gems in this Gemfile, you'll need to do so in order to get all the pieces to work.
If you're not using opentelemetry-instrumentation-all
, just add opentelemetry-instrumentation-logger
to your Gemfile.
Require the gems needed for logs to work and adjust the exporter settings to suit your needs. You can export via OTLP or via the console.
The example is set up to export to the console. ENV['OTEL_LOGS_EXPORTER']
can be set to 'otlp'
, 'console'
, or 'otlp,console'
.
Your settings for logs export should not conflict with settings for trace exports.
The example opentelemetry.rb
file will export logs to the console. You should see something like this:
#<struct OpenTelemetry::SDK::Logs::LogRecordData
timestamp=2024-04-19 15:06:50.118496 -0700,
observed_timestamp=2024-04-19 15:06:50.118496 -0700,
trace_id="\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
span_id="\x00\x00\x00\x00\x00\x00\x00\x00",
trace_flags=#<OpenTelemetry::Trace::TraceFlags:0x0000000106a73580 @flags=0>,
severity_text="INFO",
severity_number=9,
body="Instrumentation: OpenTelemetry::Instrumentation::Logger was successfully installed with the following options {}",
resource=
#<OpenTelemetry::SDK::Resources::Resource:0x0000000107b9c3c0
@attributes=
{"service.name"=>"Logs Demo - OTel Ruby Agent",
"process.pid"=>91454,
"process.command"=>"bin/rails",
"process.runtime.name"=>"ruby",
"process.runtime.version"=>"3.2.2",
"process.runtime.description"=>"ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-darwin22]",
"telemetry.sdk.name"=>"opentelemetry",
"telemetry.sdk.language"=>"ruby",
"telemetry.sdk.version"=>"1.4.1"}>,
instrumentation_scope=#<struct OpenTelemetry::SDK::InstrumentationScope name="opentelemetry-instrumentation-logger", version="0.0.0">,
attributes=nil,
total_recorded_attributes=0>
The Ruby Logger bridge/instrumentation should automatically bridge any logs that pass through Ruby's built-in Logger library. The Logger library is leveraged by many Ruby gems, including Rails. The instrumentation is not currently compatible with Rails 7.1+. This example does not provide a bridge for lograge, semantic_logger, logstash, or other logging gems.
The example opentelemetry.rb
file also instantiates an app-specific logger provider, APP_LOGGER
.
This can be used to emit your own OTel logs without the bridge.