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=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. See Add logs from services.
If trace context is added to logs and those logs are sent to SolarWinds Observability, use the Traces Explorer to see the logs associated with a traced transaction.
Trace context can be enabled in the Configuration File via the
SolarWindsAPM::Config[:log_traceId] setting, which takes the following options:
||(Default.) Never include the Trace ID.|
||Include the Trace ID only for sampled requests.|
||Include the Trace ID for all traced requests.|
||Always add a Trace ID. When there is no tracing context, the Trace ID will be
Automatic insertion works for any Logger that uses
ActiveSupport::Logger::SimpleFormatter < Logger::Formatter
ActiveSupport::TaggedLoggingwhich usually extends
Logger::Formatterwith its own formatter
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 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
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
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.