Skip to content

Commit

Permalink
Merge pull request #3025 from DataDog/tonycthsu/add-ddsource-to-log-c…
Browse files Browse the repository at this point in the history
…orrelation

Add `ddsource` to #to_log_format
  • Loading branch information
TonyCTHsu authored Aug 16, 2023
2 parents 02c1e09 + aaea975 commit 4761891
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 37 deletions.
18 changes: 1 addition & 17 deletions lib/datadog/tracing/contrib/lograge/instrumentation.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require_relative '../../../core/logging/ext'

module Datadog
module Tracing
module Contrib
Expand All @@ -23,21 +21,7 @@ def custom_options(event)
# Retrieves trace information for current thread
correlation = Tracing.correlation
# merge original lambda with datadog context

datadog_trace_log_hash = {
# Adds IDs as tags to log output
dd: {
# 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
},
ddsource: Core::Logging::Ext::DD_SOURCE
}

datadog_trace_log_hash.merge(original_custom_options)
correlation.to_h.merge(original_custom_options)
end
end
end
Expand Down
23 changes: 3 additions & 20 deletions lib/datadog/tracing/contrib/semantic_logger/instrumentation.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require_relative '../../../core/logging/ext'

module Datadog
module Tracing
module Contrib
Expand All @@ -23,25 +21,10 @@ def log(log, message = nil, progname = nil, &block)

# Retrieves trace information for current thread
correlation = Tracing.correlation
# merge original lambda with datadog context

datadog_trace_log_hash = {
# Adds IDs as tags to log output
dd: {
# 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
},
ddsource: Core::Logging::Ext::DD_SOURCE
}

# # if the user already has conflicting log_tags
# # we want them to clobber ours, because we should allow them to override
# # if needed.
log.named_tags = datadog_trace_log_hash.merge(original_named_tags)
# if the user already has conflicting log_tags
# we want them to clobber ours, because we should allow them to override if needed.
log.named_tags = correlation.to_h.merge(original_named_tags)
super(log, message, progname, &block)
end
end
Expand Down
20 changes: 20 additions & 0 deletions lib/datadog/tracing/correlation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative 'utils'
require_relative 'metadata/ext'
require_relative '../core/logging/ext'

module Datadog
module Tracing
Expand All @@ -14,6 +15,7 @@ class Identifier
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,
Expand Down Expand Up @@ -58,6 +60,23 @@ def initialize(
@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 = []
Expand All @@ -66,6 +85,7 @@ def to_log_format
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
Expand Down
73 changes: 73 additions & 0 deletions spec/datadog/tracing/correlation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,61 @@
end
end

describe '#to_h' do
context 'when given values' do
let(:trace_id) { Datadog::Tracing::Utils.next_id }
let(:span_id) { Datadog::Tracing::Utils.next_id }

it 'returns a formatted hash' do
identifier = described_class.new(
env: 'dev',
service: 'acme-api',
version: '1.0',
span_id: span_id,
trace_id: trace_id,
)

expect(identifier.to_h).to eq(
{
dd: {
env: 'dev',
service: 'acme-api',
version: '1.0',
trace_id: trace_id.to_s,
span_id: span_id.to_s
},
ddsource: 'ruby'
}
)
end
end

context 'when given `nil`' do
it 'returns a formatted hash with default values' do
identifier = described_class.new(
env: nil,
service: nil,
version: nil,
span_id: nil,
trace_id: nil,
)

expect(identifier.to_h).to eq(
{
dd: {
env: 'default-env',
service: 'default-service',
version: 'default-version',
trace_id: '0',
span_id: '0',
},
ddsource: 'ruby'
}
)
end
end
end

describe '#to_log_format' do
shared_examples_for 'a log format string' do
subject(:string) { identifier.to_log_format }
Expand All @@ -219,6 +274,24 @@
it 'doesn\'t have attributes without values' do
is_expected.to_not match(/.*=(?=\z|\s)/)
end

RSpec::Matchers.define :be_serialized_nested_hash do |expected|
match do |actual|
result = expected.each_with_object(String.new) do |(key, value), string|
if value.is_a? Hash
value.each_pair { |k, v| string << "#{key}.#{k}=#{v} " unless v.empty? }
else
string << "#{key}=#{value} "
end
end.strip!

actual == result
end
end

it 'serializes a nested hash' do
is_expected.to be_serialized_nested_hash(identifier.to_h)
end
end

# Expect string to contain the attribute, at the beginning/end of the string,
Expand Down

0 comments on commit 4761891

Please sign in to comment.