Repository: newrelic/elixir_agent (elixir)

Issue:
How to configure host `display_name`?
I'm trying to get the server infraestructure to appear under the APN for my Phoenix application without success.

It seems like we need to be able to set the host `display_name` from the Elixir agent and make it match the host name of the linux infrastructure agent.

In this documentation: https://docs.newrelic.com/docs/apm/agents/manage-apm-agents/configuration/add-rename-remove-hosts/#display_name they explain how to do it for other languages.

Is this possible in the Elixir agent?

Thanks a lot for any pointers!

Pull request:
Support host display name
Supports setting "host display name"

https://docs.newrelic.com/docs/apm/agents/manage-apm-agents/configuration/add-rename-remove-hosts/#display_name

Closes #450 

Patch:
diff --git a/lib/new_relic/config.ex b/lib/new_relic/config.ex
index a60e57a..575b5d3 100644
--- a/lib/new_relic/config.ex
+++ b/lib/new_relic/config.ex
@@ -30,9 +30,17 @@ defmodule NewRelic.Config do
   def license_key,
     do: get(:license_key)
 
-  @doc false
-  def host,
-    do: get(:host)
+  @doc """
+  Configure the Agent host display name.
+
+  This can help connecting data from the Infrastructure Agent to the APM application.
+
+  Host display name can be configured in two ways:
+  * Environment variable: `NEW_RELIC_HOST_DISPLAY_NAME=my-host-name`
+  * Application config: `config :new_relic_agent, host_display_name: "my-host-name"`
+  """
+  def host_display_name,
+    do: get(:host_display_name)
 
   @doc """
   Configure the Agent logging mechanism.
diff --git a/lib/new_relic/harvest/collector/connect.ex b/lib/new_relic/harvest/collector/connect.ex
index dd4cc47..72d5e30 100644
--- a/lib/new_relic/harvest/collector/connect.ex
+++ b/lib/new_relic/harvest/collector/connect.ex
@@ -7,6 +7,7 @@ defmodule NewRelic.Harvest.Collector.Connect do
         language: "elixir",
         pid: NewRelic.Util.pid(),
         host: NewRelic.Util.hostname(),
+        display_host: NewRelic.Config.host_display_name(),
         app_name: NewRelic.Config.app_name(),
         labels:
           Enum.map(NewRelic.Config.labels(), fn [key, value] ->
diff --git a/lib/new_relic/init.ex b/lib/new_relic/init.ex
index 6f1a618..638030e 100644
--- a/lib/new_relic/init.ex
+++ b/lib/new_relic/init.ex
@@ -30,6 +30,7 @@ defmodule NewRelic.Init do
     NewRelic.Config.put(%{
       log: determine_config(:log),
       host: host,
+      display_host: determine_config(:display_host),
       port: determine_config(:port, 443) |> parse_port,
       scheme: determine_config(:scheme, "https"),
       app_name: determine_config(:app_name) |> parse_app_names,
diff --git a/lib/new_relic/transaction/complete.ex b/lib/new_relic/transaction/complete.ex
index 2f51cd8..76182a3 100644
--- a/lib/new_relic/transaction/complete.ex
+++ b/lib/new_relic/transaction/complete.ex
@@ -13,6 +13,7 @@ defmodule NewRelic.Transaction.Complete do
       |> transform_time_attrs
       |> identify_transaction_type
       |> transform_queue_duration
+      |> add_host_display_name(NewRelic.Config.host_display_name())
       |> extract_transaction_info(pid)
 
     report_transaction_event(tx_attrs)
@@ -92,6 +93,11 @@ defmodule NewRelic.Transaction.Complete do
 
   defp transform_queue_duration(tx), do: tx
 
+  defp add_host_display_name(tx, nil), do: tx
+
+  defp add_host_display_name(tx, host_display_name),
+    do: Map.put(tx, :"host.displayName", host_display_name)
+
   defp extract_transaction_info(tx_attrs, pid) do
     {function_segments, tx_attrs} = Map.pop(tx_attrs, :function_segments, [])
     {process_spawns, tx_attrs} = Map.pop(tx_attrs, :process_spawns, [])


Test patch:
diff --git a/test/collector_protocol_test.exs b/test/collector_protocol_test.exs
index 8fa693a..3dba825 100644
--- a/test/collector_protocol_test.exs
+++ b/test/collector_protocol_test.exs
@@ -37,6 +37,8 @@ defmodule CollectorProtocolTest do
     assert get_in(payload, [:environment])
            |> Enum.find(&match?(["ERTS Version", _], &1))
 
+    assert Map.has_key?(payload, :display_host)
+
     Jason.encode!(payload)
   end
 
diff --git a/test/transaction_test.exs b/test/transaction_test.exs
index e62a0a5..c66ba8e 100644
--- a/test/transaction_test.exs
+++ b/test/transaction_test.exs
@@ -409,6 +409,21 @@ defmodule TransactionTest do
            end)
   end
 
+  test "Support setting host display name" do
+    reset_config = TestHelper.update(:nr_config, host_display_name: "my-test-host")
+    TestHelper.restart_harvest_cycle(Collector.TransactionEvent.HarvestCycle)
+
+    TestHelper.request(TestPlugApp, conn(:get, "/map"))
+
+    events = TestHelper.gather_harvest(Collector.TransactionEvent.Harvester)
+
+    assert Enum.find(events, fn [_, event] ->
+             event[:path] == "/map" && event[:"host.displayName"] == "my-test-host"
+           end)
+
+    reset_config.()
+  end
+
   test "Allow a transaction to be ignored" do
     TestHelper.restart_harvest_cycle(Collector.TransactionEvent.HarvestCycle)
     Task.Supervisor.start_link(name: TestTaskSup)