Documentation forSolarWinds Observability SaaS

Ruby trace context in logs

This topic applies to version 6 of the Ruby Library. If you are using an earlier version, see Ruby trace context in logs (legacy).

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=00 resource.service.name=service_name to the end of each logged line.

Application logs for your service entity are not included in the data sent by APM libraries. Configure your server or application to send application logs to SolarWinds Observability SaaS. See Add logs from services.

If trace context is added to logs and those logs are sent to SolarWinds Observability SaaS, use the Traces Explorer to see the logs associated with a traced transaction.

Configuration

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

:never (Default.) Never include the Trace ID.
:sampled Include the Trace ID only 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 trace_id=00000000000000000000000000000000 span_id=0000000000000000 trace_flags=00 resource.service.name=service_name

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:

Rails.application.configure do
  # lograge
  config.lograge.enabled = true
  config.lograge.custom_options = lambda do |event|
    SolarWindsAPM::API.current_trace_info.hash_for_log
  end
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.