Make prometheus service querying approach much nicer wrt to arity and default function params

This commit is contained in:
Pawel Chojnacki 2018-02-07 01:35:57 +01:00
parent eac8ad6a45
commit 277f7fef2c
4 changed files with 30 additions and 4 deletions

View file

@ -91,16 +91,17 @@ class PrometheusService < MonitoringService
end
def matched_metrics
with_reactive_cache(Gitlab::Prometheus::Queries::MatchedMetricsQuery.name, nil, &:itself)
with_reactive_cache(Gitlab::Prometheus::Queries::MatchedMetricsQuery.name, &:itself)
end
# Cache metrics for specific environment
def calculate_reactive_cache(query_class_name, environment_id, *args)
def calculate_reactive_cache(query_class_name, *args)
return unless active? && project && !project.pending_delete?
environment_id = args.first
client = client(environment_id)
data = Kernel.const_get(query_class_name).new(client).query(environment_id, *args)
data = Kernel.const_get(query_class_name).new(client).query(*args)
{
success: true,
data: data,

View file

@ -4,7 +4,7 @@ module Gitlab
class MatchedMetricsQuery < BaseQuery
MAX_QUERY_ITEMS = 40.freeze
def query(_ = nil)
def query
groups_data.map do |group, data|
{
group: group.name,

View file

@ -75,6 +75,25 @@ describe PrometheusService, :use_clean_rails_memory_store_caching do
end
end
describe '#matched_metrics' do
let(:matched_metrics_query) { Gitlab::Prometheus::Queries::MatchedMetricsQuery }
let(:client) { double(:client, label_values: nil) }
context 'with valid data' do
subject { service.matched_metrics }
before do
allow(service).to receive(:client).and_return(client)
synchronous_reactive_cache(service)
end
it 'returns reactive data' do
expect(subject[:success]).to be_truthy
expect(subject[:data]).to eq([])
end
end
end
describe '#deployment_metrics' do
let(:deployment) { build_stubbed(:deployment) }
let(:deployment_query) { Gitlab::Prometheus::Queries::DeploymentQuery }

View file

@ -13,6 +13,12 @@ module ReactiveCachingHelpers
write_reactive_cache(subject, data, *qualifiers) if data
end
def synchronous_reactive_cache(subject)
allow(service).to receive(:with_reactive_cache) do |*args, &block|
block.call(service.calculate_reactive_cache(*args))
end
end
def read_reactive_cache(subject, *qualifiers)
Rails.cache.read(reactive_cache_key(subject, *qualifiers))
end