summaryrefslogtreecommitdiff
path: root/spec/support/shared_contexts/lib/gitlab/sidekiq_middleware/server_metrics_shared_context.rb
blob: b6c54e902a2e57eefb0e7e7bb8ec3abb92500c46 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# frozen_string_literal: true

RSpec.shared_context 'server metrics with mocked prometheus' do
  let(:concurrency_metric) { double('concurrency metric') }

  let(:queue_duration_seconds) { double('queue duration seconds metric') }
  let(:completion_seconds_metric) { double('completion seconds metric') }
  let(:user_execution_seconds_metric) { double('user execution seconds metric') }
  let(:db_seconds_metric) { double('db seconds metric') }
  let(:gitaly_seconds_metric) { double('gitaly seconds metric') }
  let(:failed_total_metric) { double('failed total metric') }
  let(:retried_total_metric) { double('retried total metric') }
  let(:interrupted_total_metric) { double('interrupted total metric') }
  let(:redis_requests_total) { double('redis calls total metric') }
  let(:running_jobs_metric) { double('running jobs metric') }
  let(:redis_seconds_metric) { double('redis seconds metric') }
  let(:elasticsearch_seconds_metric) { double('elasticsearch seconds metric') }
  let(:elasticsearch_requests_total) { double('elasticsearch calls total metric') }
  let(:load_balancing_metric) { double('load balancing metric') }
  let(:sidekiq_mem_total_bytes) { double('sidekiq mem total bytes') }

  before do
    allow(Gitlab::Metrics).to receive(:histogram).and_call_original
    allow(Gitlab::Metrics).to receive(:counter).and_call_original

    allow(Gitlab::Metrics).to receive(:histogram).with(:sidekiq_jobs_queue_duration_seconds, anything, anything, anything).and_return(queue_duration_seconds)
    allow(Gitlab::Metrics).to receive(:histogram).with(:sidekiq_jobs_completion_seconds, anything, anything, anything).and_return(completion_seconds_metric)
    allow(Gitlab::Metrics).to receive(:histogram).with(:sidekiq_jobs_cpu_seconds, anything, anything, anything).and_return(user_execution_seconds_metric)
    allow(Gitlab::Metrics).to receive(:histogram).with(:sidekiq_jobs_db_seconds, anything, anything, anything).and_return(db_seconds_metric)
    allow(Gitlab::Metrics).to receive(:histogram).with(:sidekiq_jobs_gitaly_seconds, anything, anything, anything).and_return(gitaly_seconds_metric)
    allow(Gitlab::Metrics).to receive(:histogram).with(:sidekiq_redis_requests_duration_seconds, anything, anything, anything).and_return(redis_seconds_metric)
    allow(Gitlab::Metrics).to receive(:histogram).with(:sidekiq_elasticsearch_requests_duration_seconds, anything, anything, anything).and_return(elasticsearch_seconds_metric)
    allow(Gitlab::Metrics).to receive(:counter).with(:sidekiq_jobs_failed_total, anything).and_return(failed_total_metric)
    allow(Gitlab::Metrics).to receive(:counter).with(:sidekiq_jobs_retried_total, anything).and_return(retried_total_metric)
    allow(Gitlab::Metrics).to receive(:counter).with(:sidekiq_jobs_interrupted_total, anything).and_return(interrupted_total_metric)
    allow(Gitlab::Metrics).to receive(:counter).with(:sidekiq_redis_requests_total, anything).and_return(redis_requests_total)
    allow(Gitlab::Metrics).to receive(:counter).with(:sidekiq_elasticsearch_requests_total, anything).and_return(elasticsearch_requests_total)
    allow(Gitlab::Metrics).to receive(:counter).with(:sidekiq_load_balancing_count, anything).and_return(load_balancing_metric)
    allow(Gitlab::Metrics).to receive(:gauge).with(:sidekiq_running_jobs, anything, {}, :all).and_return(running_jobs_metric)
    allow(Gitlab::Metrics).to receive(:gauge).with(:sidekiq_concurrency, anything, {}, :all).and_return(concurrency_metric)
    allow(Gitlab::Metrics).to receive(:gauge).with(:sidekiq_mem_total_bytes, anything, {}, :all).and_return(sidekiq_mem_total_bytes)

    allow(concurrency_metric).to receive(:set)
  end
end

RSpec.shared_context 'server metrics call' do
  let(:thread_cputime_before) { 1 }
  let(:thread_cputime_after) { 2 }
  let(:thread_cputime_duration) { thread_cputime_after - thread_cputime_before }

  let(:monotonic_time_before) { 11 }
  let(:monotonic_time_after) { 20 }
  let(:monotonic_time_duration) { monotonic_time_after - monotonic_time_before }

  let(:queue_duration_for_job) { 0.01 }

  let(:db_duration) { 3 }
  let(:gitaly_duration) { 4 }

  let(:redis_calls) { 2 }
  let(:redis_duration) { 0.01 }

  let(:elasticsearch_calls) { 8 }
  let(:elasticsearch_duration) { 0.54 }

  let(:mem_total_bytes) { 1000000000 }
  let(:instrumentation) do
    {
      gitaly_duration_s: gitaly_duration,
      redis_calls: redis_calls,
      redis_duration_s: redis_duration,
      elasticsearch_calls: elasticsearch_calls,
      elasticsearch_duration_s: elasticsearch_duration,
      mem_total_bytes: mem_total_bytes
    }
  end

  before do
    allow(subject).to receive(:get_thread_cputime).and_return(thread_cputime_before, thread_cputime_after)
    allow(Gitlab::Metrics::System).to receive(:monotonic_time).and_return(monotonic_time_before, monotonic_time_after)
    allow(Gitlab::InstrumentationHelper).to receive(:queue_duration_for_job).with(job).and_return(queue_duration_for_job)
    allow(ActiveRecord::LogSubscriber).to receive(:runtime).and_return(db_duration * 1000)

    job[:instrumentation] = instrumentation
    job[:gitaly_duration_s] = gitaly_duration
    job[:redis_calls] = redis_calls
    job[:redis_duration_s] = redis_duration

    job[:elasticsearch_calls] = elasticsearch_calls
    job[:elasticsearch_duration_s] = elasticsearch_duration

    allow(running_jobs_metric).to receive(:increment)
    allow(redis_requests_total).to receive(:increment)
    allow(elasticsearch_requests_total).to receive(:increment)
    allow(queue_duration_seconds).to receive(:observe)
    allow(user_execution_seconds_metric).to receive(:observe)
    allow(db_seconds_metric).to receive(:observe)
    allow(gitaly_seconds_metric).to receive(:observe)
    allow(completion_seconds_metric).to receive(:observe)
    allow(redis_seconds_metric).to receive(:observe)
    allow(elasticsearch_seconds_metric).to receive(:observe)
    allow(sidekiq_mem_total_bytes).to receive(:set)
  end
end