Skip to content

Commit

Permalink
Merge pull request #3486 from DataDog/tonycthsu/fast-correlation
Browse files Browse the repository at this point in the history
Improve log correlation performance
  • Loading branch information
TonyCTHsu authored Apr 12, 2024
2 parents 9ed085b + d9dc3dd commit 3fa6e98
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 55 deletions.
6 changes: 3 additions & 3 deletions gemfiles/ruby_2.5_rails4_mysql2.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions gemfiles/ruby_2.5_rails4_postgres.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions gemfiles/ruby_2.5_rails4_postgres_redis.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions gemfiles/ruby_2.5_rails4_postgres_sidekiq.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions gemfiles/ruby_2.5_rails4_semantic_logger.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions gemfiles/ruby_3.1_multi_rack_app.gemfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions lib/datadog/tracing/correlation.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require_relative 'utils'
require_relative 'metadata/ext'
require_relative '../core/logging/ext'

module Datadog
Expand Down Expand Up @@ -36,11 +35,11 @@ def initialize(
version: nil
)
# Dup and freeze strings so they aren't modified by reference.
@env = Core::Utils::SafeDup.frozen_dup(env || Datadog.configuration.env)
@service = Core::Utils::SafeDup.frozen_dup(service || Datadog.configuration.service)
@env = env || Datadog.configuration.env
@service = service || Datadog.configuration.service
@span_id = (span_id || 0).to_s
@trace_id = trace_id || 0
@version = Core::Utils::SafeDup.frozen_dup(version || Datadog.configuration.version)
@version = version || Datadog.configuration.version
end

def to_h
Expand Down
12 changes: 12 additions & 0 deletions lib/datadog/tracing/trace_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_relative 'sampling/ext'
require_relative 'span_operation'
require_relative 'trace_digest'
require_relative 'correlation'
require_relative 'trace_segment'
require_relative 'utils'

Expand Down Expand Up @@ -306,6 +307,17 @@ def to_digest
).freeze
end

def to_correlation
# Resolve current span ID
span_id = @active_span && @active_span.id
span_id ||= @parent_span_id unless finished?

Correlation::Identifier.new(
trace_id: @id,
span_id: span_id
)
end

# Returns a copy of this trace suitable for forks (w/o spans.)
# Used for continuation of traces across forks.
def fork_clone
Expand Down
7 changes: 4 additions & 3 deletions lib/datadog/tracing/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,10 @@ def active_span(key = nil)
# @return [Datadog::Tracing::Correlation::Identifier] correlation object
def active_correlation(key = nil)
trace = active_trace(key)
Correlation.identifier_from_digest(
trace && trace.to_digest
)

return Datadog::Tracing::Correlation::Identifier.new unless trace

trace.to_correlation
end

# Setup a new trace to continue from where another
Expand Down
46 changes: 16 additions & 30 deletions spec/datadog/tracing/correlation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@
let(:env) { 'dev' }
let(:service) { 'acme-api' }
let(:span_id) { Datadog::Tracing::Utils.next_id }
let(:span_name) { 'active_record.sql' }
let(:span_resource) { 'SELECT * FROM users;' }
let(:span_service) { 'acme-mysql' }
let(:span_type) { 'db' }
let(:trace_id) { Datadog::Tracing::Utils::TraceId.next_id }
let(:trace_name) { 'rack.request' }
let(:trace_resource) { 'GET /users' }
let(:trace_service) { 'acme-api' }
let(:version) { '0.1' }
end

Expand All @@ -52,10 +45,10 @@
)
end

it 'has frozen copies of strings' do
expect(identifier.env).to be_a_frozen_copy_of(default_env)
expect(identifier.service).to be_a_frozen_copy_of(default_service)
expect(identifier.version).to be_a_frozen_copy_of(default_version)
it 'has strings' do
expect(identifier.env).to eq(default_env)
expect(identifier.service).to eq(default_service)
expect(identifier.version).to eq(default_version)
end
end

Expand All @@ -66,14 +59,7 @@
instance_double(
Datadog::Tracing::TraceDigest,
span_id: span_id,
span_name: span_name,
span_resource: span_resource,
span_service: span_service,
span_type: span_type,
trace_id: trace_id,
trace_name: trace_name,
trace_resource: trace_resource,
trace_service: trace_service
)
end

Expand All @@ -87,10 +73,10 @@
)
end

it 'has frozen copies of strings' do
expect(identifier.env).to be_a_frozen_copy_of(default_env)
expect(identifier.service).to be_a_frozen_copy_of(default_service)
expect(identifier.version).to be_a_frozen_copy_of(default_version)
it 'has strings' do
expect(identifier.env).to eq(default_env)
expect(identifier.service).to eq(default_service)
expect(identifier.version).to eq(default_version)
end
end
end
Expand All @@ -110,10 +96,10 @@
)
end

it 'has frozen copies of strings' do
expect(identifier.env).to be_a_frozen_copy_of(default_env)
expect(identifier.service).to be_a_frozen_copy_of(default_service)
expect(identifier.version).to be_a_frozen_copy_of(default_version)
it 'has strings' do
expect(identifier.env).to eq(default_env)
expect(identifier.service).to eq(default_service)
expect(identifier.version).to eq(default_version)
end
end

Expand All @@ -140,10 +126,10 @@
)
end

it 'has frozen copies of strings' do
expect(identifier.env).to be_a_frozen_copy_of(env)
expect(identifier.service).to be_a_frozen_copy_of(service)
expect(identifier.version).to be_a_frozen_copy_of(version)
it 'has strings' do
expect(identifier.env).to eq(env)
expect(identifier.service).to eq(service)
expect(identifier.version).to eq(version)
end
end
end
Expand Down
Loading

0 comments on commit 3fa6e98

Please sign in to comment.