Skip to content

Instantly share code, notes, and snippets.

@bessey
Created July 28, 2021 16:31
Show Gist options
  • Save bessey/2d3d9335c7b62fad38339d676ef80c7d to your computer and use it in GitHub Desktop.
Save bessey/2d3d9335c7b62fad38339d676ef80c7d to your computer and use it in GitHub Desktop.
Ruby log formatter with JSON output and Datadog APM trace correlation injection
# frozen_string_literal: true
require 'json'
module Utils
# Ruby Log Formatter that serves two purposes:
# 1. Formats logs to JSON, so that they can be indexed by our log processor (currently Datadog)
# 2. Enriches logs with Datadog APM trace information, so logs can be associated back to APM traces
class TracedJSONLogFormatter
def initialize
@tracer = Datadog.tracer if defined?(Datadog) && Datadog.tracer
end
def call(severity, datetime, _progname, message)
message_and_attributes = message.is_a?(Hash) ? message : { message: message.to_s }
log_line = JSON.dump(
timestamp: datetime.to_s,
severity: severity.ljust(5).to_s,
**datadog_log_to_trace_correlation,
**message_and_attributes
)
"#{log_line}\n"
end
def datadog_log_to_trace_correlation
return {} unless @tracer
correlation = @tracer.active_correlation
{
ddsource: ['ruby'],
dd: correlation_info(correlation)
}
end
def correlation_info(correlation)
{
# To preserve precision during JSON serialization, use strings for large numbers
trace_id: correlation.trace_id.to_s,
span_id: correlation.span_id.to_s,
env: correlation.env.to_s,
service: correlation.service.to_s,
version: correlation.version.to_s
}
end
end
end
@bessey
Copy link
Author

bessey commented Jul 28, 2021

  # in production.rb
  logger = Logger.new(STDOUT)
  logger.formatter = Utils::TracedJSONLogFormatter.new
  config.logger = logger
  ActiveJob::Base.logger = logger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment