summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/boot_time_tracker_spec.rb
blob: 8a17fa8dd2efe9b2b45e2e31434b37b4ea495627 (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
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe Gitlab::Metrics::BootTimeTracker do
  let(:logger) { double('logger') }
  let(:gauge) { double('gauge') }

  subject(:tracker) { described_class.instance }

  before do
    described_class.instance.reset!

    allow(logger).to receive(:info)
    allow(gauge).to receive(:set)
    allow(Gitlab::Metrics).to receive(:gauge).and_return(gauge)
  end

  describe '#track_boot_time!' do
    described_class::SUPPORTED_RUNTIMES.each do |runtime|
      context "when called on #{runtime} for the first time" do
        before do
          expect(Gitlab::Runtime).to receive(:safe_identify).and_return(runtime)
        end

        it 'set the startup_time' do
          tracker.track_boot_time!(logger: logger)

          expect(tracker.startup_time).to be > 0
        end

        it 'records the current process runtime' do
          expect(Gitlab::Metrics::System).to receive(:process_runtime_elapsed_seconds).once

          tracker.track_boot_time!(logger: logger)
        end

        it 'logs the application boot time' do
          expect(Gitlab::Metrics::System).to receive(:process_runtime_elapsed_seconds).and_return(42)
          expect(logger).to receive(:info).with(message: 'Application boot finished', runtime: runtime.to_s, duration_s: 42)

          tracker.track_boot_time!(logger: logger)
        end

        it 'tracks boot time in a prometheus gauge' do
          expect(Gitlab::Metrics::System).to receive(:process_runtime_elapsed_seconds).and_return(42)
          expect(gauge).to receive(:set).with({}, 42)

          tracker.track_boot_time!(logger: logger)
        end

        context 'on subsequent calls' do
          it 'does nothing' do
            tracker.track_boot_time!(logger: logger)

            expect(Gitlab::Metrics::System).not_to receive(:process_runtime_elapsed_seconds)
            expect(logger).not_to receive(:info)
            expect(gauge).not_to receive(:set)

            tracker.track_boot_time!(logger: logger)
          end
        end
      end
    end

    context 'when called on other runtimes' do
      it 'does nothing' do
        tracker.track_boot_time!(logger: logger)

        expect(Gitlab::Metrics::System).not_to receive(:process_runtime_elapsed_seconds)
        expect(logger).not_to receive(:info)
        expect(gauge).not_to receive(:set)

        tracker.track_boot_time!(logger: logger)
      end
    end
  end

  describe '#startup_time' do
    it 'returns 0 when boot time not tracked' do
      expect(tracker.startup_time).to eq(0)
    end
  end
end