summaryrefslogtreecommitdiff
path: root/spec/services/ci/prometheus_metrics/observe_histograms_service_spec.rb
blob: 2eef852b0f4e3aff610a3c4606dd3d41b2b654d8 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::PrometheusMetrics::ObserveHistogramsService do
  let_it_be(:project) { create(:project) }
  let(:params) { {} }

  subject(:execute) { described_class.new(project, params).execute }

  before do
    Gitlab::Metrics.reset_registry!
  end

  context 'with empty data' do
    it 'does not raise errors' do
      is_expected.to be_success
    end
  end

  context 'observes metrics successfully' do
    let(:params) do
      {
        histograms: [
          { name: 'pipeline_graph_link_calculation_duration_seconds', value: '1' },
          { name: 'pipeline_graph_links_per_job_ratio', value: '0.9' }
        ]
      }
    end

    it 'increments the metrics' do
      execute

      expect(histogram_data).to match(a_hash_including({ 0.8 => 0.0, 1 => 1.0, 2 => 1.0 }))

      expect(histogram_data(:pipeline_graph_links_per_job_ratio))
        .to match(a_hash_including({ 0.8 => 0.0, 0.9 => 1.0, 1 => 1.0 }))
    end

    it 'returns an empty body and status code' do
      is_expected.to be_success
      expect(subject.http_status).to eq(:created)
      expect(subject.payload).to eq({})
    end
  end

  context 'with unknown histograms' do
    let(:params) do
      { histograms: [{ name: 'chunky_bacon', value: '4' }] }
    end

    it 'raises ActiveRecord::RecordNotFound error' do
      expect { subject }.to raise_error ActiveRecord::RecordNotFound
    end
  end

  context 'with feature flag disabled' do
    before do
      stub_feature_flags(ci_accept_frontend_prometheus_metrics: false)
    end

    let(:params) do
      {
        histograms: [
          { name: 'pipeline_graph_link_calculation_duration_seconds', value: '4' }
        ]
      }
    end

    it 'does not register the metrics' do
      execute

      expect(histogram_data).to be_nil
    end

    it 'returns an empty body and status code' do
      is_expected.to be_success
      expect(subject.http_status).to eq(:accepted)
      expect(subject.payload).to eq({})
    end
  end

  def histogram_data(name = :pipeline_graph_link_calculation_duration_seconds)
    Gitlab::Metrics.registry.get(name)&.get({})
  end
end