Skip to content

Commit

Permalink
non-billing mode spec
Browse files Browse the repository at this point in the history
  • Loading branch information
vpellan committed Dec 2, 2024
1 parent 8ec7c4d commit 5508805
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 49 deletions.
40 changes: 0 additions & 40 deletions spec/datadog/appsec/utils/trace_operation_spec.rb

This file was deleted.

11 changes: 11 additions & 0 deletions spec/datadog/tracing/contrib/ethon/easy_patch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions spec/datadog/tracing/contrib/excon/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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') }

Expand Down
17 changes: 17 additions & 0 deletions spec/datadog/tracing/contrib/faraday/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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' }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
20 changes: 11 additions & 9 deletions spec/datadog/tracing/contrib/http/circuit_breaker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/datadog/tracing/contrib/httpclient/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/datadog/tracing/contrib/httprb/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/datadog/tracing/contrib/rest_client/request_patch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/datadog/tracing/contrib/sidekiq/distributed_tracing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions spec/datadog/tracing/trace_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5508805

Please sign in to comment.