Skip to content

Commit

Permalink
limit telemetry app-started event retries
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Jun 21, 2024
1 parent efdb2b6 commit e8bd4f1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/datadog/core/telemetry/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class Worker
include Core::Workers::Polling

DEFAULT_BUFFER_MAX_SIZE = 1000
APP_STARTED_EVENT_RETRIES = 10

TELEMETRY_STARTED_ONCE = Utils::OnlyOnceSuccessful.new
TELEMETRY_STARTED_ONCE = Utils::OnlyOnceSuccessful.new(APP_STARTED_EVENT_RETRIES)

def initialize(
heartbeat_interval_seconds:,
Expand Down Expand Up @@ -61,7 +62,11 @@ def enqueue(event)
end

def sent_started_event?
TELEMETRY_STARTED_ONCE.ran?
TELEMETRY_STARTED_ONCE.success?
end

def failed_to_start?
TELEMETRY_STARTED_ONCE.failed?
end

private
Expand Down Expand Up @@ -94,6 +99,12 @@ def heartbeat!
def started!
return unless enabled?

if failed_to_start?
Datadog.logger.debug('Telemetry app-started event exhausted retries, disabling telemetry worker')
self.enabled = false
return
end

TELEMETRY_STARTED_ONCE.run do
res = send_event(Event::AppStarted.new)

Expand Down
8 changes: 8 additions & 0 deletions lib/datadog/core/utils/only_once_successful.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ def check_limit!
def limited?
!@limit.nil? && @limit.positive?
end

def reset_ran_once_state_for_tests
@mutex.synchronize do
@ran_once = false
@failed = false
@retries = 0
end
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions sig/datadog/core/telemetry/worker.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Datadog
include Core::Workers::Queue

TELEMETRY_STARTED_ONCE: Datadog::Core::Utils::OnlyOnceSuccessful
APP_STARTED_EVENT_RETRIES: 10
DEFAULT_BUFFER_MAX_SIZE: 1000

@emitter: Emitter
Expand All @@ -23,6 +24,8 @@ module Datadog

def sent_started_event?: () -> bool

def failed_to_start?: () -> bool

def enqueue: (Event::Base event) -> void

def dequeue: () -> Array[Event::Base]
Expand Down
65 changes: 65 additions & 0 deletions spec/datadog/core/telemetry/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,71 @@
try_wait_until { sent_hearbeat }
end

context 'when app-started event fails' do
it 'retries' do
expect(emitter).to receive(:request).with(an_instance_of(Datadog::Core::Telemetry::Event::AppStarted))
.and_return(
double(
Datadog::Core::Telemetry::Http::Adapters::Net::Response,
not_found?: false,
ok?: false
)
).once

expect(emitter).to receive(:request).with(an_instance_of(Datadog::Core::Telemetry::Event::AppStarted)) do
@received_started = true

response
end

sent_hearbeat = false
allow(emitter).to receive(:request).with(kind_of(Datadog::Core::Telemetry::Event::AppHeartbeat)) do
# app-started was already sent by now
expect(@received_started).to be(true)

sent_hearbeat = true

response
end

worker.start

try_wait_until { sent_hearbeat }
end
end

context 'when app-started event exhausted retries' do
let(:heartbeat_interval_seconds) { 0.1 }

it 'stops retrying, never sends heartbeat, and disables worker' do
expect(emitter).to receive(:request).with(an_instance_of(Datadog::Core::Telemetry::Event::AppStarted))
.and_return(
double(
Datadog::Core::Telemetry::Http::Adapters::Net::Response,
not_found?: false,
ok?: false
)
).exactly(described_class::APP_STARTED_EVENT_RETRIES).times

sent_hearbeat = false
allow(emitter).to receive(:request).with(kind_of(Datadog::Core::Telemetry::Event::AppHeartbeat)) do
# app-started was already sent by now
expect(@received_started).to be(true)

sent_hearbeat = true

response
end

worker.start

try_wait_until { !worker.enabled? }

expect(sent_hearbeat).to be(false)
expect(worker.failed_to_start?).to be(true)
end
end

context 'when dependencies collection enabled' do
let(:dependency_collection) { true }

Expand Down

0 comments on commit e8bd4f1

Please sign in to comment.