Skip to content

Commit

Permalink
Only attach telemetry handlers when agent is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryseed committed Jan 8, 2025
1 parent 2d7eeae commit 87866ee
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 6 deletions.
2 changes: 2 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 3 additions & 1 deletion examples/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion lib/new_relic/harvest/collector/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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) 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]
Expand Down Expand Up @@ -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}
Expand Down
13 changes: 11 additions & 2 deletions lib/new_relic/telemetry/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions test/evil_collector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule EvilCollectorTest do

setup_all do
Application.put_env(:new_relic_agent, :collector_instance_host, "localhost")
Application.put_env(:new_relic_agent, :bypass_collector, false)

reset_config =
TestHelper.update(:nr_config,
Expand All @@ -39,6 +40,7 @@ defmodule EvilCollectorTest do

on_exit(fn ->
Application.delete_env(:new_relic_agent, :collector_instance_host)
Application.put_env(:new_relic_agent, :bypass_collector, true)
reset_config.()
end)

Expand Down
2 changes: 1 addition & 1 deletion test/integration/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/tracer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 87866ee

Please sign in to comment.