From 0fe221e32837b8c117a0f721be0e8fc4c4608e6c Mon Sep 17 00:00:00 2001 From: Vince Foley Date: Mon, 10 Feb 2025 22:14:19 -0800 Subject: [PATCH 1/2] Fix bandit response time metric --- .../phx_example_web/controllers/page_controller.ex | 1 + examples/apps/phx_example/test/phx_example_test.exs | 11 +++++++++++ lib/new_relic/telemetry/plug.ex | 2 ++ lib/new_relic/transaction/complete.ex | 6 +++--- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/examples/apps/phx_example/lib/phx_example_web/controllers/page_controller.ex b/examples/apps/phx_example/lib/phx_example_web/controllers/page_controller.ex index 4af96ed0..c188deb6 100644 --- a/examples/apps/phx_example/lib/phx_example_web/controllers/page_controller.ex +++ b/examples/apps/phx_example/lib/phx_example_web/controllers/page_controller.ex @@ -2,6 +2,7 @@ defmodule PhxExampleWeb.PageController do use PhxExampleWeb, :controller def index(conn, _params) do + Process.sleep(300) render(conn, :index) end diff --git a/examples/apps/phx_example/test/phx_example_test.exs b/examples/apps/phx_example/test/phx_example_test.exs index a8b57f86..a9381c97 100644 --- a/examples/apps/phx_example/test/phx_example_test.exs +++ b/examples/apps/phx_example/test/phx_example_test.exs @@ -34,6 +34,17 @@ defmodule PhxExampleTest do assert event[:"phoenix.controller"] == "PhxExampleWeb.PageController" assert event[:"phoenix.action"] == "index" assert event[:status] == 200 + + [ + %{name: "WebTransactionTotalTime", scope: ""}, + [1, value, _, _, _, _] + ] = + TestHelper.find_metric( + metrics, + "WebTransactionTotalTime" + ) + + assert_in_delta value, 0.3, 0.1 end test "Phoenix metrics generated for LiveView" do diff --git a/lib/new_relic/telemetry/plug.ex b/lib/new_relic/telemetry/plug.ex index 351c6902..2096ff5a 100644 --- a/lib/new_relic/telemetry/plug.ex +++ b/lib/new_relic/telemetry/plug.ex @@ -140,6 +140,7 @@ defmodule NewRelic.Telemetry.Plug do defp add_start_attrs(meta, meas, headers, :cowboy) do [ pid: inspect(self()), + "http.server": "cowboy", start_time: meas[:system_time], host: meta.req.host, path: meta.req.path, @@ -155,6 +156,7 @@ defmodule NewRelic.Telemetry.Plug do defp add_start_attrs(%{conn: conn}, meas, headers, :bandit) do [ pid: inspect(self()), + "http.server": "bandit", start_time: meas[:system_time], host: conn.host, path: conn.request_path, diff --git a/lib/new_relic/transaction/complete.ex b/lib/new_relic/transaction/complete.ex index 8bf057ff..67239a22 100644 --- a/lib/new_relic/transaction/complete.ex +++ b/lib/new_relic/transaction/complete.ex @@ -170,12 +170,12 @@ defmodule NewRelic.Transaction.Complete do {[segment_tree], tx_attrs, tx_error, span_events, apdex, tx_metrics} end - defp total_time_s(%{transactionType: :Web}, concurrent_process_time_ms) do - # Cowboy request process is already included in concurrent time + defp total_time_s(%{transactionType: :Web, "http.server": "cowboy"}, concurrent_process_time_ms) do + # Cowboy request process duration is already included in concurrent time concurrent_process_time_ms / 1000 end - defp total_time_s(%{transactionType: :Other} = tx_attrs, concurrent_process_time_ms) do + defp total_time_s(tx_attrs, concurrent_process_time_ms) do (tx_attrs.duration_ms + concurrent_process_time_ms) / 1000 end From 37829a78929fe6b36626bbca5c5b428ea0e04868 Mon Sep 17 00:00:00 2001 From: Vince Foley Date: Mon, 10 Feb 2025 22:29:39 -0800 Subject: [PATCH 2/2] Send complete TransactionTotalTime metrics --- lib/new_relic/metric/metric_data.ex | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/new_relic/metric/metric_data.ex b/lib/new_relic/metric/metric_data.ex index d16b8017..fd64736f 100644 --- a/lib/new_relic/metric/metric_data.ex +++ b/lib/new_relic/metric/metric_data.ex @@ -60,11 +60,13 @@ defmodule NewRelic.Metric.MetricData do min_call_time: total_time_s, max_call_time: total_time_s }, - # Transaction breakdown doesn't handle Elixir's level of concurrency, - # sending just call count improves things %Metric{ name: join(["WebTransactionTotalTime", name]), - call_count: 1 + call_count: 1, + total_call_time: total_time_s, + total_exclusive_time: total_time_s, + min_call_time: total_time_s, + max_call_time: total_time_s } ] @@ -98,11 +100,13 @@ defmodule NewRelic.Metric.MetricData do min_call_time: total_time_s, max_call_time: total_time_s }, - # Transaction breakdown doesn't handle Elixir's level of concurrency, - # sending just call count improves things %Metric{ name: join(["OtherTransactionTotalTime", name]), - call_count: 1 + call_count: 1, + total_call_time: total_time_s, + total_exclusive_time: total_time_s, + min_call_time: total_time_s, + max_call_time: total_time_s } ]