Skip to content

Commit

Permalink
Merge pull request #479 from newrelic/vince/extended-function-attrs
Browse files Browse the repository at this point in the history
Add extended attrs for functions, make extended attrs configurable
  • Loading branch information
tpitale authored Jan 13, 2025
2 parents 185c2b1 + 16de006 commit 4168f46
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
8 changes: 7 additions & 1 deletion lib/new_relic/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions lib/new_relic/init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions lib/new_relic/telemetry/ecto/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 19 additions & 8 deletions lib/new_relic/tracer/report.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions test/transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit 4168f46

Please sign in to comment.