Sha256: 7e24453b8ef0c5cfcf4fd90a30a0dfdb3ae55983b1f13a14e0369a022db694b8

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module NewRelic::Agent::Instrumentation
  module ViewComponent
    INSTRUMENTATION_NAME = NewRelic::Agent.base_name(name)

    def render_in_with_tracing(*args)
      NewRelic::Agent.record_instrumentation_invocation(INSTRUMENTATION_NAME)

      begin
        segment = NewRelic::Agent::Tracer.start_segment(name: metric_name)
        yield
      rescue => e
        NewRelic::Agent.notice_error(e)
        raise
      ensure
        segment&.finish
      end
    end

    def metric_name
      # ViewComponent determines a component's identifier differently depending on the version
      # https://github.com/ViewComponent/view_component/pull/2153
      component_identifier = defined?(self.class.source_location) ? self.class.source_location : self.class.identifier

      "View/#{metric_path(component_identifier)}/#{self.class.name}"
    rescue => e
      NewRelic::Agent.logger.error('Error identifying View Component metric name', e)

      'View/component'
    end

    def metric_path(identifier)
      return 'component' unless identifier

      if (parts = identifier.split('/')).size > 1
        parts[-2..-1].join('/') # Get filepath by assuming the Rails' structure: app/components/home/example_component.rb
      else
        NewRelic::Agent::UNKNOWN_METRIC
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
newrelic_rpm-9.16.0 lib/new_relic/agent/instrumentation/view_component/instrumentation.rb