lib/datadog/tracing/correlation.rb in ddtrace-1.13.1 vs lib/datadog/tracing/correlation.rb in ddtrace-1.14.0

- old
+ new

@@ -1,7 +1,8 @@ require_relative 'utils' require_relative 'metadata/ext' +require_relative '../core/logging/ext' module Datadog module Tracing # Contains behavior for managing correlations with tracing # e.g. Retrieve a correlation to the current trace for logging, etc. @@ -12,10 +13,11 @@ LOG_ATTR_ENV = 'dd.env'.freeze LOG_ATTR_SERVICE = 'dd.service'.freeze LOG_ATTR_SPAN_ID = 'dd.span_id'.freeze LOG_ATTR_TRACE_ID = 'dd.trace_id'.freeze LOG_ATTR_VERSION = 'dd.version'.freeze + LOG_ATTR_SOURCE = 'ddsource'.freeze attr_reader \ :env, :service, :span_id, @@ -56,17 +58,35 @@ @trace_resource = Core::Utils::SafeDup.frozen_dup(trace_resource) @trace_service = Core::Utils::SafeDup.frozen_dup(trace_service) @version = Core::Utils::SafeDup.frozen_dup(version || Datadog.configuration.version) end + def to_h + @to_h ||= { + # Adds IDs as tags to log output + dd: { + # To preserve precision during JSON serialization, use strings for large numbers + env: env.to_s, + service: service.to_s, + version: version.to_s, + trace_id: trace_id.to_s, + span_id: span_id.to_s + }, + ddsource: Core::Logging::Ext::DD_SOURCE + } + end + + # This method (#to_log_format) implements an algorithm by prefixing keys for nested values + # but the algorithm makes the constants implicit. Hence, we use it for validation during test. def to_log_format @log_format ||= begin attributes = [] attributes << "#{LOG_ATTR_ENV}=#{env}" unless env.nil? attributes << "#{LOG_ATTR_SERVICE}=#{service}" attributes << "#{LOG_ATTR_VERSION}=#{version}" unless version.nil? attributes << "#{LOG_ATTR_TRACE_ID}=#{trace_id}" attributes << "#{LOG_ATTR_SPAN_ID}=#{span_id}" + attributes << "#{LOG_ATTR_SOURCE}=#{Core::Logging::Ext::DD_SOURCE}" attributes.join(' ') end end # DEV-2.0: This public method was returning an Integer, but with 128 bit trace id it would return a String.