From 52bb44eddd7dc8f5285c67dc3a5a8e0f217a92c1 Mon Sep 17 00:00:00 2001 From: Hannah Ramadan <76922290+hannahramadan@users.noreply.github.com> Date: Fri, 11 Oct 2024 09:23:17 -0700 Subject: [PATCH] Update View Component metric building (#2907) * Use default `View/component` metric name for unidentified View Components --- CHANGELOG.md | 6 +++++- .../view_component/instrumentation.rb | 15 +++++++-------- .../view_component_instrumentation_test.rb | 6 ++---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e26f1faa46..f897f40b84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,11 @@ ## dev -Version resolves a bug in rdkafka instrumentation when using the karafka-rdkafka gem. +Version updates View Componment instrumentation to use a default metric name when one is unavaliable and resolves a bug in rdkafka instrumentation when using the karafka-rdkafka gem. + +- **Feature: Use default `View/component` metric name for unidentified View Components** + + Previously, when a View Component metric name could not be identified, the agent would set the name as `nil`. Now, the agent defaults to using `View/component` as the metric name when one can not be identified. [PR#2907](https://github.com/newrelic/newrelic-ruby-agent/pull/2907) - **Bugfix: Instrumentation errors when using the karafka-rdkafka gem** diff --git a/lib/new_relic/agent/instrumentation/view_component/instrumentation.rb b/lib/new_relic/agent/instrumentation/view_component/instrumentation.rb index 3005214c36..b565f81e10 100644 --- a/lib/new_relic/agent/instrumentation/view_component/instrumentation.rb +++ b/lib/new_relic/agent/instrumentation/view_component/instrumentation.rb @@ -10,12 +10,7 @@ def render_in_with_tracing(*args) NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME) begin - segment = NewRelic::Agent::Tracer.start_segment( - name: metric_name( - self.class.respond_to?(:identifier) ? self.class.identifier : nil, - self.class.name - ) - ) + segment = NewRelic::Agent::Tracer.start_segment(name: metric_name) yield rescue => e NewRelic::Agent.notice_error(e) @@ -25,8 +20,12 @@ def render_in_with_tracing(*args) end end - def metric_name(identifier, component) - "View/#{metric_path(identifier)}/#{component}" + def metric_name + "View/#{metric_path(self.class.source_location)}/#{self.class.name}" + rescue => e + NewRelic::Agent.logger.error('Error identifying View Component metric name', e) + + 'View/component' end def metric_path(identifier) diff --git a/test/multiverse/suites/view_component/view_component_instrumentation_test.rb b/test/multiverse/suites/view_component/view_component_instrumentation_test.rb index 01e36d291c..f446a1d469 100644 --- a/test/multiverse/suites/view_component/view_component_instrumentation_test.rb +++ b/test/multiverse/suites/view_component/view_component_instrumentation_test.rb @@ -58,14 +58,12 @@ def test_error_raised end end - # Test metric name being built when the controller class doesn't respond to :identifier - # https://github.com/newrelic/newrelic-ruby-agent/pull/2870 - def test_the_metric_name_omits_the_identifier_when_absent + def test_the_metric_name_records_default_name_on_error in_transaction do |txn| FAKE_CLASS.render_in_with_tracing { 11 * 38 } actual_name = txn.segments.last.name - assert_equal 'View/component/DummyViewComponentInstrumentationClass', actual_name + assert_equal 'View/component', actual_name end end end