influx_sampler_spec.rb 2.79 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'spec_helper'

5
describe Gitlab::Metrics::Samplers::InfluxSampler do
6 7 8
  let(:sampler) { described_class.new(5) }

  describe '#start' do
9 10 11
    it 'runs once and gathers a sample at a given interval' do
      expect(sampler).to receive(:sleep).with(a_kind_of(Numeric)).twice
      expect(sampler).to receive(:sample).once
12
      expect(sampler).to receive(:running).and_return(true, false)
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

      sampler.start.join
    end
  end

  describe '#sample' do
    it 'samples various statistics' do
      expect(sampler).to receive(:sample_memory_usage)
      expect(sampler).to receive(:sample_file_descriptors)
      expect(sampler).to receive(:flush)

      sampler.sample
    end
  end

  describe '#flush' do
    it 'schedules the metrics using Sidekiq' do
30 31
      expect(Gitlab::Metrics).to receive(:submit_metrics)
        .with([an_instance_of(Hash)])
32 33 34 35 36 37 38 39

      sampler.sample_memory_usage
      sampler.flush
    end
  end

  describe '#sample_memory_usage' do
    it 'adds a metric containing the memory usage' do
40 41
      expect(Gitlab::Metrics::System).to receive(:memory_usage)
        .and_return(9000)
42

43 44 45
      expect(sampler).to receive(:add_metric)
        .with(/memory_usage/, value: 9000)
        .and_call_original
46 47 48 49 50 51 52

      sampler.sample_memory_usage
    end
  end

  describe '#sample_file_descriptors' do
    it 'adds a metric containing the amount of open file descriptors' do
53 54
      expect(Gitlab::Metrics::System).to receive(:file_descriptor_count)
        .and_return(4)
55

56 57 58
      expect(sampler).to receive(:add_metric)
        .with(/file_descriptors/, value: 4)
        .and_call_original
59 60 61 62 63

      sampler.sample_file_descriptors
    end
  end

64 65
  describe '#add_metric' do
    it 'prefixes the series name for a Rails process' do
66
      expect(Gitlab::Runtime).to receive(:sidekiq?).and_return(false)
67

68 69 70
      expect(Gitlab::Metrics::Metric).to receive(:new)
        .with('rails_cats', { value: 10 }, {})
        .and_call_original
71 72 73 74 75

      sampler.add_metric('cats', value: 10)
    end

    it 'prefixes the series name for a Sidekiq process' do
76
      expect(Gitlab::Runtime).to receive(:sidekiq?).and_return(true)
77

78 79 80
      expect(Gitlab::Metrics::Metric).to receive(:new)
        .with('sidekiq_cats', { value: 10 }, {})
        .and_call_original
81 82 83 84

      sampler.add_metric('cats', value: 10)
    end
  end
85 86 87 88 89 90 91 92 93 94 95 96 97 98

  describe '#sleep_interval' do
    it 'returns a Numeric' do
      expect(sampler.sleep_interval).to be_a_kind_of(Numeric)
    end

    # Testing random behaviour is very hard, so treat this test as a basic smoke
    # test instead of a very accurate behaviour/unit test.
    it 'does not return the same interval twice in a row' do
      last = nil

      100.times do
        interval = sampler.sleep_interval

99
        expect(interval).not_to eq(last)
100 101 102 103 104

        last = interval
      end
    end
  end
105
end