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
@@ -113,11 +113,13 @@ def redis_usage_data(counter = nil, &block)
Loading
@@ -113,11 +113,13 @@ def redis_usage_data(counter = nil, &block)
end end
end end
   
def with_prometheus_client(fallback: nil, verify: true) def with_prometheus_client(fallback: {}, verify: true)
client = prometheus_client(verify: verify) client = prometheus_client(verify: verify)
return fallback unless client return fallback unless client
   
yield client yield client
rescue
fallback
end end
   
def measure_duration def measure_duration
Loading
Loading
Loading
@@ -246,6 +246,13 @@
Loading
@@ -246,6 +246,13 @@
end end
   
describe '#with_prometheus_client' do 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 shared_examples 'query data from Prometheus' do
it 'yields a client instance and returns the block result' do it 'yields a client instance and returns the block result' do
result = described_class.with_prometheus_client { |client| client } result = described_class.with_prometheus_client { |client| client }
Loading
@@ -255,10 +262,10 @@
Loading
@@ -255,10 +262,10 @@
end end
   
shared_examples 'does not query data from Prometheus' do 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 } result = described_class.with_prometheus_client { |client| client }
   
expect(result).to be_nil expect(result).to eq({})
end end
   
it 'returns fallback if provided' do 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