Repository: newrelic/elixir_agent (elixir)

Issue:
Include NEW_RELIC_RULES_IGNORE_URL_REGEXES
<!--
Thanks for your interest in the Elixir Agent

A quick note: This is an open source project, not a New Relic product.

* We want to improve and grow the Agent in ways that can benefit the majority of users
* Features that are specific to just one app are unlikely to make it in
* Instrumentation particular to a library or framework are probably a better fit for their own package which depends on this Agent

-->

**Is your feature request related to a problem? Please describe.**
We have paths for `health-checks` and `swagger` documentation which we don't want to report to New Relic.

**Describe the solution you'd like**
It would be nice to include an env variable like `NEW_RELIC_RULES_IGNORE_URL_REGEXES`, which allows to have a list of regexes in a comma-separated string to be ignored.

Another option could be a route option like `newrelic_ignore`.



Pull request:
Enable ignoring a transaction
Add a function that enables the user to mark the current transaction to be ignored. The agent will stop tracking information about it and won't report any information about it.

```elixir
NewRelic.ignore_transaction()
```

closes #52 

Patch:
diff --git a/lib/new_relic.ex b/lib/new_relic.ex
index 6a17583..39b8c32 100644
--- a/lib/new_relic.ex
+++ b/lib/new_relic.ex
@@ -57,6 +57,11 @@ defmodule NewRelic do
   @spec start_transaction(String.t(), String.t()) :: none()
   defdelegate start_transaction(category, name), to: NewRelic.Transaction
 
+  @doc """
+  Call within a transaction to prevent it from reporting.
+  """
+  defdelegate ignore_transaction(), to: NewRelic.Transaction
+
   @doc """
   Store information about the type of work the current span is doing.
 
diff --git a/lib/new_relic/transaction.ex b/lib/new_relic/transaction.ex
index 7ce3f4f..190c4c6 100644
--- a/lib/new_relic/transaction.ex
+++ b/lib/new_relic/transaction.ex
@@ -55,4 +55,10 @@ defmodule NewRelic.Transaction do
     NewRelic.DistributedTrace.generate_new_context()
     |> NewRelic.DistributedTrace.track_transaction(transport_type: "Other")
   end
+
+  @doc false
+  def ignore_transaction() do
+    NewRelic.Transaction.Reporter.ignore_transaction()
+    NewRelic.DistributedTrace.Tracker.cleanup(self())
+  end
 end
diff --git a/lib/new_relic/transaction/reporter.ex b/lib/new_relic/transaction/reporter.ex
index 472a6b4..8614d2d 100644
--- a/lib/new_relic/transaction/reporter.ex
+++ b/lib/new_relic/transaction/reporter.ex
@@ -55,6 +55,14 @@ defmodule NewRelic.Transaction.Reporter do
     end
   end
 
+  def ignore_transaction() do
+    if tracking?(self()) do
+      AttrStore.untrack(__MODULE__, self())
+      AttrStore.purge(__MODULE__, self())
+      ensure_purge(self())
+    end
+  end
+
   def fail(%{kind: kind, reason: reason, stack: stack} = error) do
     if tracking?(self()) do
       if NewRelic.Config.feature?(:error_collector) do


Test patch:
diff --git a/test/transaction_test.exs b/test/transaction_test.exs
index af8d068..6b2ab6d 100644
--- a/test/transaction_test.exs
+++ b/test/transaction_test.exs
@@ -92,6 +92,11 @@ defmodule TransactionTest do
       Process.sleep(100)
       send_resp(conn, 200, "spawn")
     end
+
+    get "/ignored" do
+      NewRelic.ignore_transaction()
+      send_resp(conn, 200, "ignored")
+    end
   end
 
   test "Basic transaction" do
@@ -241,4 +246,15 @@ defmodule TransactionTest do
                event["deep.foo.baz"] == "bar"
            end)
   end
+
+  test "Allow a transaction to be ignored" do
+    TestHelper.restart_harvest_cycle(Collector.TransactionEvent.HarvestCycle)
+    Task.Supervisor.start_link(name: TestTaskSup)
+
+    TestHelper.request(TestPlugApp, conn(:get, "/ignored"))
+
+    events = TestHelper.gather_harvest(Collector.TransactionEvent.Harvester)
+
+    assert events == []
+  end
 end