Commit c327affc authored by Douwe Maan's avatar Douwe Maan

Merge branch 'pawel/add_more_variables_to_additional_metrics-35267' into 'master'

Add kube_namespace and standardize common variables for additional metrics queries

See merge request !13169
parents 6e870344 ba97a421
---
title: Add support for kube_namespace in Metrics queries
merge_request: 16169
author:
......@@ -6,14 +6,13 @@ module Gitlab
def query(deployment_id)
Deployment.find_by(id: deployment_id).try do |deployment|
query_context = {
environment_slug: deployment.environment.slug,
environment_filter: %{container_name!="POD",environment="#{deployment.environment.slug}"},
query_metrics(
common_query_context(
deployment.environment,
timeframe_start: (deployment.created_at - 30.minutes).to_f,
timeframe_end: (deployment.created_at + 30.minutes).to_f
}
query_metrics(query_context)
)
)
end
end
end
......
......@@ -6,14 +6,9 @@ module Gitlab
def query(environment_id)
Environment.find_by(id: environment_id).try do |environment|
query_context = {
environment_slug: environment.slug,
environment_filter: %{container_name!="POD",environment="#{environment.slug}"},
timeframe_start: 8.hours.ago.to_f,
timeframe_end: Time.now.to_f
}
query_metrics(query_context)
query_metrics(
common_query_context(environment, timeframe_start: 8.hours.ago.to_f, timeframe_end: Time.now.to_f)
)
end
end
end
......
......@@ -42,15 +42,18 @@ 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
......@@ -67,6 +70,16 @@ module Gitlab
result.select { |group| group.metrics.any? }
end
def common_query_context(environment, timeframe_start:, timeframe_end:)
{
timeframe_start: timeframe_start,
timeframe_end: timeframe_end,
ci_environment_slug: environment.slug,
kube_namespace: environment.project.kubernetes_service&.actual_namespace || '',
environment_filter: %{container_name!="POD",environment="#{environment.slug}"}
}
end
end
end
end
......
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,
......
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_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
end
end
......
......@@ -10,11 +10,61 @@ RSpec.shared_examples 'additional metrics query' do
[{ 'metric': {}, 'values': [[1488758662.506, '0.00002996364761904785'], [1488758722.506, '0.00003090239047619091']] }]
end
let(:client) { double('prometheus_client') }
let(:query_result) { described_class.new(client).query(*query_params) }
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 query context' do
subject! { described_class.new(client) }
shared_examples 'query context containing environment slug and filter' do
it 'contains ci_environment_slug' do
expect(subject).to receive(:query_metrics).with(hash_including(ci_environment_slug: environment.slug))
subject.query(*query_params)
end
it 'contains environment filter' do
expect(subject).to receive(:query_metrics).with(
hash_including(
environment_filter: "container_name!=\"POD\",environment=\"#{environment.slug}\""
)
)
subject.query(*query_params)
end
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_behaves_like 'query context containing environment slug and filter'
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
describe 'project without Kubernetes service' do
it_behaves_like 'query context containing environment slug and filter'
it 'query context contains empty kube_namespace' do
expect(subject).to receive(:query_metrics).with(hash_including(kube_namespace: ''))
subject.query(*query_params)
end
end
end
context 'with one group where two metrics is found' do
before do
allow(metric_group_class).to receive(:all).and_return([simple_metric_group])
......@@ -51,6 +101,7 @@ RSpec.shared_examples 'additional metrics query' do
context 'with two groups with one metric each' do
let(:metrics) { [simple_metric(queries: [simple_query])] }
before do
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