Documentation forSolarWinds Observability

Ruby trace context in logs

When you add trace context to application logs, you can correlate the log messages from a single transaction, and if sampled, the log messages to the transaction trace detail.

The SolarWinds Observability Ruby Library can automatically add the trace context to logs. If enabled, it will add a string like trace_id=7435a9fe510ae4533414d425dadf4e18 span_id=49e60702469db05f trace_flags=01 to the end of each logged line.

Configuration

Trace context can be enabled in the Configuration File via the SolarWindsAPM::Config[:log_traceId] setting, which takes the following options:

:never (default)
:sampled only include the Trace ID for sampled requests
:traced include the Trace ID for all traced requests
:always always add a Trace ID. When there is no tracing context the Trace ID will be 0000000000000000000000000000000000000000-0

Automatic insertion

Automatic insertion works for any Logger that uses Logger::Formatter, including:

  • ActiveSupport::Logger which uses ActiveSupport::Logger::SimpleFormatter < Logger::Formatter
  • ActiveSupport::TaggedLogging which usually extends Logger::Formatter with its own formatter

The gems lumberjack and logging are also supported.

Using the SDK

If your logger is not supported for automatic insertion or you want to use finer control of trace context, you can use the Instrumentation SDK to add trace context to application logs. Below is an example for a popular logging library.

Lograge

Lograge is a formatter for Rails logs currently not supported for automatic insertion. Instead you can use its custom_options and our SDK to configure lograge to include the Trace ID:

Lograge.custom_options = lambda do |event|
 SolarWindsAPM::SDK.current_trace.hash_for_log
end

The hash_for_log SDK method respects the configuration settings for SolarWindsAPM::Config[:log_traceId]. For example: It will return an empty hash if the setting is :never

The scripts are not supported under any SolarWinds support program or service. The scripts are provided AS IS without warranty of any kind. SolarWinds further disclaims all warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The risk arising out of the use or performance of the scripts and documentation stays with you. In no event shall SolarWinds or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the scripts or documentation.