From 550880538f78981fd0f9a8d9983b3f53fa9d3cac Mon Sep 17 00:00:00 2001 From: Victor Pellan Date: Mon, 2 Dec 2024 14:42:59 +0100 Subject: [PATCH] non-billing mode spec --- .../appsec/utils/trace_operation_spec.rb | 40 ------------------- .../tracing/contrib/ethon/easy_patch_spec.rb | 11 +++++ .../contrib/excon/instrumentation_spec.rb | 25 ++++++++++++ .../contrib/faraday/middleware_spec.rb | 17 ++++++++ .../grpc/datadog_interceptor/client_spec.rb | 11 +++++ .../contrib/http/circuit_breaker_spec.rb | 20 +++++----- .../httpclient/instrumentation_spec.rb | 15 +++++++ .../contrib/httprb/instrumentation_spec.rb | 15 +++++++ .../contrib/rest_client/request_patch_spec.rb | 16 ++++++++ .../sidekiq/distributed_tracing_spec.rb | 19 +++++++++ spec/datadog/tracing/trace_operation_spec.rb | 29 ++++++++++++++ 11 files changed, 169 insertions(+), 49 deletions(-) delete mode 100644 spec/datadog/appsec/utils/trace_operation_spec.rb diff --git a/spec/datadog/appsec/utils/trace_operation_spec.rb b/spec/datadog/appsec/utils/trace_operation_spec.rb deleted file mode 100644 index 929adc10296..00000000000 --- a/spec/datadog/appsec/utils/trace_operation_spec.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'datadog/appsec/spec_helper' -require 'datadog/appsec/utils/trace_operation' - -RSpec.describe Datadog::AppSec::Utils::TraceOperation do - describe '#appsec_standalone_reject?' do - subject(:appsec_standalone_reject?) do - described_class.appsec_standalone_reject?(trace_op) - end - - let(:trace_op) { Datadog::Tracing::TraceOperation.new(**options) } - let(:options) { {} } - let(:tracing_apm_enabled) { true } - let(:distributed_appsec_event) { '0' } - - before do - allow(Datadog.configuration.tracing.apm).to receive(:enabled).and_return(tracing_apm_enabled) - trace_op.set_tag(Datadog::AppSec::Ext::TAG_DISTRIBUTED_APPSEC_EVENT, distributed_appsec_event) if trace_op - end - - it { is_expected.to be false } - - context 'when AppSec standalone is enabled' do - let(:tracing_apm_enabled) { false } - - it { is_expected.to be true } - - context 'without a trace' do - let(:trace_op) { nil } - - it { is_expected.to be true } - end - - context 'with a distributed AppSec event' do - let(:distributed_appsec_event) { '1' } - - it { is_expected.to be false } - end - end - end -end diff --git a/spec/datadog/tracing/contrib/ethon/easy_patch_spec.rb b/spec/datadog/tracing/contrib/ethon/easy_patch_spec.rb index f1ad802e935..07f00873871 100644 --- a/spec/datadog/tracing/contrib/ethon/easy_patch_spec.rb +++ b/spec/datadog/tracing/contrib/ethon/easy_patch_spec.rb @@ -115,6 +115,17 @@ it_behaves_like 'environment service name', 'DD_TRACE_ETHON_SERVICE_NAME' do let(:span) { span_op } end + + context 'with non-billing mode' do + before do + allow_any_instance_of(Datadog::Tracing::TraceOperation).to receive(:non_billing_reject?).and_return(true) + end + + it do + subject + expect(Datadog::Tracing.active_trace.sampling_priority).to eq(0) + end + end end describe '#complete' do diff --git a/spec/datadog/tracing/contrib/excon/instrumentation_spec.rb b/spec/datadog/tracing/contrib/excon/instrumentation_spec.rb index 411abc43d8e..5d1374791e3 100644 --- a/spec/datadog/tracing/contrib/excon/instrumentation_spec.rb +++ b/spec/datadog/tracing/contrib/excon/instrumentation_spec.rb @@ -364,6 +364,31 @@ end end + context 'with non-billing mode' do + subject!(:response) do + expect_any_instance_of(described_class).to receive(:request_call) + .and_wrap_original do |m, *args| + allow_any_instance_of(Datadog::Tracing::TraceOperation).to receive(:non_billing_reject?).and_return(true) + m.call(*args).tap do |datum| + # Assert request headers + span = datum[:datadog_span] + headers = datum[:headers] + expect(headers).to include( + 'x-datadog-trace-id' => low_order_trace_id(span.trace_id).to_s, + 'x-datadog-parent-id' => span.id.to_s, + 'x-datadog-sampling-priority' => '0' + ) + end + end + + connection.get(path: '/success') + end + + it do + subject + end + end + context 'global service name' do subject(:get) { connection.get(path: '/success') } diff --git a/spec/datadog/tracing/contrib/faraday/middleware_spec.rb b/spec/datadog/tracing/contrib/faraday/middleware_spec.rb index 808d4e8cd82..22097bdf1a0 100644 --- a/spec/datadog/tracing/contrib/faraday/middleware_spec.rb +++ b/spec/datadog/tracing/contrib/faraday/middleware_spec.rb @@ -28,10 +28,12 @@ let(:middleware_options) { {} } let(:configuration_options) { {} } let(:response_headers) { {} } + let(:non_billing_mode) { false } before do Datadog.configure do |c| c.tracing.instrument :faraday, configuration_options + c.tracing.apm.enabled = !non_billing_mode end end @@ -410,6 +412,21 @@ end end + context 'when non-billing mode is enabled' do + subject(:response) { client.get('/success') } + + let(:non_billing_mode) { true } + let(:headers) { response.env.request_headers } + + it do + expect(headers).to include( + 'x-datadog-trace-id' => low_order_trace_id(span.trace_id).to_s, + 'x-datadog-parent-id' => span.id.to_s, + 'x-datadog-sampling-priority' => '0' + ) + end + end + context 'global service name' do let(:service_name) { 'faraday-global' } diff --git a/spec/datadog/tracing/contrib/grpc/datadog_interceptor/client_spec.rb b/spec/datadog/tracing/contrib/grpc/datadog_interceptor/client_spec.rb index de8b3926be3..4047d5aef3b 100644 --- a/spec/datadog/tracing/contrib/grpc/datadog_interceptor/client_spec.rb +++ b/spec/datadog/tracing/contrib/grpc/datadog_interceptor/client_spec.rb @@ -17,10 +17,12 @@ let(:peer) { "#{host}:#{port}" } let(:host) { 'host.name' } let(:port) { 0 } + let(:non_billing_mode) { false } before do Datadog.configure do |c| c.tracing.instrument :grpc, configuration_options + c.tracing.apm.enabled = !non_billing_mode end end @@ -117,6 +119,15 @@ it 'injects distribution data in gRPC metadata' do expect(keywords[:metadata].keys).to include('x-datadog-trace-id', 'x-datadog-parent-id', 'x-datadog-tags') end + + context 'with non-billing mode' do + let(:non_billing_mode) { true } + + it { + expect(keywords[:metadata].keys).to include('x-datadog-sampling-priority') + expect(keywords[:metadata]['x-datadog-sampling-priority']).to eq('0') + } + end end end diff --git a/spec/datadog/tracing/contrib/http/circuit_breaker_spec.rb b/spec/datadog/tracing/contrib/http/circuit_breaker_spec.rb index d8cd3225b06..73923293695 100644 --- a/spec/datadog/tracing/contrib/http/circuit_breaker_spec.rb +++ b/spec/datadog/tracing/contrib/http/circuit_breaker_spec.rb @@ -93,19 +93,21 @@ end describe '#should_skip_distributed_tracing?' do - subject(:should_skip_distributed_tracing?) { circuit_breaker.should_skip_distributed_tracing?(client_config) } + subject(:should_skip_distributed_tracing?) do + circuit_breaker.should_skip_distributed_tracing?(client_config, active_trace) + end let(:client_config) { nil } let(:distributed_tracing) { true } - let(:tracing_apm_enabled) { false } + let(:tracing_apm_enabled) { true } let(:active_trace) { nil } - let(:distributed_appsec_event) { nil } + let(:non_billing_reject) { true } before do allow(Datadog.configuration.tracing[:http]).to receive(:[]).with(:distributed_tracing).and_return(distributed_tracing) allow(Datadog.configuration.tracing.apm).to receive(:enabled).and_return(tracing_apm_enabled) allow(Datadog::Tracing).to receive(:active_trace).and_return(active_trace) - allow(active_trace).to receive(:get_tag).with('_dd.p.appsec').and_return(distributed_appsec_event) if active_trace + allow(active_trace).to receive(:non_billing_reject?).and_return(non_billing_reject) if active_trace end context 'when distributed tracing is enabled' do @@ -118,7 +120,7 @@ it { is_expected.to be true } end - context 'when appsec standalone is enabled' do + context 'when non billing mode is enabled' do let(:tracing_apm_enabled) { false } context 'when there is no active trace' do @@ -128,15 +130,15 @@ context 'when there is an active trace' do let(:active_trace) { instance_double(Datadog::Tracing::TraceOperation) } - context 'when the active trace has no distributed appsec event' do + context 'when the active trace must be rejected' do it { is_expected.to be true } end - context 'when the active trace has a distributed appsec event' do - # This should act like standalone appsec is disabled, as it does not return in the + context 'when the active trace must not be rejected' do + # This should act like non-billing mode is disabled, as it does not return in the # `if Datadog.configuration.tracing.apm.enabled` block # so we're only testing the "no client config, distributed tracing enabled" case here - let(:distributed_appsec_event) { '1' } + let(:non_billing_reject) { false } it { is_expected.to be false } end diff --git a/spec/datadog/tracing/contrib/httpclient/instrumentation_spec.rb b/spec/datadog/tracing/contrib/httpclient/instrumentation_spec.rb index 4405748fbac..2a8d93e4715 100644 --- a/spec/datadog/tracing/contrib/httpclient/instrumentation_spec.rb +++ b/spec/datadog/tracing/contrib/httpclient/instrumentation_spec.rb @@ -233,6 +233,21 @@ it 'propogrates the trace id header' do expect(http_response.headers['X-Datadog-Trace-Id']).to eq(low_order_trace_id(span.trace_id).to_s) end + + context 'with non-billing mode' do + before do + Datadog.configure do |c| + c.tracing.apm.enabled = false + end + # This cannot happen in actual apps but we do this to + # verify that sampling priority is set to 0 through distributed tracing without mocking an agent + allow(Datadog::Tracing::Distributed::Helpers).to receive(:should_skip_distributed_tracing?).and_return(false) + end + + it 'propagates sampling priority with value 0' do + expect(response.headers['X-Datadog-Sampling-Priority']).to eq('0') + end + end end context 'distributed tracing disabled' do diff --git a/spec/datadog/tracing/contrib/httprb/instrumentation_spec.rb b/spec/datadog/tracing/contrib/httprb/instrumentation_spec.rb index 485c599fa53..183ecb9ccbd 100644 --- a/spec/datadog/tracing/contrib/httprb/instrumentation_spec.rb +++ b/spec/datadog/tracing/contrib/httprb/instrumentation_spec.rb @@ -236,6 +236,21 @@ it 'propagates the trace id header' do expect(http_response.headers['x-datadog-trace-id']).to eq(low_order_trace_id(span.trace_id).to_s) end + + context 'with non-billing mode' do + before do + Datadog.configure do |c| + c.tracing.apm.enabled = false + end + # This cannot happen in actual apps but we do this to + # verify that sampling priority is set to 0 without mocking an agent + allow(Datadog::Tracing::Distributed::Helpers).to receive(:should_skip_distributed_tracing?).and_return(false) + end + + it 'propagates sampling priority with value 0' do + expect(response.headers['X-Datadog-Sampling-Priority']).to eq('0') + end + end end context 'distributed tracing disabled' do diff --git a/spec/datadog/tracing/contrib/rest_client/request_patch_spec.rb b/spec/datadog/tracing/contrib/rest_client/request_patch_spec.rb index 889363cf201..4f00c3191d3 100644 --- a/spec/datadog/tracing/contrib/rest_client/request_patch_spec.rb +++ b/spec/datadog/tracing/contrib/rest_client/request_patch_spec.rb @@ -328,6 +328,22 @@ .to have_been_made end end + + context 'with non-billing mode' do + before do + Datadog.configure do |c| + c.tracing.apm.enabled = false + end + # This cannot happen in actual apps but we do this to + # verify that sampling priority is set to 0 through distributed tracing without mocking an agent + allow(Datadog::Tracing::Distributed::Helpers).to receive(:should_skip_distributed_tracing?).and_return(false) + end + + it 'propagates sampling priority with value 0' do + request + expect(a_request(:get, url).with(headers: { 'X-Datadog-Sampling-Priority' => '0' })).to have_been_made + end + end end context 'distributed tracing disabled' do diff --git a/spec/datadog/tracing/contrib/sidekiq/distributed_tracing_spec.rb b/spec/datadog/tracing/contrib/sidekiq/distributed_tracing_spec.rb index 15d7575585b..f8dc71e8456 100644 --- a/spec/datadog/tracing/contrib/sidekiq/distributed_tracing_spec.rb +++ b/spec/datadog/tracing/contrib/sidekiq/distributed_tracing_spec.rb @@ -63,6 +63,25 @@ def perform; end expect(job['x-datadog-tags']).to eq("_dd.p.dm=-0,_dd.p.tid=#{high_order_hex_trace_id(span.trace_id)}") expect(job).not_to include 'x-datadog-origin' end + + context 'with non-billing mode' do + before do + Datadog.configure do |c| + c.tracing.apm.enabled = false + end + # This cannot happen in actual apps but we do this to + # verify that sampling priority is set to 0 through distributed tracing without mocking an agent + allow(Datadog::Tracing::Distributed::Helpers).to receive(:should_skip_distributed_tracing?).and_return(false) + end + + it 'propagates sampling priority with value 0' do + EmptyWorker.perform_async + + job = EmptyWorker.jobs.first + + expect(job['x-datadog-sampling-priority']).to eq('0') + end + end end context 'when receiving' do diff --git a/spec/datadog/tracing/trace_operation_spec.rb b/spec/datadog/tracing/trace_operation_spec.rb index d168a99f73c..c2e4b46ffa8 100644 --- a/spec/datadog/tracing/trace_operation_spec.rb +++ b/spec/datadog/tracing/trace_operation_spec.rb @@ -2579,6 +2579,35 @@ def span end end + describe '#non_billing_reject?' do + subject(:non_billing_reject?) do + trace_op.non_billing_reject? + end + + let(:trace_op) { described_class.new(**options) } + let(:options) { { non_billing_enabled: !tracing_apm_enabled } } + let(:tracing_apm_enabled) { true } + let(:distributed_appsec_event) { '0' } + + before do + trace_op.set_tag(Datadog::AppSec::Ext::TAG_DISTRIBUTED_APPSEC_EVENT, distributed_appsec_event) + end + + it { is_expected.to be false } + + context 'when non-billing is enabled' do + let(:tracing_apm_enabled) { false } + + it { is_expected.to be true } + + context 'with a distributed AppSec event' do + let(:distributed_appsec_event) { '1' } + + it { is_expected.to be false } + end + end + end + describe 'integration tests' do context 'service_entry attributes' do context 'when service not given' do