From ed3667d52a980f10ddd8d74aecb7220c12989cce Mon Sep 17 00:00:00 2001 From: Stefano Gessa Date: Sun, 15 Jun 2025 18:17:24 +0700 Subject: [PATCH 1/3] Handle errors in `Harvest.HarvestCycle.send_harvest/2` --- .gitignore | 1 + lib/new_relic/harvest/harvest_cycle.ex | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ae4c7c37..4048883b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ erl_crash.dump tmp config/secret.exs .DS_Store +.tool-versions diff --git a/lib/new_relic/harvest/harvest_cycle.ex b/lib/new_relic/harvest/harvest_cycle.ex index a27818d6..34e1c84e 100644 --- a/lib/new_relic/harvest/harvest_cycle.ex +++ b/lib/new_relic/harvest/harvest_cycle.ex @@ -131,13 +131,22 @@ defmodule NewRelic.Harvest.HarvestCycle do :exit, _exit -> NewRelic.log(:error, "Failed to send harvest from #{inspect(supervisor)}") after - DynamicSupervisor.terminate_child(supervisor, harvester) + terminate_child(supervisor, harvester) end end, shutdown: @harvest_timeout ) end + defp terminate_child(supervisor, harvester) do + try do + DynamicSupervisor.terminate_child(supervisor, harvester) + catch + :exit, _exit -> + :ok + end + end + defp stop_harvest_cycle(timer), do: timer && Process.cancel_timer(timer) defp trigger_harvest_cycle(%{ From 49d3218072cf67d09827144d586eaecd400623ec Mon Sep 17 00:00:00 2001 From: Vince Foley Date: Mon, 21 Jul 2025 18:31:48 -0700 Subject: [PATCH 2/3] don't nest try --- lib/new_relic/harvest/harvest_cycle.ex | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/new_relic/harvest/harvest_cycle.ex b/lib/new_relic/harvest/harvest_cycle.ex index 34e1c84e..0a4df9d3 100644 --- a/lib/new_relic/harvest/harvest_cycle.ex +++ b/lib/new_relic/harvest/harvest_cycle.ex @@ -130,23 +130,18 @@ defmodule NewRelic.Harvest.HarvestCycle do catch :exit, _exit -> NewRelic.log(:error, "Failed to send harvest from #{inspect(supervisor)}") - after - terminate_child(supervisor, harvester) + end + + try do + DynamicSupervisor.terminate_child(supervisor, harvester) + catch + :exit, _exit -> :ok end end, shutdown: @harvest_timeout ) end - defp terminate_child(supervisor, harvester) do - try do - DynamicSupervisor.terminate_child(supervisor, harvester) - catch - :exit, _exit -> - :ok - end - end - defp stop_harvest_cycle(timer), do: timer && Process.cancel_timer(timer) defp trigger_harvest_cycle(%{ From c5c8d7ccc67a336e921a83c943995fcb2b09dbad Mon Sep 17 00:00:00 2001 From: Vince Foley Date: Mon, 21 Jul 2025 14:50:57 -0700 Subject: [PATCH 3/3] Fix example test domain --- test/telemetry/finch_test.exs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/test/telemetry/finch_test.exs b/test/telemetry/finch_test.exs index f047ca1d..2560808b 100644 --- a/test/telemetry/finch_test.exs +++ b/test/telemetry/finch_test.exs @@ -11,27 +11,27 @@ defmodule NewRelic.Telemetry.FinchTest do end test "finch external metrics" do - request("https://httpstat.us/200") + request("https://httpbin.org/200") metrics = TestHelper.gather_harvest(Collector.Metric.Harvester) - assert TestHelper.find_metric(metrics, "External/httpstat.us/Finch/GET", 1) - assert TestHelper.find_metric(metrics, "External/httpstat.us/all", 1) + assert TestHelper.find_metric(metrics, "External/httpbin.org/Finch/GET", 1) + assert TestHelper.find_metric(metrics, "External/httpbin.org/all", 1) assert TestHelper.find_metric(metrics, "External/all", 1) end test "[:finch, :request, :stop] - 200" do Task.async(fn -> NewRelic.start_transaction("FinchTest", "200") - request("https://httpstat.us/200") + request("https://httpbin.org/status/200") end) |> Task.await() span_events = TestHelper.gather_harvest(Collector.SpanEvent.Harvester) - external_span = TestHelper.find_event(span_events, "External/httpstat.us/Finch/GET") + external_span = TestHelper.find_event(span_events, "External/httpbin.org/Finch/GET") - assert external_span[:"http.url"] == "https://httpstat.us/200" + assert external_span[:"http.url"] == "https://httpbin.org/status/200" assert external_span[:"http.method"] == "GET" assert external_span[:component] == "Finch" assert external_span[:"response.status"] == 200 @@ -40,15 +40,15 @@ defmodule NewRelic.Telemetry.FinchTest do test "[:finch, :request, :stop] - 500" do Task.async(fn -> NewRelic.start_transaction("FinchTest", "500") - request("https://httpstat.us/500") + request("https://httpbin.org/status/500") end) |> Task.await() span_events = TestHelper.gather_harvest(Collector.SpanEvent.Harvester) - external_span = TestHelper.find_event(span_events, "External/httpstat.us/Finch/GET") + external_span = TestHelper.find_event(span_events, "External/httpbin.org/Finch/GET") - assert external_span[:"http.url"] == "https://httpstat.us/500" + assert external_span[:"http.url"] == "https://httpbin.org/status/500" assert external_span[:"response.status"] == 500 end @@ -73,7 +73,7 @@ defmodule NewRelic.Telemetry.FinchTest do {:ok, pid} = Task.start(fn -> NewRelic.start_transaction("FinchTest", "Exception") - request("https://httpstat.us/200", :exception) + request("https://httpbin.org/status/200", :exception) end) Process.monitor(pid) @@ -81,9 +81,9 @@ defmodule NewRelic.Telemetry.FinchTest do span_events = TestHelper.gather_harvest(Collector.SpanEvent.Harvester) - external_span = TestHelper.find_event(span_events, "External/httpstat.us/Finch/GET") + external_span = TestHelper.find_event(span_events, "External/httpbin.org/Finch/GET") - assert external_span[:"http.url"] == "https://httpstat.us/200" + assert external_span[:"http.url"] == "https://httpbin.org/status/200" assert external_span[:error] == true assert external_span[:"error.message"] =~ "Oops" end