summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/tracing/rails/action_view_subscriber_spec.rb
blob: c9d1a06b3e63aa93484daa9e918a16f068466c12 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# frozen_string_literal: true

require 'fast_spec_helper'
require 'rspec-parameterized'

describe Gitlab::Tracing::Rails::ActionViewSubscriber do
  using RSpec::Parameterized::TableSyntax

  shared_examples 'an actionview notification' do
    it 'should notify the tracer when the hash contains null values' do
      expect(subject).to receive(:postnotify_span).with(notification_name, start, finish, tags: expected_tags, exception: exception)

      subject.public_send(notify_method, start, finish, payload)
    end

    it 'should notify the tracer when the payload is missing values' do
      expect(subject).to receive(:postnotify_span).with(notification_name, start, finish, tags: expected_tags, exception: exception)

      subject.public_send(notify_method, start, finish, payload.compact)
    end

    it 'should not throw exceptions when with the default tracer' do
      expect { subject.public_send(notify_method, start, finish, payload) }.not_to raise_error
    end
  end

  describe '.instrument' do
    it 'is unsubscribeable' do
      unsubscribe = described_class.instrument

      expect(unsubscribe).not_to be_nil
      expect { unsubscribe.call }.not_to raise_error
    end
  end

  describe '#notify_render_template' do
    subject { described_class.new }
    let(:start) { Time.now }
    let(:finish) { Time.now }
    let(:notification_name) { 'render_template' }
    let(:notify_method) { :notify_render_template }

    where(:identifier, :layout, :exception) do
      nil         | nil           | nil
      ""          | nil           | nil
      "show.haml" | nil           | nil
      nil         | ""            | nil
      nil         | "layout.haml" | nil
      nil         | nil           | StandardError.new
    end

    with_them do
      let(:payload) do
        {
          exception: exception,
          identifier: identifier,
          layout: layout
        }
      end

      let(:expected_tags) do
        {
          'component' =>       'ActionView',
          'template.id' =>     identifier,
          'template.layout' => layout
        }
      end

      it_behaves_like 'an actionview notification'
    end
  end

  describe '#notify_render_collection' do
    subject { described_class.new }
    let(:start) { Time.now }
    let(:finish) { Time.now }
    let(:notification_name) { 'render_collection' }
    let(:notify_method) { :notify_render_collection }

    where(
      :identifier, :count, :expected_count, :cache_hits, :expected_cache_hits, :exception) do
      nil         | nil           | 0 | nil | 0 | nil
      ""          | nil           | 0 | nil | 0 | nil
      "show.haml" | nil           | 0 | nil | 0 | nil
      nil         | 0             | 0 | nil | 0 | nil
      nil         | 1             | 1 | nil | 0 | nil
      nil         | nil           | 0 | 0   | 0 | nil
      nil         | nil           | 0 | 1   | 1 | nil
      nil         | nil           | 0 | nil | 0 | StandardError.new
    end

    with_them do
      let(:payload) do
        {
          exception: exception,
          identifier: identifier,
          count: count,
          cache_hits: cache_hits
        }
      end

      let(:expected_tags) do
        {
          'component' =>            'ActionView',
          'template.id' =>          identifier,
          'template.count' =>       expected_count,
          'template.cache.hits' =>  expected_cache_hits
        }
      end

      it_behaves_like 'an actionview notification'
    end
  end

  describe '#notify_render_partial' do
    subject { described_class.new }
    let(:start) { Time.now }
    let(:finish) { Time.now }
    let(:notification_name) { 'render_partial' }
    let(:notify_method) { :notify_render_partial }

    where(:identifier, :exception) do
      nil         | nil
      ""          | nil
      "show.haml" | nil
      nil         | StandardError.new
    end

    with_them do
      let(:payload) do
        {
          exception: exception,
          identifier: identifier
        }
      end

      let(:expected_tags) do
        {
          'component' =>            'ActionView',
          'template.id' =>          identifier
        }
      end

      it_behaves_like 'an actionview notification'
    end
  end
end