summaryrefslogtreecommitdiff
path: root/lib/gitlab/metrics/subscribers/action_view.rb
blob: 3da474fc1ec708e23e81141c3037d4bc203ffb9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module Gitlab
  module Metrics
    module Subscribers
      # Class for tracking the rendering timings of views.
      class ActionView < ActiveSupport::Subscriber
        attach_to :action_view

        SERIES = 'views'.freeze

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

        alias_method :render_view, :render_template

        private

        def metric_view_rendering_duration_seconds
          @metric_view_rendering_duration_seconds ||= Gitlab::Metrics.histogram(
            :gitlab_view_rendering_duration_seconds,
            'View rendering time',
            Transaction::BASE_LABELS.merge({ path: nil }),
            [0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.500, 2.0, 10.0]
          )
        end

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

          metric_view_rendering_duration_seconds.observe(
            current_transaction.labels.merge(tags),
            event.duration
          )

          current_transaction.increment(:view_duration, event.duration)
          current_transaction.add_metric(SERIES, values, tags)
        end

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

        def values_for(event)
          { duration: event.duration }
        end

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

          { view: path }
        end

        def current_transaction
          Transaction.current
        end
      end
    end
  end
end