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

background_transaction_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: b2a53fe1626fc59cb8367bf537c633456f85cdc9 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Metrics::BackgroundTransaction do
  let(:test_worker_class) { double(:class, name: 'TestWorker') }
  let(:prometheus_metric) { instance_double(Prometheus::Client::Metric, base_labels: {}) }

  before do
    allow(described_class).to receive(:prometheus_metric).and_return(prometheus_metric)
  end

  subject { described_class.new(test_worker_class) }

  RSpec.shared_examples 'metric with worker labels' do |metric_method|
    it 'measures with correct labels and value' do
      value = 1
      expect(prometheus_metric).to receive(metric_method).with({ controller: 'TestWorker', action: 'perform', feature_category: '' }, value)

      subject.send(metric_method, :bau, value)
    end
  end

  describe '#label' do
    it 'returns labels based on class name' do
      expect(subject.labels).to eq(controller: 'TestWorker', action: 'perform', feature_category: '')
    end

    it 'contains only the labels defined for metrics' do
      expect(subject.labels.keys).to contain_exactly(*described_class.superclass::BASE_LABEL_KEYS)
    end

    it 'includes the feature category if there is one' do
      expect(test_worker_class).to receive(:get_feature_category).and_return('source_code_management')
      expect(subject.labels).to include(feature_category: 'source_code_management')
    end
  end

  describe '#increment' do
    let(:prometheus_metric) { instance_double(Prometheus::Client::Counter, :increment, base_labels: {}) }

    it_behaves_like 'metric with worker labels', :increment
  end

  describe '#set' do
    let(:prometheus_metric) { instance_double(Prometheus::Client::Gauge, :set, base_labels: {}) }

    it_behaves_like 'metric with worker labels', :set
  end

  describe '#observe' do
    let(:prometheus_metric) { instance_double(Prometheus::Client::Histogram, :observe, base_labels: {}) }

    it_behaves_like 'metric with worker labels', :observe
  end
end