Commit 8770e317 authored by Quang-Minh Nguyen (Ex-GitLab)'s avatar Quang-Minh Nguyen (Ex-GitLab) 🌴 Committed by Bob Van Landuyt
Browse files

Add ActionView's relative template identifiers to the span names

parent 883f3730
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -10,6 +10,20 @@ module Labkit
        autoload :Subscriber, "labkit/tracing/rails/action_view/subscriber"

        COMPONENT_TAG = "ActionView"

        # Returns identifier relative to Rails.root. Rails supports different template types and returns corresponding identifiers:
        # - Text template: the identifier is "text template"
        # - Html template: the identifier is "html template"
        # - Inline template: the identifier is "inline template"
        # - Raw template: the identifier is the file path of the template
        # Therefore, the amount of returned identifiers is static.
        def self.template_identifier(payload)
          return if !defined?(::Rails.root) || payload[:identifier].nil?

          # Rails.root returns a Pathname object, whose `to_s` methods returns an absolute path without ending "/"
          # Source: https://github.com/rails/rails/blob/v6.0.3.1/railties/lib/rails.rb#L64
          payload[:identifier].sub("#{::Rails.root}/", "")
        end
      end
    end
  end
+5 −0
Original line number Diff line number Diff line
@@ -7,7 +7,12 @@ module Labkit
        # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
        class RenderCollectionInstrumenter < AbstractInstrumenter
          def span_name(payload)
            identifier = ActionView.template_identifier(payload)
            if identifier.nil?
              "render_collection"
            else
              "render_collection:#{identifier}"
            end
          end

          def tags(payload)
+5 −0
Original line number Diff line number Diff line
@@ -7,7 +7,12 @@ module Labkit
        # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
        class RenderPartialInstrumenter < AbstractInstrumenter
          def span_name(payload)
            identifier = ActionView.template_identifier(payload)
            if identifier.nil?
              "render_partial"
            else
              "render_partial:#{identifier}"
            end
          end

          def tags(payload)
+5 −0
Original line number Diff line number Diff line
@@ -7,7 +7,12 @@ module Labkit
        # For more information on the payloads: https://guides.rubyonrails.org/active_support_instrumentation.html
        class RenderTemplateInstrumenter < AbstractInstrumenter
          def span_name(payload)
            identifier = ActionView.template_identifier(payload)
            if identifier.nil?
              "render_template"
            else
              "render_template:#{identifier}"
            end
          end

          def tags(payload)
+35 −0
Original line number Diff line number Diff line
# frozen_string_literal: true

require "spec_helper"
require_relative "../../../../support/tracing/rails/shared_examples"

describe Labkit::Tracing::Rails::ActionView::RenderCollectionInstrumenter do
@@ -23,4 +26,36 @@ describe Labkit::Tracing::Rails::ActionView::RenderCollectionInstrumenter do
      end
    end
  end

  describe "#span_name" do
    context "when the template identifier is nil do" do
      before do
        allow(Labkit::Tracing::Rails::ActionView).to receive(:template_identifier).and_return(nil)
      end

      it "returns plain span name" do
        expect(
          described_class.new.span_name(
            identifier: "/Users/adam/projects/notifications/app/views/posts/_form.html.erb",
          )
        ).to eql("render_collection")
      end
    end

    context "when a template identifier is returned do" do
      before do
        allow(Labkit::Tracing::Rails::ActionView).to receive(:template_identifier).and_return(
          "app/views/hello.html.erb"
        )
      end

      it "returns plain span name" do
        expect(
          described_class.new.span_name(
            identifier: "/Users/adam/projects/notifications/app/views/posts/_form.html.erb",
          )
        ).to eql("render_collection:app/views/hello.html.erb")
      end
    end
  end
end
Loading