Created
July 28, 2021 16:31
-
-
Save bessey/2d3d9335c7b62fad38339d676ef80c7d to your computer and use it in GitHub Desktop.
Ruby log formatter with JSON output and Datadog APM trace correlation injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Author
bessey
commented
Jul 28, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment