Skip to content

Commit

Permalink
pull telemtry transport creation up to Core::Configuration::Component…
Browse files Browse the repository at this point in the history
…s class
  • Loading branch information
anmarchenko committed Jul 11, 2024
1 parent b2be5b2 commit 6b8e4b1
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/datadog/core/configuration/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def build_telemetry(settings, agent_settings, logger)
end

Telemetry::Component.new(
http_transport: Datadog::Core::Telemetry::Http::Transport.new,
enabled: enabled,
metrics_enabled: enabled && settings.telemetry.metrics_enabled,
heartbeat_interval_seconds: settings.telemetry.heartbeat_interval_seconds,
Expand Down
3 changes: 2 additions & 1 deletion lib/datadog/core/telemetry/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def initialize(
heartbeat_interval_seconds:,
metrics_aggregation_interval_seconds:,
dependency_collection:,
http_transport:,
enabled: true,
metrics_enabled: true
)
Expand All @@ -39,7 +40,7 @@ def initialize(
enabled: @enabled,
heartbeat_interval_seconds: heartbeat_interval_seconds,
metrics_aggregation_interval_seconds: metrics_aggregation_interval_seconds,
emitter: Emitter.new,
emitter: Emitter.new(http_transport: http_transport),
metrics_manager: @metrics_manager,
dependency_collection: dependency_collection
)
Expand Down
20 changes: 9 additions & 11 deletions lib/datadog/core/telemetry/emitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ class Emitter

# @param http_transport [Datadog::Core::Telemetry::Http::Transport] Transport object that can be used to send
# telemetry requests via the agent
def initialize(http_transport: Datadog::Core::Telemetry::Http::Transport.new)
def initialize(http_transport:)
@http_transport = http_transport
end

# Retrieves and emits a TelemetryRequest object based on the request type specified
def request(event)
begin
seq_id = self.class.sequence.next
payload = Request.build_payload(event, seq_id)
res = @http_transport.request(request_type: event.type, payload: payload.to_json)
Datadog.logger.debug { "Telemetry sent for event `#{event.type}` (code: #{res.code.inspect})" }
res
rescue => e
Datadog.logger.debug("Unable to send telemetry request for event `#{event.type rescue 'unknown'}`: #{e}")
Telemetry::Http::InternalErrorResponse.new(e)
end
seq_id = self.class.sequence.next
payload = Request.build_payload(event, seq_id)
res = @http_transport.request(request_type: event.type, payload: payload.to_json)
Datadog.logger.debug { "Telemetry sent for event `#{event.type}` (code: #{res.code.inspect})" }
res
rescue => e
Datadog.logger.debug("Unable to send telemetry request for event `#{event.type rescue 'unknown'}`: #{e}")
Telemetry::Http::InternalErrorResponse.new(e)
end

# Initializes a Sequence object to track seq_id if not already initialized; else returns stored
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/core/telemetry/component.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Datadog

include Core::Utils::Forking

def initialize: (heartbeat_interval_seconds: Float, metrics_aggregation_interval_seconds: Float, dependency_collection: bool, ?enabled: bool, ?metrics_enabled: bool) -> void
def initialize: (http_transport: Datadog::Core::Telemetry::Http::Transport, heartbeat_interval_seconds: Float, metrics_aggregation_interval_seconds: Float, dependency_collection: bool, ?enabled: bool, ?metrics_enabled: bool) -> void

def disable!: () -> void

Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/core/telemetry/emitter.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Datadog
attr_reader http_transport: untyped

extend Core::Utils::Forking
def initialize: (?http_transport: untyped) -> void
def initialize: (http_transport: untyped) -> void
def request: (Datadog::Core::Telemetry::Event::Base event) -> Datadog::Core::Telemetry::Http::Adapters::Net::Response
def self.sequence: () -> untyped
end
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/core/telemetry/event.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Datadog

class Base
def payload: () -> (Hash[Symbol, untyped] | Array[Hash[Symbol, untyped]])
def type: -> String?
def type: -> String
end

class AppStarted < Base
Expand Down
6 changes: 4 additions & 2 deletions spec/datadog/core/configuration/components_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@
context 'given settings' do
let(:telemetry) { instance_double(Datadog::Core::Telemetry::Component) }
let(:expected_options) do
{ enabled: enabled, metrics_enabled: metrics_enabled, heartbeat_interval_seconds: heartbeat_interval_seconds,
{ enabled: enabled, http_transport: an_instance_of(Datadog::Core::Telemetry::Http::Transport),
metrics_enabled: metrics_enabled, heartbeat_interval_seconds: heartbeat_interval_seconds,
metrics_aggregation_interval_seconds: metrics_aggregation_interval_seconds,
dependency_collection: dependency_collection }
end
Expand All @@ -249,7 +250,8 @@

context 'and :unix agent adapter' do
let(:expected_options) do
{ enabled: false, metrics_enabled: false, heartbeat_interval_seconds: heartbeat_interval_seconds,
{ enabled: false, http_transport: an_instance_of(Datadog::Core::Telemetry::Http::Transport),
metrics_enabled: false, heartbeat_interval_seconds: heartbeat_interval_seconds,
metrics_aggregation_interval_seconds: metrics_aggregation_interval_seconds,
dependency_collection: dependency_collection }
end
Expand Down
3 changes: 3 additions & 0 deletions spec/datadog/core/telemetry/component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
subject(:telemetry) do
described_class.new(
enabled: enabled,
http_transport: http_transport,
metrics_enabled: metrics_enabled,
heartbeat_interval_seconds: heartbeat_interval_seconds,
metrics_aggregation_interval_seconds: metrics_aggregation_interval_seconds,
Expand All @@ -19,6 +20,7 @@
let(:metrics_aggregation_interval_seconds) { 1 }
let(:dependency_collection) { true }
let(:worker) { double(Datadog::Core::Telemetry::Worker) }
let(:http_transport) { double(Datadog::Core::Telemetry::Http::Transport) }
let(:not_found) { false }

before do
Expand All @@ -45,6 +47,7 @@
context 'with default parameters' do
subject(:telemetry) do
described_class.new(
http_transport: http_transport,
heartbeat_interval_seconds: heartbeat_interval_seconds,
metrics_aggregation_interval_seconds: metrics_aggregation_interval_seconds,
dependency_collection: dependency_collection
Expand Down
12 changes: 2 additions & 10 deletions spec/datadog/core/telemetry/emitter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,8 @@
end

describe '#initialize' do
context 'when no params provided' do
subject(:emitter) { described_class.new }
it { is_expected.to be_a_kind_of(described_class) }
end

context 'when :http_transport is provided' do
let(:http_transport) { double(Datadog::Core::Telemetry::Http::Transport) }
it { is_expected.to be_a_kind_of(described_class) }
it { expect(emitter.http_transport).to be(http_transport) }
end
it { is_expected.to be_a_kind_of(described_class) }
it { expect(emitter.http_transport).to be(http_transport) }

it 'seq_id begins with 1' do
original_seq_id = emitter.class.sequence.instance_variable_get(:@current)
Expand Down

0 comments on commit 6b8e4b1

Please sign in to comment.