Skip to content

Commit

Permalink
Merge pull request #475 from newrelic/vince/fix-backoff-samp
Browse files Browse the repository at this point in the history
Fix: Make sure we trigger backoff sampler cycles
  • Loading branch information
tpitale authored Jan 13, 2025
2 parents c308ed2 + 8654e54 commit 185c2b1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/new_relic/distributed_trace/backoff_sampler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule NewRelic.DistributedTrace.BackoffSampler do
:persistent_term.put({__MODULE__, :counter}, new(@size, []))
put(@sampling_target, AgentRun.lookup(:sampling_target) || 10)

trigger_next_cycle()
{:ok, %{}}
end

Expand Down Expand Up @@ -94,7 +95,7 @@ defmodule NewRelic.DistributedTrace.BackoffSampler do

def trigger_next_cycle() do
cycle_period = AgentRun.lookup(:sampling_target_period) || 60_000
Process.send_after(self(), :cycle, cycle_period)
Process.send_after(__MODULE__, :cycle, cycle_period)
end

def update_state(false = _sampled?) do
Expand Down
46 changes: 45 additions & 1 deletion test/backoff_sampler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule BackoffSamplerTest do
assert BackoffSampler.sample?()
assert BackoffSampler.sample?()

# The rest will be ignored
# The rest will be dropped
refute BackoffSampler.sample?()
refute BackoffSampler.sample?()
refute BackoffSampler.sample?()
Expand Down Expand Up @@ -96,4 +96,48 @@ defmodule BackoffSamplerTest do
decided_count_last: 0
})
end

test "cycles trigger" do
Application.put_env(:new_relic_agent, :sampling_target_period, 100)

BackoffSampler.reset()
BackoffSampler.trigger_next_cycle()

# Target is 10, so it will sample the first 10
assert BackoffSampler.sample?()
assert BackoffSampler.sample?()
assert BackoffSampler.sample?()
assert BackoffSampler.sample?()
assert BackoffSampler.sample?()
assert BackoffSampler.sample?()
assert BackoffSampler.sample?()
assert BackoffSampler.sample?()
assert BackoffSampler.sample?()
assert BackoffSampler.sample?()

# The rest will be dropped
refute BackoffSampler.sample?()
refute BackoffSampler.sample?()
refute BackoffSampler.sample?()
refute BackoffSampler.sample?()

# Wait until the next cycle
Process.sleep(110)

# Next cycle it will adjust and take some, but not all
decisions = [
BackoffSampler.sample?(),
BackoffSampler.sample?(),
BackoffSampler.sample?(),
BackoffSampler.sample?(),
BackoffSampler.sample?(),
BackoffSampler.sample?(),
BackoffSampler.sample?(),
BackoffSampler.sample?(),
BackoffSampler.sample?()
]

assert true in decisions
assert false in decisions
end
end

0 comments on commit 185c2b1

Please sign in to comment.