Skip to content

Commit ff8ae5b

Browse files
authored
DEBUG-3573 Split telemetry tests into multiple files (#4685)
1 parent 6b36deb commit ff8ae5b

12 files changed

+527
-451
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require 'spec_helper'
2+
3+
require 'datadog/core/telemetry/event/app_client_configuration_change'
4+
5+
RSpec.describe Datadog::Core::Telemetry::Event::AppClientConfigurationChange do
6+
let(:id) { double('seq_id') }
7+
let(:event) { event_class.new }
8+
9+
subject(:payload) { event.payload }
10+
11+
let(:event_class) { described_class }
12+
let(:event) { event_class.new(changes, origin) }
13+
let(:changes) { { name => value } }
14+
let(:origin) { double('origin') }
15+
let(:name) { 'key' }
16+
let(:value) { 'value' }
17+
18+
before do
19+
allow_any_instance_of(Datadog::Core::Utils::Sequence).to receive(:next).and_return(id)
20+
end
21+
22+
it 'has a list of client configurations' do
23+
is_expected.to eq(
24+
configuration: [{
25+
name: name,
26+
value: value,
27+
origin: origin,
28+
seq_id: id
29+
}]
30+
)
31+
end
32+
33+
context 'with env_var state configuration' do
34+
before do
35+
Datadog.configure do |c|
36+
c.appsec.sca_enabled = false
37+
end
38+
end
39+
40+
it 'includes sca enablement configuration' do
41+
is_expected.to eq(
42+
configuration:
43+
[
44+
{ name: name, value: value, origin: origin, seq_id: id },
45+
{ name: 'appsec.sca_enabled', value: false, origin: 'code', seq_id: id }
46+
]
47+
)
48+
end
49+
end
50+
51+
it 'all events to be the same' do
52+
events = [
53+
event_class.new({ 'key' => 'value' }, 'origin'),
54+
event_class.new({ 'key' => 'value' }, 'origin'),
55+
]
56+
57+
expect(events.uniq).to have(1).item
58+
end
59+
60+
it 'all events to be different' do
61+
events = [
62+
event_class.new({ 'key' => 'value' }, 'origin'),
63+
event_class.new({ 'key' => 'value' }, 'origin2'),
64+
event_class.new({ 'key' => 'value2' }, 'origin'),
65+
event_class.new({ 'key2' => 'value' }, 'origin'),
66+
event_class.new({}, 'origin'),
67+
]
68+
69+
expect(events.uniq).to eq(events)
70+
end
71+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
3+
require 'datadog/core/telemetry/event/app_closing'
4+
5+
RSpec.describe Datadog::Core::Telemetry::Event::AppClosing do
6+
let(:id) { double('seq_id') }
7+
let(:event) { event_class.new }
8+
9+
subject(:payload) { event.payload }
10+
11+
let(:event_class) { described_class }
12+
it_behaves_like 'telemetry event with no attributes'
13+
14+
it 'has no payload' do
15+
is_expected.to eq({})
16+
end
17+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'spec_helper'
2+
3+
require 'datadog/core/telemetry/event/app_dependencies_loaded'
4+
5+
RSpec.describe Datadog::Core::Telemetry::Event::AppDependenciesLoaded do
6+
let(:id) { double('seq_id') }
7+
let(:event) { event_class.new }
8+
9+
subject(:payload) { event.payload }
10+
11+
let(:event_class) { described_class }
12+
it_behaves_like 'telemetry event with no attributes'
13+
14+
it 'all have name and Ruby gem version' do
15+
is_expected.to match(dependencies: all(match(name: kind_of(String), version: kind_of(String))))
16+
end
17+
18+
it 'has a known gem with expected version' do
19+
is_expected.to match(
20+
dependencies: include(name: 'datadog', version: Datadog::Core::Environment::Identity.gem_datadog_version)
21+
)
22+
end
23+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
3+
require 'datadog/core/telemetry/event/app_heartbeat'
4+
5+
RSpec.describe Datadog::Core::Telemetry::Event::AppHeartbeat do
6+
let(:id) { double('seq_id') }
7+
let(:event) { event_class.new }
8+
9+
subject(:payload) { event.payload }
10+
11+
let(:event_class) { described_class }
12+
it_behaves_like 'telemetry event with no attributes'
13+
14+
it 'has no payload' do
15+
is_expected.to eq({})
16+
end
17+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require 'spec_helper'
2+
3+
require 'datadog/core/telemetry/event/app_integrations_change'
4+
5+
RSpec.describe Datadog::Core::Telemetry::Event::AppIntegrationsChange do
6+
let(:id) { double('seq_id') }
7+
let(:event) { event_class.new }
8+
9+
subject(:payload) { event.payload }
10+
11+
let(:event_class) { described_class }
12+
it_behaves_like 'telemetry event with no attributes'
13+
14+
it 'all have name and compatibility' do
15+
is_expected.to match(integrations: all(include(name: kind_of(String), compatible: boolean)))
16+
end
17+
18+
context 'with an instrumented integration' do
19+
context 'that applied' do
20+
before do
21+
Datadog.configure do |c|
22+
c.tracing.instrument :http
23+
end
24+
end
25+
it 'has a list of integrations' do
26+
is_expected.to match(
27+
integrations: include(
28+
name: 'http',
29+
version: RUBY_VERSION,
30+
compatible: true,
31+
enabled: true
32+
)
33+
)
34+
end
35+
end
36+
37+
context 'that failed to apply' do
38+
before do
39+
raise 'pg is loaded! This test requires integration that does not have its gem loaded' if Gem.loaded_specs['pg']
40+
41+
Datadog.configure do |c|
42+
c.tracing.instrument :pg
43+
end
44+
end
45+
46+
it 'has a list of integrations' do
47+
is_expected.to match(
48+
integrations: include(
49+
name: 'pg',
50+
compatible: false,
51+
enabled: false,
52+
error: 'Available?: false, Loaded? false, Compatible? false, Patchable? false',
53+
)
54+
)
55+
end
56+
end
57+
end
58+
end
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
require 'spec_helper'
2+
3+
require 'datadog/core/telemetry/event/app_started'
4+
5+
RSpec.describe Datadog::Core::Telemetry::Event::AppStarted do
6+
let(:id) { double('seq_id') }
7+
let(:event) { event_class.new }
8+
9+
subject(:payload) { event.payload }
10+
11+
let(:logger) do
12+
stub_const('MyLogger', Class.new(::Logger)).new(nil)
13+
end
14+
let(:default_code_configuration) do
15+
[
16+
['DD_AGENT_HOST', '1.2.3.4'],
17+
['DD_AGENT_TRANSPORT', 'TCP'],
18+
['DD_TRACE_SAMPLE_RATE', '0.5'],
19+
['DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED', true],
20+
['DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED', false],
21+
['DD_TRACE_PEER_SERVICE_MAPPING', 'foo:bar'],
22+
['dynamic_instrumentation.enabled', false],
23+
['logger.level', 0],
24+
['profiling.advanced.code_provenance_enabled', true],
25+
['profiling.advanced.endpoint.collection.enabled', true],
26+
['profiling.enabled', false],
27+
['runtime_metrics.enabled', false],
28+
['tracing.analytics.enabled', true],
29+
['tracing.propagation_style_extract', '["datadog", "tracecontext", "baggage"]'],
30+
['tracing.propagation_style_inject', '["datadog", "tracecontext", "baggage"]'],
31+
['tracing.enabled', true],
32+
['tracing.log_injection', true],
33+
['tracing.partial_flush.enabled', false],
34+
['tracing.partial_flush.min_spans_threshold', 500],
35+
['tracing.report_hostname', false],
36+
['tracing.sampling.rate_limit', 100],
37+
['tracing.auto_instrument.enabled', false],
38+
['tracing.writer_options.buffer_size', 123],
39+
['tracing.writer_options.flush_interval', 456],
40+
['tracing.opentelemetry.enabled', false],
41+
['logger.instance', 'MyLogger'],
42+
['appsec.enabled', false],
43+
['appsec.sca_enabled', false]
44+
].freeze
45+
end
46+
let(:expected_install_signature) do
47+
{ install_id: 'id', install_time: 'time', install_type: 'type' }
48+
end
49+
let(:expected_products) do
50+
{
51+
appsec: {
52+
enabled: false,
53+
},
54+
dynamic_instrumentation: {
55+
enabled: false,
56+
},
57+
profiler: hash_including(enabled: false),
58+
}
59+
end
60+
let(:event_class) { described_class }
61+
before do
62+
allow_any_instance_of(Datadog::Core::Utils::Sequence).to receive(:next).and_return(id)
63+
64+
Datadog.configure do |c|
65+
c.agent.host = '1.2.3.4'
66+
c.tracing.sampling.default_rate = 0.5
67+
c.tracing.contrib.global_default_service_name.enabled = true
68+
c.tracing.contrib.peer_service_mapping = { foo: 'bar' }
69+
c.tracing.writer_options = { buffer_size: 123, flush_interval: 456 }
70+
c.logger.instance = logger
71+
c.tracing.analytics.enabled = true
72+
c.telemetry.install_id = 'id'
73+
c.telemetry.install_type = 'type'
74+
c.telemetry.install_time = 'time'
75+
c.appsec.sca_enabled = false
76+
end
77+
78+
# Reset global cache
79+
Datadog::Core::Environment::Git.reset_for_tests
80+
end
81+
it_behaves_like 'telemetry event with no attributes'
82+
83+
# Helper to make configuration matching table easier to read
84+
def contain_code_configuration(*array)
85+
array.map { |name, value| { name: name, origin: 'code', seq_id: id, value: value } }
86+
end
87+
88+
def contain_env_configuration(*array)
89+
array.map { |name, value| { name: name, origin: 'env_var', seq_id: id, value: value } }
90+
end
91+
92+
it do
93+
is_expected.to match(
94+
products: expected_products,
95+
configuration: contain_code_configuration(
96+
*default_code_configuration
97+
),
98+
install_signature: expected_install_signature,
99+
)
100+
end
101+
102+
context 'with git/SCI environment variables set' do
103+
with_env 'DD_GIT_REPOSITORY_URL' => 'https://github.com/datadog/hello',
104+
'DD_GIT_COMMIT_SHA' => '1234hash'
105+
106+
before do
107+
# Reset global cache so that we get our values back
108+
Datadog::Core::Environment::Git.reset_for_tests
109+
end
110+
111+
after do
112+
# Do not use our values in other tests
113+
Datadog::Core::Environment::Git.reset_for_tests
114+
end
115+
116+
it 'reports git/SCI values to telemetry' do
117+
is_expected.to match(
118+
products: expected_products,
119+
configuration: contain_env_configuration(
120+
['DD_GIT_REPOSITORY_URL', 'https://github.com/datadog/hello'],
121+
['DD_GIT_COMMIT_SHA', '1234hash'],
122+
) + contain_code_configuration(
123+
*default_code_configuration
124+
),
125+
install_signature: expected_install_signature,
126+
)
127+
end
128+
end
129+
130+
context 'with nil configurations' do
131+
before do
132+
Datadog.configure do |c|
133+
c.logger.instance = nil
134+
end
135+
end
136+
137+
it 'removes empty configurations from payload' do
138+
is_expected.to_not match(
139+
configuration: include(
140+
{ name: 'logger.instance', origin: anything, seq_id: anything, value: anything }
141+
)
142+
)
143+
end
144+
end
145+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require 'spec_helper'
2+
3+
require 'datadog/core/telemetry/event/distributions'
4+
require 'datadog/core/telemetry/metric'
5+
6+
RSpec.describe Datadog::Core::Telemetry::Event::Distributions do
7+
let(:id) { double('seq_id') }
8+
let(:event) { event_class.new }
9+
10+
subject(:payload) { event.payload }
11+
12+
let(:event_class) { described_class }
13+
let(:event) { event_class.new(namespace, metrics) }
14+
15+
let(:namespace) { 'general' }
16+
let(:metric_name) { 'request_duration' }
17+
let(:metric) do
18+
Datadog::Core::Telemetry::Metric::Distribution.new(metric_name, tags: { status: '200' })
19+
end
20+
let(:metrics) { [metric] }
21+
22+
let(:expected_metric_series) { [metric.to_h] }
23+
24+
it do
25+
is_expected.to eq(
26+
{
27+
namespace: namespace,
28+
series: expected_metric_series
29+
}
30+
)
31+
end
32+
end

0 commit comments

Comments
 (0)