Extract query variables into own module
This commit is contained in:
parent
fbbe5ccd1b
commit
b47a3a4085
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
title: Extract into Gitlab::Prometheus::QueryVariables
|
||||
merge_request: 23335
|
||||
author:
|
||||
type: other
|
|
@ -83,11 +83,8 @@ module Gitlab
|
|||
end
|
||||
|
||||
def common_query_context(environment, timeframe_start:, timeframe_end:)
|
||||
base_query_context(timeframe_start, timeframe_end).merge({
|
||||
ci_environment_slug: environment.slug,
|
||||
kube_namespace: environment.deployment_platform&.actual_namespace || '',
|
||||
environment_filter: %{container_name!="POD",environment="#{environment.slug}"}
|
||||
})
|
||||
base_query_context(timeframe_start, timeframe_end)
|
||||
.merge(QueryVariables.call(environment))
|
||||
end
|
||||
|
||||
def base_query_context(timeframe_start, timeframe_end)
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Gitlab
|
||||
module Prometheus
|
||||
module QueryVariables
|
||||
def self.call(environment)
|
||||
{
|
||||
ci_environment_slug: environment.slug,
|
||||
kube_namespace: environment.deployment_platform&.actual_namespace || '',
|
||||
environment_filter: %{container_name!="POD",environment="#{environment.slug}"}
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,33 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe Gitlab::Prometheus::QueryVariables do
|
||||
describe '.call' do
|
||||
set(:environment) { create(:environment) }
|
||||
let(:slug) { environment.slug }
|
||||
|
||||
subject { described_class.call(environment) }
|
||||
|
||||
it { is_expected.to include(ci_environment_slug: slug) }
|
||||
|
||||
it do
|
||||
is_expected.to include(environment_filter:
|
||||
%{container_name!="POD",environment="#{slug}"})
|
||||
end
|
||||
|
||||
context 'without deployment platform' do
|
||||
it { is_expected.to include(kube_namespace: '') }
|
||||
end
|
||||
|
||||
context 'with deplyoment platform' do
|
||||
let(:kube_namespace) { environment.deployment_platform.actual_namespace }
|
||||
|
||||
before do
|
||||
create(:cluster, :provided_by_user, projects: [environment.project])
|
||||
end
|
||||
|
||||
it { is_expected.to include(kube_namespace: kube_namespace) }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue