Commit 277f7fef authored by Pawel Chojnacki's avatar Pawel Chojnacki

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

parent eac8ad6a
......@@ -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,
......
......@@ -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,
......
......@@ -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 }
......
......@@ -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
......
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