diff options
author | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-04-26 22:09:03 +0200 |
---|---|---|
committer | Pawel Chojnacki <pawel@chojnacki.ws> | 2017-05-11 23:07:12 +0200 |
commit | 4f824d2aecf4fec46febd767fa7f7d747c732112 (patch) | |
tree | 09b0b6458b7ec4fc414431f1d323b01bb68265ef /spec | |
parent | e7b53dd67815a306dfb57cb7545d06391bc12aa7 (diff) | |
download | gitlab-ce-4f824d2aecf4fec46febd767fa7f7d747c732112.tar.gz |
Custom queries for prometheus
- Tests for prometheus queries
- fix rubocop warnings
- Remove unused method. Add more queries to deployment queries.
- Wrap BaseQuery in module hierarchy
Renname Prometheus class to PrometheusClient
Diffstat (limited to 'spec')
-rw-r--r-- | spec/lib/gitlab/prometheus/queries/deployment_query_spec.rb | 31 | ||||
-rw-r--r-- | spec/lib/gitlab/prometheus_client_spec.rb (renamed from spec/lib/gitlab/prometheus_spec.rb) | 2 | ||||
-rw-r--r-- | spec/models/project_services/prometheus_service_spec.rb | 30 | ||||
-rw-r--r-- | spec/support/prometheus_helpers.rb | 6 |
4 files changed, 53 insertions, 16 deletions
diff --git a/spec/lib/gitlab/prometheus/queries/deployment_query_spec.rb b/spec/lib/gitlab/prometheus/queries/deployment_query_spec.rb new file mode 100644 index 00000000000..c69cd180547 --- /dev/null +++ b/spec/lib/gitlab/prometheus/queries/deployment_query_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe Gitlab::Prometheus::Queries::DeploymentQuery, lib: true do + let(:environment) { create(:environment) } + let(:deployment) { create(:deployment, environment: environment) } + + let(:client) { double('prometheus_client') } + subject { described_class.new(client) } + + it 'sends appropriate queries to prometheus' do + start_time = (deployment.created_at - 30.minutes).to_f + + stop_time = (deployment.created_at + 30.minutes).to_f + expect(client).to receive(:query_range).with('avg(container_memory_usage_bytes{container_name!="POD",environment="environment1"}) / 2^20', + start: start_time, stop: stop_time) + expect(client).to receive(:query).with('avg(avg_over_time(container_memory_usage_bytes{container_name!="POD",environment="environment1"}[30m]))', + time: deployment.created_at.to_f) + expect(client).to receive(:query).with('avg(avg_over_time(container_memory_usage_bytes{container_name!="POD",environment="environment1"}[30m]))', + time: stop_time) + + expect(client).to receive(:query_range).with('avg(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="environment1"}[2m])) * 100', + start: start_time, stop: stop_time) + expect(client).to receive(:query).with('avg(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="environment1"}[30m])) * 100', + time: deployment.created_at.to_f) + expect(client).to receive(:query).with('avg(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="environment1"}[30m])) * 100', + time: stop_time) + + expect(subject.query(deployment.id)).to eq(memory_values: nil, memory_before: nil, memory_after: nil, + cpu_values: nil, cpu_before: nil, cpu_after: nil) + end +end diff --git a/spec/lib/gitlab/prometheus_spec.rb b/spec/lib/gitlab/prometheus_client_spec.rb index 9d67e3d2f37..2d8bd2f6b97 100644 --- a/spec/lib/gitlab/prometheus_spec.rb +++ b/spec/lib/gitlab/prometheus_client_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Gitlab::Prometheus, lib: true do +describe Gitlab::PrometheusClient, lib: true do include PrometheusHelpers subject { described_class.new(api_url: 'https://prometheus.example.com') } diff --git a/spec/models/project_services/prometheus_service_spec.rb b/spec/models/project_services/prometheus_service_spec.rb index 82a3e2698c1..739850a836e 100644 --- a/spec/models/project_services/prometheus_service_spec.rb +++ b/spec/models/project_services/prometheus_service_spec.rb @@ -6,6 +6,7 @@ describe PrometheusService, models: true, caching: true do let(:project) { create(:prometheus_project) } let(:service) { project.prometheus_service } + let(:environment_query) { Gitlab::Prometheus::Queries::EnvironmentQuery } describe "Associations" do it { is_expected.to belong_to :project } @@ -45,32 +46,39 @@ describe PrometheusService, models: true, caching: true do end end - describe '#metrics' do + describe '#environment_metrics' do let(:environment) { build_stubbed(:environment, slug: 'env-slug') } around do |example| Timecop.freeze { example.run } end - context 'with valid data without time range' do - subject { service.metrics(environment) } + context 'with valid data' do + subject { service.environment_metrics(environment) } before do - stub_reactive_cache(service, prometheus_data, 'env-slug', nil, nil) + stub_reactive_cache(service, prometheus_data, environment_query, environment.id) end it 'returns reactive data' do is_expected.to eq(prometheus_data) end end + end - context 'with valid data with time range' do - let(:t_start) { 1.hour.ago.utc } - let(:t_end) { Time.now.utc } - subject { service.metrics(environment, timeframe_start: t_start, timeframe_end: t_end) } + describe '#deployment_metrics' do + let(:deployment) { build_stubbed(:deployment)} + let(:deployment_query) { Gitlab::Prometheus::Queries::DeploymentQuery } + + around do |example| + Timecop.freeze { example.run } + end + + context 'with valid data' do + subject { service.deployment_metrics(deployment) } before do - stub_reactive_cache(service, prometheus_data, 'env-slug', t_start, t_end) + stub_reactive_cache(service, prometheus_data, deployment_query, deployment.id) end it 'returns reactive data' do @@ -80,14 +88,14 @@ describe PrometheusService, models: true, caching: true do end describe '#calculate_reactive_cache' do - let(:environment) { build_stubbed(:environment, slug: 'env-slug') } + let(:environment) { create(:environment, slug: 'env-slug') } around do |example| Timecop.freeze { example.run } end subject do - service.calculate_reactive_cache(environment.slug, nil, nil) + service.calculate_reactive_cache(environment_query.to_s, environment.id) end context 'when service is inactive' do diff --git a/spec/support/prometheus_helpers.rb b/spec/support/prometheus_helpers.rb index 51987c7767d..ed7eec9e3de 100644 --- a/spec/support/prometheus_helpers.rb +++ b/spec/support/prometheus_helpers.rb @@ -1,10 +1,10 @@ module PrometheusHelpers def prometheus_memory_query(environment_slug) - %{(sum(container_memory_usage_bytes{container_name!="POD",environment="#{environment_slug}"}) / count(container_memory_usage_bytes{container_name!="POD",environment="#{environment_slug}"})) /1024/1024} + %{avg(container_memory_usage_bytes{container_name!="POD",environment="#{environment_slug}"}) / 2^20} end def prometheus_cpu_query(environment_slug) - %{sum(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="#{environment_slug}"}[2m])) / count(container_cpu_usage_seconds_total{container_name!="POD",environment="#{environment_slug}"}) * 100} + %{avg(rate(container_cpu_usage_seconds_total{container_name!="POD",environment="#{environment_slug}"}[2m])) * 100} end def prometheus_ping_url(prometheus_query) @@ -88,10 +88,8 @@ module PrometheusHelpers metrics: { memory_values: prometheus_values_body('matrix').dig(:data, :result), memory_current: prometheus_value_body('vector').dig(:data, :result), - memory_previous: prometheus_value_body('vector').dig(:data, :result), cpu_values: prometheus_values_body('matrix').dig(:data, :result), cpu_current: prometheus_value_body('vector').dig(:data, :result), - cpu_previous: prometheus_value_body('vector').dig(:data, :result) }, last_update: last_update } |