Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sql comment propagation full mode when tracing is disabled #2866

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/datadog/tracing/contrib/propagation/sql_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ def self.prepend_comment(sql, span_op, trace_op, mode)
}

if mode.full?
tags[Ext::KEY_TRACEPARENT] =
Tracing::Distributed::TraceContext.new(fetcher: nil).send(:build_traceparent, trace_op.to_digest)
# When tracing is disabled, trace_operation is a dummy object that does not contain data to build traceparent
if datadog_configuration.tracing.enabled
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Propagation could happen even if tracing is disabled?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting.

But if tracing is disabled, do we call the tracing library for instrumenting MySQL and PG?

It feels weird to see the places prepend_comment is called and have tracing disabled.

propagated_sql_statement = Contrib::Propagation::SqlComment.prepend_comment(

sql = Contrib::Propagation::SqlComment.prepend_comment(sql, span, trace_op, propagation_mode)

I would assume that if tracing is disabled, we wouldn't even instrument MySQL and PG. Is that not correct?

Copy link
Contributor Author

@TonyCTHsu TonyCTHsu May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GustavoCaso , when tracing is disabled, we are still instrumenting application, but the trace operation and span operation are dummy data to comply with our public API for user using manual instrumentation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we return a dummy trace and span operation when tracing is disabled, we should make them behave like a dummy object that responds to the same methods as a regular trace or span operation. Those methods must probably be no-op. Basically, create an object duck type like a span or trace, rather than check if tracing is enabled through the codebase.

That change would be a more involved one, that should probably be tackled on a separate PR.

I will approve the PR, but I want to make sure that we try to tackle this in the future in a more sustainable way.

tags[Ext::KEY_TRACEPARENT] =
Tracing::Distributed::TraceContext.new(fetcher: nil).send(:build_traceparent, trace_op.to_digest)
else
Datadog.logger.warn(
'Sql comment propagation with `full` mode is aborted, because tracing is disabled. '\
'Please set `Datadog.configuration.tracing.enabled = true` to continue.'
)
end
end

"#{Comment.new(tags)} #{sql}"
Expand Down
114 changes: 76 additions & 38 deletions spec/datadog/tracing/contrib/propagation/sql_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,59 +45,97 @@
without_warnings { Datadog.configuration.reset! }
end

before do
Datadog.configure do |c|
c.env = 'production'
c.service = "Traders' Joe"
c.version = '1.0.0'
end
end

let(:sql_statement) { 'SELECT 1' }

let(:span_op) { double(service: 'database_service') }
let(:trace_op) do
double(
to_digest: Datadog::Tracing::TraceDigest.new(
trace_id: 0xC0FFEE,
span_id: 0xBEE,
trace_flags: 0xFE
context 'when tracing is enabled' do
before do
Datadog.configure do |c|
c.env = 'production'
c.service = "Traders' Joe"
c.version = '1.0.0'
end
end

let(:span_op) { double(service: 'database_service') }
let(:trace_op) do
double(
to_digest: Datadog::Tracing::TraceDigest.new(
trace_id: 0xC0FFEE,
span_id: 0xBEE,
trace_flags: 0xFE
)
)
)
end
end

subject { described_class.prepend_comment(sql_statement, span_op, trace_op, propagation_mode) }
subject { described_class.prepend_comment(sql_statement, span_op, trace_op, propagation_mode) }

context 'when `disabled` mode' do
let(:mode) { 'disabled' }
context 'when `disabled` mode' do
let(:mode) { 'disabled' }

it { is_expected.to eq(sql_statement) }
end
it { is_expected.to eq(sql_statement) }
end

context 'when `service` mode' do
let(:mode) { 'service' }
context 'when `service` mode' do
let(:mode) { 'service' }

it do
is_expected.to eq(
"/*dddbs='database_service',dde='production',ddps='Traders%27%20Joe',ddpv='1.0.0'*/ #{sql_statement}"
)
it do
is_expected.to eq(
"/*dddbs='database_service',dde='production',ddps='Traders%27%20Joe',ddpv='1.0.0'*/ #{sql_statement}"
)
end
end

context 'when `full` mode' do
let(:mode) { 'full' }
let(:traceparent) { '00-00000000000000000000000000c0ffee-0000000000000bee-fe' }

it {
is_expected.to eq(
"/*dddbs='database_service',"\
"dde='production',"\
"ddps='Traders%27%20Joe',"\
"ddpv='1.0.0',"\
"traceparent='#{traceparent}'*/ "\
"#{sql_statement}"
)
}
end
end

context 'when `full` mode' do
describe 'when propagates with `full` mode but tracing is disabled ' do
before do
Datadog.configure do |c|
c.env = 'production'
c.service = "Traders' Joe"
c.version = '1.0.0'
c.tracing.enabled = false
end
end

let(:mode) { 'full' }
let(:traceparent) { '00-00000000000000000000000000c0ffee-0000000000000bee-fe' }

it {
subject(:dummy_propagation) do
result = nil

Datadog::Tracing.trace('dummy.sql') do |span_op, trace_op|
span_op.service = 'database_service'

result = described_class.prepend_comment(sql_statement, span_op, trace_op, propagation_mode)
end

result
end

it do
is_expected.to eq(
"/*dddbs='database_service',"\
"dde='production',"\
"ddps='Traders%27%20Joe',"\
"ddpv='1.0.0',"\
"traceparent='#{traceparent}'*/ "\
"#{sql_statement}"
"/*dddbs='database_service',dde='production',ddps='Traders%27%20Joe',ddpv='1.0.0'*/ #{sql_statement}"
)
}
end

it do
expect(Datadog.logger).to receive(:warn).with(/`full` mode is aborted, because tracing is disabled/)
dummy_propagation
end
end
end
end