BigW Consortium Gitlab

action_view.rb 1.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
module Gitlab
  module Metrics
    module Subscribers
      # Class for tracking the rendering timings of views.
      class ActionView < ActiveSupport::Subscriber
        attach_to :action_view

        SERIES = 'views'

        def render_template(event)
          track(event) if current_transaction
        end

        alias_method :render_view, :render_template

        private

        def track(event)
          values = values_for(event)
20
          tags   = tags_for(event)
21

22
          current_transaction.increment(:view_duration, event.duration)
23
          current_transaction.add_metric(SERIES, values, tags)
24 25 26 27 28 29 30
        end

        def relative_path(path)
          path.gsub(/^#{Rails.root.to_s}\/?/, '')
        end

        def values_for(event)
31
          { duration: event.duration }
32 33 34 35
        end

        def tags_for(event)
          path = relative_path(event.payload[:identifier])
36

37
          { view: path }
38 39 40 41 42 43 44 45 46
        end

        def current_transaction
          Transaction.current
        end
      end
    end
  end
end