Commit 6df5bd8b authored by Pawel Chojnacki's avatar Pawel Chojnacki

Context handling and tests cleanup + simple test kube_namespace context test

parent ce83e563
...@@ -75,6 +75,11 @@ class PrometheusService < MonitoringService ...@@ -75,6 +75,11 @@ class PrometheusService < MonitoringService
with_reactive_cache(Gitlab::Prometheus::Queries::MatchedMetricsQuery.name, &:itself) with_reactive_cache(Gitlab::Prometheus::Queries::MatchedMetricsQuery.name, &:itself)
end end
def with_reactive_cache(name, *args, &block)
vals = args.map(&:to_s)
yield calculate_reactive_cache(name, *vals)
end
# Cache metrics for specific environment # Cache metrics for specific environment
def calculate_reactive_cache(query_class_name, *args) def calculate_reactive_cache(query_class_name, *args)
return unless active? && project && !project.pending_delete? return unless active? && project && !project.pending_delete?
......
...@@ -42,15 +42,17 @@ module Gitlab ...@@ -42,15 +42,17 @@ module Gitlab
end end
def process_query(context, query) def process_query(context, query)
query_with_result = query.dup query = query.dup
result = result =
if query.key?(:query_range) if query.key?(:query_range)
client_query_range(query[:query_range] % context, start: context[:timeframe_start], stop: context[:timeframe_end]) query[:query_range] %= context
client_query_range(query[:query_range], start: context[:timeframe_start], stop: context[:timeframe_end])
else else
client_query(query[:query] % context, time: context[:timeframe_end]) query[:query] %= context
client_query(query[:query], time: context[:timeframe_end])
end end
query_with_result[:result] = result&.map(&:deep_symbolize_keys) query[:result] = result&.map(&:deep_symbolize_keys)
query_with_result query
end end
def available_metrics def available_metrics
...@@ -69,15 +71,11 @@ module Gitlab ...@@ -69,15 +71,11 @@ module Gitlab
end end
def common_query_context(environment) def common_query_context(environment)
variables = { {
ci_environment_slug: environment.slug, ci_environment_slug: environment.slug,
kube_namespace: environment.project.kubernetes_service.actual_namespace, kube_namespace: environment.project.kubernetes_service&.actual_namespace || '',
}.flat_map { |k, v| [[k, v], [k.upcase, v]] }.to_h
macros = {
environment_filter: %{container_name!="POD",environment="#{environment.slug}"} environment_filter: %{container_name!="POD",environment="#{environment.slug}"}
} }
variables.merge(macros)
end end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Prometheus::Queries::AdditionalMetricsDeploymentQuery do describe Gitlab::Prometheus::Queries::AdditionalMetricsDeploymentQuery do
include Prometheus::MetricBuilders
let(:client) { double('prometheus_client') }
let(:environment) { create(:environment, slug: 'environment-slug') }
let(:deployment) { create(:deployment, environment: environment) }
subject(:query_result) { described_class.new(client).query(deployment.id) }
around do |example| around do |example|
Timecop.freeze(Time.local(2008, 9, 1, 12, 0, 0)) { example.run } Timecop.freeze(Time.local(2008, 9, 1, 12, 0, 0)) { example.run }
end end
include_examples 'additional metrics query' do include_examples 'additional metrics query' do
let(:deployment) { create(:deployment, environment: environment) }
let(:query_params) { [deployment.id] }
it 'queries using specific time' do it 'queries using specific time' do
expect(client).to receive(:query_range).with(anything, expect(client).to receive(:query_range).with(anything,
start: (deployment.created_at - 30.minutes).to_f, start: (deployment.created_at - 30.minutes).to_f,
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Prometheus::Queries::AdditionalMetricsEnvironmentQuery do describe Gitlab::Prometheus::Queries::AdditionalMetricsEnvironmentQuery do
include Prometheus::MetricBuilders
let(:client) { double('prometheus_client') }
let(:environment) { create(:environment, slug: 'environment-slug') }
subject(:query_result) { described_class.new(client).query(environment.id) }
around do |example| around do |example|
Timecop.freeze { example.run } Timecop.freeze { example.run }
end end
include_examples 'additional metrics query' do include_examples 'additional metrics query' do
let(:query_result) { described_class.new(client).query(environment.id) }
let(:query_params) { [environment.id] }
it 'queries using specific time' do it 'queries using specific time' do
expect(client).to receive(:query_range).with(anything, start: 8.hours.ago.to_f, stop: Time.now.to_f) expect(client).to receive(:query_range).with(anything, start: 8.hours.ago.to_f, stop: Time.now.to_f)
expect(query_result).not_to be_nil expect(query_result).not_to be_nil
......
...@@ -10,12 +10,39 @@ RSpec.shared_examples 'additional metrics query' do ...@@ -10,12 +10,39 @@ RSpec.shared_examples 'additional metrics query' do
[{ 'metric': {}, 'values': [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']] }] [{ 'metric': {}, 'values': [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']] }]
end end
let(:client) { double('prometheus_client') }
let(:environment) { create(:environment, slug: 'environment-slug') }
before do before do
allow(client).to receive(:label_values).and_return(metric_names) allow(client).to receive(:label_values).and_return(metric_names)
allow(metric_group_class).to receive(:all).and_return([simple_metric_group(metrics: [simple_metric])]) allow(metric_group_class).to receive(:all).and_return([simple_metric_group(metrics: [simple_metric])])
end end
context 'metrics rendering' do
subject! { described_class.new(client) }
before do
end
describe 'project has kubernetes service' do
let(:project) { create(:kubernetes_project) }
let(:environment) { create(:environment, slug: 'environment-slug', project: project) }
let(:kube_namespace) { project.kubernetes_service.actual_namespace }
it 'query context contains kube namespace' do
expect(subject).to receive(:query_metrics).with(
hash_including(
kube_namespace: kube_namespace)
)
subject.query(*query_params)
end
end
end
context 'with one group where two metrics is found' do context 'with one group where two metrics is found' do
let(:query_result) { described_class.new(client).query(*query_params) }
before do before do
allow(metric_group_class).to receive(:all).and_return([simple_metric_group]) allow(metric_group_class).to receive(:all).and_return([simple_metric_group])
end end
...@@ -50,7 +77,9 @@ RSpec.shared_examples 'additional metrics query' do ...@@ -50,7 +77,9 @@ RSpec.shared_examples 'additional metrics query' do
end end
context 'with two groups with one metric each' do context 'with two groups with one metric each' do
let(:query_result) { described_class.new(client).query(*query_params) }
let(:metrics) { [simple_metric(queries: [simple_query])] } let(:metrics) { [simple_metric(queries: [simple_query])] }
before do before do
allow(metric_group_class).to receive(:all).and_return( allow(metric_group_class).to receive(:all).and_return(
[ [
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment