diff --git a/docs/GettingStarted.md b/docs/GettingStarted.md index e21358043a..84007059be 100644 --- a/docs/GettingStarted.md +++ b/docs/GettingStarted.md @@ -1304,7 +1304,7 @@ client.query("SELECT * FROM users WHERE group='x'") | `enabled` | `DD_TRACE_MYSQL2_ENABLED` | `Bool` | Whether the integration should create spans. | `true` | | `service_name` | `DD_TRACE_MYSQL2_SERVICE_NAME` | `String` | Name of application running the `mysql2` instrumentation. May be overridden by `global_default_service_name`. [See _Additional Configuration_ for more details](#additional-configuration) | `mysql2` | | `peer_service` | `DD_TRACE_MYSQL2_PEER_SERVICE` | `String` | Name of external service the application connects to | `nil` | -| `comment_propagation` | `DD_DBM_PROPAGATION_MODE` | `String` | SQL comment propagation mode for database monitoring.
(example: `disabled` \| `service`\| `full`).

**Important**: _Note that enabling SQL comment propagation results in potentially confidential data (service names) being stored in the databases which can then be accessed by other third parties that have been granted access to the database._ | `'disabled'` | +| `comment_propagation` | `DD_DBM_PROPAGATION_MODE` | `String` | SQL comment propagation mode for database monitoring.
(example: `disabled` \| `service`\| `dynamic_service` \| `full`).

**Important**: _Note that enabling SQL comment propagation results in potentially confidential data (service names) being stored in the databases which can then be accessed by other third parties that have been granted access to the database._ | `'disabled'` | | `append_comment` | | `Bool` | Appends the SQL comment propagation to the query string. Prepends the comment if `false`. For long query strings, the appended propagation comment might be truncated, causing loss of correlation between the query and trace. | `false` | | `on_error` | | `Proc` | Custom error handler invoked when MySQL raises an error. Provided `span` and `error` as arguments. Sets error on the span by default. Useful for ignoring errors that are handled at the application level. | `proc { \|span, error\| span.set_error(error) unless span.nil? }` | @@ -1407,7 +1407,7 @@ end | `enabled` | `DD_TRACE_PG_ENABLED` | `true` | Whether the integration should create spans. | `true` | | `service_name` | `DD_TRACE_PG_SERVICE_NAME` | `String` | Name of application running the `pg` instrumentation. May be overridden by `global_default_service_name`. [See _Additional Configuration_ for more details](#additional-configuration) | `pg` | | `peer_service` | `DD_TRACE_PG_PEER_SERVICE` | `String` | Name of external service the application connects to | `nil` | -| `comment_propagation` | `DD_DBM_PROPAGATION_MODE` | `String` | SQL comment propagation mode for database monitoring.
(example: `disabled` \| `service`\| `full`).

**Important**: _Note that enabling sql comment propagation results in potentially confidential data (service names) being stored in the databases which can then be accessed by other 3rd parties that have been granted access to the database._ | `'disabled'` | +| `comment_propagation` | `DD_DBM_PROPAGATION_MODE` | `String` | SQL comment propagation mode for database monitoring.
(example: `disabled` \| `service`\| `dynamic_service` \| `full`).

**Important**: _Note that enabling sql comment propagation results in potentially confidential data (service names) being stored in the databases which can then be accessed by other 3rd parties that have been granted access to the database._ | `'disabled'` | | `append_comment` | | `Bool` | Appends the SQL comment propagation to the query string. Prepends the comment if `false`. For long query strings, the appended propagation comment might be truncated, causing loss of correlation between the query and trace. | `false` | | `on_error` | | `Proc` | Custom error handler invoked when PG raises an error. Provided `span` and `error` as arguments. Sets error on the span by default. Useful for ignoring errors from Postgres that are handled at the application level. | `proc { \|span, error\| span.set_error(error) unless span.nil? }` | @@ -2135,7 +2135,7 @@ client.query("SELECT * FROM users WHERE group='x'") | `enabled` | `DD_TRACE_TRILOGY_ENABLED` | `Bool` | Whether the integration should create spans. | `true` | | `service_name` | `DD_TRACE_TRILOGY_SERVICE_NAME` | `String` | Name of application running the `trilogy` instrumentation. May be overridden by `global_default_service_name`. [See _Additional Configuration_ for more details](#additional-configuration) | `trilogy` | | `peer_service` | `DD_TRACE_TRILOGY_PEER_SERVICE` | `String` | Name of external service the application connects to | `nil` | -| `comment_propagation` | `DD_DBM_PROPAGATION_MODE` | `String` | SQL comment propagation mode for database monitoring.
(example: `disabled` \| `service`\| `full`).

**Important**: _Note that enabling SQL comment propagation results in potentially confidential data (service names) being stored in the databases which can then be accessed by other third parties that have been granted access to the database._ | `'disabled'` | +| `comment_propagation` | `DD_DBM_PROPAGATION_MODE` | `String` | SQL comment propagation mode for database monitoring.
(example: `disabled` \| `service`\| `dynamic_service` \| `full`).

**Important**: _Note that enabling SQL comment propagation results in potentially confidential data (service names) being stored in the databases which can then be accessed by other third parties that have been granted access to the database._ | `'disabled'` | | `append_comment` | | `Bool` | Appends the SQL comment propagation to the query string. Prepends the comment if `false`. For long query strings, the appended propagation comment might be truncated, causing loss of correlation between the query and trace. | `false` | | `on_error` | | `Proc` | Custom error handler invoked when MySQL raises an error. Provided `span` and `error` as arguments. Sets error on the span by default. Useful for ignoring errors that are handled at the application level. | `proc { \|span, error\| span.set_error(error) unless span.nil? }` | diff --git a/lib/datadog/tracing/contrib/propagation/sql_comment/ext.rb b/lib/datadog/tracing/contrib/propagation/sql_comment/ext.rb index cb8639fe60..1e31c0360e 100644 --- a/lib/datadog/tracing/contrib/propagation/sql_comment/ext.rb +++ b/lib/datadog/tracing/contrib/propagation/sql_comment/ext.rb @@ -18,6 +18,9 @@ module Ext # The `service` mode propagates service configuration SERVICE = 'service' + # The `dynamic_service` mode propagates service configuration + DBM service hash + DYNAMIC_SERVICE = 'dynamic_service' + # The `full` mode propagates service configuration + trace context FULL = 'full' diff --git a/lib/datadog/tracing/contrib/propagation/sql_comment/mode.rb b/lib/datadog/tracing/contrib/propagation/sql_comment/mode.rb index 5255a64aa8..81aaa97cf8 100644 --- a/lib/datadog/tracing/contrib/propagation/sql_comment/mode.rb +++ b/lib/datadog/tracing/contrib/propagation/sql_comment/mode.rb @@ -14,7 +14,7 @@ def enabled? end def service? - mode == Ext::SERVICE + mode == Ext::SERVICE || mode == Ext::DYNAMIC_SERVICE end def full? @@ -26,7 +26,7 @@ def append? end def inject_sql_basehash? - inject_sql_basehash + inject_sql_basehash || mode == Ext::DYNAMIC_SERVICE end end end diff --git a/sig/datadog/tracing/contrib/propagation/sql_comment/ext.rbs b/sig/datadog/tracing/contrib/propagation/sql_comment/ext.rbs index 7673a19f3f..b01628f0a9 100644 --- a/sig/datadog/tracing/contrib/propagation/sql_comment/ext.rbs +++ b/sig/datadog/tracing/contrib/propagation/sql_comment/ext.rbs @@ -11,6 +11,7 @@ module Datadog KEY_HOSTNAME: string KEY_PEER_SERVICE: string SERVICE: "service" + DYNAMIC_SERVICE: "dynamic_service" FULL: "full" TAG_DBM_TRACE_INJECTED: "_dd.dbm_trace_injected" TAG_PROPAGATED_HASH: "_dd.propagated_hash" diff --git a/spec/datadog/tracing/contrib/propagation/sql_comment/mode_spec.rb b/spec/datadog/tracing/contrib/propagation/sql_comment/mode_spec.rb index 4149b10fe8..e13319ef52 100644 --- a/spec/datadog/tracing/contrib/propagation/sql_comment/mode_spec.rb +++ b/spec/datadog/tracing/contrib/propagation/sql_comment/mode_spec.rb @@ -8,6 +8,7 @@ [ ['disabled', false], ['service', true], + ['dynamic_service', true], ['full', true], ['undefined', false] ].each do |string, result| @@ -22,6 +23,7 @@ [ ['disabled', false], ['service', true], + ['dynamic_service', true], ['full', false], ['undefined', false] ].each do |string, result| @@ -36,6 +38,7 @@ [ ['disabled', false], ['service', false], + ['dynamic_service', false], ['full', true], ['undefined', false] ].each do |string, result| @@ -60,11 +63,13 @@ describe '#inject_sql_basehash?' do [ - [false, false], - [true, true] - ].each do |value, result| - context "when given `#{value}`" do - subject { described_class.new('service', false, value).inject_sql_basehash? } + ['service', false, false], + ['service', true, true], + ['dynamic_service', false, true], + ['dynamic_service', true, true] + ].each do |mode, value, result| + context "when given mode `#{mode}` and inject_sql_basehash `#{value}`" do + subject { described_class.new(mode, false, value).inject_sql_basehash? } it { is_expected.to be result } end end diff --git a/spec/datadog/tracing/contrib/propagation/sql_comment_spec.rb b/spec/datadog/tracing/contrib/propagation/sql_comment_spec.rb index cf2f96154f..6067889e96 100644 --- a/spec/datadog/tracing/contrib/propagation/sql_comment_spec.rb +++ b/spec/datadog/tracing/contrib/propagation/sql_comment_spec.rb @@ -35,6 +35,22 @@ end end + context 'when `dynamic_service` mode' do + let(:mode) { 'dynamic_service' } + + before do + allow(Datadog.configuration).to receive(:experimental_propagate_process_tags_enabled).and_return(true) + allow(agent_info).to receive(:propagation_checksum).and_return(1234567890) + end + + it 'sets the propagated hash and does not mark full trace injection' do + described_class.annotate!(span_op, propagation_mode) + + expect(span_op.get_tag('_dd.propagated_hash')).to eq('1234567890') + expect(span_op.get_tag('_dd.dbm_trace_injected')).to be_nil + end + end + context 'when `full` mode' do let(:mode) { 'full' } @@ -265,6 +281,21 @@ end end + context 'when `dynamic_service` mode' do + let(:mode) { 'dynamic_service' } + + before do + Datadog.configuration.experimental_propagate_process_tags_enabled = true + allow(agent_info).to receive(:propagation_checksum).and_return(1234567890) + end + + it 'includes service metadata and the propagation hash in the SQL comment' do + is_expected.to eq( + "/*dde='dev',ddps='api',ddpv='1.2',ddsh='1234567890',dddbs='db_service'*/ #{sql_statement}" + ) + end + end + context 'when `full` mode' do let(:mode) { 'full' } let(:traceparent) { '00-00000000000000000000000000c0ffee-0000000000000bee-fe' } diff --git a/spec/datadog/tracing/contrib/sql_comment_propagation_examples.rb b/spec/datadog/tracing/contrib/sql_comment_propagation_examples.rb index 06df02abd5..931f397120 100644 --- a/spec/datadog/tracing/contrib/sql_comment_propagation_examples.rb +++ b/spec/datadog/tracing/contrib/sql_comment_propagation_examples.rb @@ -13,7 +13,15 @@ end end - %w[disabled service full].each do |mode| + context 'when ENV variable `DD_DBM_PROPAGATION_MODE` is set to dynamic_service' do + with_env 'DD_DBM_PROPAGATION_MODE' => 'dynamic_service' + + it_behaves_like 'propagates with sql comment', mode: 'dynamic_service', span_op_name: span_op_name, error: error do + let(:propagation_mode) { Datadog::Tracing::Contrib::Propagation::SqlComment::Mode.new('dynamic_service', append_comment, inject_sql_basehash) } + end + end + + %w[disabled service dynamic_service full].each do |mode| context "when `comment_propagation` is configured to #{mode}" do let(:configuration_options) do {comment_propagation: mode, service_name: service_name} @@ -183,4 +191,22 @@ expect(span.get_tag('_dd.propagated_hash')).to be_nil end end + + context 'when comment_propagation is dynamic_service and experimental_propagate_process_tags_enabled is true' do + before do + allow(Datadog.configuration).to receive(:experimental_propagate_process_tags_enabled).and_return(true) + end + + let(:configuration_options) do + {comment_propagation: 'dynamic_service', service_name: service_name} + end + + it 'injects base hash in the _dd.propagated_hash span tag' do + subject + + span = spans.find { |s| s.name == span_op_name } + expect(span).not_to be_nil + expect(span.get_tag('_dd.propagated_hash')).to eq('1234567890') + end + end end