Welcome to mirror list, hosted at ThFree Co, Russian Federation.

global_search_slis_spec.rb « metrics « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68793db6e41040e389bff9eeb7a11c173c1d8dce (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Metrics::GlobalSearchSlis, feature_category: :global_search do
  using RSpec::Parameterized::TableSyntax

  describe '#initialize_slis!' do
    let(:api_endpoint_labels) do
      [a_hash_including(endpoint_id: 'GET /api/:version/search')]
    end

    let(:web_endpoint_labels) do
      [a_hash_including(endpoint_id: "SearchController#show")]
    end

    let(:all_endpoint_labels) do
      api_endpoint_labels + web_endpoint_labels
    end

    it 'initializes Apdex SLIs for global_search' do
      expect(Gitlab::Metrics::Sli::Apdex).to receive(:initialize_sli).with(
        :global_search,
        array_including(all_endpoint_labels)
      )

      described_class.initialize_slis!
    end

    it 'initializes ErrorRate SLIs for global_search' do
      expect(Gitlab::Metrics::Sli::ErrorRate).to receive(:initialize_sli).with(
        :global_search,
        array_including(all_endpoint_labels)
      )

      described_class.initialize_slis!
    end

    context "when initializeing for limited types" do
      where(:api, :web) do
        [true, false].repeated_permutation(2).to_a
      end

      with_them do
        it 'only initializes for the relevant endpoints', :aggregate_failures do
          allow(Gitlab::Metrics::Environment).to receive(:api?).and_return(api)
          allow(Gitlab::Metrics::Environment).to receive(:web?).and_return(web)
          allow(Gitlab::Metrics::Sli::Apdex).to receive(:initialize_sli)
          allow(Gitlab::Metrics::Sli::ErrorRate).to receive(:initialize_sli)

          described_class.initialize_slis!

          if api
            expect(Gitlab::Metrics::Sli::Apdex).to(
              have_received(:initialize_sli).with(:global_search, array_including(*api_endpoint_labels))
            )
            expect(Gitlab::Metrics::Sli::ErrorRate).to(
              have_received(:initialize_sli).with(:global_search, array_including(*api_endpoint_labels))
            )
          else
            expect(Gitlab::Metrics::Sli::Apdex).not_to(
              have_received(:initialize_sli).with(:global_search, array_including(*api_endpoint_labels))
            )
            expect(Gitlab::Metrics::Sli::ErrorRate).not_to(
              have_received(:initialize_sli).with(:global_search, array_including(*api_endpoint_labels))
            )
          end

          if web
            expect(Gitlab::Metrics::Sli::Apdex).to(
              have_received(:initialize_sli).with(:global_search, array_including(*web_endpoint_labels))
            )
            expect(Gitlab::Metrics::Sli::ErrorRate).to(
              have_received(:initialize_sli).with(:global_search, array_including(*web_endpoint_labels))
            )
          else
            expect(Gitlab::Metrics::Sli::Apdex).not_to(
              have_received(:initialize_sli).with(:global_search, array_including(*web_endpoint_labels))
            )
            expect(Gitlab::Metrics::Sli::ErrorRate).not_to(
              have_received(:initialize_sli).with(:global_search, array_including(*web_endpoint_labels))
            )
          end
        end
      end
    end
  end

  describe '#record_apdex' do
    where(:search_type, :code_search, :duration_target) do
      'basic'    | false | 8.812
      'basic'    | true  | 27.538
      'advanced' | false | 2.452
      'advanced' | true  | 15.52
      'zoekt'    | true  | 15.52
    end

    with_them do
      before do
        allow(::Gitlab::ApplicationContext).to receive(:current_context_attribute).with(:caller_id).and_return('end')
      end

      let(:search_scope) { code_search ? 'blobs' : 'issues' }

      it 'increments the global_search SLI as a success if the elapsed time is within the target' do
        duration = duration_target - 0.1

        expect(Gitlab::Metrics::Sli::Apdex[:global_search]).to receive(:increment).with(
          labels: {
            search_type: search_type,
            search_level: 'global',
            search_scope: search_scope,
            endpoint_id: 'end'
          },
          success: true
        )

        described_class.record_apdex(
          elapsed: duration,
          search_type: search_type,
          search_level: 'global',
          search_scope: search_scope
        )
      end

      it 'increments the global_search SLI as a failure if the elapsed time is not within the target' do
        duration = duration_target + 0.1

        expect(Gitlab::Metrics::Sli::Apdex[:global_search]).to receive(:increment).with(
          labels: {
            search_type: search_type,
            search_level: 'global',
            search_scope: search_scope,
            endpoint_id: 'end'
          },
          success: false
        )

        described_class.record_apdex(
          elapsed: duration,
          search_type: search_type,
          search_level: 'global',
          search_scope: search_scope
        )
      end
    end
  end

  describe '#record_error_rate' do
    it 'calls increment on the error rate SLI' do
      expect(Gitlab::Metrics::Sli::ErrorRate[:global_search]).to receive(:increment)

      described_class.record_error_rate(
        error: true,
        search_type: 'basic',
        search_level: 'global',
        search_scope: 'issues'
      )
    end
  end
end