BigW Consortium Gitlab

matched_metrics_query_spec.rb 5.05 KB
Newer Older
1 2
require 'spec_helper'

3
describe Gitlab::Prometheus::Queries::MatchedMetricsQuery do
4
  include Prometheus::MetricBuilders
5 6 7

  let(:metric_group_class) { Gitlab::Prometheus::MetricGroup }
  let(:metric_class) { Gitlab::Prometheus::Metric }
8

9 10 11
  def series_info_with_environment(*more_metrics)
    %w{metric_a metric_b}.concat(more_metrics).map { |metric_name| { '__name__' => metric_name, 'environment' => '' } }
  end
12

13 14 15 16 17 18 19 20
  let(:metric_names) { %w{metric_a metric_b} }
  let(:series_info_without_environment) do
    [{ '__name__' => 'metric_a' },
     { '__name__' => 'metric_b' }]
  end
  let(:partialy_empty_series_info) { [{ '__name__' => 'metric_a', 'environment' => '' }] }
  let(:empty_series_info) { [] }

21
  let(:client) { double('prometheus_client') }
22

23 24
  subject { described_class.new(client) }

25
  context 'with one group where two metrics is found' do
26 27 28 29
    before do
      allow(metric_group_class).to receive(:all).and_return([simple_metric_group])
      allow(client).to receive(:label_values).and_return(metric_names)
    end
30

31 32 33 34
    context 'both metrics in the group pass requirements' do
      before do
        allow(client).to receive(:series).and_return(series_info_with_environment)
      end
35

36 37 38 39
      it 'responds with both metrics as actve' do
        expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 2, metrics_missing_requirements: 0 }])
      end
    end
40

41 42 43 44
    context 'none of the metrics pass requirements' do
      before do
        allow(client).to receive(:series).and_return(series_info_without_environment)
      end
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
      it 'responds with both metrics missing requirements' do
        expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 0, metrics_missing_requirements: 2 }])
      end
    end

    context 'no series information found about the metrics' do
      before do
        allow(client).to receive(:series).and_return(empty_series_info)
      end

      it 'responds with both metrics missing requirements' do
        expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 0, metrics_missing_requirements: 2 }])
      end
    end

    context 'one of the series info was not found' do
      before do
        allow(client).to receive(:series).and_return(partialy_empty_series_info)
      end
      it 'responds with one active and one missing metric' do
        expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 1, metrics_missing_requirements: 1 }])
      end
    end
69 70
  end

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  context 'with one group where only one metric is found' do
    before do
      allow(metric_group_class).to receive(:all).and_return([simple_metric_group])
      allow(client).to receive(:label_values).and_return('metric_a')
    end

    context 'both metrics in the group pass requirements' do
      before do
        allow(client).to receive(:series).and_return(series_info_with_environment)
      end

      it 'responds with one metrics as active and no missing requiremens' do
        expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 1, metrics_missing_requirements: 0 }])
      end
    end
86

87 88 89 90 91 92 93 94 95
    context 'no metrics in group pass requirements' do
      before do
        allow(client).to receive(:series).and_return(series_info_without_environment)
      end

      it 'responds with one metrics as active and no missing requiremens' do
        expect(subject.query).to eq([{ group: 'name', priority: 1, active_metrics: 0, metrics_missing_requirements: 1 }])
      end
    end
96 97
  end

98 99 100
  context 'with two groups where metrics are found in each group' do
    let(:second_metric_group) { simple_metric_group(name: 'nameb', metrics: simple_metrics(added_metric_name: 'metric_c')) }

101
    before do
102
      allow(metric_group_class).to receive(:all).and_return([simple_metric_group, second_metric_group])
103 104 105
      allow(client).to receive(:label_values).and_return('metric_c')
    end

106
    context 'all metrics in both groups pass requirements' do
107 108 109 110 111
      before do
        allow(client).to receive(:series).and_return(series_info_with_environment('metric_c'))
      end

      it 'responds with one metrics as active and no missing requiremens' do
112 113
        expect(subject.query).to eq([
                                      { group: 'name', priority: 1, active_metrics: 1, metrics_missing_requirements: 0 },
114 115 116
                                      { group: 'nameb', priority: 1, active_metrics: 2, metrics_missing_requirements: 0 }
                                    ]
                                   )
117 118 119
      end
    end

120
    context 'no metrics in groups pass requirements' do
121 122 123
      before do
        allow(client).to receive(:series).and_return(series_info_without_environment)
      end
124

125
      it 'responds with one metrics as active and no missing requiremens' do
126 127
        expect(subject.query).to eq([
                                      { group: 'name', priority: 1, active_metrics: 0, metrics_missing_requirements: 1 },
128 129 130
                                      { group: 'nameb', priority: 1, active_metrics: 0, metrics_missing_requirements: 2 }
                                    ]
                                   )
131 132
      end
    end
133 134
  end
end