From 16de0064181634df18d5a8f8221cb4f5a4d37214 Mon Sep 17 00:00:00 2001 From: Vince Foley Date: Thu, 9 Jan 2025 13:58:50 -0800 Subject: [PATCH] Add extended attrs for functions, make extended attrs configurable --- lib/new_relic/config.ex | 8 +++++- lib/new_relic/init.ex | 5 ++++ lib/new_relic/telemetry/ecto/handler.ex | 11 ++++++--- lib/new_relic/tracer/report.ex | 27 ++++++++++++++------ test/transaction_test.exs | 33 +++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 12 deletions(-) diff --git a/lib/new_relic/config.ex b/lib/new_relic/config.ex index 4b24f60d..81d888fc 100644 --- a/lib/new_relic/config.ex +++ b/lib/new_relic/config.ex @@ -153,8 +153,10 @@ defmodule NewRelic.Config do * Controls all Redix instrumentation * `:oban_instrumentation_enabled` (default `true`) * Controls all Oban instrumentation - * `:request_queuing_metrics_enabled` + * `:request_queuing_metrics_enabled` (default `true`) * Controls collection of request queuing metrics + * `:extended_attributes` (default `true`) + * Controls reporting extended per-source attributes for datastore, external and function traces ### Configuration @@ -209,6 +211,10 @@ defmodule NewRelic.Config do get(:features, :request_queuing_metrics) end + def feature?(:extended_attributes) do + get(:features, :extended_attributes) + end + @doc """ Some Agent features can be controlled via configuration. diff --git a/lib/new_relic/init.ex b/lib/new_relic/init.ex index c507ced5..64947cf5 100644 --- a/lib/new_relic/init.ex +++ b/lib/new_relic/init.ex @@ -117,6 +117,11 @@ defmodule NewRelic.Init do determine_feature( "NEW_RELIC_REQUEST_QUEUING_METRICS_ENABLED", :request_queuing_metrics_enabled + ), + extended_attributes: + determine_feature( + "NEW_RELIC_EXTENDED_ATTRIBUTES_ENABLED", + :extended_attributes_enabled ) }) end diff --git a/lib/new_relic/telemetry/ecto/handler.ex b/lib/new_relic/telemetry/ecto/handler.ex index 9b875867..27ba1ade 100644 --- a/lib/new_relic/telemetry/ecto/handler.ex +++ b/lib/new_relic/telemetry/ecto/handler.ex @@ -88,10 +88,15 @@ defmodule NewRelic.Telemetry.Ecto.Handler do databaseCallCount: 1, databaseDuration: duration_s, datastore_call_count: 1, - datastore_duration_ms: duration_ms, - "datastore.#{table}.call_count": 1, - "datastore.#{table}.duration_ms": duration_ms + datastore_duration_ms: duration_ms ) + + if NewRelic.Config.feature?(:extended_attributes) do + NewRelic.incr_attributes( + "datastore.#{table}.call_count": 1, + "datastore.#{table}.duration_ms": duration_ms + ) + end end end diff --git a/lib/new_relic/tracer/report.ex b/lib/new_relic/tracer/report.ex index 0d6107c4..e402a64f 100644 --- a/lib/new_relic/tracer/report.ex +++ b/lib/new_relic/tracer/report.ex @@ -86,10 +86,12 @@ defmodule NewRelic.Tracer.Report do }) ) - NewRelic.incr_attributes( - "external.#{host}.call_count": 1, - "external.#{host}.duration_ms": duration_ms - ) + if NewRelic.Config.feature?(:extended_attributes) do + NewRelic.incr_attributes( + "external.#{host}.call_count": 1, + "external.#{host}.duration_ms": duration_ms + ) + end NewRelic.report_metric({:external, url, component, method}, duration_s: duration_s) @@ -126,10 +128,12 @@ defmodule NewRelic.Tracer.Report do }) ) - NewRelic.incr_attributes( - "external.#{function_name}.call_count": 1, - "external.#{function_name}.duration_ms": duration_ms - ) + if NewRelic.Config.feature?(:extended_attributes) do + NewRelic.incr_attributes( + "external.#{function_name}.call_count": 1, + "external.#{function_name}.duration_ms": duration_ms + ) + end NewRelic.report_metric({:external, function_name}, duration_s: duration_s) @@ -193,6 +197,13 @@ defmodule NewRelic.Tracer.Report do }) ) + if NewRelic.Config.feature?(:extended_attributes) do + NewRelic.incr_attributes( + "function.#{function_name}.call_count": 1, + "function.#{function_name}.duration_ms": duration_ms + ) + end + NewRelic.report_metric( {:function, function_name}, duration_s: duration_s, diff --git a/test/transaction_test.exs b/test/transaction_test.exs index c66ba8e3..4fe26518 100644 --- a/test/transaction_test.exs +++ b/test/transaction_test.exs @@ -29,6 +29,11 @@ defmodule TransactionTest do send_resp(conn, 200, "bar") end + get "/fn_trace" do + HelperModule.function(10) + send_resp(conn, 200, "fn_trace") + end + get "/incr" do NewRelic.set_transaction_name("/incr") NewRelic.incr_attributes(one: 1, two: 1, four: 2) @@ -540,4 +545,32 @@ defmodule TransactionTest do assert event[:"cowboy.connection_error"] == "timeout" end + + describe "Extended attributes" do + test "can be turned on" do + reset_features = TestHelper.update(:nr_features, extended_attributes: true) + TestHelper.restart_harvest_cycle(Collector.TransactionEvent.HarvestCycle) + + TestHelper.request(TestPlugApp, conn(:get, "/fn_trace")) + + [[_, event]] = TestHelper.gather_harvest(Collector.TransactionEvent.Harvester) + + assert event[:"function.TransactionTest.HelperModule.function/1.call_count"] == 1 + + reset_features.() + end + + test "can be turned off" do + reset_features = TestHelper.update(:nr_features, extended_attributes: false) + TestHelper.restart_harvest_cycle(Collector.TransactionEvent.HarvestCycle) + + TestHelper.request(TestPlugApp, conn(:get, "/fn_trace")) + + [[_, event]] = TestHelper.gather_harvest(Collector.TransactionEvent.Harvester) + + refute event[:"function.TransactionTest.HelperModule.function/1.call_count"] + + reset_features.() + end + end end