From 7d1c4b3290cbd6d230f18937d92d14343911a5b8 Mon Sep 17 00:00:00 2001 From: Vince Foley Date: Tue, 7 Jan 2025 13:37:28 -0800 Subject: [PATCH] Only attach telemetry handlers when agent is enabled --- config/test.exs | 2 ++ examples/config/config.exs | 4 +++- lib/new_relic/harvest/collector/protocol.ex | 14 +++++++++++++- lib/new_relic/telemetry/supervisor.ex | 13 +++++++++++-- test/integration/integration_test.exs | 2 +- test/tracer_test.exs | 2 +- 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/config/test.exs b/config/test.exs index 64822d16..0c3df653 100644 --- a/config/test.exs +++ b/config/test.exs @@ -4,6 +4,8 @@ config :logger, level: :warning config :new_relic_agent, app_name: "ElixirAgentTest", + license_key: "license_key", + bypass_collector: true, automatic_attributes: [test_attribute: "test_value"], ignore_paths: ["/ignore/this", ~r(ignore/these/*.)], log: "Logger" diff --git a/examples/config/config.exs b/examples/config/config.exs index 09f3d0e0..161e298e 100644 --- a/examples/config/config.exs +++ b/examples/config/config.exs @@ -4,7 +4,9 @@ config :logger, level: :debug config :new_relic_agent, app_name: "ExampleApps", - trusted_account_key: "trusted_account_key" + trusted_account_key: "trusted_account_key", + license_key: "license_key", + bypass_collector: true for config <- "../apps/*/config/config.exs" |> Path.expand(__DIR__) |> Path.wildcard() do import_config config diff --git a/lib/new_relic/harvest/collector/protocol.ex b/lib/new_relic/harvest/collector/protocol.ex index fda29964..6f436657 100644 --- a/lib/new_relic/harvest/collector/protocol.ex +++ b/lib/new_relic/harvest/collector/protocol.ex @@ -55,9 +55,17 @@ defmodule NewRelic.Harvest.Collector.Protocol do do: params |> collector_method_url - |> NewRelic.Util.HTTP.post(payload, collector_headers()) + |> post(payload) |> parse_http_response(params) + defp post(url, payload) do + if Application.get_env(:new_relic_agent, :bypass_collector, false) |> IO.inspect(label: "bypass") do + {:error, :bypass_collector} + else + NewRelic.Util.HTTP.post(url, payload, collector_headers()) + end + end + defp retry_call({:ok, response}, _params, _payload), do: {:ok, response} @retryable [408, 429, 500, 503] @@ -120,6 +128,10 @@ defmodule NewRelic.Harvest.Collector.Protocol do {:error, status} end + defp parse_http_response({:error, :bypass_collector}, _params) do + {:error, :bypass_collector} + end + defp parse_http_response({:error, reason}, params) do log_error(:failed_request, reason, params) {:error, reason} diff --git a/lib/new_relic/telemetry/supervisor.ex b/lib/new_relic/telemetry/supervisor.ex index c8961035..1f535e6e 100644 --- a/lib/new_relic/telemetry/supervisor.ex +++ b/lib/new_relic/telemetry/supervisor.ex @@ -8,14 +8,23 @@ defmodule NewRelic.Telemetry.Supervisor do end def init(_) do - children = [ + Supervisor.init( + children(enabled: NewRelic.Config.enabled?()), + strategy: :one_for_one + ) + end + + defp children(enabled: true) do + [ NewRelic.Telemetry.Ecto.Supervisor, NewRelic.Telemetry.Redix, NewRelic.Telemetry.Plug, NewRelic.Telemetry.Phoenix, NewRelic.Telemetry.Oban ] + end - Supervisor.init(children, strategy: :one_for_one) + defp children(enabled: false) do + [] end end diff --git a/test/integration/integration_test.exs b/test/integration/integration_test.exs index 2258de3f..b0712be3 100644 --- a/test/integration/integration_test.exs +++ b/test/integration/integration_test.exs @@ -32,7 +32,7 @@ defmodule IntegrationTest do test "connects to proper collector host" do {:ok, %{"redirect_host" => redirect_host}} = Collector.Protocol.preconnect() - assert redirect_host =~ "collector-" + assert redirect_host =~ "collector" end test "Agent re-connect ability" do diff --git a/test/tracer_test.exs b/test/tracer_test.exs index 9fce9a83..99db342e 100644 --- a/test/tracer_test.exs +++ b/test/tracer_test.exs @@ -214,6 +214,6 @@ defmodule TracerTest do assert Traced.db_query() == :result metrics = TestHelper.gather_harvest(Collector.Metric.Harvester) - assert metrics == [] + refute Enum.any?(metrics, fn [%{name: name}, _] -> name =~ "db_query" end) end end