Context handling and tests cleanup + simple test kube_namespace context test

This commit is contained in:
Pawel Chojnacki 2017-07-31 13:29:00 +02:00
parent ce83e5635d
commit 6df5bd8b84
5 changed files with 49 additions and 26 deletions

View file

@ -75,6 +75,11 @@ class PrometheusService < MonitoringService
with_reactive_cache(Gitlab::Prometheus::Queries::MatchedMetricsQuery.name, &:itself)
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
def calculate_reactive_cache(query_class_name, *args)
return unless active? && project && !project.pending_delete?

View file

@ -42,15 +42,17 @@ module Gitlab
end
def process_query(context, query)
query_with_result = query.dup
query = query.dup
result =
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
client_query(query[:query] % context, time: context[:timeframe_end])
query[:query] %= context
client_query(query[:query], time: context[:timeframe_end])
end
query_with_result[:result] = result&.map(&:deep_symbolize_keys)
query_with_result
query[:result] = result&.map(&:deep_symbolize_keys)
query
end
def available_metrics
@ -69,15 +71,11 @@ module Gitlab
end
def common_query_context(environment)
variables = {
{
ci_environment_slug: environment.slug,
kube_namespace: environment.project.kubernetes_service.actual_namespace,
}.flat_map { |k, v| [[k, v], [k.upcase, v]] }.to_h
macros = {
kube_namespace: environment.project.kubernetes_service&.actual_namespace || '',
environment_filter: %{container_name!="POD",environment="#{environment.slug}"}
}
variables.merge(macros)
end
end
end

View file

@ -1,19 +1,14 @@
require 'spec_helper'
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|
Timecop.freeze(Time.local(2008, 9, 1, 12, 0, 0)) { example.run }
end
include_examples 'additional metrics query' do
let(:deployment) { create(:deployment, environment: environment) }
let(:query_params) { [deployment.id] }
it 'queries using specific time' do
expect(client).to receive(:query_range).with(anything,
start: (deployment.created_at - 30.minutes).to_f,

View file

@ -1,18 +1,14 @@
require 'spec_helper'
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|
Timecop.freeze { example.run }
end
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
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

View file

@ -10,12 +10,39 @@ RSpec.shared_examples 'additional metrics query' do
[{ 'metric': {}, 'values': [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']] }]
end
let(:client) { double('prometheus_client') }
let(:environment) { create(:environment, slug: 'environment-slug') }
before do
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])])
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
let(:query_result) { described_class.new(client).query(*query_params) }
before do
allow(metric_group_class).to receive(:all).and_return([simple_metric_group])
end
@ -50,7 +77,9 @@ RSpec.shared_examples 'additional metrics query' do
end
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])] }
before do
allow(metric_group_class).to receive(:all).and_return(
[