Skip to content
Snippets Groups Projects
Unverified Commit dc9ee8e2 authored by Alper Akgun's avatar Alper Akgun
Browse files

Harden Prometheus client wrapper

parent 77a98f10
No related branches found
No related tags found
No related merge requests found
---
title: Harden Prometheus client usage data wrapper
merge_request: 56210
author:
type: other
Loading
Loading
@@ -113,11 +113,13 @@ def redis_usage_data(counter = nil, &block)
end
end
 
def with_prometheus_client(fallback: nil, verify: true)
def with_prometheus_client(fallback: {}, verify: true)
client = prometheus_client(verify: verify)
return fallback unless client
 
yield client
rescue
fallback
end
 
def measure_duration
Loading
Loading
Loading
Loading
@@ -246,6 +246,13 @@
end
 
describe '#with_prometheus_client' do
it 'returns fallback with for an exception in yield block' do
allow(described_class).to receive(:prometheus_client).and_return(Gitlab::PrometheusClient.new('http://localhost:9090'))
result = described_class.with_prometheus_client(fallback: -42) { |client| raise StandardError }
expect(result).to be(-42)
end
shared_examples 'query data from Prometheus' do
it 'yields a client instance and returns the block result' do
result = described_class.with_prometheus_client { |client| client }
Loading
Loading
@@ -255,10 +262,10 @@
end
 
shared_examples 'does not query data from Prometheus' do
it 'returns nil by default' do
it 'returns {} by default' do
result = described_class.with_prometheus_client { |client| client }
 
expect(result).to be_nil
expect(result).to eq({})
end
 
it 'returns fallback if provided' do
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment