Skip to content

Commit

Permalink
Add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Oct 27, 2023
1 parent 21cb61e commit c9d3e35
Show file tree
Hide file tree
Showing 6 changed files with 518 additions and 2 deletions.
90 changes: 90 additions & 0 deletions spec/datadog/core/telemetry/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,28 @@
after do
client.worker.stop(true)
client.worker.join
client.metrics_worker.stop(true)
client.metrics_worker.join
end

context 'with default parameters' do
subject(:client) { described_class.new(heartbeat_interval_seconds: heartbeat_interval_seconds) }
it { is_expected.to be_a_kind_of(described_class) }
it { expect(client.enabled).to be(true) }
it { expect(client.emitter).to be(emitter) }

it 'set Metric::Rate interval value' do
expect(Datadog::Core::Telemetry::Metric::Rate).to receive(:'interval=').with(heartbeat_interval_seconds)
client
end
end

context 'when :enabled is false' do
let(:enabled) { false }
it { is_expected.to be_a_kind_of(described_class) }
it { expect(client.enabled).to be(false) }
it { expect(client.worker.enabled?).to be(false) }
it { expect(client.metrics_worker.enabled?).to be(false) }
end

context 'when enabled' do
Expand All @@ -42,17 +50,21 @@
it { is_expected.to be_a_kind_of(described_class) }
it { expect(client.enabled).to be(true) }
it { expect(client.worker.enabled?).to be(true) }
it { expect(client.metrics_worker.enabled?).to be(true) }
end
end

describe '#disable!' do
after do
client.worker.stop(true)
client.worker.join
client.metrics_worker.stop(true)
client.metrics_worker.join
end

it { expect { client.disable! }.to change { client.enabled }.from(true).to(false) }
it { expect { client.disable! }.to change { client.worker.enabled? }.from(true).to(false) }
it { expect { client.disable! }.to change { client.metrics_worker.enabled? }.from(true).to(false) }
end

describe '#started!' do
Expand All @@ -61,6 +73,8 @@
after do
client.worker.stop(true)
client.worker.join
client.metrics_worker.stop(true)
client.metrics_worker.join
end

context 'when disabled' do
Expand Down Expand Up @@ -127,6 +141,8 @@
after do
client.worker.stop(true)
client.worker.join
client.metrics_worker.stop(true)
client.metrics_worker.join
end

context 'when disabled' do
Expand Down Expand Up @@ -208,6 +224,8 @@
after do
client.worker.stop(true)
client.worker.join
client.metrics_worker.stop(true)
client.metrics_worker.join
end

context 'when disabled' do
Expand Down Expand Up @@ -248,6 +266,8 @@
after do
client.worker.stop(true)
client.worker.join
client.metrics_worker.stop(true)
client.metrics_worker.join
end

context 'when disabled' do
Expand Down Expand Up @@ -283,4 +303,74 @@
end
end
end

context 'metrics' do
[
[:add_count_metric, Datadog::Core::Telemetry::Metric::Count],
[:add_rate_metric, Datadog::Core::Telemetry::Metric::Rate],
[:add_gauge_metric, Datadog::Core::Telemetry::Metric::Gauge],
[:add_distribution_metric, Datadog::Core::Telemetry::Metric::Distribution],
].each do |metric_method, metric_klass|
context 'when disabled' do
let(:enabled) { false }
it do
expect(client.send(metric_method, 'test_namespace', 'name', 1, {})).to be_nil
end
end

context 'when enabled' do
let(:enabled) { true }
it do
expect_any_instance_of(Datadog::Core::Telemetry::MetricQueue).to receive(:add_metric).with(
'test_namespace',
'name',
1,
{},
metric_klass
)
client.send(metric_method, 'test_namespace', 'name', 1, {})
end
end
end

describe '#flush_metrics!' do
after do
client.worker.stop(true)
client.worker.join
client.metrics_worker.stop(true)
client.metrics_worker.join
end

context 'when disabled' do
let(:enabled) { false }
it do
expect(client.send(:flush_metrics!)).to be_nil
end
end

context 'when enabled' do
let(:enabled) { true }
it 'send metrics to the emitter and reset the metric_queue' do
old_metric_queue = client.instance_variable_get(:@metric_queue)

client.add_distribution_metric('test_namespace', 'name', 1, {})
expected_payload = {
:namespace => 'test_namespace',
:series => [
{
:metric => 'name', :tags => [], :values => [1],
:type => 'distributions',
:common => true
}
]
}
expect(emitter).to receive(:request).with('distributions', payload: expected_payload)
client.send(:flush_metrics!)

new_metric_queue = client.instance_variable_get(:@metric_queue)
expect(old_metric_queue).to_not eq new_metric_queue
end
end
end
end
end
47 changes: 46 additions & 1 deletion spec/datadog/core/telemetry/emitter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,55 @@

it 'creates a telemetry event with data' do
expect(Datadog::Core::Telemetry::Event).to receive(:new).and_return(event)
expect(event).to receive(:telemetry_request).with(request_type: request_type, seq_id: be_a(Integer), data: data)
expect(event).to receive(:telemetry_request).with(
request_type: request_type,
seq_id: be_a(Integer),
data: data,
payload: nil
)
request
end
end

context 'with data and payload' do
subject(:request) { emitter.request(:'app-started', data: {}, payload: {}) }

it 'fails to send request' do
request
expect(Datadog.logger).to have_received(:debug) do |message|
expect(message).to include('Can not provide data and payload')
end
end
end

context 'metrics' do
let(:request_type) { 'generate-metrics' }
let(:event) { double('event') }
let(:payload) { {} }
subject(:request) { emitter.request(request_type, payload: payload) }

it 'creates a telemetry metric with payload' do
expect(Datadog::Core::Telemetry::Event).to receive(:new).and_return(event)
expect(event).to receive(:telemetry_request).with(
request_type: request_type,
seq_id: be_a(Integer),
data: nil,
payload: payload
)
request
end

context 'missing payload' do
let(:payload) { nil }

it 'fail to send metric evenet' do
request
expect(Datadog.logger).to have_received(:debug) do |message|
expect(message).to include('Unable to send telemetry request')
end
end
end
end
end

describe 'when initialized multiple times' do
Expand Down
25 changes: 24 additions & 1 deletion spec/datadog/core/telemetry/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@
end

describe '#telemetry_request' do
subject(:telemetry_request) { event.telemetry_request(request_type: request_type, seq_id: seq_id, data: data) }
subject(:telemetry_request) do
event.telemetry_request(request_type: request_type, seq_id: seq_id, data: data, payload: payload)
end

let(:request_type) { :'app-started' }
let(:seq_id) { 1 }
let(:data) { nil }
let(:payload) { nil }

it { is_expected.to be_a_kind_of(Datadog::Core::Telemetry::V1::TelemetryRequest) }
it { expect(telemetry_request.api_version).to eql('v1') }
Expand Down Expand Up @@ -62,6 +65,26 @@
end
end

['generate-metrics', 'distributions'].each do |request_type|
context request_type do
let(:request_type) { request_type }

context 'when payload is nil' do
let(:payload) { nil }

it 'raise ArgumentError' do
expect { telemetry_request }.to raise_error(ArgumentError)
end
end

context 'when payload is not nil' do
let(:payload) { { foo: :bar } }

it { expect(telemetry_request.payload).to eq({ foo: :bar }) }
end
end
end

context 'is nil' do
let(:request_type) { nil }
it { expect { telemetry_request }.to raise_error(ArgumentError) }
Expand Down
128 changes: 128 additions & 0 deletions spec/datadog/core/telemetry/metric_queue_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
require 'spec_helper'

require 'datadog/core/telemetry/metric_queue'
require 'datadog/core/telemetry/metric'

RSpec.describe Datadog::Core::Telemetry::MetricQueue do
let(:metric_queue) { described_class.new }
let(:metric_klass) { Datadog::Core::Telemetry::Metric::Count }
subject(:empty_metrics_queue) do
{
'generate-metrics' => {},
'distributions' => {},
}
end

describe '#add_metric' do
context 'no previous metric' do
it 'creates a new metric entry point and stores it' do
expect(metric_queue.metrics).to eq(empty_metrics_queue)
expect(metric_klass).to receive(:new).with('test_metric_name', { foo: :bar }).and_call_original
expect_any_instance_of(metric_klass).to receive(:update_value).with(1).and_call_original
metric_queue.add_metric(
'test_namespace',
'test_metric_name',
1,
{ foo: :bar },
metric_klass
)

expect(metric_queue.metrics[metric_klass.request_type]['test_namespace']).to_not be_nil
expect(metric_queue.metrics[metric_klass.request_type]['test_namespace']['test_metric_name']).to_not be_nil

metric_instace = metric_queue.metrics[metric_klass.request_type]['test_namespace']['test_metric_name']
expect(metric_instace).to be_a(metric_klass)
end
end

context 'previous metric' do
it 'just updates values and stores metric back' do
metric_queue.add_metric(
'test_namespace',
'test_metric_name',
1,
{ foo: :bar },
metric_klass
)

expect(metric_klass).to_not receive(:new)
expect_any_instance_of(metric_klass).to receive(:update_value).with(2).and_call_original

metric_queue.add_metric(
'test_namespace',
'test_metric_name',
2,
{ foo: :bar },
metric_klass
)

metric_instace = metric_queue.metrics[metric_klass.request_type]['test_namespace']['test_metric_name']
expect(metric_instace).to be_a(metric_klass)
end
end
end

describe '#build_metrics_payload' do
it 'yields metric_type and assiciated payload' do
expect(Time).to receive(:now).and_return(1234)

metric_queue.add_metric(
'test_namespace',
'test_metric_name',
1,
{ foo: :bar },
metric_klass
)

metric_queue.add_metric(
'test_namespace_two',
'test_metric_name_distribution',
1,
{ foo: :bar },
Datadog::Core::Telemetry::Metric::Distribution
)

expect do |b|
metric_queue.build_metrics_payload(&b)
end.to yield_successive_args(
[
'generate-metrics', {
:namespace => 'test_namespace',
:series =>
[
{
:metric => 'test_metric_name',
:tags => ['foo:bar'],
:values => [[1234, 1]],
:type => 'count',
:common => true
}
]
}
],
[
'distributions', {
:namespace => 'test_namespace_two',
:series => [
{
:metric => 'test_metric_name_distribution',
:tags => ['foo:bar'],
:values => [1],
:type => 'distributions',
:common => true
}
]
}
]
)
end

context 'empty metrics' do
it 'does not yield information' do
expect do |b|
metric_queue.build_metrics_payload(&b)
end.not_to yield_control
end
end
end
end
Loading

0 comments on commit c9d3e35

Please sign in to comment.